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