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 && m
.appear
.length
== 1);
11 // Stop at the first capture found (if any)
13 const color
= this.turn
;
14 for (let i
= 0; i
< V
.size
.x
; i
++) {
15 for (let j
= 0; j
< V
.size
.y
; j
++) {
17 this.board
[i
][j
] != V
.EMPTY
&&
18 this.getColor(i
, j
) == color
&&
19 this.getPotentialMovesFrom([i
, j
]).some(m
=> {
21 // Warning: discard castle moves
22 m
.vanish
.length
== 2 && m
.appear
.length
== 1 &&
23 this.filterValid([m
]).length
== 1
34 getPossibleMovesFrom(sq
) {
35 let moves
= this.filterValid(this.getPotentialMovesFrom(sq
));
36 const captureMoves
= V
.KeepCaptures(moves
);
38 console
.log(this.atLeastOneCapture());
40 if (captureMoves
.length
> 0) return captureMoves
;
41 if (this.atLeastOneCapture()) return [];
46 const moves
= super.getAllValidMoves();
47 if (moves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
48 return V
.KeepCaptures(moves
);
53 // If only my king remains, I win
54 const color
= this.turn
;
56 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
57 for (let j
=0; j
<V
.size
.y
; j
++) {
59 this.board
[i
][j
] != V
.EMPTY
&&
60 this.getColor(i
,j
) == color
&&
61 this.getPiece(i
,j
) != V
.KING
68 if (onlyKing
) return color
== "w" ? "1-0" : "0-1";
69 if (this.atLeastOneMove()) return "*";
70 // No valid move: the side who cannot move (or is checkmated) wins
71 return this.turn
== "w" ? "1-0" : "0-1";
75 // Less material is better (more subtle in fact but...)
76 return -super.evalPosition();