Skip to content

Commit

Permalink
finish status
Browse files Browse the repository at this point in the history
  • Loading branch information
icecream17 committed Mar 7, 2021
1 parent e0059be commit 219d48b
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 167 deletions.
51 changes: 24 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@
"warn",
3
],
"@typescript-eslint/restrict-template-expressions": [
"error",
{
"allowNumber": true,
"allowBoolean": true,
"allowAny": true,
"allowNullish": true
}
],

"comma-dangle": [
"error",
"only-multiline"
],
"object-property-newline": "off"
}
}
}
8 changes: 8 additions & 0 deletions src/anytypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/** An object where every property is allowed */
export interface AnyObject extends Object {
[key: string]: any
[key: number]: any
// @ts-expect-error WAIT UNTIL VERSION: 4.3
[key: symbol]: any
}
114 changes: 41 additions & 73 deletions src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
/* never gonna turn around, and, desert you. */

import { Color, PieceType, None } from './types'
import { ColorClass, PieceTypeClass } from './classtypes'
import { IntFlagEnum } from './enums'

/**
* @fileoverview
Expand All @@ -33,59 +35,6 @@ import { Color, PieceType, None } from './types'
* Syzygy tablebase probing, and XBoard/UCI engine communication.
*/

/**
* Instead of using the constructor, use the ColorClass.from() method instead
* true instanceof ColorClass === true
* false instanceof ColorClass === true
* Everything else, even Object(Boolean), instanceof Color === false
*/
class ColorClass extends Boolean {
static from (value?: any): boolean {
return Boolean(value)
}

static [Symbol.hasInstance] (value?: unknown): value is boolean {
return typeof value === 'boolean'
}

static readonly CorrespondingPythonClass = 'bool'
static readonly WHITE: Color = true
static readonly BLACK: Color = false
}

class PieceTypeClass extends Number {
constructor (...args: any[]) {
super(...args)

if (!Number.isInteger(this.valueOf())) {
throw RangeError('Number created must be an integer')
} else if (this.valueOf() < 1 || this.valueOf() > 6) {
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
}
}

static from (value?: any): number {
return Number(new PieceTypeClass(value))
}

static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
if (typeof value === 'number' && Number.isInteger(value)) {
return (value >= 1 && value < 7) ? true : 'outside of range'
} else {
return false
}
}

static readonly CorrespondingPythonClass = 'int'
static readonly PAWN: PieceType = 1
static readonly KNIGHT: PieceType = 2
static readonly BISHOP: PieceType = 3
static readonly ROOK: PieceType = 4
static readonly QUEEN: PieceType = 5
static readonly KING: PieceType = 6
static readonly PIECE_TYPES: PieceType[] = [1, 2, 3, 4, 5, 6]
}

