1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class DynamoRules
extends ChessRules
{
6 // TODO? later, allow to push out pawns on a and h files
7 static get HasEnpassant() {
11 canIplay(side
, [x
, y
]) {
12 // Sometimes opponent's pieces can be moved directly
13 return this.turn
== side
;
16 setOtherVariables(fen
) {
17 super.setOtherVariables(fen
);
19 // Local stack of "action moves"
21 const amove
= V
.ParseFen(fen
).amove
;
23 const amoveParts
= amove
.split("/");
25 // No need for start & end
30 if (amoveParts
[i
] != "-") {
31 amoveParts
[i
].split(".").forEach(av
=> {
33 const xy
= V
.SquareToCoords(av
.substr(2));
34 move[i
== 0 ? "appear" : "vanish"].push(
45 this.amoves
.push(move);
47 // Stack "first moves" (on subTurn 1) to merge and check opposite moves
51 static ParseFen(fen
) {
53 ChessRules
.ParseFen(fen
),
54 { amove: fen
.split(" ")[4] }
58 static IsGoodFen(fen
) {
59 if (!ChessRules
.IsGoodFen(fen
)) return false;
60 const fenParts
= fen
.split(" ");
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
) {
69 for (let psq
of part
.split("."))
70 if (!psq
.match(/^[a-r]{3}[1-8]$/)) return false;
78 return super.getFen() + " " + this.getAmoveFen();
82 return super.getFenForRepeat() + "_" + this.getAmoveFen();
86 const L
= this.amoves
.length
;
87 if (L
== 0) return "-";
89 ["appear","vanish"].map(
91 if (this.amoves
[L
-1][mpart
].length
== 0) return "-";
93 this.amoves
[L
-1][mpart
].map(
95 const square
= V
.CoordsToSquare({ x: av
.x
, y: av
.y
});
96 return av
.c
+ av
.p
+ square
;
106 // Captures don't occur (only pulls & pushes)
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
];
116 const color
= this.getColor(x
, y
);
117 const piece
= this.getPiece(x
, y
);
118 const lastRank
= (color
== 'w' ? 0 : 7);
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
});
128 else moves
.push(super.getBasicMove([x
, y
], [i
, j
]));
129 if (++counter
> nbSteps
) break;
133 if (!V
.OnBoard(i
, j
) && piece
!= V
.KING
) {
134 // Add special "exit" move, by "taking king"
137 start: { x: x
, y: y
},
138 end: { x: this.kingPos
[color
][0], y: this.kingPos
[color
][1] },
140 vanish: [{ x: x
, y: y
, c: color
, p: piece
}]
147 // Normalize direction to know the step
148 getNormalizedDirection([dx
, dy
]) {
149 const absDir
= [Math
.abs(dx
), Math
.abs(dy
)];
151 if (absDir
[0] != 0 && absDir
[1] != 0 && absDir
[0] != absDir
[1])
153 divisor
= Math
.min(absDir
[0], absDir
[1]);
155 // Standard slider (or maybe a pawn or king: same)
156 divisor
= Math
.max(absDir
[0], absDir
[1]);
157 return [dx
/ divisor
, dy
/ divisor
];
160 // There was something on x2,y2, maybe our color, pushed or (self)pulled
161 isAprioriValidExit([x1
, y1
], [x2
, y2
], color2
, piece2
) {
162 const color1
= this.getColor(x1
, y1
);
163 const pawnShift
= (color1
== 'w' ? -1 : 1);
164 const lastRank
= (color1
== 'w' ? 0 : 7);
165 const deltaX
= Math
.abs(x1
- x2
);
166 const deltaY
= Math
.abs(y1
- y2
);
167 const checkSlider
= () => {
168 const dir
= this.getNormalizedDirection([x2
- x1
, y2
- y1
]);
169 let [i
, j
] = [x1
+ dir
[0], y1
+ dir
[1]];
170 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
174 return !V
.OnBoard(i
, j
);
176 switch (piece2
|| this.getPiece(x1
, y1
)) {
179 x1
+ pawnShift
== x2
&&
181 (color1
== color2
&& x2
== lastRank
&& y1
== y2
) ||
185 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
190 if (x1
!= x2
&& y1
!= y2
) return false;
191 return checkSlider();
194 deltaX
+ deltaY
== 3 &&
195 (deltaX
== 1 || deltaY
== 1) &&
196 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
199 if (deltaX
!= deltaY
) return false;
200 return checkSlider();
202 if (deltaX
!= 0 && deltaY
!= 0 && deltaX
!= deltaY
) return false;
203 return checkSlider();
208 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
214 isAprioriValidVertical([x1
, y1
], x2
) {
215 const piece
= this.getPiece(x1
, y1
);
216 const deltaX
= Math
.abs(x1
- x2
);
217 const startRank
= (this.getColor(x1
, y1
) == 'w' ? 6 : 1);
219 [V
.QUEEN
, V
.ROOK
].includes(piece
) ||
221 [V
.KING
, V
.PAWN
].includes(piece
) &&
224 (deltaX
== 2 && piece
== V
.PAWN
&& x1
== startRank
)
230 // NOTE: for pushes, play the pushed piece first.
231 // for pulls: play the piece doing the action first
232 // NOTE: to push a piece out of the board, make it slide until its king
233 getPotentialMovesFrom([x
, y
]) {
234 const color
= this.turn
;
235 const sqCol
= this.getColor(x
, y
);
236 const pawnShift
= (color
== 'w' ? -1 : 1);
237 const pawnStartRank
= (color
== 'w' ? 6 : 1);
238 const getMoveHash
= (m
) => {
239 return V
.CoordsToSquare(m
.start
) + V
.CoordsToSquare(m
.end
);
241 if (this.subTurn
== 1) {
242 const addMoves
= (dir
, nbSteps
) => {
244 this.getMovesInDirection([x
, y
], [-dir
[0], -dir
[1]], nbSteps
)
245 .filter(m
=> !movesHash
[getMoveHash(m
)]);
246 newMoves
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
247 Array
.prototype.push
.apply(moves
, newMoves
);
249 // Free to play any move (if piece of my color):
252 ? super.getPotentialMovesFrom([x
, y
])
254 // There may be several suicide moves: keep only one
256 moves
= moves
.filter(m
=> {
257 const suicide
= (m
.appear
.length
== 0);
259 if (hasExit
) return false;
264 // Structure to avoid adding moves twice (can be action & move)
266 moves
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
267 // [x, y] is pushed by 'color'
268 for (let step
of V
.steps
[V
.KNIGHT
]) {
269 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
272 this.board
[i
][j
] != V
.EMPTY
&&
273 this.getColor(i
, j
) == color
&&
274 this.getPiece(i
, j
) == V
.KNIGHT
279 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
280 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
281 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
287 this.board
[i
][j
] != V
.EMPTY
&&
288 this.getColor(i
, j
) == color
290 const deltaX
= Math
.abs(i
- x
);
291 const deltaY
= Math
.abs(j
- y
);
292 switch (this.getPiece(i
, j
)) {
295 (x
- i
) / deltaX
== pawnShift
&&
299 if (sqCol
== color
&& deltaY
== 0) {
301 const maxSteps
= (i
== pawnStartRank
&& deltaX
== 1 ? 2 : 1);
302 addMoves(step
, maxSteps
);
304 else if (sqCol
!= color
&& deltaY
== 1 && deltaX
== 1)
310 if (deltaX
== 0 || deltaY
== 0) addMoves(step
);
313 if (deltaX
== deltaY
) addMoves(step
);
316 // All steps are valid for a queen:
320 if (deltaX
<= 1 && deltaY
<= 1) addMoves(step
, 1);
327 // If subTurn == 2 then we should have a first move,
328 // which restrict what we can play now: only in the first move direction
329 const L
= this.firstMove
.length
;
330 const fm
= this.firstMove
[L
-1];
332 (fm
.appear
.length
== 2 && fm
.vanish
.length
== 2) ||
333 (fm
.vanish
[0].c
== sqCol
&& sqCol
!= color
)
335 // Castle or again opponent color: no move playable then.
338 const piece
= this.getPiece(x
, y
);
339 const getPushExit
= () => {
340 // Piece at subTurn 1 exited: can I have caused the exit?
342 this.isAprioriValidExit(
344 [fm
.start
.x
, fm
.start
.y
],
349 const dir
= this.getNormalizedDirection(
350 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
352 [V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece
)
355 return this.getMovesInDirection([x
, y
], dir
, nbSteps
);
359 const getPushMoves
= () => {
360 // Piece from subTurn 1 is still on board:
361 const dirM
= this.getNormalizedDirection(
362 [fm
.end
.x
- fm
.start
.x
, fm
.end
.y
- fm
.start
.y
]);
363 const dir
= this.getNormalizedDirection(
364 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
365 // Normalized directions should match
366 if (dir
[0] == dirM
[0] && dir
[1] == dirM
[1]) {
367 // We don't know if first move is a pushed piece or normal move,
368 // so still must check if the push is valid.
369 const deltaX
= Math
.abs(fm
.start
.x
- x
);
370 const deltaY
= Math
.abs(fm
.start
.y
- y
);
373 if (x
== pawnStartRank
) {
375 (fm
.start
.x
- x
) * pawnShift
< 0 ||
378 (fm
.vanish
[0].c
== color
&& deltaY
> 0) ||
379 (fm
.vanish
[0].c
!= color
&& deltaY
== 0) ||
380 Math
.abs(fm
.end
.x
- fm
.start
.x
) > deltaX
||
381 fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
388 fm
.start
.x
- x
!= pawnShift
||
390 (fm
.vanish
[0].c
== color
&& deltaY
== 1) ||
391 (fm
.vanish
[0].c
!= color
&& deltaY
== 0) ||
392 fm
.end
.x
- fm
.start
.x
!= pawnShift
||
393 fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
401 (deltaX
+ deltaY
!= 3 || (deltaX
== 0 && deltaY
== 0)) ||
402 (fm
.end
.x
- fm
.start
.x
!= fm
.start
.x
- x
) ||
403 (fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
)
410 (deltaX
>= 2 || deltaY
>= 2) ||
411 (fm
.end
.x
- fm
.start
.x
!= fm
.start
.x
- x
) ||
412 (fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
)
418 if (deltaX
!= deltaY
) return [];
421 if (deltaX
!= 0 && deltaY
!= 0) return [];
424 if (deltaX
!= deltaY
&& deltaX
!= 0 && deltaY
!= 0) return [];
427 // Nothing should stand between [x, y] and the square fm.start
428 let [i
, j
] = [x
+ dir
[0], y
+ dir
[1]];
430 (i
!= fm
.start
.x
|| j
!= fm
.start
.y
) &&
431 this.board
[i
][j
] == V
.EMPTY
436 if (i
== fm
.start
.x
&& j
== fm
.start
.y
)
437 return this.getMovesInDirection([x
, y
], dir
);
441 const getPullExit
= () => {
442 // Piece at subTurn 1 exited: can I be pulled?
443 // Note: kings cannot suicide, so fm.vanish[0].p is not KING.
444 // Could be PAWN though, if a pawn was pushed out of board.
446 fm
.vanish
[0].p
!= V
.PAWN
&& //pawns cannot pull
447 this.isAprioriValidExit(
449 [fm
.start
.x
, fm
.start
.y
],
455 const dir
= this.getNormalizedDirection(
456 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
457 const nbSteps
= (fm
.vanish
[0].p
== V
.KNIGHT
? 1 : null);
458 return this.getMovesInDirection([x
, y
], dir
, nbSteps
);
462 const getPullMoves
= () => {
463 if (fm
.vanish
[0].p
== V
.PAWN
)
466 const dirM
= this.getNormalizedDirection(
467 [fm
.end
.x
- fm
.start
.x
, fm
.end
.y
- fm
.start
.y
]);
468 const dir
= this.getNormalizedDirection(
469 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
470 // Normalized directions should match
471 if (dir
[0] == dirM
[0] && dir
[1] == dirM
[1]) {
472 // Am I at the right distance?
473 const deltaX
= Math
.abs(x
- fm
.start
.x
);
474 const deltaY
= Math
.abs(y
- fm
.start
.y
);
476 (fm
.vanish
[0].p
== V
.KING
&& (deltaX
> 1 || deltaY
> 1)) ||
477 (fm
.vanish
[0].p
== V
.KNIGHT
&&
478 (deltaX
+ deltaY
!= 3 || deltaX
== 0 || deltaY
== 0))
482 // Nothing should stand between [x, y] and the square fm.start
483 let [i
, j
] = [x
+ dir
[0], y
+ dir
[1]];
485 (i
!= fm
.start
.x
|| j
!= fm
.start
.y
) &&
486 this.board
[i
][j
] == V
.EMPTY
491 if (i
== fm
.start
.x
&& j
== fm
.start
.y
)
492 return this.getMovesInDirection([x
, y
], dir
);
496 if (fm
.vanish
[0].c
!= color
) {
497 // Only possible action is a push:
498 if (fm
.appear
.length
== 0) return getPushExit();
499 return getPushMoves();
501 else if (sqCol
!= color
) {
502 // Only possible action is a pull, considering moving piece abilities
503 if (fm
.appear
.length
== 0) return getPullExit();
504 return getPullMoves();
507 // My color + my color: both actions possible
508 // Structure to avoid adding moves twice (can be action & move)
510 if (fm
.appear
.length
== 0) {
511 const pushes
= getPushExit();
512 pushes
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
514 pushes
.concat(getPullExit().filter(m
=> !movesHash
[getMoveHash(m
)]))
517 const pushes
= getPushMoves();
518 pushes
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
520 pushes
.concat(getPullMoves().filter(m
=> !movesHash
[getMoveHash(m
)]))
526 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
528 const c
= this.getColor(x
, y
);
529 const piece
= this.getPiece(x
, y
);
530 outerLoop: for (let step
of steps
) {
533 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
534 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
535 if (oneStep
) continue outerLoop
;
539 if (V
.OnBoard(i
, j
)) {
540 if (this.canTake([x
, y
], [i
, j
]))
541 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
544 // Add potential board exit (suicide), except for the king
545 if (piece
!= V
.KING
) {
547 start: { x: x
, y: y
},
548 end: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
565 // Does m2 un-do m1 ? (to disallow undoing actions)
566 oppositeMoves(m1
, m2
) {
567 const isEqual
= (av1
, av2
) => {
568 for (let av
of av1
) {
569 const avInAv2
= av2
.find(elt
=> {
577 if (!avInAv2
) return false;
581 // All appear and vanish arrays must have the same length
582 const mL
= m1
.appear
.length
;
584 m2
.appear
.length
== mL
&&
585 m1
.vanish
.length
== mL
&&
586 m2
.vanish
.length
== mL
&&
587 isEqual(m1
.appear
, m2
.vanish
) &&
588 isEqual(m1
.vanish
, m2
.appear
)
592 getAmove(move1
, move2
) {
593 // Just merge (one is action one is move, one may be empty)
595 appear: move1
.appear
.concat(move2
.appear
),
596 vanish: move1
.vanish
.concat(move2
.vanish
)
601 const color
= this.turn
;
602 const La
= this.amoves
.length
;
603 if (this.subTurn
== 1) {
604 return moves
.filter(m
=> {
605 // A move is valid either if it doesn't result in a check,
606 // or if a second move is possible to counter the check
607 // (not undoing a potential move + action of the opponent)
609 let res
= this.underCheck(color
);
610 let isOpposite
= La
> 0 && this.oppositeMoves(this.amoves
[La
-1], m
);
611 if (res
|| isOpposite
) {
612 const moves2
= this.getAllPotentialMoves();
613 for (let m2
of moves2
) {
615 const res2
= this.underCheck(color
);
616 const amove
= this.getAmove(m
, m2
);
618 La
> 0 && this.oppositeMoves(this.amoves
[La
-1], amove
);
620 if (!res2
&& !isOpposite
) {
630 if (La
== 0) return super.filterValid(moves
);
631 const Lf
= this.firstMove
.length
;
635 // Move shouldn't undo another:
636 const amove
= this.getAmove(this.firstMove
[Lf
-1], m
);
637 return !this.oppositeMoves(this.amoves
[La
-1], amove
);
643 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
644 for (let step
of steps
) {
645 let rx
= x
+ step
[0],
647 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
653 this.getPiece(rx
, ry
) == piece
&&
654 this.getColor(rx
, ry
) == color
656 // Continue some steps in the same direction (pull)
661 this.board
[rx
][ry
] == V
.EMPTY
&&
667 if (!V
.OnBoard(rx
, ry
)) return true;
668 // Step in the other direction (push)
673 this.board
[rx
][ry
] == V
.EMPTY
&&
679 if (!V
.OnBoard(rx
, ry
)) return true;
685 isAttackedByPawn([x
, y
], color
) {
686 // The king can be pushed out by a pawn on last rank or near the edge
687 const pawnShift
= (color
== "w" ? 1 : -1);
688 for (let i
of [-1, 1]) {
690 V
.OnBoard(x
+ pawnShift
, y
+ i
) &&
691 this.board
[x
+ pawnShift
][y
+ i
] != V
.EMPTY
&&
692 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
693 this.getColor(x
+ pawnShift
, y
+ i
) == color
695 if (!V
.OnBoard(x
- pawnShift
, y
- i
)) return true;
701 static OnTheEdge(x
, y
) {
702 return (x
== 0 || x
== 7 || y
== 0 || y
== 7);
705 isAttackedByKing([x
, y
], color
) {
706 // Attacked if I'm on the edge and the opponent king just next,
707 // but not on the edge.
708 if (V
.OnTheEdge(x
, y
)) {
709 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
710 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
713 !V
.OnTheEdge(i
, j
) &&
714 this.board
[i
][j
] != V
.EMPTY
&&
715 this.getPiece(i
, j
) == V
.KING
716 // NOTE: since only one king of each color, and (x, y) is occupied
717 // by our king, no need to check other king's color.
726 // No consideration of color: all pieces could be played
727 getAllPotentialMoves() {
728 let potentialMoves
= [];
729 for (let i
= 0; i
< V
.size
.x
; i
++) {
730 for (let j
= 0; j
< V
.size
.y
; j
++) {
731 if (this.board
[i
][j
] != V
.EMPTY
) {
732 Array
.prototype.push
.apply(
734 this.getPotentialMovesFrom([i
, j
])
739 return potentialMoves
;
744 start: { x: -1, y: -1 },
745 end: { x: -1, y: -1 },
752 // A click to promote a piece on subTurn 2 would trigger this.
753 // For now it would then return [NaN, NaN] because surrounding squares
754 // have no IDs in the promotion modal. TODO: improve this?
755 if (isNaN(square
[0])) return null;
756 // If subTurn == 2 && square is empty && !underCheck && !isOpposite,
757 // then return an empty move, allowing to "pass" subTurn2
758 const La
= this.amoves
.length
;
759 const Lf
= this.firstMove
.length
;
762 this.board
[square
[0]][square
[1]] == V
.EMPTY
&&
763 !this.underCheck(this.turn
) &&
764 (La
== 0 || !this.oppositeMoves(this.amoves
[La
-1], this.firstMove
[Lf
-1]))
766 return this.getEmptyMove();
772 const color
= this.turn
;
773 move.subTurn
= this.subTurn
; //for undo
774 const gotoNext
= (mv
) => {
775 const L
= this.firstMove
.length
;
776 this.amoves
.push(this.getAmove(this.firstMove
[L
-1], mv
));
777 this.turn
= V
.GetOppCol(color
);
781 move.flags
= JSON
.stringify(this.aggregateFlags());
782 V
.PlayOnBoard(this.board
, move);
783 if (this.subTurn
== 2) gotoNext(move);
786 this.firstMove
.push(move);
787 this.toNewKingPos(move);
789 // Condition is true on empty arrays:
790 this.getAllPotentialMoves().every(m
=> {
791 V
.PlayOnBoard(this.board
, m
);
792 this.toNewKingPos(m
);
793 const res
= this.underCheck(color
);
794 V
.UndoOnBoard(this.board
, m
);
795 this.toOldKingPos(m
);
799 // No valid move at subTurn 2
800 gotoNext(this.getEmptyMove());
802 this.toOldKingPos(move);
808 for (let a
of move.appear
)
809 if (a
.p
== V
.KING
) this.kingPos
[a
.c
] = [a
.x
, a
.y
];
813 if (move.start
.x
< 0) return;
814 this.toNewKingPos(move);
815 this.updateCastleFlags(move);
818 updateCastleFlags(move) {
819 const firstRank
= { 'w': V
.size
.x
- 1, 'b': 0 };
820 for (let v
of move.vanish
) {
821 if (v
.p
== V
.KING
) this.castleFlags
[v
.c
] = [V
.size
.y
, V
.size
.y
];
822 else if (v
.x
== firstRank
[v
.c
] && this.castleFlags
[v
.c
].includes(v
.y
)) {
823 const flagIdx
= (v
.y
== this.castleFlags
[v
.c
][0] ? 0 : 1);
824 this.castleFlags
[v
.c
][flagIdx
] = V
.size
.y
;
830 this.disaggregateFlags(JSON
.parse(move.flags
));
831 V
.UndoOnBoard(this.board
, move);
832 if (this.subTurn
== 1) {
834 this.turn
= V
.GetOppCol(this.turn
);
837 if (move.subTurn
== 1) this.firstMove
.pop();
838 this.subTurn
= move.subTurn
;
839 this.toOldKingPos(move);
843 // (Potentially) Reset king position
844 for (let v
of move.vanish
)
845 if (v
.p
== V
.KING
) this.kingPos
[v
.c
] = [v
.x
, v
.y
];
849 let moves
= this.getAllValidMoves();
850 if (moves
.length
== 0) return null;
851 // "Search" at depth 1 for now
852 const maxeval
= V
.INFINITY
;
853 const color
= this.turn
;
855 start: { x: -1, y: -1 },
856 end: { x: -1, y: -1 },
862 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
863 const moves2
= this.getAllValidMoves().concat([emptyMove
]);
865 moves2
.forEach(m2
=> {
867 const score
= this.getCurrentScore();
869 if (score
!= "1/2") {
870 if (score
!= "*") mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
871 else mvEval
= this.evalPosition();
874 (color
== 'w' && mvEval
> m
.eval
) ||
875 (color
== 'b' && mvEval
< m
.eval
)
884 moves
.sort((a
, b
) => {
885 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
887 let candidates
= [0];
888 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
890 const mIdx
= candidates
[randInt(candidates
.length
)];
891 const move2
= moves
[mIdx
].next
;
892 delete moves
[mIdx
]["next"];
893 return [moves
[mIdx
], move2
];
897 if (move.start
.x
< 0)
898 // A second move is always required, but may be empty
900 const initialSquare
= V
.CoordsToSquare(move.start
);
901 const finalSquare
= V
.CoordsToSquare(move.end
);
902 if (move.appear
.length
== 0)
903 // Pushed or pulled out of the board
904 return initialSquare
+ "R";
905 return move.appear
[0].p
.toUpperCase() + initialSquare
+ finalSquare
;