forked from jhlywa/chess.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
678 lines (613 loc) · 17.4 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Type definitions for chess.js 0.11
// Project: https://github.com/jhlywa/chess.js
// Definitions by: Jacob Fischer <https://github.com/JacobFischer>
// Zachary Svoboda <https://github.com/zacnomore>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.3
/**
* One of the possible squares on a chess board in san format,
* e.g. "a8" to "h1".
*/
type MoveType = 'range' | 'melee'
export type Square =
| 'a8'
| 'b8'
| 'c8'
| 'd8'
| 'e8'
| 'f8'
| 'g8'
| 'h8'
| 'a7'
| 'b7'
| 'c7'
| 'd7'
| 'e7'
| 'f7'
| 'g7'
| 'h7'
| 'a6'
| 'b6'
| 'c6'
| 'd6'
| 'e6'
| 'f6'
| 'g6'
| 'h6'
| 'a5'
| 'b5'
| 'c5'
| 'd5'
| 'e5'
| 'f5'
| 'g5'
| 'h5'
| 'a4'
| 'b4'
| 'c4'
| 'd4'
| 'e4'
| 'f4'
| 'g4'
| 'h4'
| 'a3'
| 'b3'
| 'c3'
| 'd3'
| 'e3'
| 'f3'
| 'g3'
| 'h3'
| 'a2'
| 'b2'
| 'c2'
| 'd2'
| 'e2'
| 'f2'
| 'g2'
| 'h2'
| 'a1'
| 'b1'
| 'c1'
| 'd1'
| 'e1'
| 'f1'
| 'g1'
| 'h1'
/**
* - "p" for Pawn
* - "n" for Knight
* - "b" for Bishop
* - "r" for Rook
* - "q" for Queen
* - "k" for King
*/
export type PieceType = 'p' | 'n' | 'b' | 'r' | 'q' | 'k'
/**
* Partial data about a chess move including the from and to square, and if a
* promotion occured.
*/
export interface ShortMove {
/**
* The location the piece is moving from.
* Must be in san format, e.g "h8"
*/
from: Square
/**
* The location the piece is moving to.
* Must be in san format, e.g "a1"
*/
to: Square
/**
* If this move results in a promotion, this will have the unit promotion.
*/
promotion?: Exclude<PieceType, 'p' | 'k'> | undefined
}
/**
* The full data about a chess move
*/
export interface Move extends ShortMove {
/**
* The color of the piece that moved
* - "b" for Black
* - "w" for White
*/
color: 'b' | 'w'
/** Flags indicating what occurred, combined into one string */
flags: string
/**
* The type of the piece that moved
*/
piece: PieceType
/** The Standard Algebraic Notation (SAN) representation of the move */
san: string
/**
* If an enemy piece was captured this is their type
*/
captured?: Exclude<PieceType, 'k'> | undefined
}
export interface Piece {
/**
* The type of the piece to place
*/
type: PieceType
/**
* The color of the piece
* - "b" for Black
* - "w" for White
*/
color: 'b' | 'w'
}
export interface Comment {
fen: string
comment: string
}
export interface ChessInstance {
/** The string that represents the White color side */
readonly WHITE: 'w'
/** The string that represents the Black color side */
readonly BLACK: 'b'
/** The string that represents a Pawn */
readonly PAWN: 'p'
/** The string that represents a Knight */
readonly KNIGHT: 'n'
/** The string that represents a Bishop */
readonly BISHOP: 'b'
/** The string that represents a Rook */
readonly ROOK: 'r'
/** The string that represents a Queen */
readonly QUEEN: 'q'
/** The string that represents a King */
readonly KING: 'k'
/** A list of all the squares in the game, from "a1" to "h8" */
readonly SQUARES: [
'a8',
'b8',
'c8',
'd8',
'e8',
'f8',
'g8',
'h8',
'a7',
'b7',
'c7',
'd7',
'e7',
'f7',
'g7',
'h7',
'a6',
'b6',
'c6',
'd6',
'e6',
'f6',
'g6',
'h6',
'a5',
'b5',
'c5',
'd5',
'e5',
'f5',
'g5',
'h5',
'a4',
'b4',
'c4',
'd4',
'e4',
'f4',
'g4',
'h4',
'a3',
'b3',
'c3',
'd3',
'e3',
'f3',
'g3',
'h3',
'a2',
'b2',
'c2',
'd2',
'e2',
'f2',
'g2',
'h2',
'a1',
'b1',
'c1',
'd1',
'e1',
'f1',
'g1',
'h1'
]
/** Flags used to build flag strings for moves */
readonly FLAGS: {
/** a non-capture */
NORMAL: 'n'
/** a standard capture */
CAPTURE: 'c'
/** a pawn push of two squares */
BIG_PAWN: 'b'
/** an en passant capture */
EP_CAPTURE: 'e'
/** a promotion */
PROMOTION: 'p'
/** kingside castling */
KSIDE_CASTLE: 'k'
/** queenside castling */
QSIDE_CASTLE: 'q'
}
/**
* The board is cleared, and the FEN string is loaded.
* Returns true if the position was successfully loaded, otherwise false
* @param fen the fen formatted string to load
* @returns true if the position was successfully loaded, otherwise
* false
*/
load(fen: string): boolean
/**
* Reset the board to the initial starting position.
*/
reset(): void
/**
* Returns a list of legal moves from the current position.
* The function takes an optional parameter which controls the
* single-square move generation and verbosity.
* @param options an optional parameter which controls the single-square
* move generation and verbosity.
* @returns The list of all valid moves, either in SAN format, or as
* verbose objects.
*/
moves(
options: {
/** Set to true to return verbose move objects instead of strings */
verbose: true
/**
* The string to test if it is a valid move, if it is not then an
* empty array is returned
*/
square?: string | undefined
},
type: MoveType
): Move[]
/**
* Returns a list of legal moves from the current position.
* The function takes an optional parameter which controls the
* single-square move generation and verbosity.
* @param options an optional parameter which controls the single-square
* move generation and verbosity.
* @returns The list of all valid moves, either in SAN format, or as
* verbose objects.
*/
moves(
options?: {
/** Set to true to return verbose move objects instead of strings */
verbose?: false | undefined
/**
* The string to test if it is a valid move, if it is not then an
* empty array is returned
*/
square?: string | undefined
},
type: MoveType
): string[]
/**
* Returns a list of legal moves from the current position.
* The function takes an optional parameter which controls the
* single-square move generation and verbosity.
* @param options an optional parameter which controls the single-square
* move generation and verbosity.
* @returns The list of all valid moves, either in SAN format, or as
* verbose objects.
*/
moves(
options?: {
/** Set to true to return verbose move objects instead of strings */
verbose?: boolean | undefined
/**
* The string to test if it is a valid move, if it is not then an
* empty array is returned
*/
square?: string | undefined
},
type: MoveType
): string[] | Move[]
/**
* Returns true or false if the side to move is in check.
* @returns true or false if the side to move is in check.
*/
in_check(): boolean
/**
* Returns true or false if the side to move has been checkmated.
* @returns true or false if the side to move has been checkmated.
*/
in_checkmate(): boolean
/**
* Returns true or false if the side to move has been stalemated.
* @returns true or false if the side to move has been stalemated.
*/
in_stalemate(): boolean
/**
* Returns true or false if the game is drawn (50-move rule or
* insufficient material).
* @returns true or false if the game is drawn (50-move rule or
* insufficient material).
*/
in_draw(): boolean
/**
* Returns true if the game is drawn due to insufficient material
* (K vs. K, K vs. KB, or K vs. KN); otherwise false.
* @returns True if the game is drawn due to insufficient material
* (K vs. K, K vs. KB, or K vs. KN); otherwise false.
*/
insufficient_material(): boolean
/**
* Returns true or false if the current board position has occurred three
* or more times.
* @returns true or false if the current board position has occurred three
* or more times.
*/
in_threefold_repetition(): boolean
/**
* Returns true if the game has ended via checkmate, stalemate, draw,
* threefold repetition, or insufficient material.
* Otherwise, returns false.
* @returns True if the game has ended via checkmate, stalemate, draw,
* threefold repetition, or insufficient material. Otherwise, returns
* false.
*/
game_over(): boolean
/**
* Returns a validation object specifying validity or the errors found
* within the FEN string.
* @param fen the fen formatted string to validate
*/
validate_fen(fen: string): {
/** Indicates if the fen is valid or not. */
valid: boolean
/**
* If not valid, then this will a type of error used internally in
* chess.js. Otherwise 0.
*/
error_number: number
/**
* The string "No errors." if valid. Otherwise a string explaining why
* it is not valid.
*/
error: string
}
/**
* Returns the FEN string for the current position.
* @returns the FEN string for the current position.
*/
fen(): string
/**
* Returns the game in PGN format.
* Options is an optional parameter which may include max width and/or a
* newline character settings.
* @param options optional object which may include max width and/or a
* newline character settings.
* @returns the current game state in PGN format.
*/
pgn(options?: {
/** the maximum width of a line */
max_width?: number | undefined
/** Specific newline character */
newline_char?: string | undefined
}): string
/**
* Load the moves of a game stored in Portable Game Notation.
* @param pgn the pgn should be a string in Portable Game Notation.
* @param options An optional object which may contain a string
* newline_char and a boolean sloppy.
* @returns The method will return true if the PGN was parsed successfully,
* otherwise false.
*/
load_pgn(
pgn: string,
options?: {
/**
* The newline_char is a string representation of a valid RegExp
* fragment and is used to process the PGN.
* It defaults to \r?\n.
* Special characters should not be pre-escaped, but any literal
* special characters should be escaped as is normal for a RegExp.
* Keep in mind that backslashes in JavaScript strings must
* themselves be escaped.
* Avoid using a newline_char that may occur elsewhere in a PGN,
* such as . or x, as this will result in unexpected behavior.
*/
newline_char?: string | undefined
/**
* The sloppy flag is a boolean that permits chess.js to parse moves in
* non-standard notations.
* See .move documentation for more information about non-SAN
* notations.
*/
sloppy?: boolean | undefined
}
): boolean
/**
* Allows header information to be added to PGN output.
* Any number of key/value pairs can be passed to .header(), with each
* first arg being treated as a header key, and each second as the value.
* @param args (optional) Header pairs to store in the header.
* @returns The current header information after storing any values.
*/
header(...args: string[]): { [key: string]: string | undefined }
/**
* Returns a string containing an ASCII diagram of the current position.
* @returns A string containing an ASCII diagram of the current position.
*/
ascii(): string
/**
* Returns the current side to move.
* @returns "b" if Black is the side to move, otherwise "w" for White.
*/
turn(): 'b' | 'w'
/**
* Attempts to make a move on the board, returning a move object if the
* move was legal, otherwise null.
* The .move function can be called two ways, by passing a string in
* Standard Algebraic Notation (SAN),
* Or by passing .move() a move object (only the 'to', 'from', and when
* necessary 'promotion', fields are needed).
* @param move Must be either a string in Standard Algebraic Notation
* (SAN), or a move object (only the 'to', 'from', and when necessary
* 'promotion', fields are needed)
* @param options An optional sloppy flag can be used to parse a variety of
* non-standard move notations:
* @returns The move as a full object is returned if the move was valid,
* and the chess board's state changes.
* If the move was invalid, null is returned and the state does not update.
*/
move(
move: string | ShortMove,
options?: {
/**
* An optional sloppy flag can be used to parse a variety of
* non-standard move notations.
*/
sloppy?: boolean | undefined
},
type: MoveType
): Move | null
moveFromDistance(
move: string | ShortMove,
options?: {
/**
* An optional sloppy flag can be used to parse a variety of
* non-standard move notations.
*/
sloppy?: boolean | undefined
}
): Move | null
/**
* Take back the last half-move, returning a move object if successful,
* otherwise null.
* @returns the move object that was undone if successful, otherwise null.
*/
undo(): Move | null
/**
* Clears the board of all pieces.
*/
clear(): void
/**
* Place a piece on the square where piece is an object with the form
* { type: ..., color: ... }.
* put() will fail when passed an invalid piece or square, or when two
* or more kings of the same color are placed.
* @param piece the piece to put somewhere on the game board.
* @param square the square on the chess board to place the piece at.
* @returns true if the piece was successfully placed, otherwise, the
* board remains unchanged and false is returned.
*/
put(piece: Piece, square: Square): boolean
/**
* Returns the piece on the square
* @param square the square to get the piece on.
* @returns null if no piece is on that square, or it is not a valid
* square. Otherwise a piece object.
*/
get(square: Square): Piece | null
/**
* Remove and return the piece on square.
* @param square the square to remove the piece from, e.g. "b6"
* @returns null if no piece was removed, otherwise an object with the
* removed piece's type and color.
*/
remove(square: Square): Piece | null
// This is a debugging utility, and is not documented in their readme.
// So it is not included in these type definitions.
// perft(depth: number): number,
/**
* Returns the color of the square ('light' or 'dark').
* @param square the square to check if it is light or dark.
* @returns "light" if a light square, "dark" if a dark square, or null if
* not a valid square.
*/
square_color(square: Square): 'light' | 'dark'
/**
* Returns the color of the square ('light' or 'dark').
* @param square the square to check if it is light or dark.
* @returns "light" if a light square, "dark" if a dark square, or null if
* not a valid square.
*/
square_color(square: string): 'light' | 'dark' | null
/**
* Returns a list containing the moves of the current game.
* Options is an optional parameter which may contain a 'verbose' flag.
* See .moves() for a description of the verbose move fields.
* @param options an optional parameter which may contain a 'verbose' flag.
* @returns a list of all moves of the current game. They will be strings
* if not passed the verbose flag.
*/
history(options?: {
/**
* Pass true if you want this function to output verbose objects
* instead of strings.
*/
verbose?: false | undefined
}): string[]
/**
* Returns a list containing the moves of the current game.
* Options is an optional parameter which may contain a 'verbose' flag.
* See .moves() for a description of the verbose move fields.
* @param options an optional parameter which may contain a 'verbose' flag.
* @returns a list of all moves of the current game. They will be strings
* if not passed the verbose flag.
*/
history(options: {
/**
* Pass true if you want this function to output verbose objects
* instead of strings.
*/
verbose: true
}): Move[]
/**
* Returns a list containing the moves of the current game.
* Options is an optional parameter which may contain a 'verbose' flag.
* See .moves() for a description of the verbose move fields.
* @param options an optional parameter which may contain a 'verbose' flag.
* @returns a list of all moves of the current game. They will be strings
* if not passed the verbose flag.
*/
history(options?: {
/**
* Pass true if you want this function to output verbose objects
* instead of strings.
*/
verbose?: boolean | undefined
}): string[] | Move[]
board(): Array<Array<{ type: PieceType; color: 'w' | 'b' } | null>>
get_comment(): string | undefined
set_comment(comment: string): void
delete_comment(): string | undefined
get_comments(): Comment[]
delete_comments(): Comment[]
}
/**
* The chess.js function that is used to build chess game instances.
* It can be used with or without `new` to build your instance. Both variants
* work the same.
*/
export const Chess: {
/**
* The Chess() constructor takes an optional parameter which specifies
* the board configuration in Forsyth-Edwards Notation.
* @param fen specifies the board configuration in Forsyth-Edwards Notation.
*/
(fen?: string): ChessInstance
/**
* The Chess() constructor takes an optional parameter which specifies
* the board configuration in Forsyth-Edwards Notation.
* @param fen specifies the board configuration in Forsyth-Edwards Notation.
*/
new (fen?: string): ChessInstance
}