783eb440dfdd17d44e812035dbeeb3f84280bc9c
[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 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 }
173 }
174
175 return moves;
176 }
177
178 // TODO: wildebeest castle
179
180 getPotentialCamelMoves(sq)
181 {
182 return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep");
183 }
184
185 getPotentialWildebeestMoves(sq)
186 {
187 return this.getSlideNJumpMoves(
188 sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
189 }
190
191 isAttacked(sq, colors)
192 {
193 return super.isAttacked(sq, colors)
194 || this.isAttackedByCamel(sq, colors)
195 || this.isAttackedByWildebeest(sq, colors);
196 }
197
198 isAttackedByCamel(sq, colors)
199 {
200 return this.isAttackedBySlideNJump(sq, colors,
201 V.CAMEL, V.steps[V.CAMEL], "oneStep");
202 }
203
204 isAttackedByWildebeest(sq, colors)
205 {
206 return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST,
207 V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep");
208 }
209
210 checkGameEnd()
211 {
212 // No valid move: game is lost (stalemate is a win)
213 return this.turn == "w" ? "0-1" : "1-0";
214 }
215
216 static get VALUES() {
217 return Object.assign(
218 ChessRules.VALUES,
219 {'c': 3, 'w': 7} //experimental
220 );
221 }
222
223 static get SEARCH_DEPTH() { return 2; }
224
225 static GenRandInitFen()
226 {
227 let pieces = { "w": new Array(10), "b": new Array(10) };
228 for (let c of ["w","b"])
229 {
230 let positions = _.range(11);
231
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]];
240 for (let idx of randIndexes.concat(randIndexes_tmp)
241 .sort((a,b) => { return b-a; })) //largest indices first
242 {
243 positions.splice(idx, 1);
244 }
245
246 let randIndex = _.random(6);
247 let knight1Pos = positions[randIndex];
248 positions.splice(randIndex, 1);
249 randIndex = _.random(5);
250 let knight2Pos = positions[randIndex];
251 positions.splice(randIndex, 1);
252
253 randIndex = _.random(4);
254 let queenPos = positions[randIndex];
255 positions.splice(randIndex, 1);
256
257 // Random square for wildebeest
258 randIndex = _.random(3);
259 let wildebeestPos = positions[randIndex];
260 positions.splice(randIndex, 1);
261
262 let rook1Pos = positions[0];
263 let kingPos = positions[1];
264 let rook2Pos = positions[2];
265
266 pieces[c][rook1Pos] = 'r';
267 pieces[c][knight1Pos] = 'n';
268 pieces[c][bishop1Pos] = 'b';
269 pieces[c][queenPos] = 'q';
270 pieces[c][camel1Pos] = 'c';
271 pieces[c][camel2Pos] = 'c';
272 pieces[c][wildebeestPos] = 'w';
273 pieces[c][kingPos] = 'k';
274 pieces[c][bishop2Pos] = 'b';
275 pieces[c][knight2Pos] = 'n';
276 pieces[c][rook2Pos] = 'r';
277 }
278 return pieces["b"].join("") +
279 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
280 pieces["w"].join("").toUpperCase() +
281 " w 1111 -";
282 }
283 }
284
285 const VariantRules = WildebeestRules;