Update TODO
[vchess.git] / client / src / variants / Dynamo.js
CommitLineData
b866a62a 1import { ChessRules, Move, PiPo } from "@/base_rules";
ad1e629e 2import { randInt } from "@/utils/alea";
b866a62a 3
0d5335de 4export class DynamoRules extends ChessRules {
7ddfec38 5 // TODO: later, allow to push out pawns on a and h files
61656127
BA
6 static get HasEnpassant() {
7 return false;
8 }
9
c7550017
BA
10 canIplay(side, [x, y]) {
11 // Sometimes opponent's pieces can be moved directly
e78633db 12 return this.turn == side;
c7550017
BA
13 }
14
61656127
BA
15 setOtherVariables(fen) {
16 super.setOtherVariables(fen);
17 this.subTurn = 1;
18 // Local stack of "action moves"
19 this.amoves = [];
20 const amove = V.ParseFen(fen).amove;
93ce6119 21 if (amove != "-") {
61656127 22 const amoveParts = amove.split("/");
ad1e629e 23 let move = {
61656127
BA
24 // No need for start & end
25 appear: [],
26 vanish: []
27 };
28 [0, 1].map(i => {
ad1e629e
BA
29 if (amoveParts[i] != "-") {
30 amoveParts[i].split(".").forEach(av => {
31 // Format is "bpe3"
32 const xy = V.SquareToCoords(av.substr(2));
33 move[i == 0 ? "appear" : "vanish"].push(
34 new PiPo({
35 x: xy.x,
36 y: xy.y,
37 c: av[0],
38 p: av[1]
39 })
40 );
41 });
42 }
61656127
BA
43 });
44 this.amoves.push(move);
c7550017 45 }
7b53b5a7 46 this.subTurn = 1;
a443d256
BA
47 // Stack "first moves" (on subTurn 1) to merge and check opposite moves
48 this.firstMove = [];
c7550017
BA
49 }
50
61656127
BA
51 static ParseFen(fen) {
52 return Object.assign(
53 ChessRules.ParseFen(fen),
93ce6119 54 { amove: fen.split(" ")[4] }
61656127
BA
55 );
56 }
57
58 static IsGoodFen(fen) {
59 if (!ChessRules.IsGoodFen(fen)) return false;
60 const fenParts = fen.split(" ");
ad1e629e
BA
61 if (fenParts.length != 5) return false;
62 if (fenParts[4] != "-") {
63 // TODO: a single regexp instead.
64 // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-'
65 const amoveParts = fenParts[4].split("/");
66 if (amoveParts.length != 2) return false;
67 for (let part of amoveParts) {
68 if (part != "-") {
69 for (let psq of part.split("."))
70 if (!psq.match(/^[a-r]{3}[1-8]$/)) return false;
71 }
72 }
73 }
61656127
BA
74 return true;
75 }
76
93ce6119
BA
77 getFen() {
78 return super.getFen() + " " + this.getAmoveFen();
61656127
BA
79 }
80
93ce6119
BA
81 getFenForRepeat() {
82 return super.getFenForRepeat() + "_" + this.getAmoveFen();
83 }
84
85 getAmoveFen() {
86 const L = this.amoves.length;
7b53b5a7 87 if (L == 0) return "-";
93ce6119
BA
88 return (
89 ["appear","vanish"].map(
90 mpart => {
156986e6 91 if (this.amoves[L-1][mpart].length == 0) return "-";
93ce6119
BA
92 return (
93 this.amoves[L-1][mpart].map(
94 av => {
95 const square = V.CoordsToSquare({ x: av.x, y: av.y });
96 return av.c + av.p + square;
97 }
98 ).join(".")
99 );
100 }
101 ).join("/")
102 );
61656127
BA
103 }
104
105 canTake() {
106 // Captures don't occur (only pulls & pushes)
107 return false;
108 }
109
b2655276
BA
110 // Step is right, just add (push/pull) moves in this direction
111 // Direction is assumed normalized.
112 getMovesInDirection([x, y], [dx, dy], nbSteps) {
113 nbSteps = nbSteps || 8; //max 8 steps anyway
114 let [i, j] = [x + dx, y + dy];
61656127 115 let moves = [];
b2655276
BA
116 const color = this.getColor(x, y);
117 const piece = this.getPiece(x, y);
118 const lastRank = (color == 'w' ? 0 : 7);
7b53b5a7 119 let counter = 1;
b2655276
BA
120 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
121 if (i == lastRank && piece == V.PAWN) {
122 // Promotion by push or pull
123 V.PawnSpecs.promotions.forEach(p => {
124 let move = super.getBasicMove([x, y], [i, j], { c: color, p: p });
125 moves.push(move);
126 });
127 }
128 else moves.push(super.getBasicMove([x, y], [i, j]));
7b53b5a7
BA
129 if (++counter > nbSteps) break;
130 i += dx;
131 j += dy;
b2655276
BA
132 }
133 if (!V.OnBoard(i, j) && piece != V.KING) {
134 // Add special "exit" move, by "taking king"
135 moves.push(
136 new Move({
137 start: { x: x, y: y },
7b53b5a7 138 end: { x: this.kingPos[color][0], y: this.kingPos[color][1] },
b2655276
BA
139 appear: [],
140 vanish: [{ x: x, y: y, c: color, p: piece }]
141 })
142 );
143 }
61656127
BA
144 return moves;
145 }
146
b2655276
BA
147 // Normalize direction to know the step
148 getNormalizedDirection([dx, dy]) {
149 const absDir = [Math.abs(dx), Math.abs(dy)];
150 let divisor = 0;
151 if (absDir[0] != 0 && absDir[1] != 0 && absDir[0] != absDir[1])
152 // Knight
153 divisor = Math.min(absDir[0], absDir[1]);
154 else
155 // Standard slider (or maybe a pawn or king: same)
156 divisor = Math.max(absDir[0], absDir[1]);
157 return [dx / divisor, dy / divisor];
158 }
159
ad1e629e
BA
160 // There was something on x2,y2, maybe our color, pushed/pulled.
161 // Also, the pushed/pulled piece must exit the board.
162 isAprioriValidExit([x1, y1], [x2, y2], color2) {
b2655276 163 const color1 = this.getColor(x1, y1);
b2655276 164 const pawnShift = (color1 == 'w' ? -1 : 1);
ad1e629e 165 const lastRank = (color1 == 'w' ? 0 : 7);
b2655276
BA
166 const deltaX = Math.abs(x1 - x2);
167 const deltaY = Math.abs(y1 - y2);
ad1e629e
BA
168 const checkSlider = () => {
169 const dir = this.getNormalizedDirection([x2 - x1, y2 - y1]);
170 let [i, j] = [x1 + dir[0], y1 + dir[1]];
171 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
172 i += dir[0];
173 j += dir[1];
174 }
175 return !V.OnBoard(i, j);
176 };
b2655276
BA
177 switch (this.getPiece(x1, y1)) {
178 case V.PAWN:
179 return (
ad1e629e 180 x1 + pawnShift == x2 &&
b2655276 181 (
ad1e629e
BA
182 (color1 == color2 && x2 == lastRank && y1 == y2) ||
183 (color1 != color2 && deltaY == 1 && !V.OnBoard(x2, 2 * y2 - y1))
b2655276
BA
184 )
185 );
186 case V.ROOK:
ad1e629e
BA
187 if (x1 != x2 && y1 != y2) return false;
188 return checkSlider();
189 case V.KNIGHT:
190 return (
191 deltaX + deltaY == 3 &&
192 (deltaX == 1 || deltaY == 1) &&
193 !V.OnBoard(2 * x2 - x1, 2 * y2 - y1)
194 );
b2655276 195 case V.BISHOP:
ad1e629e
BA
196 if (deltaX != deltaY) return false;
197 return checkSlider();
b2655276 198 case V.QUEEN:
ad1e629e
BA
199 if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) return false;
200 return checkSlider();
201 case V.KING:
b2655276 202 return (
ad1e629e
BA
203 deltaX <= 1 &&
204 deltaY <= 1 &&
205 !V.OnBoard(2 * x2 - x1, 2 * y2 - y1)
b2655276 206 );
93ce6119 207 }
b2655276 208 return false;
b866a62a
BA
209 }
210
93ce6119 211 // NOTE: for pushes, play the pushed piece first.
61656127 212 // for pulls: play the piece doing the action first
b2655276 213 // NOTE: to push a piece out of the board, make it slide until its king
b866a62a
BA
214 getPotentialMovesFrom([x, y]) {
215 const color = this.turn;
93ce6119 216 if (this.subTurn == 1) {
b2655276
BA
217 const getMoveHash = (m) => {
218 return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end);
219 };
220 const addMoves = (dir, nbSteps) => {
221 const newMoves =
222 this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps)
223 .filter(m => !movesHash[getMoveHash(m)]);
7b53b5a7 224 newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; });
b2655276
BA
225 Array.prototype.push.apply(moves, newMoves);
226 };
8c267d0c 227 // Free to play any move (if piece of my color):
cfceecba 228 let moves =
8c267d0c
BA
229 this.getColor(x, y) == color
230 ? super.getPotentialMovesFrom([x, y])
231 : [];
cfceecba
BA
232 // There may be several suicide moves: keep only one
233 let hasExit = false;
234 moves = moves.filter(m => {
235 const suicide = (m.appear.length == 0);
236 if (suicide) {
237 if (hasExit) return false;
238 hasExit = true;
239 }
240 return true;
241 });
b2655276
BA
242 const pawnShift = (color == 'w' ? -1 : 1);
243 const pawnStartRank = (color == 'w' ? 6 : 1);
7b53b5a7
BA
244 // Structure to avoid adding moves twice (can be action & move)
245 let movesHash = {};
246 moves.forEach(m => { movesHash[getMoveHash(m)] = true; });
b2655276
BA
247 // [x, y] is pushed by 'color'
248 for (let step of V.steps[V.KNIGHT]) {
249 const [i, j] = [x + step[0], y + step[1]];
7b53b5a7
BA
250 if (
251 V.OnBoard(i, j) &&
252 this.board[i][j] != V.EMPTY &&
253 this.getColor(i, j) == color &&
254 this.getPiece(i, j) == V.KNIGHT
255 ) {
256 addMoves(step, 1);
b2655276
BA
257 }
258 }
7b53b5a7 259 for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
b2655276
BA
260 let [i, j] = [x + step[0], y + step[1]];
261 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
262 i += step[0];
263 j += step[1];
264 }
265 if (
266 V.OnBoard(i, j) &&
267 this.board[i][j] != V.EMPTY &&
268 this.getColor(i, j) == color
269 ) {
270 const deltaX = Math.abs(i - x);
271 const deltaY = Math.abs(j - y);
b2655276
BA
272 switch (this.getPiece(i, j)) {
273 case V.PAWN:
ad1e629e
BA
274 if (
275 (x - i) / deltaX == pawnShift &&
276 deltaX <= 2 &&
277 deltaY <= 1
278 ) {
b2655276
BA
279 const pColor = this.getColor(x, y);
280 if (pColor == color && deltaY == 0) {
281 // Pushed forward
282 const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1);
283 addMoves(step, maxSteps);
284 }
285 else if (pColor != color && deltaY == 1 && deltaX == 1)
286 // Pushed diagonally
287 addMoves(step, 1);
288 }
289 break;
290 case V.ROOK:
291 if (deltaX == 0 || deltaY == 0) addMoves(step);
292 break;
b2655276
BA
293 case V.BISHOP:
294 if (deltaX == deltaY) addMoves(step);
295 break;
296 case V.QUEEN:
cfceecba
BA
297 // All steps are valid for a queen:
298 addMoves(step);
b2655276
BA
299 break;
300 case V.KING:
301 if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1);
302 break;
303 }
304 }
305 }
306 return moves;
b866a62a 307 }
93ce6119 308 // If subTurn == 2 then we should have a first move,
b2655276
BA
309 // which restrict what we can play now: only in the first move direction
310 // NOTE: no need for knight or pawn checks, because the move will be
311 // naturally limited in those cases.
93ce6119
BA
312 const L = this.firstMove.length;
313 const fm = this.firstMove[L-1];
b2655276
BA
314 if (fm.appear.length == 2 && fm.vanish.length == 2)
315 // Castle: no real move playable then.
316 return [];
317 if (fm.appear.length == 0) {
318 // Piece at subTurn 1 just exited the board.
319 // Can I be a piece which caused the exit?
ad1e629e
BA
320 if (
321 this.isAprioriValidExit(
322 [x, y],
323 [fm.start.x, fm.start.y],
324 fm.vanish[0].c
325 )
326 ) {
b2655276
BA
327 // Seems so:
328 const dir = this.getNormalizedDirection(
329 [fm.start.x - x, fm.start.y - y]);
330 return this.getMovesInDirection([x, y], dir);
331 }
93ce6119
BA
332 }
333 else {
b2655276
BA
334 const dirM = this.getNormalizedDirection(
335 [fm.end.x - fm.start.x, fm.end.y - fm.start.y]);
336 const dir = this.getNormalizedDirection(
337 [fm.start.x - x, fm.start.y - y]);
ad1e629e
BA
338 // Normalized directions should match
339 if (dir[0] == dirM[0] && dir[1] == dirM[1]) {
340 // And nothing should stand between [x, y] and the square fm.start
341 let [i, j] = [x + dir[0], y + dir[1]];
342 while (
343 (i != fm.start.x || j != fm.start.y) &&
344 this.board[i][j] == V.EMPTY
345 ) {
346 i += dir[0];
347 j += dir[1];
348 }
349 if (i == fm.start.x && j == fm.start.y)
350 return this.getMovesInDirection([x, y], dir);
351 }
93ce6119 352 }
b2655276 353 return [];
61656127
BA
354 }
355
cfceecba
BA
356 getSlideNJumpMoves([x, y], steps, oneStep) {
357 let moves = [];
358 outerLoop: for (let step of steps) {
359 let i = x + step[0];
360 let j = y + step[1];
361 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
362 moves.push(this.getBasicMove([x, y], [i, j]));
363 if (oneStep) continue outerLoop;
364 i += step[0];
365 j += step[1];
366 }
367 if (V.OnBoard(i, j)) {
368 if (this.canTake([x, y], [i, j]))
369 moves.push(this.getBasicMove([x, y], [i, j]));
370 }
371 else {
372 // Add potential board exit (suicide), except for the king
373 const piece = this.getPiece(x, y);
374 if (piece != V.KING) {
375 const c = this.getColor(x, y);
376 moves.push({
377 start: { x: x, y: y},
378 end: { x: this.kingPos[c][0], y: this.kingPos[c][1] },
379 appear: [],
380 vanish: [
381 new PiPo({
382 x: x,
383 y: y,
384 c: c,
385 p: piece
386 })
387 ]
388 });
389 }
390 }
391 }
392 return moves;
393 }
394
61656127
BA
395 // Does m2 un-do m1 ? (to disallow undoing actions)
396 oppositeMoves(m1, m2) {
397 const isEqual = (av1, av2) => {
398 // Precondition: av1 and av2 length = 2
399 for (let av of av1) {
400 const avInAv2 = av2.find(elt => {
401 return (
402 elt.x == av.x &&
403 elt.y == av.y &&
404 elt.c == av.c &&
405 elt.p == av.p
406 );
407 });
408 if (!avInAv2) return false;
409 }
410 return true;
411 };
412 return (
61656127
BA
413 m1.appear.length == 2 &&
414 m2.appear.length == 2 &&
415 m1.vanish.length == 2 &&
416 m2.vanish.length == 2 &&
417 isEqual(m1.appear, m2.vanish) &&
418 isEqual(m1.vanish, m2.appear)
419 );
420 }
421
93ce6119
BA
422 getAmove(move1, move2) {
423 // Just merge (one is action one is move, one may be empty)
424 return {
425 appear: move1.appear.concat(move2.appear),
426 vanish: move1.vanish.concat(move2.vanish)
427 }
428 }
429
61656127 430 filterValid(moves) {
93ce6119
BA
431 const color = this.turn;
432 if (this.subTurn == 1) {
433 return moves.filter(m => {
434 // A move is valid either if it doesn't result in a check,
435 // or if a second move is possible to counter the check
436 // (not undoing a potential move + action of the opponent)
437 this.play(m);
438 let res = this.underCheck(color);
439 if (res) {
440 const moves2 = this.getAllPotentialMoves();
ad1e629e 441 for (let m2 of moves2) {
93ce6119
BA
442 this.play(m2);
443 const res2 = this.underCheck(color);
444 this.undo(m2);
445 if (!res2) {
446 res = false;
447 break;
448 }
449 }
450 }
451 this.undo(m);
452 return !res;
453 });
454 }
455 const Lf = this.firstMove.length;
456 const La = this.amoves.length;
457 if (La == 0) return super.filterValid(moves);
a443d256 458 return (
93ce6119 459 super.filterValid(
a443d256
BA
460 moves.filter(m => {
461 // Move shouldn't undo another:
93ce6119
BA
462 const amove = this.getAmove(this.firstMove[Lf-1], m);
463 return !this.oppositeMoves(this.amoves[La-1], amove);
a443d256
BA
464 })
465 )
466 );
b866a62a
BA
467 }
468
c7550017
BA
469 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
470 for (let step of steps) {
471 let rx = x + step[0],
472 ry = y + step[1];
473 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
474 rx += step[0];
475 ry += step[1];
476 }
477 if (
478 V.OnBoard(rx, ry) &&
479 this.getPiece(rx, ry) == piece &&
480 this.getColor(rx, ry) == color
481 ) {
8c267d0c
BA
482 // Continue some steps in the same direction (pull)
483 rx += step[0];
484 ry += step[1];
485 while (
486 V.OnBoard(rx, ry) &&
487 this.board[rx][ry] == V.EMPTY &&
488 !oneStep
489 ) {
490 rx += step[0];
491 ry += step[1];
492 }
493 if (!V.OnBoard(rx, ry)) return true;
494 // Step in the other direction (push)
c7550017
BA
495 rx = x - step[0];
496 ry = y - step[1];
2c5d7b20
BA
497 while (
498 V.OnBoard(rx, ry) &&
499 this.board[rx][ry] == V.EMPTY &&
500 !oneStep
501 ) {
c7550017
BA
502 rx -= step[0];
503 ry -= step[1];
504 }
505 if (!V.OnBoard(rx, ry)) return true;
506 }
507 }
508 return false;
509 }
510
511 isAttackedByPawn([x, y], color) {
512 const lastRank = (color == 'w' ? 0 : 7);
ad1e629e 513 if (x != lastRank)
c7550017
BA
514 // The king can be pushed out by a pawn only on last rank
515 return false;
516 const pawnShift = (color == "w" ? 1 : -1);
517 for (let i of [-1, 1]) {
518 if (
519 y + i >= 0 &&
520 y + i < V.size.y &&
521 this.getPiece(x + pawnShift, y + i) == V.PAWN &&
522 this.getColor(x + pawnShift, y + i) == color
523 ) {
524 return true;
525 }
526 }
527 return false;
528 }
61656127 529
156986e6
BA
530 // No consideration of color: all pieces could be played
531 getAllPotentialMoves() {
532 let potentialMoves = [];
533 for (let i = 0; i < V.size.x; i++) {
534 for (let j = 0; j < V.size.y; j++) {
535 if (this.board[i][j] != V.EMPTY) {
536 Array.prototype.push.apply(
537 potentialMoves,
538 this.getPotentialMovesFrom([i, j])
539 );
540 }
541 }
542 }
543 return potentialMoves;
544 }
545
61656127
BA
546 getCurrentScore() {
547 if (this.subTurn == 2)
548 // Move not over
549 return "*";
550 return super.getCurrentScore();
551 }
552
93ce6119 553 doClick(square) {
b2655276
BA
554 // If subTurn == 2 && square is empty && !underCheck,
555 // then return an empty move, allowing to "pass" subTurn2
93ce6119
BA
556 if (
557 this.subTurn == 2 &&
7b53b5a7 558 this.board[square[0]][square[1]] == V.EMPTY &&
b2655276 559 !this.underCheck(this.turn)
93ce6119
BA
560 ) {
561 return {
7b53b5a7
BA
562 start: { x: -1, y: -1 },
563 end: { x: -1, y: -1 },
93ce6119
BA
564 appear: [],
565 vanish: []
566 };
567 }
568 return null;
569 }
570
61656127
BA
571 play(move) {
572 move.flags = JSON.stringify(this.aggregateFlags());
573 V.PlayOnBoard(this.board, move);
7ddfec38 574 if (this.subTurn == 2) {
7b53b5a7
BA
575 const L = this.firstMove.length;
576 this.amoves.push(this.getAmove(this.firstMove[L-1], move));
61656127 577 this.turn = V.GetOppCol(this.turn);
7ddfec38 578 this.movesCount++;
61656127 579 }
a443d256 580 else this.firstMove.push(move);
7ddfec38 581 this.subTurn = 3 - this.subTurn;
61656127
BA
582 this.postPlay(move);
583 }
584
7b53b5a7
BA
585 postPlay(move) {
586 if (move.start.x < 0) return;
587 for (let a of move.appear)
588 if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y];
589 this.updateCastleFlags(move);
590 }
591
592 updateCastleFlags(move) {
593 const firstRank = { 'w': V.size.x - 1, 'b': 0 };
7ddfec38 594 for (let v of move.vanish) {
7b53b5a7
BA
595 if (v.p == V.KING) this.castleFlags[v.c] = [V.size.y, V.size.y];
596 else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) {
597 const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1);
598 this.castleFlags[v.c][flagIdx] = V.size.y;
7ddfec38 599 }
61656127
BA
600 }
601 }
602
603 undo(move) {
604 this.disaggregateFlags(JSON.parse(move.flags));
605 V.UndoOnBoard(this.board, move);
7ddfec38 606 if (this.subTurn == 1) {
61656127 607 this.turn = V.GetOppCol(this.turn);
7ddfec38 608 this.movesCount--;
61656127 609 }
a443d256 610 else this.firstMove.pop();
7ddfec38 611 this.subTurn = 3 - this.subTurn;
61656127
BA
612 this.postUndo(move);
613 }
7b53b5a7
BA
614
615 postUndo(move) {
616 // (Potentially) Reset king position
617 for (let v of move.vanish)
618 if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y];
619 }
620
ad1e629e
BA
621 getComputerMove() {
622 let moves = this.getAllValidMoves();
623 if (moves.length == 0) return null;
624 // "Search" at depth 1 for now
625 const maxeval = V.INFINITY;
626 const color = this.turn;
627 const emptyMove = {
628 start: { x: -1, y: -1 },
629 end: { x: -1, y: -1 },
630 appear: [],
631 vanish: []
632 };
633 moves.forEach(m => {
634 this.play(m);
635 m.eval = (color == "w" ? -1 : 1) * maxeval;
636 const moves2 = this.getAllValidMoves().concat([emptyMove]);
637 m.next = moves2[0];
638 moves2.forEach(m2 => {
639 this.play(m2);
640 const score = this.getCurrentScore();
641 let mvEval = 0;
642 if (score != "1/2") {
643 if (score != "*") mvEval = (score == "1-0" ? 1 : -1) * maxeval;
644 else mvEval = this.evalPosition();
645 }
646 if (
647 (color == 'w' && mvEval > m.eval) ||
648 (color == 'b' && mvEval < m.eval)
649 ) {
650 m.eval = mvEval;
651 m.next = m2;
652 }
653 this.undo(m2);
654 });
655 this.undo(m);
656 });
657 moves.sort((a, b) => {
658 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
659 });
660 let candidates = [0];
661 for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++)
662 candidates.push(i);
663 const mIdx = candidates[randInt(candidates.length)];
664 const move2 = moves[mIdx].next;
665 delete moves[mIdx]["next"];
666 return [moves[mIdx], move2];
667 }
668
7b53b5a7
BA
669 getNotation(move) {
670 if (move.start.x < 0)
671 // A second move is always required, but may be empty
672 return "-";
673 const initialSquare = V.CoordsToSquare(move.start);
674 const finalSquare = V.CoordsToSquare(move.end);
675 if (move.appear.length == 0)
676 // Pushed or pulled out of the board
677 return initialSquare + "R";
678 return move.appear[0].p.toUpperCase() + initialSquare + finalSquare;
679 }
0d5335de 680};