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