Fix moves list scrolling. Seemingly a remaining issue in SynchroneChess... (kings...
[vchess.git] / client / src / variants / Wildebeest.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { sample, randInt } from "@/utils/alea";
4
32f6285e 5export class WildebeestRules extends ChessRules {
6808d7a1
BA
6 static get size() {
7 return { x: 10, y: 11 };
8 }
8a196305 9
6808d7a1
BA
10 static get CAMEL() {
11 return "c";
12 }
13 static get WILDEBEEST() {
14 return "w";
15 }
a37076f1 16
6808d7a1
BA
17 static get PIECES() {
18 return ChessRules.PIECES.concat([V.CAMEL, V.WILDEBEEST]);
dac39588 19 }
7931e479 20
6808d7a1 21 static get steps() {
dac39588 22 return Object.assign(
6f2f9437
BA
23 {},
24 ChessRules.steps,
25 // Add camel moves:
6808d7a1
BA
26 {
27 c: [
28 [-3, -1],
29 [-3, 1],
30 [-1, -3],
31 [-1, 3],
32 [1, -3],
33 [1, 3],
34 [3, -1],
35 [3, 1]
36 ]
37 }
dac39588
BA
38 );
39 }
a37076f1 40
6808d7a1
BA
41 static IsGoodEnpassant(enpassant) {
42 if (enpassant != "-") {
dac39588 43 const squares = enpassant.split(",");
6808d7a1
BA
44 if (squares.length > 2) return false;
45 for (let sq of squares) {
dac39588 46 const ep = V.SquareToCoords(sq);
6808d7a1 47 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
dac39588
BA
48 }
49 }
50 return true;
51 }
6e62b1c7 52
241bf8f2
BA
53 getPpath(b) {
54 return ([V.CAMEL, V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
55 }
56
dac39588 57 // There may be 2 enPassant squares (if pawn jump 3 squares)
6808d7a1 58 getEnpassantFen() {
dac39588 59 const L = this.epSquares.length;
6808d7a1 60 if (!this.epSquares[L - 1]) return "-"; //no en-passant
dac39588 61 let res = "";
6808d7a1 62 this.epSquares[L - 1].forEach(sq => {
dac39588
BA
63 res += V.CoordsToSquare(sq) + ",";
64 });
6808d7a1 65 return res.slice(0, -1); //remove last comma
dac39588 66 }
2d7194bd 67
dac39588 68 // En-passant after 2-sq or 3-sq jumps
6808d7a1
BA
69 getEpSquare(moveOrSquare) {
70 if (!moveOrSquare) return undefined;
71 if (typeof moveOrSquare === "string") {
dac39588 72 const square = moveOrSquare;
6808d7a1 73 if (square == "-") return undefined;
dac39588
BA
74 let res = [];
75 square.split(",").forEach(sq => {
76 res.push(V.SquareToCoords(sq));
77 });
78 return res;
79 }
80 // Argument is a move:
81 const move = moveOrSquare;
6808d7a1
BA
82 const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x];
83 if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) {
84 const step = (ex - sx) / Math.abs(ex - sx);
85 let res = [
86 {
87 x: sx + step,
88 y: sy
89 }
90 ];
91 if (sx + 2 * step != ex) {
92 //3-squares move
dac39588 93 res.push({
6808d7a1 94 x: sx + 2 * step,
dac39588
BA
95 y: sy
96 });
97 }
98 return res;
99 }
100 return undefined; //default
101 }
a37076f1 102
6808d7a1
BA
103 getPotentialMovesFrom([x, y]) {
104 switch (this.getPiece(x, y)) {
dac39588 105 case V.CAMEL:
6808d7a1 106 return this.getPotentialCamelMoves([x, y]);
dac39588 107 case V.WILDEBEEST:
6808d7a1 108 return this.getPotentialWildebeestMoves([x, y]);
dac39588 109 default:
6808d7a1 110 return super.getPotentialMovesFrom([x, y]);
dac39588
BA
111 }
112 }
a37076f1 113
dac39588 114 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
6808d7a1 115 getPotentialPawnMoves([x, y]) {
dac39588
BA
116 const color = this.turn;
117 let moves = [];
6808d7a1
BA
118 const [sizeX, sizeY] = [V.size.x, V.size.y];
119 const shiftX = color == "w" ? -1 : 1;
120 const startRanks = color == "w" ? [sizeX - 2, sizeX - 3] : [1, 2];
121 const lastRank = color == "w" ? 0 : sizeX - 1;
e3ef199c
BA
122 const finalPieces = x + shiftX == lastRank
123 ? [V.WILDEBEEST, V.QUEEN]
124 : [V.PAWN];
a37076f1 125
6808d7a1 126 if (this.board[x + shiftX][y] == V.EMPTY) {
dac39588
BA
127 // One square forward
128 for (let piece of finalPieces)
6808d7a1
BA
129 moves.push(
130 this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
131 );
132 if (startRanks.includes(x)) {
133 if (this.board[x + 2 * shiftX][y] == V.EMPTY) {
dac39588 134 // Two squares jump
6808d7a1
BA
135 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
136 if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) {
dac39588 137 // Three squares jump
6808d7a1 138 moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
dac39588
BA
139 }
140 }
141 }
142 }
143 // Captures
6808d7a1
BA
144 for (let shiftY of [-1, 1]) {
145 if (
146 y + shiftY >= 0 &&
147 y + shiftY < sizeY &&
148 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
149 this.canTake([x, y], [x + shiftX, y + shiftY])
150 ) {
151 for (let piece of finalPieces) {
152 moves.push(
153 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
154 c: color,
155 p: piece
156 })
157 );
dac39588
BA
158 }
159 }
160 }
a37076f1 161
dac39588
BA
162 // En passant
163 const Lep = this.epSquares.length;
6808d7a1
BA
164 const epSquare = this.epSquares[Lep - 1];
165 if (epSquare) {
166 for (let epsq of epSquare) {
dac39588 167 // TODO: some redundant checks
6808d7a1
BA
168 if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) {
169 var enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]);
dac39588
BA
170 // WARNING: the captured pawn may be diagonally behind us,
171 // if it's a 3-squares jump and we take on 1st passing square
6808d7a1 172 const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX;
dac39588
BA
173 enpassantMove.vanish.push({
174 x: px,
175 y: epsq.y,
6808d7a1
BA
176 p: "p",
177 c: this.getColor(px, epsq.y)
dac39588
BA
178 });
179 moves.push(enpassantMove);
180 }
181 }
182 }
a37076f1 183
dac39588
BA
184 return moves;
185 }
a37076f1 186
dac39588 187 // TODO: wildebeest castle
8a196305 188
6808d7a1 189 getPotentialCamelMoves(sq) {
dac39588
BA
190 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
191 }
a37076f1 192
6808d7a1 193 getPotentialWildebeestMoves(sq) {
dac39588 194 return this.getSlideNJumpMoves(
6808d7a1
BA
195 sq,
196 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
197 "oneStep"
198 );
dac39588 199 }
a37076f1 200
68e19a44 201 isAttacked(sq, color) {
6808d7a1 202 return (
68e19a44
BA
203 super.isAttacked(sq, color) ||
204 this.isAttackedByCamel(sq, color) ||
205 this.isAttackedByWildebeest(sq, color)
6808d7a1 206 );
dac39588 207 }
a37076f1 208
68e19a44 209 isAttackedByCamel(sq, color) {
6808d7a1
BA
210 return this.isAttackedBySlideNJump(
211 sq,
68e19a44 212 color,
6808d7a1
BA
213 V.CAMEL,
214 V.steps[V.CAMEL],
215 "oneStep"
216 );
dac39588 217 }
a37076f1 218
68e19a44 219 isAttackedByWildebeest(sq, color) {
6808d7a1
BA
220 return this.isAttackedBySlideNJump(
221 sq,
68e19a44 222 color,
6808d7a1
BA
223 V.WILDEBEEST,
224 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
225 "oneStep"
226 );
dac39588 227 }
a37076f1 228
6808d7a1 229 getCurrentScore() {
bb688df5 230 if (this.atLeastOneMove()) return "*";
dac39588 231 // No valid move: game is lost (stalemate is a win)
6808d7a1 232 return this.turn == "w" ? "0-1" : "1-0";
dac39588 233 }
efb20746 234
dac39588
BA
235 static get VALUES() {
236 return Object.assign(
a97bdbda
BA
237 { c: 3, w: 7 }, //experimental
238 ChessRules.VALUES
dac39588
BA
239 );
240 }
a37076f1 241
6808d7a1
BA
242 static get SEARCH_DEPTH() {
243 return 2;
244 }
3c09dc49 245
7ba4a5bc
BA
246 static GenRandInitFen(randomness) {
247 if (!randomness) randomness = 2;
248 if (randomness == 0)
6f2f9437 249 return "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR w 0 akak -";
7ba4a5bc 250
6f2f9437 251 let pieces = { w: new Array(11), b: new Array(11) };
3a2a7b5f 252 let flags = "";
6808d7a1 253 for (let c of ["w", "b"]) {
7ba4a5bc
BA
254 if (c == 'b' && randomness == 1) {
255 pieces['b'] = pieces['w'];
3a2a7b5f 256 flags += flags;
7ba4a5bc
BA
257 break;
258 }
259
dac39588 260 let positions = ArrayFun.range(11);
a37076f1 261
dac39588 262 // Get random squares for bishops + camels (different colors)
6808d7a1
BA
263 let randIndexes = sample(ArrayFun.range(6), 2).map(i => {
264 return 2 * i;
265 });
dac39588
BA
266 let bishop1Pos = positions[randIndexes[0]];
267 let camel1Pos = positions[randIndexes[1]];
268 // The second bishop (camel) must be on a square of different color
6808d7a1
BA
269 let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => {
270 return 2 * i + 1;
271 });
dac39588
BA
272 let bishop2Pos = positions[randIndexes_tmp[0]];
273 let camel2Pos = positions[randIndexes_tmp[1]];
6808d7a1
BA
274 for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => {
275 return b - a;
276 })) {
7ba4a5bc 277 // Largest indices first
dac39588
BA
278 positions.splice(idx, 1);
279 }
a37076f1 280
dac39588
BA
281 let randIndex = randInt(7);
282 let knight1Pos = positions[randIndex];
283 positions.splice(randIndex, 1);
284 randIndex = randInt(6);
285 let knight2Pos = positions[randIndex];
286 positions.splice(randIndex, 1);
a37076f1 287
dac39588
BA
288 randIndex = randInt(5);
289 let queenPos = positions[randIndex];
290 positions.splice(randIndex, 1);
8a196305 291
dac39588
BA
292 // Random square for wildebeest
293 randIndex = randInt(4);
294 let wildebeestPos = positions[randIndex];
295 positions.splice(randIndex, 1);
8a196305 296
dac39588
BA
297 let rook1Pos = positions[0];
298 let kingPos = positions[1];
299 let rook2Pos = positions[2];
a37076f1 300
6808d7a1
BA
301 pieces[c][rook1Pos] = "r";
302 pieces[c][knight1Pos] = "n";
303 pieces[c][bishop1Pos] = "b";
304 pieces[c][queenPos] = "q";
305 pieces[c][camel1Pos] = "c";
306 pieces[c][camel2Pos] = "c";
307 pieces[c][wildebeestPos] = "w";
308 pieces[c][kingPos] = "k";
309 pieces[c][bishop2Pos] = "b";
310 pieces[c][knight2Pos] = "n";
311 pieces[c][rook2Pos] = "r";
3a2a7b5f 312 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
dac39588 313 }
6808d7a1
BA
314 return (
315 pieces["b"].join("") +
6f2f9437 316 "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" +
dac39588 317 pieces["w"].join("").toUpperCase() +
3a2a7b5f 318 " w 0 " + flags + " -"
6808d7a1 319 );
dac39588 320 }
6808d7a1 321};