Skip to content

Commit 219d48b

Browse files
committed
finish status
1 parent e0059be commit 219d48b

9 files changed

+243
-167
lines changed

package-lock.json

Lines changed: 24 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/.eslintrc.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@
2424
"warn",
2525
3
2626
],
27+
"@typescript-eslint/restrict-template-expressions": [
28+
"error",
29+
{
30+
"allowNumber": true,
31+
"allowBoolean": true,
32+
"allowAny": true,
33+
"allowNullish": true
34+
}
35+
],
2736

2837
"comma-dangle": [
2938
"error",
3039
"only-multiline"
3140
],
3241
"object-property-newline": "off"
3342
}
34-
}
43+
}

src/anytypes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/** An object where every property is allowed */
3+
export interface AnyObject extends Object {
4+
[key: string]: any
5+
[key: number]: any
6+
// @ts-expect-error WAIT UNTIL VERSION: 4.3
7+
[key: symbol]: any
8+
}

src/chess.ts

Lines changed: 41 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
/* never gonna turn around, and, desert you. */
2323

2424
import { Color, PieceType, None } from './types'
25+
import { ColorClass, PieceTypeClass } from './classtypes'
26+
import { IntFlagEnum } from './enums'
2527

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

36-
/**
37-
* Instead of using the constructor, use the ColorClass.from() method instead
38-
* true instanceof ColorClass === true
39-
* false instanceof ColorClass === true
40-
* Everything else, even Object(Boolean), instanceof Color === false
41-
*/
42-
class ColorClass extends Boolean {
43-
static from (value?: any): boolean {
44-
return Boolean(value)
45-
}
46-
47-
static [Symbol.hasInstance] (value?: unknown): value is boolean {
48-
return typeof value === 'boolean'
49-
}
50-
51-
static readonly CorrespondingPythonClass = 'bool'
52-
static readonly WHITE: Color = true
53-
static readonly BLACK: Color = false
54-
}
55-
56-
class PieceTypeClass extends Number {
57-
constructor (...args: any[]) {
58-
super(...args)
59-
60-
if (!Number.isInteger(this.valueOf())) {
61-
throw RangeError('Number created must be an integer')
62-
} else if (this.valueOf() < 1 || this.valueOf() > 6) {
63-
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
64-
}
65-
}
66-
67-
static from (value?: any): number {
68-
return Number(new PieceTypeClass(value))
69-
}
70-
71-
static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
72-
if (typeof value === 'number' && Number.isInteger(value)) {
73-
return (value >= 1 && value < 7) ? true : 'outside of range'
74-
} else {
75-
return false
76-
}
77-
}
78-
79-
static readonly CorrespondingPythonClass = 'int'
80-
static readonly PAWN: PieceType = 1
81-
static readonly KNIGHT: PieceType = 2
82-
static readonly BISHOP: PieceType = 3
83-
static readonly ROOK: PieceType = 4
84-
static readonly QUEEN: PieceType = 5
85-
static readonly KING: PieceType = 6
86-
static readonly PIECE_TYPES: PieceType[] = [1, 2, 3, 4, 5, 6]
87-
}
88-
8938
export const Chess = {
9039
Color: ColorClass,
9140
isColor: ColorClass[Symbol.hasInstance],
@@ -139,24 +88,43 @@ export const Chess = {
13988
/** The board part of the FEN for the standard chess starting position. */
14089
STARTING_BOARD_FEN: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR',
14190

142-
// Status: CreateIntFlagEnum('Status', {
143-
// VALID: 0,
144-
// NO_WHITE_KING: 1 << 0,
145-
// NO_BLACK_KING: 1 << 1,
146-
// TOO_MANY_KINGS: 1 << 2,
147-
// TOO_MANY_WHITE_PAWNS: 1 << 3,
148-
// TOO_MANY_BLACK_PAWNS: 1 << 4,
149-
// PAWNS_ON_BACKRANK: 1 << 5,
150-
// TOO_MANY_WHITE_PIECES: 1 << 6,
151-
// TOO_MANY_BLACK_PIECES: 1 << 7,
152-
// BAD_CASTLING_RIGHTS: 1 << 8,
153-
// INVALID_EP_SQUARE: 1 << 9,
154-
// OPPOSITE_CHECK: 1 << 10,
155-
// EMPTY: 1 << 11,
156-
// RACE_CHECK: 1 << 12,
157-
// RACE_OVER: 1 << 13,
158-
// RACE_MATERIAL: 1 << 14,
159-
// TOO_MANY_CHECKERS: 1 << 15,
160-
// IMPOSSIBLE_CHECK: 1 << 16,
161-
// })
91+
Status: new IntFlagEnum({
92+
VALID: 0,
93+
NO_WHITE_KING: 1 << 0,
94+
NO_BLACK_KING: 1 << 1,
95+
TOO_MANY_KINGS: 1 << 2,
96+
TOO_MANY_WHITE_PAWNS: 1 << 3,
97+
TOO_MANY_BLACK_PAWNS: 1 << 4,
98+
PAWNS_ON_BACKRANK: 1 << 5,
99+
TOO_MANY_WHITE_PIECES: 1 << 6,
100+
TOO_MANY_BLACK_PIECES: 1 << 7,
101+
BAD_CASTLING_RIGHTS: 1 << 8,
102+
INVALID_EP_SQUARE: 1 << 9,
103+
OPPOSITE_CHECK: 1 << 10,
104+
EMPTY: 1 << 11,
105+
RACE_CHECK: 1 << 12,
106+
RACE_OVER: 1 << 13,
107+
RACE_MATERIAL: 1 << 14,
108+
TOO_MANY_CHECKERS: 1 << 15,
109+
IMPOSSIBLE_CHECK: 1 << 16,
110+
}),
111+
112+
STATUS_VALID: 0,
113+
STATUS_NO_WHITE_KING: 1 << 0,
114+
STATUS_NO_BLACK_KING: 1 << 1,
115+
STATUS_TOO_MANY_KINGS: 1 << 2,
116+
STATUS_TOO_MANY_WHITE_PAWNS: 1 << 3,
117+
STATUS_TOO_MANY_BLACK_PAWNS: 1 << 4,
118+
STATUS_PAWNS_ON_BACKRANK: 1 << 5,
119+
STATUS_TOO_MANY_WHITE_PIECES: 1 << 6,
120+
STATUS_TOO_MANY_BLACK_PIECES: 1 << 7,
121+
STATUS_BAD_CASTLING_RIGHTS: 1 << 8,
122+
STATUS_INVALID_EP_SQUARE: 1 << 9,
123+
STATUS_OPPOSITE_CHECK: 1 << 10,
124+
STATUS_EMPTY: 1 << 11,
125+
STATUS_RACE_CHECK: 1 << 12,
126+
STATUS_RACE_OVER: 1 << 13,
127+
STATUS_RACE_MATERIAL: 1 << 14,
128+
STATUS_TOO_MANY_CHECKERS: 1 << 15,
129+
STATUS_IMPOSSIBLE_CHECK: 1 << 16,
162130
}

src/classtypes.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
import { Color, PieceType } from './types'
3+
4+
export class Integer extends Number {
5+
constructor (...args: any[]) {
6+
super(...args)
7+
8+
if (!Number.isInteger(this.valueOf())) {
9+
throw RangeError('Number created must be an integer')
10+
} else if ('MIN' in this.constructor && 'MAX' in this.constructor) {
11+
if (this.valueOf() < (this.constructor as {MIN: number}).MIN || this.valueOf() > (this.constructor as {MAX: number}).MAX) {
12+
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
13+
}
14+
}
15+
}
16+
17+
static from (value?: any): number {
18+
return Number(new PieceTypeClass(value))
19+
}
20+
21+
static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
22+
if (typeof value === 'number' && Number.isInteger(value)) {
23+
return (value >= 1 && value < 7) ? true : 'outside of range'
24+
} else {
25+
return false
26+
}
27+
}
28+
}
29+
30+
/**
31+
* Instead of using the constructor, use the ColorClass.from() method instead
32+
* true instanceof ColorClass === true
33+
* false instanceof ColorClass === true
34+
* Everything else, even Object(Boolean), instanceof Color === false
35+
*/
36+
export class ColorClass extends Boolean {
37+
static from (value?: any): boolean {
38+
return Boolean(value)
39+
}
40+
41+
static [Symbol.hasInstance] (value?: unknown): value is boolean {
42+
return typeof value === 'boolean'
43+
}
44+
45+
static readonly CorrespondingPythonClass = 'bool'
46+
static readonly WHITE: Color = true
47+
static readonly BLACK: Color = false
48+
}
49+
50+
export class PieceTypeClass extends Number {
51+
constructor (...args: any[]) {
52+
super(...args)
53+
54+
if (!Number.isInteger(this.valueOf())) {
55+
throw RangeError('Number created must be an integer')
56+
} else if (this.valueOf() < 1 || this.valueOf() > 6) {
57+
console.warn(`PieceType value ${this.valueOf()} is outside of valid PieceType range`)
58+
}
59+
}
60+
61+
static from (value?: any): number {
62+
return Number(new PieceTypeClass(value))
63+
}
64+
65+
static [Symbol.hasInstance] (value?: unknown): boolean | 'outside of range' {
66+
if (typeof value === 'number' && Number.isInteger(value)) {
67+
return (value >= 1 && value < 7) ? true : 'outside of range'
68+
} else {
69+
return false
70+
}
71+
}
72+
73+
static readonly CorrespondingPythonClass = 'int'
74+
static readonly PAWN: PieceType = 1
75+
static readonly KNIGHT: PieceType = 2
76+
static readonly BISHOP: PieceType = 3
77+
static readonly ROOK: PieceType = 4
78+
static readonly QUEEN: PieceType = 5
79+
static readonly KING: PieceType = 6
80+
static readonly PIECE_TYPES: PieceType[] = [1, 2, 3, 4, 5, 6]
81+
}

0 commit comments

Comments
 (0)