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