Draft Ball variant + some fixes, enhancements and code cleaning
[vchess.git] / client / src / variants / Wildebeest.js
... / ...
CommitLineData
1import { ChessRules } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { sample, randInt } from "@/utils/alea";
4
5export 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 != "-") {
43 const squares = enpassant.split(",");
44 if (squares.length > 2) return false;
45 for (let sq of squares) {
46 const ep = V.SquareToCoords(sq);
47 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
48 }
49 }
50 return true;
51 }
52
53 getPpath(b) {
54 return ([V.CAMEL, V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
55 }
56
57 // There may be 2 enPassant squares (if pawn jump 3 squares)
58 getEnpassantFen() {
59 const L = this.epSquares.length;
60 if (!this.epSquares[L - 1]) return "-"; //no en-passant
61 let res = "";
62 this.epSquares[L - 1].forEach(sq => {
63 res += V.CoordsToSquare(sq) + ",";
64 });
65 return res.slice(0, -1); //remove last comma
66 }
67
68 // En-passant after 2-sq or 3-sq jumps
69 getEpSquare(moveOrSquare) {
70 if (!moveOrSquare) return undefined;
71 if (typeof moveOrSquare === "string") {
72 const square = moveOrSquare;
73 if (square == "-") return undefined;
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;
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
93 res.push({
94 x: sx + 2 * step,
95 y: sy
96 });
97 }
98 return res;
99 }
100 return undefined; //default
101 }
102
103 getPotentialMovesFrom([x, y]) {
104 switch (this.getPiece(x, y)) {
105 case V.CAMEL:
106 return this.getPotentialCamelMoves([x, y]);
107 case V.WILDEBEEST:
108 return this.getPotentialWildebeestMoves([x, y]);
109 default:
110 return super.getPotentialMovesFrom([x, y]);
111 }
112 }
113
114 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
115 getPotentialPawnMoves([x, y]) {
116 const color = this.turn;
117 let moves = [];
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;
122 const finalPieces = x + shiftX == lastRank
123 ? [V.WILDEBEEST, V.QUEEN]
124 : [V.PAWN];
125
126 if (this.board[x + shiftX][y] == V.EMPTY) {
127 // One square forward
128 for (let piece of finalPieces)
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) {
134 // Two squares jump
135 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
136 if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) {
137 // Three squares jump
138 moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
139 }
140 }
141 }
142 }
143 // Captures
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 );
158 }
159 }
160 }
161
162 // En passant
163 const Lep = this.epSquares.length;
164 const epSquare = this.epSquares[Lep - 1];
165 if (epSquare) {
166 for (let epsq of epSquare) {
167 // TODO: some redundant checks
168 if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) {
169 var enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]);
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
172 const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX;
173 enpassantMove.vanish.push({
174 x: px,
175 y: epsq.y,
176 p: "p",
177 c: this.getColor(px, epsq.y)
178 });
179 moves.push(enpassantMove);
180 }
181 }
182 }
183
184 return moves;
185 }
186
187 // TODO: wildebeest castle
188
189 getPotentialCamelMoves(sq) {
190 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
191 }
192
193 getPotentialWildebeestMoves(sq) {
194 return this.getSlideNJumpMoves(
195 sq,
196 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
197 "oneStep"
198 );
199 }
200
201 isAttacked(sq, color) {
202 return (
203 super.isAttacked(sq, color) ||
204 this.isAttackedByCamel(sq, color) ||
205 this.isAttackedByWildebeest(sq, color)
206 );
207 }
208
209 isAttackedByCamel(sq, color) {
210 return this.isAttackedBySlideNJump(
211 sq,
212 color,
213 V.CAMEL,
214 V.steps[V.CAMEL],
215 "oneStep"
216 );
217 }
218
219 isAttackedByWildebeest(sq, color) {
220 return this.isAttackedBySlideNJump(
221 sq,
222 color,
223 V.WILDEBEEST,
224 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
225 "oneStep"
226 );
227 }
228
229 getCurrentScore() {
230 if (this.atLeastOneMove()) return "*";
231 // No valid move: game is lost (stalemate is a win)
232 return this.turn == "w" ? "0-1" : "1-0";
233 }
234
235 static get VALUES() {
236 return Object.assign(
237 { c: 3, w: 7 }, //experimental
238 ChessRules.VALUES
239 );
240 }
241
242 static get SEARCH_DEPTH() {
243 return 2;
244 }
245
246 static GenRandInitFen(randomness) {
247 if (!randomness) randomness = 2;
248 if (randomness == 0)
249 return "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR w 0 akak -";
250
251 let pieces = { w: new Array(11), b: new Array(11) };
252 let flags = "";
253 for (let c of ["w", "b"]) {
254 if (c == 'b' && randomness == 1) {
255 pieces['b'] = pieces['w'];
256 flags += flags;
257 break;
258 }
259
260 let positions = ArrayFun.range(11);
261
262 // Get random squares for bishops + camels (different colors)
263 let randIndexes = sample(ArrayFun.range(6), 2).map(i => {
264 return 2 * i;
265 });
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
269 let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => {
270 return 2 * i + 1;
271 });
272 let bishop2Pos = positions[randIndexes_tmp[0]];
273 let camel2Pos = positions[randIndexes_tmp[1]];
274 for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => {
275 return b - a;
276 })) {
277 // Largest indices first
278 positions.splice(idx, 1);
279 }
280
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);
287
288 randIndex = randInt(5);
289 let queenPos = positions[randIndex];
290 positions.splice(randIndex, 1);
291
292 // Random square for wildebeest
293 randIndex = randInt(4);
294 let wildebeestPos = positions[randIndex];
295 positions.splice(randIndex, 1);
296
297 let rook1Pos = positions[0];
298 let kingPos = positions[1];
299 let rook2Pos = positions[2];
300
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";
312 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
313 }
314 return (
315 pieces["b"].join("") +
316 "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" +
317 pieces["w"].join("").toUpperCase() +
318 " w 0 " + flags + " -"
319 );
320 }
321};