1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class DynamoRules
extends ChessRules
{
5 // TODO? later, allow to push out pawns on a and h files
6 static get HasEnpassant() {
10 canIplay(side
, [x
, y
]) {
11 // Sometimes opponent's pieces can be moved directly
12 return this.turn
== side
;
15 setOtherVariables(fen
) {
16 super.setOtherVariables(fen
);
18 // Local stack of "action moves"
20 const amove
= V
.ParseFen(fen
).amove
;
22 const amoveParts
= amove
.split("/");
24 // No need for start & end
29 if (amoveParts
[i
] != "-") {
30 amoveParts
[i
].split(".").forEach(av
=> {
32 const xy
= V
.SquareToCoords(av
.substr(2));
33 move[i
== 0 ? "appear" : "vanish"].push(
44 this.amoves
.push(move);
46 // Stack "first moves" (on subTurn 1) to merge and check opposite moves
50 static ParseFen(fen
) {
52 ChessRules
.ParseFen(fen
),
53 { amove: fen
.split(" ")[4] }
57 static IsGoodFen(fen
) {
58 if (!ChessRules
.IsGoodFen(fen
)) return false;
59 const fenParts
= fen
.split(" ");
60 if (fenParts
.length
!= 5) return false;
61 if (fenParts
[4] != "-") {
62 // TODO: a single regexp instead.
63 // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-'
64 const amoveParts
= fenParts
[4].split("/");
65 if (amoveParts
.length
!= 2) return false;
66 for (let part
of amoveParts
) {
68 for (let psq
of part
.split("."))
69 if (!psq
.match(/^[a-r]{3}[1-8]$/)) return false;
77 return super.getFen() + " " + this.getAmoveFen();
81 return super.getFenForRepeat() + "_" + this.getAmoveFen();
85 const L
= this.amoves
.length
;
86 if (L
== 0) return "-";
88 ["appear","vanish"].map(
90 if (this.amoves
[L
-1][mpart
].length
== 0) return "-";
92 this.amoves
[L
-1][mpart
].map(
94 const square
= V
.CoordsToSquare({ x: av
.x
, y: av
.y
});
95 return av
.c
+ av
.p
+ square
;
105 // Captures don't occur (only pulls & pushes)
109 // Step is right, just add (push/pull) moves in this direction
110 // Direction is assumed normalized.
111 getMovesInDirection([x
, y
], [dx
, dy
], nbSteps
) {
112 nbSteps
= nbSteps
|| 8; //max 8 steps anyway
113 let [i
, j
] = [x
+ dx
, y
+ dy
];
115 const color
= this.getColor(x
, y
);
116 const piece
= this.getPiece(x
, y
);
117 const lastRank
= (color
== 'w' ? 0 : 7);
119 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
120 if (i
== lastRank
&& piece
== V
.PAWN
) {
121 // Promotion by push or pull
122 V
.PawnSpecs
.promotions
.forEach(p
=> {
123 let move = super.getBasicMove([x
, y
], [i
, j
], { c: color
, p: p
});
127 else moves
.push(super.getBasicMove([x
, y
], [i
, j
]));
128 if (++counter
> nbSteps
) break;
132 if (!V
.OnBoard(i
, j
) && piece
!= V
.KING
) {
133 // Add special "exit" move, by "taking king"
136 start: { x: x
, y: y
},
137 end: { x: this.kingPos
[color
][0], y: this.kingPos
[color
][1] },
139 vanish: [{ x: x
, y: y
, c: color
, p: piece
}]
146 // Normalize direction to know the step
147 getNormalizedDirection([dx
, dy
]) {
148 const absDir
= [Math
.abs(dx
), Math
.abs(dy
)];
150 if (absDir
[0] != 0 && absDir
[1] != 0 && absDir
[0] != absDir
[1])
152 divisor
= Math
.min(absDir
[0], absDir
[1]);
154 // Standard slider (or maybe a pawn or king: same)
155 divisor
= Math
.max(absDir
[0], absDir
[1]);
156 return [dx
/ divisor
, dy
/ divisor
];
159 // There was something on x2,y2, maybe our color, pushed or (self)pulled
160 isAprioriValidExit([x1
, y1
], [x2
, y2
], color2
, piece2
) {
161 const color1
= this.getColor(x1
, y1
);
162 const pawnShift
= (color1
== 'w' ? -1 : 1);
163 const lastRank
= (color1
== 'w' ? 0 : 7);
164 const deltaX
= Math
.abs(x1
- x2
);
165 const deltaY
= Math
.abs(y1
- y2
);
166 const checkSlider
= () => {
167 const dir
= this.getNormalizedDirection([x2
- x1
, y2
- y1
]);
168 let [i
, j
] = [x1
+ dir
[0], y1
+ dir
[1]];
169 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
173 return !V
.OnBoard(i
, j
);
175 switch (piece2
|| this.getPiece(x1
, y1
)) {
178 x1
+ pawnShift
== x2
&&
180 (color1
== color2
&& x2
== lastRank
&& y1
== y2
) ||
184 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
189 if (x1
!= x2
&& y1
!= y2
) return false;
190 return checkSlider();
193 deltaX
+ deltaY
== 3 &&
194 (deltaX
== 1 || deltaY
== 1) &&
195 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
198 if (deltaX
!= deltaY
) return false;
199 return checkSlider();
201 if (deltaX
!= 0 && deltaY
!= 0 && deltaX
!= deltaY
) return false;
202 return checkSlider();
207 !V
.OnBoard(2 * x2
- x1
, 2 * y2
- y1
)
213 isAprioriValidVertical([x1
, y1
], x2
) {
214 const piece
= this.getPiece(x1
, y1
);
215 const deltaX
= Math
.abs(x1
- x2
);
216 const startRank
= (this.getColor(x1
, y1
) == 'w' ? 6 : 1);
218 [V
.QUEEN
, V
.ROOK
].includes(piece
) ||
220 [V
.KING
, V
.PAWN
].includes(piece
) &&
223 (deltaX
== 2 && piece
== V
.PAWN
&& x1
== startRank
)
229 // NOTE: for pushes, play the pushed piece first.
230 // for pulls: play the piece doing the action first
231 // NOTE: to push a piece out of the board, make it slide until its king
232 getPotentialMovesFrom([x
, y
]) {
233 const color
= this.turn
;
234 const sqCol
= this.getColor(x
, y
);
235 const pawnShift
= (color
== 'w' ? -1 : 1);
236 const pawnStartRank
= (color
== 'w' ? 6 : 1);
237 const getMoveHash
= (m
) => {
238 return V
.CoordsToSquare(m
.start
) + V
.CoordsToSquare(m
.end
);
240 if (this.subTurn
== 1) {
241 const addMoves
= (dir
, nbSteps
) => {
243 this.getMovesInDirection([x
, y
], [-dir
[0], -dir
[1]], nbSteps
)
244 .filter(m
=> !movesHash
[getMoveHash(m
)]);
245 newMoves
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
246 Array
.prototype.push
.apply(moves
, newMoves
);
248 // Free to play any move (if piece of my color):
251 ? super.getPotentialMovesFrom([x
, y
])
253 // There may be several suicide moves: keep only one
255 moves
= moves
.filter(m
=> {
256 const suicide
= (m
.appear
.length
== 0);
258 if (hasExit
) return false;
263 // Structure to avoid adding moves twice (can be action & move)
265 moves
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
266 // [x, y] is pushed by 'color'
267 for (let step
of V
.steps
[V
.KNIGHT
]) {
268 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
271 this.board
[i
][j
] != V
.EMPTY
&&
272 this.getColor(i
, j
) == color
&&
273 this.getPiece(i
, j
) == V
.KNIGHT
278 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
279 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
280 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
286 this.board
[i
][j
] != V
.EMPTY
&&
287 this.getColor(i
, j
) == color
289 const deltaX
= Math
.abs(i
- x
);
290 const deltaY
= Math
.abs(j
- y
);
291 switch (this.getPiece(i
, j
)) {
294 (x
- i
) / deltaX
== pawnShift
&&
298 if (sqCol
== color
&& deltaY
== 0) {
300 const maxSteps
= (i
== pawnStartRank
&& deltaX
== 1 ? 2 : 1);
301 addMoves(step
, maxSteps
);
303 else if (sqCol
!= color
&& deltaY
== 1 && deltaX
== 1)
309 if (deltaX
== 0 || deltaY
== 0) addMoves(step
);
312 if (deltaX
== deltaY
) addMoves(step
);
315 // All steps are valid for a queen:
319 if (deltaX
<= 1 && deltaY
<= 1) addMoves(step
, 1);
326 // If subTurn == 2 then we should have a first move,
327 // which restrict what we can play now: only in the first move direction
328 const L
= this.firstMove
.length
;
329 const fm
= this.firstMove
[L
-1];
331 (fm
.appear
.length
== 2 && fm
.vanish
.length
== 2) ||
332 (fm
.vanish
[0].c
== sqCol
&& sqCol
!= color
)
334 // Castle or again opponent color: no move playable then.
337 const piece
= this.getPiece(x
, y
);
338 const getPushExit
= () => {
339 // Piece at subTurn 1 exited: can I have caused the exit?
341 this.isAprioriValidExit(
343 [fm
.start
.x
, fm
.start
.y
],
348 const dir
= this.getNormalizedDirection(
349 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
351 [V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece
)
354 return this.getMovesInDirection([x
, y
], dir
, nbSteps
);
358 const getPushMoves
= () => {
359 // Piece from subTurn 1 is still on board:
360 const dirM
= this.getNormalizedDirection(
361 [fm
.end
.x
- fm
.start
.x
, fm
.end
.y
- fm
.start
.y
]);
362 const dir
= this.getNormalizedDirection(
363 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
364 // Normalized directions should match
365 if (dir
[0] == dirM
[0] && dir
[1] == dirM
[1]) {
366 // We don't know if first move is a pushed piece or normal move,
367 // so still must check if the push is valid.
368 const deltaX
= Math
.abs(fm
.start
.x
- x
);
369 const deltaY
= Math
.abs(fm
.start
.y
- y
);
372 if (x
== pawnStartRank
) {
374 (fm
.start
.x
- x
) * pawnShift
< 0 ||
377 (fm
.vanish
[0].c
== color
&& deltaY
> 0) ||
378 (fm
.vanish
[0].c
!= color
&& deltaY
== 0) ||
379 Math
.abs(fm
.end
.x
- fm
.start
.x
) > deltaX
||
380 fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
387 fm
.start
.x
- x
!= pawnShift
||
389 (fm
.vanish
[0].c
== color
&& deltaY
== 1) ||
390 (fm
.vanish
[0].c
!= color
&& deltaY
== 0) ||
391 fm
.end
.x
- fm
.start
.x
!= pawnShift
||
392 fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
400 (deltaX
+ deltaY
!= 3 || (deltaX
== 0 && deltaY
== 0)) ||
401 (fm
.end
.x
- fm
.start
.x
!= fm
.start
.x
- x
) ||
402 (fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
)
409 (deltaX
>= 2 || deltaY
>= 2) ||
410 (fm
.end
.x
- fm
.start
.x
!= fm
.start
.x
- x
) ||
411 (fm
.end
.y
- fm
.start
.y
!= fm
.start
.y
- y
)
417 if (deltaX
!= deltaY
) return [];
420 if (deltaX
!= 0 && deltaY
!= 0) return [];
423 if (deltaX
!= deltaY
&& deltaX
!= 0 && deltaY
!= 0) return [];
426 // Nothing should stand between [x, y] and the square fm.start
427 let [i
, j
] = [x
+ dir
[0], y
+ dir
[1]];
429 (i
!= fm
.start
.x
|| j
!= fm
.start
.y
) &&
430 this.board
[i
][j
] == V
.EMPTY
435 if (i
== fm
.start
.x
&& j
== fm
.start
.y
)
436 return this.getMovesInDirection([x
, y
], dir
);
440 const getPullExit
= () => {
441 // Piece at subTurn 1 exited: can I be pulled?
442 // Note: kings cannot suicide, so fm.vanish[0].p is not KING.
443 // Could be PAWN though, if a pawn was pushed out of board.
445 fm
.vanish
[0].p
!= V
.PAWN
&& //pawns cannot pull
446 this.isAprioriValidExit(
448 [fm
.start
.x
, fm
.start
.y
],
454 const dir
= this.getNormalizedDirection(
455 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
456 const nbSteps
= (fm
.vanish
[0].p
== V
.KNIGHT
? 1 : null);
457 return this.getMovesInDirection([x
, y
], dir
, nbSteps
);
461 const getPullMoves
= () => {
462 if (fm
.vanish
[0].p
== V
.PAWN
)
465 const dirM
= this.getNormalizedDirection(
466 [fm
.end
.x
- fm
.start
.x
, fm
.end
.y
- fm
.start
.y
]);
467 const dir
= this.getNormalizedDirection(
468 [fm
.start
.x
- x
, fm
.start
.y
- y
]);
469 // Normalized directions should match
470 if (dir
[0] == dirM
[0] && dir
[1] == dirM
[1]) {
471 // Am I at the right distance?
472 const deltaX
= Math
.abs(x
- fm
.start
.x
);
473 const deltaY
= Math
.abs(y
- fm
.start
.y
);
475 (fm
.vanish
[0].p
== V
.KING
&& (deltaX
> 1 || deltaY
> 1)) ||
476 (fm
.vanish
[0].p
== V
.KNIGHT
&&
477 (deltaX
+ deltaY
!= 3 || deltaX
== 0 || deltaY
== 0))
481 // Nothing should stand between [x, y] and the square fm.start
482 let [i
, j
] = [x
+ dir
[0], y
+ dir
[1]];
484 (i
!= fm
.start
.x
|| j
!= fm
.start
.y
) &&
485 this.board
[i
][j
] == V
.EMPTY
490 if (i
== fm
.start
.x
&& j
== fm
.start
.y
)
491 return this.getMovesInDirection([x
, y
], dir
);
495 if (fm
.vanish
[0].c
!= color
) {
496 // Only possible action is a push:
497 if (fm
.appear
.length
== 0) return getPushExit();
498 return getPushMoves();
500 else if (sqCol
!= color
) {
501 // Only possible action is a pull, considering moving piece abilities
502 if (fm
.appear
.length
== 0) return getPullExit();
503 return getPullMoves();
506 // My color + my color: both actions possible
507 // Structure to avoid adding moves twice (can be action & move)
509 if (fm
.appear
.length
== 0) {
510 const pushes
= getPushExit();
511 pushes
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
513 pushes
.concat(getPullExit().filter(m
=> !movesHash
[getMoveHash(m
)]))
516 const pushes
= getPushMoves();
517 pushes
.forEach(m
=> { movesHash
[getMoveHash(m
)] = true; });
519 pushes
.concat(getPullMoves().filter(m
=> !movesHash
[getMoveHash(m
)]))
525 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
527 const c
= this.getColor(x
, y
);
528 const piece
= this.getPiece(x
, y
);
529 outerLoop: for (let step
of steps
) {
532 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
533 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
534 if (oneStep
) continue outerLoop
;
538 if (V
.OnBoard(i
, j
)) {
539 if (this.canTake([x
, y
], [i
, j
]))
540 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
543 // Add potential board exit (suicide), except for the king
544 if (piece
!= V
.KING
) {
546 start: { x: x
, y: y
},
547 end: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
564 // Does m2 un-do m1 ? (to disallow undoing actions)
565 oppositeMoves(m1
, m2
) {
566 const isEqual
= (av1
, av2
) => {
567 for (let av
of av1
) {
568 const avInAv2
= av2
.find(elt
=> {
576 if (!avInAv2
) return false;
580 // All appear and vanish arrays must have the same length
581 const mL
= m1
.appear
.length
;
583 m2
.appear
.length
== mL
&&
584 m1
.vanish
.length
== mL
&&
585 m2
.vanish
.length
== mL
&&
586 isEqual(m1
.appear
, m2
.vanish
) &&
587 isEqual(m1
.vanish
, m2
.appear
)
591 getAmove(move1
, move2
) {
592 // Just merge (one is action one is move, one may be empty)
594 appear: move1
.appear
.concat(move2
.appear
),
595 vanish: move1
.vanish
.concat(move2
.vanish
)
600 const color
= this.turn
;
601 const La
= this.amoves
.length
;
602 if (this.subTurn
== 1) {
603 return moves
.filter(m
=> {
604 // A move is valid either if it doesn't result in a check,
605 // or if a second move is possible to counter the check
606 // (not undoing a potential move + action of the opponent)
608 let res
= this.underCheck(color
);
609 let isOpposite
= La
> 0 && this.oppositeMoves(this.amoves
[La
-1], m
);
610 if (res
|| isOpposite
) {
611 const moves2
= this.getAllPotentialMoves();
612 for (let m2
of moves2
) {
614 const res2
= this.underCheck(color
);
615 const amove
= this.getAmove(m
, m2
);
617 La
> 0 && this.oppositeMoves(this.amoves
[La
-1], amove
);
619 if (!res2
&& !isOpposite
) {
629 if (La
== 0) return super.filterValid(moves
);
630 const Lf
= this.firstMove
.length
;
634 // Move shouldn't undo another:
635 const amove
= this.getAmove(this.firstMove
[Lf
-1], m
);
636 return !this.oppositeMoves(this.amoves
[La
-1], amove
);
642 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
643 for (let step
of steps
) {
644 let rx
= x
+ step
[0],
646 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
652 this.getPiece(rx
, ry
) == piece
&&
653 this.getColor(rx
, ry
) == color
655 // Continue some steps in the same direction (pull)
660 this.board
[rx
][ry
] == V
.EMPTY
&&
666 if (!V
.OnBoard(rx
, ry
)) return true;
667 // Step in the other direction (push)
672 this.board
[rx
][ry
] == V
.EMPTY
&&
678 if (!V
.OnBoard(rx
, ry
)) return true;
684 isAttackedByPawn([x
, y
], color
) {
685 // The king can be pushed out by a pawn on last rank or near the edge
686 const pawnShift
= (color
== "w" ? 1 : -1);
687 for (let i
of [-1, 1]) {
689 V
.OnBoard(x
+ pawnShift
, y
+ i
) &&
690 this.board
[x
+ pawnShift
][y
+ i
] != V
.EMPTY
&&
691 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
692 this.getColor(x
+ pawnShift
, y
+ i
) == color
694 if (!V
.OnBoard(x
- pawnShift
, y
- i
)) return true;
700 static OnTheEdge(x
, y
) {
701 return (x
== 0 || x
== 7 || y
== 0 || y
== 7);
704 isAttackedByKing([x
, y
], color
) {
705 // Attacked if I'm on the edge and the opponent king just next,
706 // but not on the edge.
707 if (V
.OnTheEdge(x
, y
)) {
708 for (let step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
709 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
712 !V
.OnTheEdge(i
, j
) &&
713 this.board
[i
][j
] != V
.EMPTY
&&
714 this.getPiece(i
, j
) == V
.KING
715 // NOTE: since only one king of each color, and (x, y) is occupied
716 // by our king, no need to check other king's color.
725 // No consideration of color: all pieces could be played
726 getAllPotentialMoves() {
727 let potentialMoves
= [];
728 for (let i
= 0; i
< V
.size
.x
; i
++) {
729 for (let j
= 0; j
< V
.size
.y
; j
++) {
730 if (this.board
[i
][j
] != V
.EMPTY
) {
731 Array
.prototype.push
.apply(
733 this.getPotentialMovesFrom([i
, j
])
738 return potentialMoves
;
742 if (this.subTurn
== 2)
745 return super.getCurrentScore();
749 // A click to promote a piece on subTurn 2 would trigger this.
750 // For now it would then return [NaN, NaN] because surrounding squares
751 // have no IDs in the promotion modal. TODO: improve this?
752 if (isNaN(square
[0])) return null;
753 // If subTurn == 2 && square is empty && !underCheck && !isOpposite,
754 // then return an empty move, allowing to "pass" subTurn2
755 const La
= this.amoves
.length
;
756 const Lf
= this.firstMove
.length
;
759 this.board
[square
[0]][square
[1]] == V
.EMPTY
&&
760 !this.underCheck(this.turn
) &&
761 (La
== 0 || !this.oppositeMoves(this.amoves
[La
-1], this.firstMove
[Lf
-1]))
764 start: { x: -1, y: -1 },
765 end: { x: -1, y: -1 },
774 move.flags
= JSON
.stringify(this.aggregateFlags());
775 V
.PlayOnBoard(this.board
, move);
776 if (this.subTurn
== 2) {
777 const L
= this.firstMove
.length
;
778 this.amoves
.push(this.getAmove(this.firstMove
[L
-1], move));
779 this.turn
= V
.GetOppCol(this.turn
);
782 else this.firstMove
.push(move);
783 this.subTurn
= 3 - this.subTurn
;
788 if (move.start
.x
< 0) return;
789 for (let a
of move.appear
)
790 if (a
.p
== V
.KING
) this.kingPos
[a
.c
] = [a
.x
, a
.y
];
791 this.updateCastleFlags(move);
794 updateCastleFlags(move) {
795 const firstRank
= { 'w': V
.size
.x
- 1, 'b': 0 };
796 for (let v
of move.vanish
) {
797 if (v
.p
== V
.KING
) this.castleFlags
[v
.c
] = [V
.size
.y
, V
.size
.y
];
798 else if (v
.x
== firstRank
[v
.c
] && this.castleFlags
[v
.c
].includes(v
.y
)) {
799 const flagIdx
= (v
.y
== this.castleFlags
[v
.c
][0] ? 0 : 1);
800 this.castleFlags
[v
.c
][flagIdx
] = V
.size
.y
;
806 this.disaggregateFlags(JSON
.parse(move.flags
));
807 V
.UndoOnBoard(this.board
, move);
808 if (this.subTurn
== 1) {
810 this.turn
= V
.GetOppCol(this.turn
);
813 else this.firstMove
.pop();
814 this.subTurn
= 3 - this.subTurn
;
819 // (Potentially) Reset king position
820 for (let v
of move.vanish
)
821 if (v
.p
== V
.KING
) this.kingPos
[v
.c
] = [v
.x
, v
.y
];
825 let moves
= this.getAllValidMoves();
826 if (moves
.length
== 0) return null;
827 // "Search" at depth 1 for now
828 const maxeval
= V
.INFINITY
;
829 const color
= this.turn
;
831 start: { x: -1, y: -1 },
832 end: { x: -1, y: -1 },
838 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
839 const moves2
= this.getAllValidMoves().concat([emptyMove
]);
841 moves2
.forEach(m2
=> {
843 const score
= this.getCurrentScore();
845 if (score
!= "1/2") {
846 if (score
!= "*") mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
847 else mvEval
= this.evalPosition();
850 (color
== 'w' && mvEval
> m
.eval
) ||
851 (color
== 'b' && mvEval
< m
.eval
)
860 moves
.sort((a
, b
) => {
861 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
863 let candidates
= [0];
864 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
866 const mIdx
= candidates
[randInt(candidates
.length
)];
867 const move2
= moves
[mIdx
].next
;
868 delete moves
[mIdx
]["next"];
869 return [moves
[mIdx
], move2
];
873 if (move.start
.x
< 0)
874 // A second move is always required, but may be empty
876 const initialSquare
= V
.CoordsToSquare(move.start
);
877 const finalSquare
= V
.CoordsToSquare(move.end
);
878 if (move.appear
.length
== 0)
879 // Pushed or pulled out of the board
880 return initialSquare
+ "R";
881 return move.appear
[0].p
.toUpperCase() + initialSquare
+ finalSquare
;