Finish Ultima rules + a few technical fixes
[vchess.git] / public / javascripts / base_rules.js
CommitLineData
92342261
BA
1// (Orthodox) Chess rules are defined in ChessRules class.
2// Variants generally inherit from it, and modify some parts.
3
1d184b4c
BA
4class PiPo //Piece+Position
5{
6 // o: {piece[p], color[c], posX[x], posY[y]}
7 constructor(o)
8 {
9 this.p = o.p;
10 this.c = o.c;
11 this.x = o.x;
12 this.y = o.y;
13 }
14}
15
098e8468 16// TODO: for animation, moves should contains "moving" and "fading" maybe...
1d184b4c
BA
17class Move
18{
19 // o: {appear, vanish, [start,] [end,]}
20 // appear,vanish = arrays of PiPo
21 // start,end = coordinates to apply to trigger move visually (think castle)
22 constructor(o)
23 {
24 this.appear = o.appear;
25 this.vanish = o.vanish;
26 this.start = !!o.start ? o.start : {x:o.vanish[0].x, y:o.vanish[0].y};
27 this.end = !!o.end ? o.end : {x:o.appear[0].x, y:o.appear[0].y};
28 }
29}
30
31// NOTE: x coords = top to bottom; y = left to right (from white player perspective)
32class ChessRules
33{
34 // Path to pieces
35 static getPpath(b)
36 {
37 return b; //usual pieces in pieces/ folder
38 }
39 // Turn "wb" into "B" (for FEN)
40 static board2fen(b)
41 {
42 return b[0]=='w' ? b[1].toUpperCase() : b[1];
43 }
44 // Turn "p" into "bp" (for board)
45 static fen2board(f)
46 {
47 return f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f;
48 }
49
50 /////////////////
51 // INITIALIZATION
52
d3334c3a 53 // fen == "position flags"
dfb4afc1 54 constructor(fen, moves)
1d184b4c 55 {
dfb4afc1 56 this.moves = moves;
1d184b4c 57 // Use fen string to initialize variables, flags and board
1d184b4c 58 this.board = VariantRules.GetBoard(fen);
2526c041 59 this.setFlags(fen);
ffbae57a 60 this.initVariables(fen);
1d184b4c
BA
61 }
62
63 initVariables(fen)
64 {
65 this.INIT_COL_KING = {'w':-1, 'b':-1};
66 this.INIT_COL_ROOK = {'w':[-1,-1], 'b':[-1,-1]};
92342261 67 this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; //squares of white and black king
1d184b4c
BA
68 const fenParts = fen.split(" ");
69 const position = fenParts[0].split("/");
70 for (let i=0; i<position.length; i++)
71 {
6037f1d8
BA
72 let k = 0; //column index on board
73 for (let j=0; j<position[i].length; j++)
1d184b4c
BA
74 {
75 switch (position[i].charAt(j))
76 {
77 case 'k':
6037f1d8
BA
78 this.kingPos['b'] = [i,k];
79 this.INIT_COL_KING['b'] = k;
1d184b4c
BA
80 break;
81 case 'K':
6037f1d8
BA
82 this.kingPos['w'] = [i,k];
83 this.INIT_COL_KING['w'] = k;
1d184b4c
BA
84 break;
85 case 'r':
86 if (this.INIT_COL_ROOK['b'][0] < 0)
6037f1d8 87 this.INIT_COL_ROOK['b'][0] = k;
1d184b4c 88 else
6037f1d8 89 this.INIT_COL_ROOK['b'][1] = k;
1d184b4c
BA
90 break;
91 case 'R':
92 if (this.INIT_COL_ROOK['w'][0] < 0)
6037f1d8 93 this.INIT_COL_ROOK['w'][0] = k;
1d184b4c 94 else
6037f1d8 95 this.INIT_COL_ROOK['w'][1] = k;
1d184b4c
BA
96 break;
97 default:
98 let num = parseInt(position[i].charAt(j));
99 if (!isNaN(num))
6037f1d8 100 k += (num-1);
1d184b4c 101 }
6037f1d8 102 k++;
1d184b4c
BA
103 }
104 }
f3802fcd 105 const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
1d184b4c 106 this.epSquares = [ epSq ];
1d184b4c
BA
107 }
108
109 // Turn diagram fen into double array ["wb","wp","bk",...]
110 static GetBoard(fen)
111 {
112 let rows = fen.split(" ")[0].split("/");
46302e64 113 const [sizeX,sizeY] = VariantRules.size;
1d184b4c
BA
114 let board = doubleArray(sizeX, sizeY, "");
115 for (let i=0; i<rows.length; i++)
116 {
117 let j = 0;
118 for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++)
119 {
120 let character = rows[i][indexInRow];
121 let num = parseInt(character);
122 if (!isNaN(num))
123 j += num; //just shift j
124 else //something at position i,j
125 board[i][j++] = VariantRules.fen2board(character);
126 }
127 }
128 return board;
129 }
130
dda21a71 131 // Extract (relevant) flags from fen
2526c041 132 setFlags(fen)
1d184b4c
BA
133 {
134 // white a-castle, h-castle, black a-castle, h-castle
2526c041
BA
135 this.castleFlags = {'w': new Array(2), 'b': new Array(2)};
136 let flags = fen.split(" ")[1]; //flags right after position
1d184b4c 137 for (let i=0; i<4; i++)
2526c041 138 this.castleFlags[i < 2 ? 'w' : 'b'][i%2] = (flags.charAt(i) == '1');
1d184b4c
BA
139 }
140
141 ///////////////////
142 // GETTERS, SETTERS
143
1d184b4c
BA
144 static get size() { return [8,8]; }
145 // Two next functions return 'undefined' if called on empty square
146 getColor(i,j) { return this.board[i][j].charAt(0); }
147 getPiece(i,j) { return this.board[i][j].charAt(1); }
148
149 // Color
150 getOppCol(color) { return color=="w" ? "b" : "w"; }
151
152 get lastMove() {
153 const L = this.moves.length;
154 return L>0 ? this.moves[L-1] : null;
155 }
156 get turn() {
d3334c3a 157 return this.moves.length%2==0 ? 'w' : 'b';
1d184b4c
BA
158 }
159
160 // Pieces codes
161 static get PAWN() { return 'p'; }
162 static get ROOK() { return 'r'; }
163 static get KNIGHT() { return 'n'; }
164 static get BISHOP() { return 'b'; }
165 static get QUEEN() { return 'q'; }
166 static get KING() { return 'k'; }
167
168 // Empty square
169 static get EMPTY() { return ''; }
170
171 // Some pieces movements
172 static get steps() {
173 return {
174 'r': [ [-1,0],[1,0],[0,-1],[0,1] ],
175 'n': [ [-1,-2],[-1,2],[1,-2],[1,2],[-2,-1],[-2,1],[2,-1],[2,1] ],
176 'b': [ [-1,-1],[-1,1],[1,-1],[1,1] ],
1d184b4c
BA
177 };
178 }
179
2526c041
BA
180 // Aggregates flags into one object
181 get flags() {
182 return this.castleFlags;
183 }
184
185 // Reverse operation
186 parseFlags(flags)
187 {
188 this.castleFlags = flags;
189 }
190
1d184b4c
BA
191 // En-passant square, if any
192 getEpSquare(move)
193 {
194 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
195 if (this.getPiece(sx,sy) == VariantRules.PAWN && Math.abs(sx - ex) == 2)
196 {
197 return {
198 x: (sx + ex)/2,
199 y: sy
200 };
201 }
202 return undefined; //default
203 }
204
dda21a71 205 // Can thing on square1 take thing on square2
46302e64 206 canTake([x1,y1], [x2,y2])
1d184b4c 207 {
46302e64 208 return this.getColor(x1,y1) != this.getColor(x2,y2);
1d184b4c
BA
209 }
210
211 ///////////////////
212 // MOVES GENERATION
213
214 // All possible moves from selected square (assumption: color is OK)
215 getPotentialMovesFrom([x,y])
216 {
1d184b4c
BA
217 switch (this.getPiece(x,y))
218 {
219 case VariantRules.PAWN:
46302e64 220 return this.getPotentialPawnMoves([x,y]);
1d184b4c 221 case VariantRules.ROOK:
46302e64 222 return this.getPotentialRookMoves([x,y]);
1d184b4c 223 case VariantRules.KNIGHT:
46302e64 224 return this.getPotentialKnightMoves([x,y]);
1d184b4c 225 case VariantRules.BISHOP:
46302e64 226 return this.getPotentialBishopMoves([x,y]);
1d184b4c 227 case VariantRules.QUEEN:
46302e64 228 return this.getPotentialQueenMoves([x,y]);
1d184b4c 229 case VariantRules.KING:
46302e64 230 return this.getPotentialKingMoves([x,y]);
1d184b4c
BA
231 }
232 }
233
234 // Build a regular move from its initial and destination squares; tr: transformation
46302e64 235 getBasicMove([sx,sy], [ex,ey], tr)
1d184b4c 236 {
2526c041 237 let mv = new Move({
1d184b4c
BA
238 appear: [
239 new PiPo({
240 x: ex,
241 y: ey,
46302e64
BA
242 c: !!tr ? tr.c : this.getColor(sx,sy),
243 p: !!tr ? tr.p : this.getPiece(sx,sy)
1d184b4c
BA
244 })
245 ],
246 vanish: [
247 new PiPo({
248 x: sx,
249 y: sy,
250 c: this.getColor(sx,sy),
251 p: this.getPiece(sx,sy)
252 })
253 ]
254 });
255
256 // The opponent piece disappears if we take it
257 if (this.board[ex][ey] != VariantRules.EMPTY)
258 {
259 mv.vanish.push(
260 new PiPo({
261 x: ex,
262 y: ey,
263 c: this.getColor(ex,ey),
264 p: this.getPiece(ex,ey)
265 })
266 );
267 }
268 return mv;
269 }
270
271 // Generic method to find possible moves of non-pawn pieces ("sliding or jumping")
46302e64 272 getSlideNJumpMoves([x,y], steps, oneStep)
1d184b4c 273 {
46302e64 274 const color = this.getColor(x,y);
2526c041 275 let moves = [];
46302e64 276 const [sizeX,sizeY] = VariantRules.size;
1d184b4c
BA
277 outerLoop:
278 for (let step of steps)
279 {
46302e64
BA
280 let i = x + step[0];
281 let j = y + step[1];
1221ac47
BA
282 while (i>=0 && i<sizeX && j>=0 && j<sizeY
283 && this.board[i][j] == VariantRules.EMPTY)
1d184b4c 284 {
46302e64 285 moves.push(this.getBasicMove([x,y], [i,j]));
1d184b4c
BA
286 if (oneStep !== undefined)
287 continue outerLoop;
288 i += step[0];
289 j += step[1];
290 }
efb20746 291 if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.canTake([x,y], [i,j]))
46302e64 292 moves.push(this.getBasicMove([x,y], [i,j]));
1d184b4c
BA
293 }
294 return moves;
295 }
296
dda21a71 297 // What are the pawn moves from square x,y ?
46302e64 298 getPotentialPawnMoves([x,y])
1d184b4c 299 {
2526c041
BA
300 const color = this.turn;
301 let moves = [];
302 const V = VariantRules;
1221ac47 303 const [sizeX,sizeY] = V.size;
2526c041 304 const shift = (color == "w" ? -1 : 1);
cf130369
BA
305 const firstRank = (color == 'w' ? sizeX-1 : 0);
306 const startRank = (color == "w" ? sizeX-2 : 1);
307 const lastRank = (color == "w" ? 0 : sizeX-1);
1d184b4c
BA
308
309 if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
310 {
311 // Normal moves
312 if (this.board[x+shift][y] == V.EMPTY)
313 {
46302e64 314 moves.push(this.getBasicMove([x,y], [x+shift,y]));
1221ac47 315 // Next condition because variants with pawns on 1st rank allow them to jump
2526c041 316 if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY)
1d184b4c
BA
317 {
318 // Two squares jump
46302e64 319 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
1d184b4c
BA
320 }
321 }
322 // Captures
1221ac47
BA
323 if (y>0 && this.canTake([x,y], [x+shift,y-1])
324 && this.board[x+shift][y-1] != V.EMPTY)
325 {
46302e64 326 moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
1221ac47
BA
327 }
328 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
329 && this.board[x+shift][y+1] != V.EMPTY)
330 {
46302e64 331 moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
1221ac47 332 }
1d184b4c
BA
333 }
334
335 if (x+shift == lastRank)
336 {
337 // Promotion
68f5ccc8 338 const pawnColor = this.getColor(x,y); //can be different for checkered
1d184b4c
BA
339 let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
340 promotionPieces.forEach(p => {
341 // Normal move
342 if (this.board[x+shift][y] == V.EMPTY)
68f5ccc8 343 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:pawnColor,p:p}));
1d184b4c 344 // Captures
1221ac47
BA
345 if (y>0 && this.canTake([x,y], [x+shift,y-1])
346 && this.board[x+shift][y-1] != V.EMPTY)
347 {
68f5ccc8 348 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p}));
1221ac47
BA
349 }
350 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
351 && this.board[x+shift][y+1] != V.EMPTY)
352 {
68f5ccc8 353 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:pawnColor,p:p}));
1221ac47 354 }
1d184b4c
BA
355 });
356 }
357
358 // En passant
359 const Lep = this.epSquares.length;
360 const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
361 if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1)
362 {
363 let epStep = epSquare.y - y;
46302e64 364 var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
1d184b4c
BA
365 enpassantMove.vanish.push({
366 x: x,
367 y: y+epStep,
368 p: 'p',
369 c: this.getColor(x,y+epStep)
370 });
371 moves.push(enpassantMove);
372 }
373
374 return moves;
375 }
376
377 // What are the rook moves from square x,y ?
46302e64 378 getPotentialRookMoves(sq)
1d184b4c 379 {
46302e64 380 return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.ROOK]);
1d184b4c
BA
381 }
382
383 // What are the knight moves from square x,y ?
46302e64 384 getPotentialKnightMoves(sq)
1d184b4c 385 {
92342261
BA
386 return this.getSlideNJumpMoves(
387 sq, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
1d184b4c
BA
388 }
389
390 // What are the bishop moves from square x,y ?
46302e64 391 getPotentialBishopMoves(sq)
1d184b4c 392 {
46302e64 393 return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.BISHOP]);
1d184b4c
BA
394 }
395
396 // What are the queen moves from square x,y ?
46302e64 397 getPotentialQueenMoves(sq)
1d184b4c 398 {
a37076f1
BA
399 const V = VariantRules;
400 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
1d184b4c
BA
401 }
402
403 // What are the king moves from square x,y ?
46302e64 404 getPotentialKingMoves(sq)
1d184b4c 405 {
a37076f1 406 const V = VariantRules;
1d184b4c 407 // Initialize with normal moves
a37076f1
BA
408 let moves = this.getSlideNJumpMoves(sq,
409 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
46302e64 410 return moves.concat(this.getCastleMoves(sq));
1d184b4c
BA
411 }
412
46302e64 413 getCastleMoves([x,y])
1d184b4c 414 {
46302e64 415 const c = this.getColor(x,y);
c6052161
BA
416 const [sizeX,sizeY] = VariantRules.size;
417 if (x != (c=="w" ? sizeX-1 : 0) || y != this.INIT_COL_KING[c])
1d184b4c
BA
418 return []; //x isn't first rank, or king has moved (shortcut)
419
420 const V = VariantRules;
421
422 // Castling ?
423 const oppCol = this.getOppCol(c);
424 let moves = [];
425 let i = 0;
c6052161 426 const finalSquares = [ [2,3], [sizeY-2,sizeY-3] ]; //king, then rook
1d184b4c
BA
427 castlingCheck:
428 for (let castleSide=0; castleSide < 2; castleSide++) //large, then small
429 {
2526c041 430 if (!this.castleFlags[c][castleSide])
1d184b4c
BA
431 continue;
432 // If this code is reached, rooks and king are on initial position
433
434 // Nothing on the path of the king (and no checks; OK also if y==finalSquare)?
435 let step = finalSquares[castleSide][0] < y ? -1 : 1;
436 for (i=y; i!=finalSquares[castleSide][0]; i+=step)
437 {
cf130369 438 if (this.isAttacked([x,i], [oppCol]) || (this.board[x][i] != V.EMPTY &&
1d184b4c
BA
439 // NOTE: next check is enough, because of chessboard constraints
440 (this.getColor(x,i) != c || ![V.KING,V.ROOK].includes(this.getPiece(x,i)))))
441 {
442 continue castlingCheck;
443 }
444 }
445
446 // Nothing on the path to the rook?
447 step = castleSide == 0 ? -1 : 1;
448 for (i = y + step; i != this.INIT_COL_ROOK[c][castleSide]; i += step)
449 {
450 if (this.board[x][i] != V.EMPTY)
451 continue castlingCheck;
452 }
453 const rookPos = this.INIT_COL_ROOK[c][castleSide];
454
455 // Nothing on final squares, except maybe king and castling rook?
456 for (i=0; i<2; i++)
457 {
458 if (this.board[x][finalSquares[castleSide][i]] != V.EMPTY &&
459 this.getPiece(x,finalSquares[castleSide][i]) != V.KING &&
460 finalSquares[castleSide][i] != rookPos)
461 {
462 continue castlingCheck;
463 }
464 }
465
466 // If this code is reached, castle is valid
467 moves.push( new Move({
468 appear: [
469 new PiPo({x:x,y:finalSquares[castleSide][0],p:V.KING,c:c}),
470 new PiPo({x:x,y:finalSquares[castleSide][1],p:V.ROOK,c:c})],
471 vanish: [
472 new PiPo({x:x,y:y,p:V.KING,c:c}),
473 new PiPo({x:x,y:rookPos,p:V.ROOK,c:c})],
474 end: Math.abs(y - rookPos) <= 2
475 ? {x:x, y:rookPos}
476 : {x:x, y:y + 2 * (castleSide==0 ? -1 : 1)}
477 }) );
478 }
479
480 return moves;
481 }
482
483 ///////////////////
484 // MOVES VALIDATION
485
46302e64 486 canIplay(side, [x,y])
1d184b4c 487 {
92342261
BA
488 return ((side=='w' && this.moves.length%2==0)
489 || (side=='b' && this.moves.length%2==1))
46302e64 490 && this.getColor(x,y) == side;
1d184b4c
BA
491 }
492
493 getPossibleMovesFrom(sq)
494 {
495 // Assuming color is right (already checked)
496 return this.filterValid( this.getPotentialMovesFrom(sq) );
497 }
498
92342261 499 // TODO: promotions (into R,B,N,Q) should be filtered only once
1d184b4c
BA
500 filterValid(moves)
501 {
502 if (moves.length == 0)
503 return [];
b8121223 504 return moves.filter(m => { return !this.underCheck(m); });
1d184b4c
BA
505 }
506
507 // Search for all valid moves considering current turn (for engine and game end)
46302e64 508 getAllValidMoves()
1d184b4c 509 {
46302e64 510 const color = this.turn;
1d184b4c 511 const oppCol = this.getOppCol(color);
c6052161
BA
512 let potentialMoves = [];
513 const [sizeX,sizeY] = VariantRules.size;
a6abf094 514 for (let i=0; i<sizeX; i++)
1d184b4c 515 {
a6abf094 516 for (let j=0; j<sizeY; j++)
1d184b4c 517 {
92342261 518 // Next condition "!= oppCol" = harmless hack to work with checkered variant
1d184b4c
BA
519 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
520 Array.prototype.push.apply(potentialMoves, this.getPotentialMovesFrom([i,j]));
521 }
522 }
523 // NOTE: prefer lazy undercheck tests, letting the king being taken?
524 // No: if happen on last 1/2 move, could lead to forbidden moves, wrong evals
525 return this.filterValid(potentialMoves);
526 }
9de73b71 527
e64a4eff 528 // Stop at the first move found
46302e64 529 atLeastOneMove()
e64a4eff 530 {
46302e64 531 const color = this.turn;
e64a4eff 532 const oppCol = this.getOppCol(color);
c6052161 533 const [sizeX,sizeY] = VariantRules.size;
9de73b71 534 for (let i=0; i<sizeX; i++)
e64a4eff 535 {
9de73b71 536 for (let j=0; j<sizeY; j++)
e64a4eff
BA
537 {
538 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
539 {
540 const moves = this.getPotentialMovesFrom([i,j]);
541 if (moves.length > 0)
542 {
9de73b71 543 for (let k=0; k<moves.length; k++)
e64a4eff 544 {
9de73b71 545 if (this.filterValid([moves[k]]).length > 0)
e64a4eff
BA
546 return true;
547 }
548 }
549 }
550 }
551 }
552 return false;
553 }
1d184b4c 554
dda21a71 555 // Check if pieces of color in array 'colors' are attacking square x,y
46302e64 556 isAttacked(sq, colors)
1d184b4c 557 {
46302e64
BA
558 return (this.isAttackedByPawn(sq, colors)
559 || this.isAttackedByRook(sq, colors)
560 || this.isAttackedByKnight(sq, colors)
561 || this.isAttackedByBishop(sq, colors)
562 || this.isAttackedByQueen(sq, colors)
563 || this.isAttackedByKing(sq, colors));
1d184b4c
BA
564 }
565
dda21a71 566 // Is square x,y attacked by 'colors' pawns ?
46302e64 567 isAttackedByPawn([x,y], colors)
1d184b4c 568 {
efb20746 569 const [sizeX,sizeY] = VariantRules.size;
46302e64 570 for (let c of colors)
1d184b4c 571 {
46302e64 572 let pawnShift = (c=="w" ? 1 : -1);
efb20746 573 if (x+pawnShift>=0 && x+pawnShift<sizeX)
1d184b4c 574 {
46302e64 575 for (let i of [-1,1])
1d184b4c 576 {
efb20746 577 if (y+i>=0 && y+i<sizeY && this.getPiece(x+pawnShift,y+i)==VariantRules.PAWN
46302e64
BA
578 && this.getColor(x+pawnShift,y+i)==c)
579 {
580 return true;
581 }
1d184b4c
BA
582 }
583 }
584 }
585 return false;
586 }
587
dda21a71 588 // Is square x,y attacked by 'colors' rooks ?
46302e64 589 isAttackedByRook(sq, colors)
1d184b4c 590 {
46302e64 591 return this.isAttackedBySlideNJump(sq, colors,
1d184b4c
BA
592 VariantRules.ROOK, VariantRules.steps[VariantRules.ROOK]);
593 }
594
dda21a71 595 // Is square x,y attacked by 'colors' knights ?
46302e64 596 isAttackedByKnight(sq, colors)
1d184b4c 597 {
46302e64 598 return this.isAttackedBySlideNJump(sq, colors,
1d184b4c
BA
599 VariantRules.KNIGHT, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
600 }
601
dda21a71 602 // Is square x,y attacked by 'colors' bishops ?
46302e64 603 isAttackedByBishop(sq, colors)
1d184b4c 604 {
46302e64 605 return this.isAttackedBySlideNJump(sq, colors,
1d184b4c
BA
606 VariantRules.BISHOP, VariantRules.steps[VariantRules.BISHOP]);
607 }
608
dda21a71 609 // Is square x,y attacked by 'colors' queens ?
46302e64 610 isAttackedByQueen(sq, colors)
1d184b4c 611 {
a37076f1
BA
612 const V = VariantRules;
613 return this.isAttackedBySlideNJump(sq, colors, V.QUEEN,
614 V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
1d184b4c
BA
615 }
616
dda21a71 617 // Is square x,y attacked by 'colors' king(s) ?
46302e64 618 isAttackedByKing(sq, colors)
1d184b4c 619 {
a37076f1
BA
620 const V = VariantRules;
621 return this.isAttackedBySlideNJump(sq, colors, V.KING,
622 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
1d184b4c
BA
623 }
624
1221ac47 625 // Generic method for non-pawn pieces ("sliding or jumping"):
dda21a71 626 // is x,y attacked by a piece of color in array 'colors' ?
46302e64 627 isAttackedBySlideNJump([x,y], colors, piece, steps, oneStep)
1d184b4c 628 {
efb20746 629 const [sizeX,sizeY] = VariantRules.size;
1d184b4c
BA
630 for (let step of steps)
631 {
632 let rx = x+step[0], ry = y+step[1];
efb20746
BA
633 while (rx>=0 && rx<sizeX && ry>=0 && ry<sizeY
634 && this.board[rx][ry] == VariantRules.EMPTY && !oneStep)
1d184b4c
BA
635 {
636 rx += step[0];
637 ry += step[1];
638 }
efb20746
BA
639 if (rx>=0 && rx<sizeX && ry>=0 && ry<sizeY
640 && this.board[rx][ry] != VariantRules.EMPTY
46302e64 641 && this.getPiece(rx,ry) == piece && colors.includes(this.getColor(rx,ry)))
1d184b4c
BA
642 {
643 return true;
644 }
645 }
646 return false;
647 }
648
dda21a71 649 // Is current player under check after his move ?
46302e64 650 underCheck(move)
1d184b4c 651 {
46302e64 652 const color = this.turn;
1d184b4c 653 this.play(move);
cf130369 654 let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
1d184b4c
BA
655 this.undo(move);
656 return res;
657 }
658
dda21a71 659 // On which squares is opponent under check after our move ?
46302e64 660 getCheckSquares(move)
4b5fe306
BA
661 {
662 this.play(move);
204e289b 663 const color = this.turn; //opponent
cf130369 664 let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
46302e64 665 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate!
4b5fe306
BA
666 : [ ];
667 this.undo(move);
668 return res;
669 }
670
1d184b4c
BA
671 // Apply a move on board
672 static PlayOnBoard(board, move)
673 {
674 for (let psq of move.vanish)
675 board[psq.x][psq.y] = VariantRules.EMPTY;
676 for (let psq of move.appear)
677 board[psq.x][psq.y] = psq.c + psq.p;
678 }
679 // Un-apply the played move
680 static UndoOnBoard(board, move)
681 {
682 for (let psq of move.appear)
683 board[psq.x][psq.y] = VariantRules.EMPTY;
684 for (let psq of move.vanish)
685 board[psq.x][psq.y] = psq.c + psq.p;
686 }
687
d3334c3a 688 // Before move is played, update variables + flags
1d184b4c
BA
689 updateVariables(move)
690 {
691 const piece = this.getPiece(move.start.x,move.start.y);
692 const c = this.getColor(move.start.x,move.start.y);
c6052161
BA
693 const [sizeX,sizeY] = VariantRules.size;
694 const firstRank = (c == "w" ? sizeX-1 : 0);
1d184b4c
BA
695
696 // Update king position + flags
697 if (piece == VariantRules.KING && move.appear.length > 0)
698 {
699 this.kingPos[c][0] = move.appear[0].x;
700 this.kingPos[c][1] = move.appear[0].y;
2526c041 701 this.castleFlags[c] = [false,false];
1d184b4c
BA
702 return;
703 }
704 const oppCol = this.getOppCol(c);
c6052161 705 const oppFirstRank = (sizeX-1) - firstRank;
1d184b4c
BA
706 if (move.start.x == firstRank //our rook moves?
707 && this.INIT_COL_ROOK[c].includes(move.start.y))
708 {
2526c041
BA
709 const flagIdx = (move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1);
710 this.castleFlags[c][flagIdx] = false;
1d184b4c
BA
711 }
712 else if (move.end.x == oppFirstRank //we took opponent rook?
aea1443e 713 && this.INIT_COL_ROOK[oppCol].includes(move.end.y))
1d184b4c 714 {
2526c041
BA
715 const flagIdx = (move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1);
716 this.castleFlags[oppCol][flagIdx] = false;
1d184b4c
BA
717 }
718 }
719
dda21a71
BA
720 // After move is undo-ed, un-update variables (flags are reset)
721 // TODO: more symmetry, by storing flags increment in move...
d3334c3a 722 unupdateVariables(move)
1d184b4c 723 {
d3334c3a
BA
724 // (Potentially) Reset king position
725 const c = this.getColor(move.start.x,move.start.y);
726 if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING)
727 this.kingPos[c] = [move.start.x, move.start.y];
728 }
1d184b4c 729
15c1295a
BA
730 // Hash of position+flags+turn after a move is played (to detect repetitions)
731 getHashState()
331fc58c 732 {
15c1295a 733 return hex_md5(this.getFen() + " " + this.turn);
331fc58c
BA
734 }
735
d3334c3a
BA
736 play(move, ingame)
737 {
a3c86ec9
BA
738 // DEBUG:
739// if (!this.states) this.states = [];
740// if (!ingame) this.states.push(JSON.stringify(this.board));
741
dfb4afc1 742 if (!!ingame)
6752407b 743 move.notation = [this.getNotation(move), this.getLongNotation(move)];
dfb4afc1 744
2526c041 745 move.flags = JSON.stringify(this.flags); //save flags (for undo)
d3334c3a
BA
746 this.updateVariables(move);
747 this.moves.push(move);
1d184b4c
BA
748 this.epSquares.push( this.getEpSquare(move) );
749 VariantRules.PlayOnBoard(this.board, move);
331fc58c
BA
750
751 if (!!ingame)
15c1295a 752 move.hash = this.getHashState();
1d184b4c
BA
753 }
754
cd4cad04 755 undo(move)
1d184b4c
BA
756 {
757 VariantRules.UndoOnBoard(this.board, move);
758 this.epSquares.pop();
d3334c3a
BA
759 this.moves.pop();
760 this.unupdateVariables(move);
2526c041 761 this.parseFlags(JSON.parse(move.flags));
a3c86ec9
BA
762
763 // DEBUG:
764// if (JSON.stringify(this.board) != this.states[this.states.length-1])
765// debugger;
766// this.states.pop();
1d184b4c
BA
767 }
768
769 //////////////
770 // END OF GAME
771
331fc58c 772 // Check for 3 repetitions (position + flags + turn)
1af36beb 773 checkRepetition()
1d184b4c 774 {
15c1295a
BA
775 if (!this.hashStates)
776 this.hashStates = {};
777 const startIndex =
778 Object.values(this.hashStates).reduce((a,b) => { return a+b; }, 0)
779 // Update this.hashStates with last move (or all moves if continuation)
780 // NOTE: redundant storage, but faster and moderate size
781 for (let i=startIndex; i<this.moves.length; i++)
782 {
783 const move = this.moves[i];
784 if (!this.hashStates[move.hash])
785 this.hashStates[move.hash] = 1;
786 else
787 this.hashStates[move.hash]++;
788 }
331fc58c 789 return Object.values(this.hashStates).some(elt => { return (elt >= 3); });
1af36beb 790 }
1d184b4c 791
dda21a71 792 // Is game over ? And if yes, what is the score ?
1af36beb
BA
793 checkGameOver()
794 {
795 if (this.checkRepetition())
796 return "1/2";
797
798 if (this.atLeastOneMove()) // game not over
1d184b4c 799 return "*";
1d184b4c
BA
800
801 // Game over
46302e64 802 return this.checkGameEnd();
1d184b4c
BA
803 }
804
46302e64
BA
805 // No moves are possible: compute score
806 checkGameEnd()
1d184b4c 807 {
46302e64 808 const color = this.turn;
1d184b4c 809 // No valid move: stalemate or checkmate?
cf130369 810 if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
1d184b4c
BA
811 return "1/2";
812 // OK, checkmate
813 return color == "w" ? "0-1" : "1-0";
814 }
815
816 ////////
817 //ENGINE
818
819 // Pieces values
820 static get VALUES() {
821 return {
822 'p': 1,
823 'r': 5,
824 'n': 3,
825 'b': 3,
826 'q': 9,
827 'k': 1000
828 };
829 }
830
9e42b4dd
BA
831 static get INFINITY() {
832 return 9999; //"checkmate" (unreachable eval)
833 }
834
835 static get THRESHOLD_MATE() {
836 // At this value or above, the game is over
837 return VariantRules.INFINITY;
838 }
839
3c09dc49
BA
840 static get SEARCH_DEPTH() {
841 return 3; //2 for high branching factor, 4 for small (Loser chess)
842 }
843
1d184b4c 844 // Assumption: at least one legal move
a6abf094
BA
845 // NOTE: works also for extinction chess because depth is 3...
846 getComputerMove()
1d184b4c 847 {
9e42b4dd 848 const maxeval = VariantRules.INFINITY;
46302e64 849 const color = this.turn;
15952ada
BA
850 // Some variants may show a bigger moves list to the human (Switching),
851 // thus the argument "computer" below (which is generally ignored)
852 let moves1 = this.getAllValidMoves("computer");
a6abf094
BA
853
854 // Can I mate in 1 ? (for Magnetic & Extinction)
855 for (let i of _.shuffle(_.range(moves1.length)))
856 {
857 this.play(moves1[i]);
858 const finish = (Math.abs(this.evalPosition()) >= VariantRules.THRESHOLD_MATE);
859 this.undo(moves1[i]);
860 if (finish)
861 return moves1[i];
862 }
1d184b4c 863
06ddfe34 864 // Rank moves using a min-max at depth 2
1d184b4c
BA
865 for (let i=0; i<moves1.length; i++)
866 {
9e42b4dd 867 moves1[i].eval = (color=="w" ? -1 : 1) * maxeval; //very low, I'm checkmated
1d184b4c 868 this.play(moves1[i]);
68f5ccc8
BA
869 let eval2 = undefined;
870 if (this.atLeastOneMove())
1d184b4c 871 {
68f5ccc8
BA
872 eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value
873 // Second half-move:
15952ada 874 let moves2 = this.getAllValidMoves("computer");
68f5ccc8
BA
875 for (let j=0; j<moves2.length; j++)
876 {
877 this.play(moves2[j]);
878 let evalPos = undefined;
879 if (this.atLeastOneMove())
880 evalPos = this.evalPosition()
881 else
882 {
883 // Work with scores for Loser variant
884 const score = this.checkGameEnd();
885 evalPos = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
886 }
887 if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > eval2))
888 eval2 = evalPos;
889 this.undo(moves2[j]);
890 }
891 }
892 else
893 {
894 const score = this.checkGameEnd();
895 eval2 = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
1d184b4c 896 }
92342261
BA
897 if ((color=="w" && eval2 > moves1[i].eval)
898 || (color=="b" && eval2 < moves1[i].eval))
899 {
1d184b4c 900 moves1[i].eval = eval2;
92342261 901 }
1d184b4c
BA
902 this.undo(moves1[i]);
903 }
904 moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
11a31682 905 //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
1d184b4c 906
3c09dc49
BA
907 let candidates = [0]; //indices of candidates moves
908 for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
909 candidates.push(j);
910 let currentBest = moves1[_.sample(candidates, 1)];
911
e82cd979
BA
912 // From here, depth >= 3: may take a while, so we control time
913 const timeStart = Date.now();
914
a6abf094 915 // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
3c09dc49
BA
916 if (VariantRules.SEARCH_DEPTH >= 3
917 && Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
e64a4eff 918 {
9e42b4dd
BA
919 for (let i=0; i<moves1.length; i++)
920 {
e82cd979
BA
921 if (Date.now()-timeStart >= 5000) //more than 5 seconds
922 return currentBest; //depth 2 at least
9e42b4dd
BA
923 this.play(moves1[i]);
924 // 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
3c09dc49
BA
925 moves1[i].eval = 0.1*moves1[i].eval +
926 this.alphabeta(VariantRules.SEARCH_DEPTH-1, -maxeval, maxeval);
9e42b4dd
BA
927 this.undo(moves1[i]);
928 }
929 moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
e64a4eff 930 }
3c09dc49
BA
931 else
932 return currentBest;
68f5ccc8 933 //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
1d184b4c 934
3c09dc49 935 candidates = [0];
1d184b4c
BA
936 for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
937 candidates.push(j);
1d184b4c
BA
938 return moves1[_.sample(candidates, 1)];
939 }
940
46302e64 941 alphabeta(depth, alpha, beta)
1d184b4c 942 {
9e42b4dd 943 const maxeval = VariantRules.INFINITY;
46302e64
BA
944 const color = this.turn;
945 if (!this.atLeastOneMove())
1d184b4c 946 {
46302e64 947 switch (this.checkGameEnd())
1d184b4c 948 {
68f5ccc8
BA
949 case "1/2":
950 return 0;
951 default:
952 const score = this.checkGameEnd();
953 return (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
1d184b4c
BA
954 }
955 }
956 if (depth == 0)
957 return this.evalPosition();
15952ada 958 const moves = this.getAllValidMoves("computer");
9e42b4dd 959 let v = color=="w" ? -maxeval : maxeval;
1d184b4c
BA
960 if (color == "w")
961 {
962 for (let i=0; i<moves.length; i++)
963 {
964 this.play(moves[i]);
46302e64 965 v = Math.max(v, this.alphabeta(depth-1, alpha, beta));
1d184b4c
BA
966 this.undo(moves[i]);
967 alpha = Math.max(alpha, v);
968 if (alpha >= beta)
969 break; //beta cutoff
970 }
971 }
972 else //color=="b"
973 {
974 for (let i=0; i<moves.length; i++)
975 {
976 this.play(moves[i]);
46302e64 977 v = Math.min(v, this.alphabeta(depth-1, alpha, beta));
1d184b4c
BA
978 this.undo(moves[i]);
979 beta = Math.min(beta, v);
980 if (alpha >= beta)
981 break; //alpha cutoff
982 }
983 }
984 return v;
985 }
986
987 evalPosition()
988 {
989 const [sizeX,sizeY] = VariantRules.size;
990 let evaluation = 0;
a6abf094 991 // Just count material for now
1d184b4c
BA
992 for (let i=0; i<sizeX; i++)
993 {
994 for (let j=0; j<sizeY; j++)
995 {
996 if (this.board[i][j] != VariantRules.EMPTY)
997 {
998 const sign = this.getColor(i,j) == "w" ? 1 : -1;
999 evaluation += sign * VariantRules.VALUES[this.getPiece(i,j)];
1000 }
1001 }
1002 }
1003 return evaluation;
1004 }
1005
1006 ////////////
1007 // FEN utils
1008
dda21a71 1009 // Setup the initial random (assymetric) position
1d184b4c
BA
1010 static GenRandInitFen()
1011 {
32cfcea4 1012 let pieces = { "w": new Array(8), "b": new Array(8) };
1d184b4c 1013 // Shuffle pieces on first and last rank
32cfcea4 1014 for (let c of ["w","b"])
1d184b4c
BA
1015 {
1016 let positions = _.range(8);
1017
1018 // Get random squares for bishops
1019 let randIndex = 2 * _.random(3);
1020 let bishop1Pos = positions[randIndex];
1021 // The second bishop must be on a square of different color
1022 let randIndex_tmp = 2 * _.random(3) + 1;
1023 let bishop2Pos = positions[randIndex_tmp];
1024 // Remove chosen squares
1025 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
1026 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
1027
1028 // Get random squares for knights
1029 randIndex = _.random(5);
1030 let knight1Pos = positions[randIndex];
1031 positions.splice(randIndex, 1);
1032 randIndex = _.random(4);
1033 let knight2Pos = positions[randIndex];
1034 positions.splice(randIndex, 1);
1035
1036 // Get random square for queen
1037 randIndex = _.random(3);
1038 let queenPos = positions[randIndex];
1039 positions.splice(randIndex, 1);
1040
1041 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
1042 let rook1Pos = positions[0];
1043 let kingPos = positions[1];
1044 let rook2Pos = positions[2];
1045
1046 // Finally put the shuffled pieces in the board array
1047 pieces[c][rook1Pos] = 'r';
1048 pieces[c][knight1Pos] = 'n';
1049 pieces[c][bishop1Pos] = 'b';
1050 pieces[c][queenPos] = 'q';
1051 pieces[c][kingPos] = 'k';
1052 pieces[c][bishop2Pos] = 'b';
1053 pieces[c][knight2Pos] = 'n';
1054 pieces[c][rook2Pos] = 'r';
1055 }
2eef6db6 1056 return pieces["b"].join("") +
1d184b4c 1057 "/pppppppp/8/8/8/8/PPPPPPPP/" +
32cfcea4 1058 pieces["w"].join("").toUpperCase() +
f3802fcd 1059 " 1111"; //add flags
1d184b4c
BA
1060 }
1061
1062 // Return current fen according to pieces+colors state
1063 getFen()
1064 {
f3802fcd 1065 return this.getBaseFen() + " " + this.getFlagsFen();
1d184b4c
BA
1066 }
1067
dda21a71 1068 // Position part of the FEN string
1d184b4c
BA
1069 getBaseFen()
1070 {
1071 let fen = "";
1072 let [sizeX,sizeY] = VariantRules.size;
1073 for (let i=0; i<sizeX; i++)
1074 {
1075 let emptyCount = 0;
1076 for (let j=0; j<sizeY; j++)
1077 {
1078 if (this.board[i][j] == VariantRules.EMPTY)
1079 emptyCount++;
1080 else
1081 {
1082 if (emptyCount > 0)
1083 {
1084 // Add empty squares in-between
1085 fen += emptyCount;
1086 emptyCount = 0;
1087 }
1088 fen += VariantRules.board2fen(this.board[i][j]);
1089 }
1090 }
1091 if (emptyCount > 0)
1092 {
1093 // "Flush remainder"
1094 fen += emptyCount;
1095 }
1096 if (i < sizeX - 1)
1097 fen += "/"; //separate rows
1098 }
1099 return fen;
1100 }
1101
dda21a71 1102 // Flags part of the FEN string
1d184b4c
BA
1103 getFlagsFen()
1104 {
1105 let fen = "";
1106 // Add castling flags
1107 for (let i of ['w','b'])
1108 {
1109 for (let j=0; j<2; j++)
77e1ec78 1110 fen += (this.castleFlags[i][j] ? '1' : '0');
1d184b4c
BA
1111 }
1112 return fen;
1113 }
1114
1115 // Context: just before move is played, turn hasn't changed
1116 getNotation(move)
1117 {
15952ada
BA
1118 if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING) //castle
1119 return (move.end.y < move.start.y ? "0-0-0" : "0-0");
1d184b4c
BA
1120
1121 // Translate final square
270968d6 1122 const finalSquare =
1d184b4c
BA
1123 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
1124
270968d6 1125 const piece = this.getPiece(move.start.x, move.start.y);
1d184b4c
BA
1126 if (piece == VariantRules.PAWN)
1127 {
1128 // Pawn move
1129 let notation = "";
5bfb0956 1130 if (move.vanish.length > move.appear.length)
1d184b4c
BA
1131 {
1132 // Capture
270968d6 1133 const startColumn = String.fromCharCode(97 + move.start.y);
1d184b4c
BA
1134 notation = startColumn + "x" + finalSquare;
1135 }
1136 else //no capture
1137 notation = finalSquare;
1138 if (move.appear.length > 0 && piece != move.appear[0].p) //promotion
1139 notation += "=" + move.appear[0].p.toUpperCase();
1140 return notation;
1141 }
1142
1143 else
1144 {
1145 // Piece movement
f3c10e18
BA
1146 return piece.toUpperCase() +
1147 (move.vanish.length > move.appear.length ? "x" : "") + finalSquare;
1d184b4c
BA
1148 }
1149 }
dfb4afc1 1150
6752407b
BA
1151 // Complete the usual notation, may be required for de-ambiguification
1152 getLongNotation(move)
1153 {
1154 const startSquare =
1155 String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
1156 const finalSquare =
1157 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
1158 return startSquare + finalSquare; //not encoding move. But short+long is enough
1159 }
1160
dfb4afc1 1161 // The score is already computed when calling this function
01a135e2 1162 getPGN(mycolor, score, fenStart, mode)
dfb4afc1 1163 {
16984853 1164 const zeroPad = x => { return (x<10 ? "0" : "") + x; };
dfb4afc1
BA
1165 let pgn = "";
1166 pgn += '[Site "vchess.club"]<br>';
1167 const d = new Date();
5e622704 1168 const opponent = mode=="human" ? "Anonymous" : "Computer";
0f51ef98 1169 pgn += '[Variant "' + variant + '"]<br>';
92342261
BA
1170 pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) +
1171 '-' + zeroPad(d.getDate()) + '"]<br>';
01a135e2
BA
1172 pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>';
1173 pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>';
04449c97
BA
1174 pgn += '[FenStart "' + fenStart + '"]<br>';
1175 pgn += '[Fen "' + this.getFen() + '"]<br>';
762b7c9c 1176 pgn += '[Result "' + score + '"]<br><br>';
dfb4afc1 1177
6752407b 1178 // Standard PGN
dfb4afc1
BA
1179 for (let i=0; i<this.moves.length; i++)
1180 {
1181 if (i % 2 == 0)
1182 pgn += ((i/2)+1) + ".";
6752407b 1183 pgn += this.moves[i].notation[0] + " ";
dfb4afc1 1184 }
97fc8bf7 1185 pgn += "<br><br>";
dfb4afc1 1186
6752407b
BA
1187 // "Complete moves" PGN (helping in ambiguous cases)
1188 for (let i=0; i<this.moves.length; i++)
1189 {
1190 if (i % 2 == 0)
1191 pgn += ((i/2)+1) + ".";
1192 pgn += this.moves[i].notation[1] + " ";
1193 }
6752407b 1194
dfb4afc1
BA
1195 return pgn;
1196 }
1d184b4c 1197}