-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameParser.ts
548 lines (481 loc) · 13.6 KB
/
GameParser.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
import {
Action,
Game,
GameProcessorAnalysisConfig,
Move,
MoveAction
} from '../interfaces/index.js';
import { FileLetter, PieceToken, PlayerColor, Token } from '../types/index.js';
import ChessBoard from './ChessBoard.js';
import Utils from './Utils.js';
const moveCfg = {
Q: {
line: true,
diag: true
},
R: {
line: true,
diag: false
},
B: {
line: false,
diag: true
},
N: {
line: false,
diag: false
}
};
class MoveNotFoundException extends Error {
constructor(
token: string,
player: PlayerColor,
tarRow: number,
tarCol: number
) {
super(
`${player}: No piece for move ${token} to (${tarRow},${tarCol}) found!`
);
this.name = 'MoveNotFoundError';
}
}
class GameParser {
board: ChessBoard;
activePlayer: PlayerColor;
constructor() {
this.board = new ChessBoard();
this.activePlayer = 'w';
}
/**
* Main function for parsing a read-in PGN game. In here the moves are transformed from algebraic notation
* to a list of different Actions like `MoveAction` or `CaptureAction`. This parsed data is the passed
* into the Trackers for generating the stats.
* @param game A game read-in by the GameProcessor class.
* @param analysisCfg Trackers into which the parsed data should be passed.
*/
processGame(game: Game, analysisCfg: GameProcessorAnalysisConfig): void {
// game based analyzers
for (const analyzer of analysisCfg.analyzers.game) {
analyzer.analyze(game);
}
const { moves } = game;
this.activePlayer = 'w';
try {
for (const move of moves) {
// parse move from san to coordinates (and meta info)
const currentMoveActions = this.parseMove(move);
// move based analyzers
for (const analyzer of analysisCfg.analyzers.move) {
analyzer.analyze(currentMoveActions);
}
this.board.applyActions(currentMoveActions);
this.activePlayer = this.activePlayer === 'w' ? 'b' : 'w';
}
} catch (err) {
console.log(game);
this.board.printPosition();
throw err;
}
// notify move analyzers that the current game is done
for (const analyzer of analysisCfg.analyzers.move) {
analyzer.nextGame?.();
}
analysisCfg.processedMoves += moves.length;
analysisCfg.processedGames += 1;
this.board.reset();
}
/**
* Resets the board so a new game can be started.
*/
reset(): void {
this.board.reset();
this.activePlayer = 'w';
}
/**
* Main function to parse a single move from algebraic notation to a list of `Action`s.
* @param rawMove The move in standard algebraic notation.
* @returns A list of `Action`s to perform on the board.
*/
private parseMove(rawMove: string): Action[] {
const token = rawMove.substring(0, 1) as Token;
const san = GameParser.preprocess(rawMove);
if (token.toLowerCase() === token) {
return this.pawnMove(san);
} else if (token === 'O') {
return this.castle(san);
}
return this.pieceMove(san);
}
/**
* Parses a single pawn move from algebraic notation to a list of `Action`s.
* @param san The move in standard algebraic notation.
* @returns A list of `Action`s to perform on the board.
*/
private pawnMove(san: string): Action[] {
const actions: Action[] = [];
const player = this.activePlayer;
const direction = player === 'w' ? 1 : -1;
let offset = 0;
const coords: Move = { from: [], to: [] };
// capture
if (san.includes('x')) {
const sanAdapted = san.replace('x', '');
coords.to[0] = 8 - parseInt(sanAdapted.substring(2, 3), 10);
coords.to[1] = Utils.getFileNumber(
sanAdapted.substring(1, 2) as FileLetter
);
coords.from[0] = coords.to[0] + direction;
coords.from[1] = Utils.getFileNumber(
sanAdapted.substring(0, 1) as FileLetter
);
// en passant
if (this.board.getPieceOnCoords(coords.to) === null) {
offset = player === 'w' ? 1 : -1;
}
const takenOn = [coords.to[0] + offset, coords.to[1]];
const takingPiece = this.board.getPieceOnCoords(coords.from)?.name;
const takenPiece = this.board.getPieceOnCoords(takenOn)?.name;
// if (!takingPiece || !takenPiece) throw new Error();
actions.push({
type: 'capture',
san,
player,
on: takenOn,
takingPiece,
takenPiece
});
// moves
} else {
const tarRow = 8 - parseInt(san.substring(1, 2), 10);
const tarCol = Utils.getFileNumber(
san.substring(0, 1) as FileLetter
);
coords.from = [-1, tarCol]; // -1: to be found in for loop
coords.to = [tarRow, tarCol];
for (let i = 1; i <= 2; i += 1) {
if (
this.board
.getPieceOnCoords([tarRow + i * direction, tarCol])
?.name.startsWith('P')
) {
coords.from[0] = tarRow + i * direction;
break;
}
}
}
const piece = this.board.getPieceOnCoords(coords.from)?.name;
// if (!piece) throw new Error();
// move action must always come after capture action
actions.push({
type: 'move',
san,
player,
piece,
from: coords.from,
to: coords.to
});
// promotes
if (san.includes('=')) {
actions.push({
type: 'promote',
san,
player,
on: coords.to,
to: san.slice(-1)
});
}
return actions;
}
/**
* Parses a single piece move (KQBNR) from algebraic notation to a list of `Action`s.
* @param san The move in standard algebraic notation.
* @returns A list of `Action`s to perform on the board.
*/
private pieceMove(san: string): Action[] {
const actions: Action[] = [];
const player = this.activePlayer;
let capture = false;
let coords: Move = { from: [], to: [] };
const token = san.substring(0, 1) as PieceToken;
// create copy of san to be able to remove characters without altering the original san
// remove token
let tempSan = san.substring(1);
// capture
if (tempSan.includes('x')) {
capture = true;
tempSan = tempSan.replace('x', '');
}
// e.g. Re3f5
if (tempSan.length === 4) {
coords.from[0] = 8 - parseInt(tempSan.substring(1, 2), 10);
coords.from[1] = Utils.getFileNumber(
tempSan.substring(0, 1) as FileLetter
);
coords.to[0] = 8 - parseInt(tempSan.substring(3, 4), 10);
coords.to[1] = Utils.getFileNumber(
tempSan.substring(2, 3) as FileLetter
);
// e.g. Ref3
} else if (tempSan.length === 3) {
const tarRow = 8 - parseInt(tempSan.substring(2, 3), 10);
const tarCol = Utils.getFileNumber(
tempSan.substring(1, 2) as FileLetter
);
let mustBeInRow: number | null = null; // to be found
let mustBeInCol: number | null = null; // to be found
// file is specified
const firstChar = tempSan.substring(0, 1);
if (firstChar.match(/\D/)) {
mustBeInCol = Utils.getFileNumber(firstChar as FileLetter);
// rank is specified
} else {
mustBeInRow = 8 - Number(firstChar);
}
coords = this.findPiece(
tarRow,
tarCol,
mustBeInRow,
mustBeInCol,
token,
player
);
// e.g. Rf3
} else {
const tarRow = 8 - parseInt(tempSan.substring(1, 2), 10);
const tarCol = Utils.getFileNumber(
tempSan.substring(0, 1) as FileLetter
);
coords = this.findPiece(tarRow, tarCol, null, null, token, player);
}
const piece = this.board.getPieceOnCoords(coords.from)?.name;
// if (!piece) throw new Error();
if (capture) {
const takenPiece = this.board.getPieceOnCoords(coords.to)?.name;
// if (!takenPiece) throw new Error();
actions.push({
type: 'capture',
san,
player,
on: coords.to,
takingPiece: piece,
takenPiece
});
}
// move action must always come after capture action
actions.push({
type: 'move',
san,
player,
piece,
from: coords.from,
to: coords.to
});
return actions;
}
/**
* Parses a castle move ('O-O' or 'O-O-O') from algebraic notation to a list of `MoveAction`s.
* @param san The move in standard algebraic notation.
* @returns A list of `MoveAction`s to perform on the board.
*/
private castle(san: string): MoveAction[] {
const actions: MoveAction[] = [];
const player = this.activePlayer;
const row = player === 'w' ? 7 : 0;
switch (san) {
case 'O-O': {
actions.push({
type: 'move',
san,
player,
piece: 'Ke',
from: [row, 4],
to: [row, 6]
});
actions.push({
type: 'move',
san,
player,
piece: 'Rh',
from: [row, 7],
to: [row, 5]
});
break;
}
case 'O-O-O':
actions.push({
type: 'move',
san,
player,
piece: 'Ke',
from: [row, 4],
to: [row, 2]
});
actions.push({
type: 'move',
san,
player,
piece: 'Ra',
from: [row, 0],
to: [row, 3]
});
break;
default:
console.log(
'Castle action called without a castle SAN. This is probably a bug.'
);
break;
}
return actions;
}
/**
* Search the current position for a piece that could perform the move.
* @param tarRow Row number (0-7) of the square the piece should move to.
* @param tarCol Column number (0-7) of the square the piece should move to.
* @param mustBeInRow If value is passed, only pieces in that row are checked.
* @param mustBeInCol If value is passed, only pieces in that column are checked.
* @param token Type of piece that is looked for.
* @param player The moving player
* @returns The move which will fulfill all criteria.
*/
private findPiece(
tarRow: number,
tarCol: number,
mustBeInRow: number | null,
mustBeInCol: number | null,
token: PieceToken,
player: PlayerColor
): Move {
const to = [tarRow, tarCol];
// get array of positions of pieces of type <token>
let validPieces = this.board.getPositionsForToken(player, token);
// filter pieces that can reach target square
if (validPieces.length > 1) {
validPieces = validPieces.filter((val) => {
const mustBeInFulfilled =
(mustBeInRow === null || val[0] === mustBeInRow) &&
(mustBeInCol === null || val[1] === mustBeInCol);
return (
mustBeInFulfilled &&
((moveCfg[token as Exclude<PieceToken, 'K'>].line &&
(val[0] === tarRow || val[1] === tarCol)) ||
(moveCfg[token as Exclude<PieceToken, 'K'>].diag &&
Math.abs(val[0] - tarRow) ===
Math.abs(val[1] - tarCol)) ||
(token === 'N' &&
((Math.abs(val[0] - tarRow) === 2 &&
Math.abs(val[1] - tarCol) === 1) ||
(Math.abs(val[0] - tarRow) === 1 &&
Math.abs(val[1] - tarCol) === 2))))
);
});
}
// if only one piece is left, move is found
if (validPieces.length === 1) {
return {
from: validPieces[0],
to
};
}
// else: one of the remaining pieces cannot move because of obstruction or it
// would result in the king being in check. Find the allowed piece.
for (const piece of validPieces) {
let obstructed = false;
if (token !== 'N') {
const diff = [tarRow - piece[0], tarCol - piece[1]];
const steps = Math.max(...diff.map((val) => Math.abs(val)));
const dir = [Math.sign(diff[0]), Math.sign(diff[1])];
for (let i = 1; i < steps && !obstructed; i += 1) {
if (
this.board.getPieceOnCoords([
piece[0] + i * dir[0],
piece[1] + i * dir[1]
])
) {
obstructed = true;
}
}
}
if (!obstructed && !this.checkCheck({ from: piece, to }, player)) {
return {
from: piece,
to
};
}
}
throw new MoveNotFoundException(token, player, tarRow, tarCol);
}
/**
* Checks if performing the move would result in the king being in check.
*
* @param move The move to be checked.
* @param player The moving player.
* @returns King would be in check true/false
*/
private checkCheck(move: Move, player: PlayerColor): boolean {
const { from, to } = move;
const opColor = player === 'w' ? 'b' : 'w';
const king = this.board.getPiecePosition(player, 'Ke');
// check if moving piece is on same line/diag as king, else exit
const diff = [from[0] - king[0], from[1] - king[1]];
const checkFor: string[] = [];
if (diff[0] === 0 || diff[1] === 0) {
checkFor[0] = 'Q';
checkFor[1] = 'R';
} else if (Math.abs(diff[0]) === Math.abs(diff[1])) {
checkFor[0] = 'Q';
checkFor[1] = 'B';
} else {
return false;
}
const vertDir = Math.sign(diff[0]);
const horzDir = Math.sign(diff[1]);
// calculate distance from king to edge of board in direction of the moving piece
let distanceHorizontal = 8;
if (horzDir !== 0) {
distanceHorizontal = horzDir === -1 ? king[1] : 8 - 1 - king[1];
}
let distanceVertical = 8;
if (vertDir !== 0) {
distanceVertical = vertDir === -1 ? king[0] : 8 - 1 - king[0];
}
const distanceToEdge = Math.min(distanceHorizontal, distanceVertical);
if (distanceToEdge < 2) return false;
const srcTilePiece = this.board.getPieceOnCoords(from);
const tarTilePiece = this.board.getPieceOnCoords(to);
// premove
this.board.tiles[from[0]][from[1]] = null;
this.board.tiles[to[0]][to[1]] = srcTilePiece;
// check for check
let isInCheck = false;
for (let i = 1; i <= distanceToEdge; i += 1) {
const row = king[0] + i * vertDir;
const col = king[1] + i * horzDir;
const piece = this.board.getPieceOnCoords([row, col]);
if (piece) {
if (
checkFor.some((token) => piece.name.startsWith(token)) &&
piece.color === opColor
) {
isInCheck = true;
break;
} else {
// way is obstructed by other piece
break;
}
}
}
// revert premove
this.board.tiles[from[0]][from[1]] = srcTilePiece;
this.board.tiles[to[0]][to[1]] = tarTilePiece;
return isInCheck;
}
/**
* Helper function to remove special tokens (#, +, ?, !) like ratings (??, ?!) or the check mark (+) from the move.
* @param move The raw move in standard algebraic notation.
* @returns The cleaned move with removed special tokens.
*/
private static preprocess(move: string): string {
return move.replace(/[#+?!]/g, '');
}
}
export default GameParser;