Prepare some more variants (unfinished)
[vchess.git] / public / javascripts / variants / Wildebeest.js
1 class WildebeestRules extends ChessRules
2 {
3 static getPpath(b)
4 {
5 return ([V.CAMEL,V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b;
6 }
7
8 static get size() { return {x:10,y:11}; }
9
10 static get CAMEL() { return 'c'; }
11 static get WILDEBEEST() { return 'w'; }
12
13 static get PIECES()
14 {
15 return ChessRules.PIECES.concat([V.CAMEL,V.WILDEBEEST]);
16 }
17
18 static get steps()
19 {
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
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
39 // En-passant after 2-sq or 3-sq jumps
40 getEpSquare(moveOrSquare)
41 {
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;
57 const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
58 if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2)
59 {
60 const step = (ex-sx) / Math.abs(ex-sx);
61 let res = [{
62 x: sx + step,
63 y: sy
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;
73 }
74 return undefined; //default
75 }
76
77 getPotentialMovesFrom([x,y])
78 {
79 switch (this.getPiece(x,y))
80 {
81 case V.CAMEL:
82 return this.getPotentialCamelMoves([x,y]);
83 case V.WILDEBEEST:
84 return this.getPotentialWildebeestMoves([x,y]);
85 default:
86 return super.getPotentialMovesFrom([x,y])
87 }
88 }
89
90 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
91 getPotentialPawnMoves([x,y])
92 {
93 const color = this.turn;
94 let moves = [];
95 const [sizeX,sizeY] = [V.size.x,V.size.y];
96 const shift = (color == "w" ? -1 : 1);
97 const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]);
98 const lastRank = (color == "w" ? 0 : sizeX-1);
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]));
106 if (startRanks.includes(x) && this.board[x+2*shift][y] == V.EMPTY)
107 {
108 // Two squares jump
109 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
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 }
115 }
116 }
117 // Captures
118 if (y>0 && this.canTake([x,y], [x+shift,y-1])
119 && this.board[x+shift][y-1] != V.EMPTY)
120 {
121 moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
122 }
123 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
124 && this.board[x+shift][y+1] != V.EMPTY)
125 {
126 moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
127 }
128 }
129
130 if (x+shift == lastRank)
131 {
132 // Promotion
133 let promotionPieces = [V.QUEEN,V.WILDEBEEST];
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
139 if (y>0 && this.canTake([x,y], [x+shift,y-1])
140 && this.board[x+shift][y-1] != V.EMPTY)
141 {
142 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
143 }
144 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
145 && this.board[x+shift][y+1] != V.EMPTY)
146 {
147 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
148 }
149 });
150 }
151
152 // En passant
153 const Lep = this.epSquares.length;
154 const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined;
155 if (!!epSquare)
156 {
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 var enpassantMove = this.getBasicMove([x,y], [x+shift,epsq.y]);
163 enpassantMove.vanish.push({
164 x: x,
165 y: epsq.y,
166 p: 'p',
167 c: this.getColor(x,epsq.y)
168 });
169 moves.push(enpassantMove);
170 }
171 }
172 }
173
174 return moves;
175 }
176
177 // TODO: wildebeest castle
178
179 getPotentialCamelMoves(sq)
180 {
181 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
182 }
183
184 getPotentialWildebeestMoves(sq)
185 {
186 return this.getSlideNJumpMoves(
187 sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
188 }
189
190 isAttacked(sq, colors)
191 {
192 return super.isAttacked(sq, colors)
193 || this.isAttackedByCamel(sq, colors)
194 || this.isAttackedByWildebeest(sq, colors);
195 }
196
197 isAttackedByCamel(sq, colors)
198 {
199 return this.isAttackedBySlideNJump(sq, colors,
200 V.CAMEL, V.steps[V.CAMEL], "oneStep");
201 }
202
203 isAttackedByWildebeest(sq, colors)
204 {
205 return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST,
206 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
207 }
208
209 checkGameEnd()
210 {
211 // No valid move: game is lost (stalemate is a win)
212 return this.turn == "w" ? "0-1" : "1-0";
213 }
214
215 static get VALUES() {
216 return Object.assign(
217 ChessRules.VALUES,
218 {'c': 3, 'w': 7} //experimental
219 );
220 }
221
222 static get SEARCH_DEPTH() { return 2; }
223
224 static GenRandInitFen()
225 {
226 let pieces = { "w": new Array(10), "b": new Array(10) };
227 for (let c of ["w","b"])
228 {
229 let positions = _.range(11);
230
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]];
239 for (let idx of randIndexes.concat(randIndexes_tmp)
240 .sort((a,b) => { return b-a; })) //largest indices first
241 {
242 positions.splice(idx, 1);
243 }
244
245 let randIndex = _.random(6);
246 let knight1Pos = positions[randIndex];
247 positions.splice(randIndex, 1);
248 randIndex = _.random(5);
249 let knight2Pos = positions[randIndex];
250 positions.splice(randIndex, 1);
251
252 randIndex = _.random(4);
253 let queenPos = positions[randIndex];
254 positions.splice(randIndex, 1);
255
256 // Random square for wildebeest
257 randIndex = _.random(3);
258 let wildebeestPos = positions[randIndex];
259 positions.splice(randIndex, 1);
260
261 let rook1Pos = positions[0];
262 let kingPos = positions[1];
263 let rook2Pos = positions[2];
264
265 pieces[c][rook1Pos] = 'r';
266 pieces[c][knight1Pos] = 'n';
267 pieces[c][bishop1Pos] = 'b';
268 pieces[c][queenPos] = 'q';
269 pieces[c][camel1Pos] = 'c';
270 pieces[c][camel2Pos] = 'c';
271 pieces[c][wildebeestPos] = 'w';
272 pieces[c][kingPos] = 'k';
273 pieces[c][bishop2Pos] = 'b';
274 pieces[c][knight2Pos] = 'n';
275 pieces[c][rook2Pos] = 'r';
276 }
277 return pieces["b"].join("") +
278 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
279 pieces["w"].join("").toUpperCase() +
280 " w 1111 -";
281 }
282 }
283
284 const VariantRules = WildebeestRules;