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