1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class SuicideRules
extends ChessRules
{
7 static get HasFlags() {
11 static get PawnSpecs() {
15 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.KING
]) }
19 static IsGoodPosition(position
) {
20 if (position
.length
== 0) return false;
21 const rows
= position
.split("/");
22 if (rows
.length
!= V
.size
.x
) return false;
23 // Just check that at least one piece of each color is there:
24 let pieces
= { "w": 0, "b": 0 };
25 for (let row
of rows
) {
27 for (let i
= 0; i
< row
.length
; i
++) {
28 const lowerRi
= row
[i
].toLowerCase();
29 if (V
.PIECES
.includes(lowerRi
)) {
30 pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
34 const num
= parseInt(row
[i
], 10);
35 if (isNaN(num
)) return false;
39 if (sumElts
!= V
.size
.y
) return false;
41 if (Object
.values(pieces
).some(v
=> v
== 0)) return false;
47 // Trim all non-capturing moves (not the most efficient, but easy)
48 static KeepCaptures(moves
) {
49 return moves
.filter(m
=> m
.vanish
.length
== 2);
52 // Stop at the first capture found (if any)
54 const color
= this.turn
;
55 for (let i
= 0; i
< V
.size
.x
; i
++) {
56 for (let j
= 0; j
< V
.size
.y
; j
++) {
58 this.board
[i
][j
] != V
.EMPTY
&&
59 this.getColor(i
, j
) == color
&&
60 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
69 getPossibleMovesFrom(sq
) {
70 let moves
= this.getPotentialMovesFrom(sq
);
71 const captureMoves
= V
.KeepCaptures(moves
);
72 if (captureMoves
.length
> 0) return captureMoves
;
73 if (this.atLeastOneCapture()) return [];
82 const moves
= super.getAllValidMoves();
83 if (moves
.some(m
=> m
.vanish
.length
== 2)) return V
.KeepCaptures(moves
);
88 const color
= this.turn
;
89 for (let i
= 0; i
< V
.size
.x
; i
++) {
90 for (let j
= 0; j
< V
.size
.y
; j
++) {
92 this.getColor(i
, j
) == color
&&
93 this.getPotentialMovesFrom([i
, j
]).length
> 0
106 // No variables update because no royal king + no castling
113 if (this.atLeastOneMove()) return "*";
114 // No valid move: the side who cannot move wins
115 return this.turn
== "w" ? "1-0" : "0-1";
118 static get VALUES() {
129 static get SEARCH_DEPTH() {
134 // Less material is better:
135 return -super.evalPosition();
138 static GenRandInitFen(randomness
) {
140 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
142 let pieces
= { w: new Array(8), b: new Array(8) };
143 for (let c
of ["w", "b"]) {
144 if (c
== 'b' && randomness
== 1) {
145 pieces
['b'] = pieces
['w'];
149 // Get random squares for every piece, totally freely
150 let positions
= shuffle(ArrayFun
.range(8));
151 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
152 const rem2
= positions
[0] % 2;
153 if (rem2
== positions
[1] % 2) {
154 // Fix bishops (on different colors)
155 for (let i
=2; i
<8; i
++) {
156 if (positions
[i
] % 2 != rem2
) {
157 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
162 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
165 pieces
["b"].join("") +
166 "/pppppppp/8/8/8/8/PPPPPPPP/" +
167 pieces
["w"].join("").toUpperCase() +
168 // En-passant allowed, but no flags