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