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