1304ba985a23fd19ccfdfa87a5d535564981b4fe
1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class SuicideRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasCastle() {
14 static get PawnSpecs() {
18 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.KING
]) }
22 // Trim all non-capturing moves (not the most efficient, but easy)
23 static KeepCaptures(moves
) {
24 return moves
.filter(m
=> m
.vanish
.length
== 2);
27 // Stop at the first capture found (if any)
29 const color
= this.turn
;
30 const oppCol
= V
.GetOppCol(color
);
31 for (let i
= 0; i
< V
.size
.x
; i
++) {
32 for (let j
= 0; j
< V
.size
.y
; j
++) {
34 this.board
[i
][j
] != V
.EMPTY
&&
35 this.getColor(i
, j
) != oppCol
&&
36 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
45 getPossibleMovesFrom(sq
) {
46 let moves
= this.getPotentialMovesFrom(sq
);
47 const captureMoves
= V
.KeepCaptures(moves
);
48 if (captureMoves
.length
> 0) return captureMoves
;
49 if (this.atLeastOneCapture()) return [];
58 const moves
= super.getAllValidMoves();
59 if (moves
.some(m
=> m
.vanish
.length
== 2)) return V
.KeepCaptures(moves
);
64 const color
= this.turn
;
65 for (let i
= 0; i
< V
.size
.x
; i
++) {
66 for (let j
= 0; j
< V
.size
.y
; j
++) {
68 this.getColor(i
, j
) == color
&&
69 this.getPotentialMovesFrom([i
, j
]).length
> 0
82 // No variables update because no royal king + no castling
89 if (this.atLeastOneMove()) return "*";
90 // No valid move: the side who cannot move wins
91 return this.turn
== "w" ? "1-0" : "0-1";
105 static get SEARCH_DEPTH() {
110 // Less material is better:
111 return -super.evalPosition();
114 static GenRandInitFen(randomness
) {
116 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
118 let pieces
= { w: new Array(8), b: new Array(8) };
119 // Shuffle pieces on first and last rank
120 for (let c
of ["w", "b"]) {
121 if (c
== 'b' && randomness
== 1) {
122 pieces
['b'] = pieces
['w'];
126 // Get random squares for every piece, totally freely
127 let positions
= shuffle(ArrayFun
.range(8));
128 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
129 const rem2
= positions
[0] % 2;
130 if (rem2
== positions
[1] % 2) {
131 // Fix bishops (on different colors)
132 for (let i
=2; i
<8; i
++) {
133 if (positions
[i
] % 2 != rem2
)
134 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
137 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
140 pieces
["b"].join("") +
141 "/pppppppp/8/8/8/8/PPPPPPPP/" +
142 pieces
["w"].join("").toUpperCase() +
143 // En-passant allowed, but no flags