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