1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class LosersRules
extends ChessRules
{
6 // Trim all non-capturing moves
7 static KeepCaptures(moves
) {
8 return moves
.filter(m
=> m
.vanish
.length
== 2);
11 // Stop at the first capture found (if any)
13 const color
= this.turn
;
14 const oppCol
= V
.GetOppCol(color
);
15 for (let i
= 0; i
< V
.size
.x
; i
++) {
16 for (let j
= 0; j
< V
.size
.y
; j
++) {
18 this.board
[i
][j
] != V
.EMPTY
&&
19 this.getColor(i
, j
) != oppCol
&&
20 this.getPotentialMovesFrom([i
, j
]).some(m
=>
21 // Warning: duscard castle moves
22 m
.vanish
.length
== 2 && m
.appear
.length
== 1)
31 getPossibleMovesFrom(sq
) {
32 let moves
= this.filterValid(this.getPotentialMovesFrom(sq
));
33 const captureMoves
= V
.KeepCaptures(moves
);
34 if (captureMoves
.length
> 0) return captureMoves
;
35 if (this.atLeastOneCapture()) return [];
40 const moves
= super.getAllValidMoves();
41 if (moves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
42 return V
.KeepCaptures(moves
);
47 // If only my king remains, I win
48 const color
= this.turn
;
50 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
51 for (let j
=0; j
<V
.size
.y
; j
++) {
53 this.board
[i
][j
] != V
.EMPTY
&&
54 this.getColor(i
,j
) == color
&&
55 this.getPiece(i
,j
) != V
.KING
62 if (onlyKing
) return color
== "w" ? "1-0" : "0-1";
63 if (this.atLeastOneMove()) return "*";
64 // No valid move: the side who cannot move (or is checkmated) wins
65 return this.turn
== "w" ? "1-0" : "0-1";
68 static get SEARCH_DEPTH() {
73 // Less material is better (more subtle in fact but...)
74 return -super.evalPosition();