1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export const VariantRules
= class SuicideRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasCastle() {
14 getPotentialPawnMoves([x
, y
]) {
15 let moves
= super.getPotentialPawnMoves([x
, y
]);
17 // Complete with promotion(s) into king, if possible
18 const color
= this.turn
;
19 const shift
= color
== "w" ? -1 : 1;
20 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
21 if (x
+ shift
== lastRank
) {
23 if (this.board
[x
+ shift
][y
] == V
.EMPTY
)
25 this.getBasicMove([x
, y
], [x
+ shift
, y
], { c: color
, p: V
.KING
})
30 this.canTake([x
, y
], [x
+ shift
, y
- 1]) &&
31 this.board
[x
+ shift
][y
- 1] != V
.EMPTY
34 this.getBasicMove([x
, y
], [x
+ shift
, y
- 1], { c: color
, p: V
.KING
})
39 this.canTake([x
, y
], [x
+ shift
, y
+ 1]) &&
40 this.board
[x
+ shift
][y
+ 1] != V
.EMPTY
43 this.getBasicMove([x
, y
], [x
+ shift
, y
+ 1], { c: color
, p: V
.KING
})
51 // Trim all non-capturing moves (not the most efficient, but easy)
52 static KeepCaptures(moves
) {
53 return moves
.filter(m
=> m
.vanish
.length
== 2);
56 // Stop at the first capture found (if any)
58 const color
= this.turn
;
59 const oppCol
= V
.GetOppCol(color
);
60 for (let i
= 0; i
< V
.size
.x
; i
++) {
61 for (let j
= 0; j
< V
.size
.y
; j
++) {
63 this.board
[i
][j
] != V
.EMPTY
&&
64 this.getColor(i
, j
) != oppCol
&&
65 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
74 getPossibleMovesFrom(sq
) {
75 let moves
= this.getPotentialMovesFrom(sq
);
76 const captureMoves
= V
.KeepCaptures(moves
);
77 if (captureMoves
.length
> 0) return captureMoves
;
78 if (this.atLeastOneCapture()) return [];
87 const moves
= super.getAllValidMoves();
88 if (moves
.some(m
=> m
.vanish
.length
== 2)) return V
.KeepCaptures(moves
);
93 const color
= this.turn
;
94 for (let i
= 0; i
< V
.size
.x
; i
++) {
95 for (let j
= 0; j
< V
.size
.y
; j
++) {
97 this.getColor(i
, j
) == color
&&
98 this.getPotentialMovesFrom([i
, j
]).length
> 0
111 // No variables update because no royal king + no castling
118 if (this.atLeastOneMove()) return "*";
119 // No valid move: the side who cannot move wins
120 return this.turn
== "w" ? "1-0" : "0-1";
123 static get VALUES() {
134 static get SEARCH_DEPTH() {
139 // Less material is better:
140 return -super.evalPosition();
143 static GenRandInitFen(randomness
) {
145 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
147 let pieces
= { w: new Array(8), b: new Array(8) };
148 // Shuffle pieces on first and last rank
149 for (let c
of ["w", "b"]) {
150 if (c
== 'b' && randomness
== 1) {
151 pieces
['b'] = pieces
['w'];
155 let positions
= ArrayFun
.range(8);
157 // Get random squares for bishops
158 let randIndex
= 2 * randInt(4);
159 let bishop1Pos
= positions
[randIndex
];
160 // The second bishop must be on a square of different color
161 let randIndex_tmp
= 2 * randInt(4) + 1;
162 let bishop2Pos
= positions
[randIndex_tmp
];
163 // Remove chosen squares
164 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
165 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
167 // Get random squares for knights
168 randIndex
= randInt(6);
169 let knight1Pos
= positions
[randIndex
];
170 positions
.splice(randIndex
, 1);
171 randIndex
= randInt(5);
172 let knight2Pos
= positions
[randIndex
];
173 positions
.splice(randIndex
, 1);
175 // Get random square for queen
176 randIndex
= randInt(4);
177 let queenPos
= positions
[randIndex
];
178 positions
.splice(randIndex
, 1);
180 // Random square for king (no castle)
181 randIndex
= randInt(3);
182 let kingPos
= positions
[randIndex
];
183 positions
.splice(randIndex
, 1);
185 // Rooks positions are now fixed
186 let rook1Pos
= positions
[0];
187 let rook2Pos
= positions
[1];
189 // Finally put the shuffled pieces in the board array
190 pieces
[c
][rook1Pos
] = "r";
191 pieces
[c
][knight1Pos
] = "n";
192 pieces
[c
][bishop1Pos
] = "b";
193 pieces
[c
][queenPos
] = "q";
194 pieces
[c
][kingPos
] = "k";
195 pieces
[c
][bishop2Pos
] = "b";
196 pieces
[c
][knight2Pos
] = "n";
197 pieces
[c
][rook2Pos
] = "r";
200 pieces
["b"].join("") +
201 "/pppppppp/8/8/8/8/PPPPPPPP/" +
202 pieces
["w"].join("").toUpperCase() +
203 // En-passant allowed, but no flags