'update'
[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 6{
dac39588
BA
7 static getPpath(b)
8 {
9 return ([V.CAMEL,V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
10 }
a37076f1 11
dac39588 12 static get size() { return {x:10,y:11}; }
8a196305 13
dac39588
BA
14 static get CAMEL() { return 'c'; }
15 static get WILDEBEEST() { return 'w'; }
a37076f1 16
dac39588
BA
17 static get PIECES()
18 {
19 return ChessRules.PIECES.concat([V.CAMEL,V.WILDEBEEST]);
20 }
7931e479 21
dac39588
BA
22 static get steps()
23 {
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 }
a37076f1 29
dac39588
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 }
6e62b1c7 46
dac39588
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 }
2d7194bd 59
dac39588
BA
60 // En-passant after 2-sq or 3-sq jumps
61 getEpSquare(moveOrSquare)
62 {
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;
78 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
79 if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2)
80 {
81 const step = (ex-sx) / Math.abs(ex-sx);
82 let res = [{
83 x: sx + step,
84 y: sy
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;
94 }
95 return undefined; //default
96 }
a37076f1 97
dac39588
BA
98 getPotentialMovesFrom([x,y])
99 {
100 switch (this.getPiece(x,y))
101 {
102 case V.CAMEL:
103 return this.getPotentialCamelMoves([x,y]);
104 case V.WILDEBEEST:
105 return this.getPotentialWildebeestMoves([x,y]);
106 default:
107 return super.getPotentialMovesFrom([x,y])
108 }
109 }
a37076f1 110
dac39588
BA
111 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
112 getPotentialPawnMoves([x,y])
113 {
114 const color = this.turn;
115 let moves = [];
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 = x + shiftX == lastRank
121 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
122 : [V.PAWN];
a37076f1 123
dac39588
BA
124 if (this.board[x+shiftX][y] == V.EMPTY)
125 {
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))
130 {
131 if (this.board[x+2*shiftX][y] == V.EMPTY)
132 {
133 // Two squares jump
134 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
135 if (x==startRanks[0] && this.board[x+3*shiftX][y] == V.EMPTY)
136 {
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 {
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)
151 {
152 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
153 {c:color,p:piece}));
154 }
155 }
156 }
a37076f1 157
dac39588
BA
158 // En passant
159 const Lep = this.epSquares.length;
160 const epSquare = this.epSquares[Lep-1];
161 if (!!epSquare)
162 {
163 for (let epsq of epSquare)
164 {
165 // TODO: some redundant checks
166 if (epsq.x == x+shiftX && Math.abs(epsq.y - y) == 1)
167 {
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);
172 enpassantMove.vanish.push({
173 x: px,
174 y: epsq.y,
175 p: 'p',
176 c: this.getColor(px,epsq.y)
177 });
178 moves.push(enpassantMove);
179 }
180 }
181 }
a37076f1 182
dac39588
BA
183 return moves;
184 }
a37076f1 185
dac39588 186 // TODO: wildebeest castle
8a196305 187
dac39588
BA
188 getPotentialCamelMoves(sq)
189 {
190 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
191 }
a37076f1 192
dac39588
BA
193 getPotentialWildebeestMoves(sq)
194 {
195 return this.getSlideNJumpMoves(
196 sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
197 }
a37076f1 198
dac39588
BA
199 isAttacked(sq, colors)
200 {
201 return super.isAttacked(sq, colors)
202 || this.isAttackedByCamel(sq, colors)
203 || this.isAttackedByWildebeest(sq, colors);
204 }
a37076f1 205
dac39588
BA
206 isAttackedByCamel(sq, colors)
207 {
208 return this.isAttackedBySlideNJump(sq, colors,
209 V.CAMEL, V.steps[V.CAMEL], "oneStep");
210 }
a37076f1 211
dac39588
BA
212 isAttackedByWildebeest(sq, colors)
213 {
214 return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST,
215 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
216 }
a37076f1 217
0c3fe8a6
BA
218 getCurrentScore()
219 {
220 if (this.atLeastOneMove()) // game not over
221 return "*";
222
dac39588
BA
223 // No valid move: game is lost (stalemate is a win)
224 return (this.turn == "w" ? "0-1" : "1-0");
225 }
efb20746 226
dac39588
BA
227 static get VALUES() {
228 return Object.assign(
229 ChessRules.VALUES,
230 {'c': 3, 'w': 7} //experimental
231 );
232 }
a37076f1 233
dac39588 234 static get SEARCH_DEPTH() { return 2; }
3c09dc49 235
dac39588
BA
236 static GenRandInitFen()
237 {
238 let pieces = { "w": new Array(10), "b": new Array(10) };
239 for (let c of ["w","b"])
240 {
241 let positions = ArrayFun.range(11);
a37076f1 242
dac39588
BA
243 // Get random squares for bishops + camels (different colors)
244 let randIndexes = sample(ArrayFun.range(6), 2)
0c3fe8a6 245 .map(i => { return 2*i; });
dac39588
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
249 let randIndexes_tmp = sample(ArrayFun.range(5), 2)
0c3fe8a6 250 .map(i => { return 2*i+1; });
dac39588
BA
251 let bishop2Pos = positions[randIndexes_tmp[0]];
252 let camel2Pos = positions[randIndexes_tmp[1]];
253 for (let idx of randIndexes.concat(randIndexes_tmp)
254 .sort((a,b) => { return b-a; })) //largest indices first
255 {
256 positions.splice(idx, 1);
257 }
a37076f1 258
dac39588
BA
259 let randIndex = randInt(7);
260 let knight1Pos = positions[randIndex];
261 positions.splice(randIndex, 1);
262 randIndex = randInt(6);
263 let knight2Pos = positions[randIndex];
264 positions.splice(randIndex, 1);
a37076f1 265
dac39588
BA
266 randIndex = randInt(5);
267 let queenPos = positions[randIndex];
268 positions.splice(randIndex, 1);
8a196305 269
dac39588
BA
270 // Random square for wildebeest
271 randIndex = randInt(4);
272 let wildebeestPos = positions[randIndex];
273 positions.splice(randIndex, 1);
8a196305 274
dac39588
BA
275 let rook1Pos = positions[0];
276 let kingPos = positions[1];
277 let rook2Pos = positions[2];
a37076f1 278
dac39588
BA
279 pieces[c][rook1Pos] = 'r';
280 pieces[c][knight1Pos] = 'n';
281 pieces[c][bishop1Pos] = 'b';
282 pieces[c][queenPos] = 'q';
283 pieces[c][camel1Pos] = 'c';
284 pieces[c][camel2Pos] = 'c';
285 pieces[c][wildebeestPos] = 'w';
286 pieces[c][kingPos] = 'k';
287 pieces[c][bishop2Pos] = 'b';
288 pieces[c][knight2Pos] = 'n';
289 pieces[c][rook2Pos] = 'r';
290 }
291 return pieces["b"].join("") +
292 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
293 pieces["w"].join("").toUpperCase() +
294 " w 0 1111 -";
295 }
a37076f1 296}