export const Chess = {
Color: ColorClass,
isColor: ColorClass[Symbol.hasInstance],
Expand Down Expand Up @@ -139,24 +88,43 @@ export const Chess = {
/** The board part of the FEN for the standard chess starting position. */
STARTING_BOARD_FEN: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR',

// Status: CreateIntFlagEnum('Status', {
// VALID: 0,
// NO_WHITE_KING: 1 << 0,
// NO_BLACK_KING: 1 << 1,
// TOO_MANY_KINGS: 1 << 2,
// TOO_MANY_WHITE_PAWNS: 1 << 3,
// TOO_MANY_BLACK_PAWNS: 1 << 4,
// PAWNS_ON_BACKRANK: 1 << 5,
// TOO_MANY_WHITE_PIECES: 1 << 6,
// TOO_MANY_BLACK_PIECES: 1 << 7,
// BAD_CASTLING_RIGHTS: 1 << 8,
// INVALID_EP_SQUARE: 1 << 9,
// OPPOSITE_CHECK: 1 << 10,
// EMPTY: 1 << 11,
// RACE_CHECK: 1 << 12,
// RACE_OVER: 1 << 13,
// RACE_MATERIAL: 1 << 14,
// TOO_MANY_CHECKERS: 1 << 15,
// IMPOSSIBLE_CHECK: 1 << 16,
// })
Status: new IntFlagEnum({
VALID: 0,
NO_WHITE_KING: 1 << 0,
NO_BLACK_KING: 1 << 1,
TOO_MANY_KINGS: 1 << 2,
TOO_MANY_WHITE_PAWNS: 1 << 3,
TOO_MANY_BLACK_PAWNS: 1 << 4,
PAWNS_ON_BACKRANK: 1 << 5,
TOO_MANY_WHITE_PIECES: 1 << 6,
TOO_MANY_BLACK_PIECES: 1 << 7,
BAD_CASTLING_RIGHTS: 1 << 8,
INVALID_EP_SQUARE: 1 << 9,
OPPOSITE_CHECK: 1 << 10,
EMPTY: 1 << 11,
RACE_CHECK: 1 << 12,
RACE_OVER: 1 << 13,
RACE_MATERIAL: 1 << 14,
TOO_MANY_CHECKERS: 1 << 15,
IMPOSSIBLE_CHECK: 1 << 16,
}),

STATUS_VALID: 0,
STATUS_NO_WHITE_KING: 1 << 0,
STATUS_NO_BLACK_KING: 1 << 1,
STATUS_TOO_MANY_KINGS: 1 << 2,
STATUS_TOO_MANY_WHITE_PAWNS: 1 << 3,
STATUS_TOO_MANY_BLACK_PAWNS: 1 << 4,
STATUS_PAWNS_ON_BACKRANK: 1 << 5,
STATUS_TOO_MANY_WHITE_PIECES: 1 << 6,
STATUS_TOO_MANY_BLACK_PIECES: 1 << 7,
STATUS_BAD_CASTLING_RIGHTS: 1 << 8,
STATUS_INVALID_EP_SQUARE: 1 << 9,
STATUS_OPPOSITE_CHECK: 1 << 10,
STATUS_EMPTY: 1 << 11,
STATUS_RACE_CHECK: 1 << 12,
STATUS_RACE_OVER: 1 << 13,
STATUS_RACE_MATERIAL: 1 << 14,
STATUS_TOO_MANY_CHECKERS: 1 << 15,
STATUS_IMPOSSIBLE_CHECK: 1 << 16,
}
81 changes: 81 additions & 0 deletions src/classtypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

import { Color, PieceType } from './types'

export class Integer extends Number {
constructor (...args: any[]) {
super(...args)

if (!Number.isInteger(this.valueOf())) {
throw RangeError('Number created must be an integer')
} else if ('MIN' in this.constructor && 'MAX' in this.constructor) {
if (this.valueOf() < (this.constructor as {MIN: number}).MIN || this.valueOf() > (this.constructor as {MAX: number}).MAX) {
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
}
}
}

static from (value?: any): number {
return Number(new PieceTypeClass(value))
}

static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
if (typeof value === 'number' && Number.isInteger(value)) {
return (value >= 1 && value < 7) ? true : 'outside of range'
} else {
return false
}
}
}

/**
* Instead of using the constructor, use the ColorClass.from() method instead
* true instanceof ColorClass === true
* false instanceof ColorClass === true
* Everything else, even Object(Boolean), instanceof Color === false
*/
export class ColorClass extends Boolean {
static from (value?: any): boolean {
return Boolean(value)
}

static [Symbol.hasInstance] (value?: unknown): value is boolean {
return typeof value === 'boolean'
}

static readonly CorrespondingPythonClass = 'bool'
static readonly WHITE: Color = true
static readonly BLACK: Color = false
}

export class PieceTypeClass extends Number {
constructor (...args: any[]) {
super(...args)

if (!Number.isInteger(this.valueOf())) {
throw RangeError('Number created must be an integer')
} else if (this.valueOf() < 1 || this.valueOf() > 6) {
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
}
}

static from (value?: any): number {
return Number(new PieceTypeClass(value))
}

static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
if (typeof value === 'number' && Number.isInteger(value)) {
return (value >= 1 && value < 7) ? true : 'outside of range'
} else {
return false
}
}

static readonly CorrespondingPythonClass = 'int'
static readonly PAWN: PieceType = 1
static readonly KNIGHT: PieceType = 2
static readonly BISHOP: PieceType = 3
static readonly ROOK: PieceType = 4
static readonly QUEEN: PieceType = 5
static readonly KING: PieceType = 6
static readonly PIECE_TYPES: PieceType[] = [1, 2, 3, 4, 5, 6]
}

0 comments on commit 219d48b

Please sign in to comment.