Prepare some more variants (unfinished)
[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 {
375ecdd1 162 var enpassantMove = this.getBasicMove([x,y], [x+shift,epsq.y]);
8a196305
BA
163 enpassantMove.vanish.push({
164 x: x,
375ecdd1 165 y: epsq.y,
8a196305 166 p: 'p',
375ecdd1 167 c: this.getColor(x,epsq.y)
8a196305
BA
168 });
169 moves.push(enpassantMove);
170 }
171 }
a37076f1
BA
172 }
173
174 return moves;
175 }
176
8a196305
BA
177 // TODO: wildebeest castle
178
a37076f1
BA
179 getPotentialCamelMoves(sq)
180 {
0b7d99ec 181 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
a37076f1
BA
182 }
183
184 getPotentialWildebeestMoves(sq)
185 {
92342261
BA
186 return this.getSlideNJumpMoves(
187 sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
a37076f1
BA
188 }
189
a37076f1
BA
190 isAttacked(sq, colors)
191 {
8a196305 192 return super.isAttacked(sq, colors)
a37076f1
BA
193 || this.isAttackedByCamel(sq, colors)
194 || this.isAttackedByWildebeest(sq, colors);
195 }
196
197 isAttackedByCamel(sq, colors)
198 {
199 return this.isAttackedBySlideNJump(sq, colors,
0b7d99ec 200 V.CAMEL, V.steps[V.CAMEL], "oneStep");
a37076f1
BA
201 }
202
203 isAttackedByWildebeest(sq, colors)
204 {
a37076f1 205 return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST,
cf130369 206 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
a37076f1
BA
207 }
208
cf130369
BA
209 checkGameEnd()
210 {
211 // No valid move: game is lost (stalemate is a win)
212 return this.turn == "w" ? "0-1" : "1-0";
213 }
efb20746 214
a37076f1
BA
215 static get VALUES() {
216 return Object.assign(
217 ChessRules.VALUES,
218 {'c': 3, 'w': 7} //experimental
219 );
220 }
221
3c09dc49
BA
222 static get SEARCH_DEPTH() { return 2; }
223
a37076f1
BA
224 static GenRandInitFen()
225 {
92342261
BA
226 let pieces = { "w": new Array(10), "b": new Array(10) };
227 for (let c of ["w","b"])
a37076f1 228 {
8a196305 229 let positions = _.range(11);
a37076f1 230
dca02599
BA
231 // Get random squares for bishops + camels (different colors)
232 let randIndexes = _.sample(_.range(6), 2).map(i => { return 2*i; });
233 let bishop1Pos = positions[randIndexes[0]];
234 let camel1Pos = positions[randIndexes[1]];
235 // The second bishop (camel) must be on a square of different color
236 let randIndexes_tmp = _.sample(_.range(5), 2).map(i => { return 2*i+1; });
237 let bishop2Pos = positions[randIndexes_tmp[0]];
238 let camel2Pos = positions[randIndexes_tmp[1]];
92342261
BA
239 for (let idx of randIndexes.concat(randIndexes_tmp)
240 .sort((a,b) => { return b-a; })) //largest indices first
241 {
dca02599 242 positions.splice(idx, 1);
92342261 243 }
a37076f1 244
dca02599 245 let randIndex = _.random(6);
a37076f1
BA
246 let knight1Pos = positions[randIndex];
247 positions.splice(randIndex, 1);
dca02599 248 randIndex = _.random(5);
a37076f1
BA
249 let knight2Pos = positions[randIndex];
250 positions.splice(randIndex, 1);
251
8a196305 252 randIndex = _.random(4);
dca02599 253 let queenPos = positions[randIndex];
8a196305
BA
254 positions.splice(randIndex, 1);
255
92342261 256 // Random square for wildebeest
8a196305
BA
257 randIndex = _.random(3);
258 let wildebeestPos = positions[randIndex];
259 positions.splice(randIndex, 1);
260
a37076f1
BA
261 let rook1Pos = positions[0];
262 let kingPos = positions[1];
263 let rook2Pos = positions[2];
264
a37076f1
BA
265 pieces[c][rook1Pos] = 'r';
266 pieces[c][knight1Pos] = 'n';
267 pieces[c][bishop1Pos] = 'b';
268 pieces[c][queenPos] = 'q';
8a196305
BA
269 pieces[c][camel1Pos] = 'c';
270 pieces[c][camel2Pos] = 'c';
271 pieces[c][wildebeestPos] = 'w';
a37076f1
BA
272 pieces[c][kingPos] = 'k';
273 pieces[c][bishop2Pos] = 'b';
274 pieces[c][knight2Pos] = 'n';
275 pieces[c][rook2Pos] = 'r';
276 }
c794dbb8 277 return pieces["b"].join("") +
8a196305 278 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
92342261 279 pieces["w"].join("").toUpperCase() +
2d7194bd 280 " w 1111 -";
a37076f1
BA
281 }
282}
643479f8
BA
283
284const VariantRules = WildebeestRules;