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 getPotentialPawnMoves([x
, y
]) {
11 let moves
= super.getPotentialPawnMoves([x
, y
]);
13 // Complete with promotion(s) into king, if possible
14 const color
= this.turn
;
15 const shift
= color
== "w" ? -1 : 1;
16 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
17 if (x
+ shift
== lastRank
) {
19 if (this.board
[x
+ shift
][y
] == V
.EMPTY
)
21 this.getBasicMove([x
, y
], [x
+ shift
, y
], { c: color
, p: V
.KING
})
26 this.canTake([x
, y
], [x
+ shift
, y
- 1]) &&
27 this.board
[x
+ shift
][y
- 1] != V
.EMPTY
30 this.getBasicMove([x
, y
], [x
+ shift
, y
- 1], { c: color
, p: V
.KING
})
35 this.canTake([x
, y
], [x
+ shift
, y
+ 1]) &&
36 this.board
[x
+ shift
][y
+ 1] != V
.EMPTY
39 this.getBasicMove([x
, y
], [x
+ shift
, y
+ 1], { c: color
, p: V
.KING
})
47 getPotentialKingMoves(sq
) {
49 return this.getSlideNJumpMoves(
51 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
56 // Trim all non-capturing moves (not the most efficient, but easy)
57 static KeepCaptures(moves
) {
58 return moves
.filter(m
=> m
.vanish
.length
== 2);
61 // Stop at the first capture found (if any)
63 const color
= this.turn
;
64 const oppCol
= V
.GetOppCol(color
);
65 for (let i
= 0; i
< V
.size
.x
; i
++) {
66 for (let j
= 0; j
< V
.size
.y
; j
++) {
68 this.board
[i
][j
] != V
.EMPTY
&&
69 this.getColor(i
, j
) != oppCol
&&
70 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
79 getPossibleMovesFrom(sq
) {
80 let moves
= this.getPotentialMovesFrom(sq
);
81 const captureMoves
= V
.KeepCaptures(moves
);
82 if (captureMoves
.length
> 0) return captureMoves
;
83 if (this.atLeastOneCapture()) return [];
92 const moves
= super.getAllValidMoves();
93 if (moves
.some(m
=> m
.vanish
.length
== 2)) return V
.KeepCaptures(moves
);
98 const color
= this.turn
;
99 for (let i
= 0; i
< V
.size
.x
; i
++) {
100 for (let j
= 0; j
< V
.size
.y
; j
++) {
102 this.getColor(i
, j
) == color
&&
103 this.getPotentialMovesFrom([i
, j
]).length
> 0
116 // No variables update because no royal king + no castling
118 unupdateVariables() {}
121 if (this.atLeastOneMove()) return "*";
122 // No valid move: the side who cannot move wins
123 return this.turn
== "w" ? "1-0" : "0-1";
126 static get VALUES() {
137 static get SEARCH_DEPTH() {
142 // Less material is better:
143 return -super.evalPosition();
146 static GenRandInitFen(randomness
) {
148 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
150 let pieces
= { w: new Array(8), b: new Array(8) };
151 // Shuffle pieces on first and last rank
152 for (let c
of ["w", "b"]) {
153 if (c
== 'b' && randomness
== 1) {
154 pieces
['b'] = pieces
['w'];
158 let positions
= ArrayFun
.range(8);
160 // Get random squares for bishops
161 let randIndex
= 2 * randInt(4);
162 let bishop1Pos
= positions
[randIndex
];
163 // The second bishop must be on a square of different color
164 let randIndex_tmp
= 2 * randInt(4) + 1;
165 let bishop2Pos
= positions
[randIndex_tmp
];
166 // Remove chosen squares
167 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
168 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
170 // Get random squares for knights
171 randIndex
= randInt(6);
172 let knight1Pos
= positions
[randIndex
];
173 positions
.splice(randIndex
, 1);
174 randIndex
= randInt(5);
175 let knight2Pos
= positions
[randIndex
];
176 positions
.splice(randIndex
, 1);
178 // Get random square for queen
179 randIndex
= randInt(4);
180 let queenPos
= positions
[randIndex
];
181 positions
.splice(randIndex
, 1);
183 // Random square for king (no castle)
184 randIndex
= randInt(3);
185 let kingPos
= positions
[randIndex
];
186 positions
.splice(randIndex
, 1);
188 // Rooks positions are now fixed
189 let rook1Pos
= positions
[0];
190 let rook2Pos
= positions
[1];
192 // Finally put the shuffled pieces in the board array
193 pieces
[c
][rook1Pos
] = "r";
194 pieces
[c
][knight1Pos
] = "n";
195 pieces
[c
][bishop1Pos
] = "b";
196 pieces
[c
][queenPos
] = "q";
197 pieces
[c
][kingPos
] = "k";
198 pieces
[c
][bishop2Pos
] = "b";
199 pieces
[c
][knight2Pos
] = "n";
200 pieces
[c
][rook2Pos
] = "r";
203 pieces
["b"].join("") +
204 "/pppppppp/8/8/8/8/PPPPPPPP/" +
205 pieces
["w"].join("").toUpperCase() +
206 // En-passant allowed, but no flags