Started code review + some fixes (unfinished)
[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
6808d7a1
BA
5export const VariantRules = class WildebeestRules extends ChessRules {
6 static getPpath(b) {
7 return ([V.CAMEL, V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
dac39588 8 }
a37076f1 9
6808d7a1
BA
10 static get size() {
11 return { x: 10, y: 11 };
12 }
8a196305 13
6808d7a1
BA
14 static get CAMEL() {
15 return "c";
16 }
17 static get WILDEBEEST() {
18 return "w";
19 }
a37076f1 20
6808d7a1
BA
21 static get PIECES() {
22 return ChessRules.PIECES.concat([V.CAMEL, V.WILDEBEEST]);
dac39588 23 }
7931e479 24
6808d7a1 25 static get steps() {
dac39588
BA
26 return Object.assign(
27 ChessRules.steps, //add camel moves:
6808d7a1
BA
28 {
29 c: [
30 [-3, -1],
31 [-3, 1],
32 [-1, -3],
33 [-1, 3],
34 [1, -3],
35 [1, 3],
36 [3, -1],
37 [3, 1]
38 ]
39 }
dac39588
BA
40 );
41 }
a37076f1 42
6808d7a1
BA
43 static IsGoodEnpassant(enpassant) {
44 if (enpassant != "-") {
dac39588 45 const squares = enpassant.split(",");
6808d7a1
BA
46 if (squares.length > 2) return false;
47 for (let sq of squares) {
dac39588 48 const ep = V.SquareToCoords(sq);
6808d7a1 49 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
dac39588
BA
50 }
51 }
52 return true;
53 }
6e62b1c7 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;
120 const finalPieces =
121 x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN];
a37076f1 122
6808d7a1 123 if (this.board[x + shiftX][y] == V.EMPTY) {
dac39588
BA
124 // One square forward
125 for (let piece of finalPieces)
6808d7a1
BA
126 moves.push(
127 this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
128 );
129 if (startRanks.includes(x)) {
130 if (this.board[x + 2 * shiftX][y] == V.EMPTY) {
dac39588 131 // Two squares jump
6808d7a1
BA
132 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
133 if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) {
dac39588 134 // Three squares jump
6808d7a1 135 moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
dac39588
BA
136 }
137 }
138 }
139 }
140 // Captures
6808d7a1
BA
141 for (let shiftY of [-1, 1]) {
142 if (
143 y + shiftY >= 0 &&
144 y + shiftY < sizeY &&
145 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
146 this.canTake([x, y], [x + shiftX, y + shiftY])
147 ) {
148 for (let piece of finalPieces) {
149 moves.push(
150 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
151 c: color,
152 p: piece
153 })
154 );
dac39588
BA
155 }
156 }
157 }
a37076f1 158
dac39588
BA
159 // En passant
160 const Lep = this.epSquares.length;
6808d7a1
BA
161 const epSquare = this.epSquares[Lep - 1];
162 if (epSquare) {
163 for (let epsq of epSquare) {
dac39588 164 // TODO: some redundant checks
6808d7a1
BA
165 if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) {
166 var enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]);
dac39588
BA
167 // WARNING: the captured pawn may be diagonally behind us,
168 // if it's a 3-squares jump and we take on 1st passing square
6808d7a1 169 const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX;
dac39588
BA
170 enpassantMove.vanish.push({
171 x: px,
172 y: epsq.y,
6808d7a1
BA
173 p: "p",
174 c: this.getColor(px, epsq.y)
dac39588
BA
175 });
176 moves.push(enpassantMove);
177 }
178 }
179 }
a37076f1 180
dac39588
BA
181 return moves;
182 }
a37076f1 183
dac39588 184 // TODO: wildebeest castle
8a196305 185
6808d7a1 186 getPotentialCamelMoves(sq) {
dac39588
BA
187 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
188 }
a37076f1 189
6808d7a1 190 getPotentialWildebeestMoves(sq) {
dac39588 191 return this.getSlideNJumpMoves(
6808d7a1
BA
192 sq,
193 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
194 "oneStep"
195 );
dac39588 196 }
a37076f1 197
6808d7a1
BA
198 isAttacked(sq, colors) {
199 return (
200 super.isAttacked(sq, colors) ||
201 this.isAttackedByCamel(sq, colors) ||
202 this.isAttackedByWildebeest(sq, colors)
203 );
dac39588 204 }
a37076f1 205
6808d7a1
BA
206 isAttackedByCamel(sq, colors) {
207 return this.isAttackedBySlideNJump(
208 sq,
209 colors,
210 V.CAMEL,
211 V.steps[V.CAMEL],
212 "oneStep"
213 );
dac39588 214 }
a37076f1 215
6808d7a1
BA
216 isAttackedByWildebeest(sq, colors) {
217 return this.isAttackedBySlideNJump(
218 sq,
219 colors,
220 V.WILDEBEEST,
221 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]),
222 "oneStep"
223 );
dac39588 224 }
a37076f1 225
6808d7a1
BA
226 getCurrentScore() {
227 if (this.atLeastOneMove())
228 // game not over
0c3fe8a6
BA
229 return "*";
230
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(
237 ChessRules.VALUES,
6808d7a1 238 { c: 3, w: 7 } //experimental
dac39588
BA
239 );
240 }
a37076f1 241
6808d7a1
BA
242 static get SEARCH_DEPTH() {
243 return 2;
244 }
3c09dc49 245
6808d7a1
BA
246 static GenRandInitFen() {
247 let pieces = { w: new Array(10), b: new Array(10) };
248 for (let c of ["w", "b"]) {
dac39588 249 let positions = ArrayFun.range(11);
a37076f1 250
dac39588 251 // Get random squares for bishops + camels (different colors)
6808d7a1
BA
252 let randIndexes = sample(ArrayFun.range(6), 2).map(i => {
253 return 2 * i;
254 });
dac39588
BA
255 let bishop1Pos = positions[randIndexes[0]];
256 let camel1Pos = positions[randIndexes[1]];
257 // The second bishop (camel) must be on a square of different color
6808d7a1
BA
258 let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => {
259 return 2 * i + 1;
260 });
dac39588
BA
261 let bishop2Pos = positions[randIndexes_tmp[0]];
262 let camel2Pos = positions[randIndexes_tmp[1]];
6808d7a1
BA
263 for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => {
264 return b - a;
265 })) {
266 //largest indices first
dac39588
BA
267 positions.splice(idx, 1);
268 }
a37076f1 269
dac39588
BA
270 let randIndex = randInt(7);
271 let knight1Pos = positions[randIndex];
272 positions.splice(randIndex, 1);
273 randIndex = randInt(6);
274 let knight2Pos = positions[randIndex];
275 positions.splice(randIndex, 1);
a37076f1 276
dac39588
BA
277 randIndex = randInt(5);
278 let queenPos = positions[randIndex];
279 positions.splice(randIndex, 1);
8a196305 280
dac39588
BA
281 // Random square for wildebeest
282 randIndex = randInt(4);
283 let wildebeestPos = positions[randIndex];
284 positions.splice(randIndex, 1);
8a196305 285
dac39588
BA
286 let rook1Pos = positions[0];
287 let kingPos = positions[1];
288 let rook2Pos = positions[2];
a37076f1 289
6808d7a1
BA
290 pieces[c][rook1Pos] = "r";
291 pieces[c][knight1Pos] = "n";
292 pieces[c][bishop1Pos] = "b";
293 pieces[c][queenPos] = "q";
294 pieces[c][camel1Pos] = "c";
295 pieces[c][camel2Pos] = "c";
296 pieces[c][wildebeestPos] = "w";
297 pieces[c][kingPos] = "k";
298 pieces[c][bishop2Pos] = "b";
299 pieces[c][knight2Pos] = "n";
300 pieces[c][rook2Pos] = "r";
dac39588 301 }
6808d7a1
BA
302 return (
303 pieces["b"].join("") +
dac39588
BA
304 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
305 pieces["w"].join("").toUpperCase() +
6808d7a1
BA
306 " w 0 1111 -"
307 );
dac39588 308 }
6808d7a1 309};