| 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 | 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 | |
| 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 | |
| 56 | // En-passant after 2-sq or 3-sq jumps |
| 57 | getEpSquare(moveOrSquare) |
| 58 | { |
| 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; |
| 74 | const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x]; |
| 75 | if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2) |
| 76 | { |
| 77 | const step = (ex-sx) / Math.abs(ex-sx); |
| 78 | let res = [{ |
| 79 | x: sx + step, |
| 80 | y: sy |
| 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; |
| 90 | } |
| 91 | return undefined; //default |
| 92 | } |
| 93 | |
| 94 | getPotentialMovesFrom([x,y]) |
| 95 | { |
| 96 | switch (this.getPiece(x,y)) |
| 97 | { |
| 98 | case V.CAMEL: |
| 99 | return this.getPotentialCamelMoves([x,y]); |
| 100 | case V.WILDEBEEST: |
| 101 | return this.getPotentialWildebeestMoves([x,y]); |
| 102 | default: |
| 103 | return super.getPotentialMovesFrom([x,y]) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Pawns jump 2 or 3 squares, and promote to queen or wildebeest |
| 108 | getPotentialPawnMoves([x,y]) |
| 109 | { |
| 110 | const color = this.turn; |
| 111 | let moves = []; |
| 112 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
| 113 | const shiftX = (color == "w" ? -1 : 1); |
| 114 | const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]); |
| 115 | const lastRank = (color == "w" ? 0 : sizeX-1); |
| 116 | const finalPieces = x + shiftX == lastRank |
| 117 | ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] |
| 118 | : [V.PAWN]; |
| 119 | |
| 120 | if (this.board[x+shiftX][y] == V.EMPTY) |
| 121 | { |
| 122 | // One square forward |
| 123 | for (let piece of finalPieces) |
| 124 | moves.push(this.getBasicMove([x,y], [x+shiftX,y], {c:color,p:piece})); |
| 125 | if (startRanks.includes(x)) |
| 126 | { |
| 127 | if (this.board[x+2*shiftX][y] == V.EMPTY) |
| 128 | { |
| 129 | // Two squares jump |
| 130 | moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); |
| 131 | if (x==startRanks[0] && this.board[x+3*shiftX][y] == V.EMPTY) |
| 132 | { |
| 133 | // Three squares jump |
| 134 | moves.push(this.getBasicMove([x,y], [x+3*shiftX,y])); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | // Captures |
| 140 | for (let shiftY of [-1,1]) |
| 141 | { |
| 142 | if (y + shiftY >= 0 && y + shiftY < sizeY |
| 143 | && this.board[x+shiftX][y+shiftY] != V.EMPTY |
| 144 | && this.canTake([x,y], [x+shiftX,y+shiftY])) |
| 145 | { |
| 146 | for (let piece of finalPieces) |
| 147 | { |
| 148 | moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], |
| 149 | {c:color,p:piece})); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // En passant |
| 155 | const Lep = this.epSquares.length; |
| 156 | const epSquare = this.epSquares[Lep-1]; |
| 157 | if (!!epSquare) |
| 158 | { |
| 159 | for (let epsq of epSquare) |
| 160 | { |
| 161 | // TODO: some redundant checks |
| 162 | if (epsq.x == x+shiftX && Math.abs(epsq.y - y) == 1) |
| 163 | { |
| 164 | var enpassantMove = this.getBasicMove([x,y], [epsq.x,epsq.y]); |
| 165 | // WARNING: the captured pawn may be diagonally behind us, |
| 166 | // if it's a 3-squares jump and we take on 1st passing square |
| 167 | const px = (this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX); |
| 168 | enpassantMove.vanish.push({ |
| 169 | x: px, |
| 170 | y: epsq.y, |
| 171 | p: 'p', |
| 172 | c: this.getColor(px,epsq.y) |
| 173 | }); |
| 174 | moves.push(enpassantMove); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return moves; |
| 180 | } |
| 181 | |
| 182 | // TODO: wildebeest castle |
| 183 | |
| 184 | getPotentialCamelMoves(sq) |
| 185 | { |
| 186 | return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep"); |
| 187 | } |
| 188 | |
| 189 | getPotentialWildebeestMoves(sq) |
| 190 | { |
| 191 | return this.getSlideNJumpMoves( |
| 192 | sq, V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep"); |
| 193 | } |
| 194 | |
| 195 | isAttacked(sq, colors) |
| 196 | { |
| 197 | return super.isAttacked(sq, colors) |
| 198 | || this.isAttackedByCamel(sq, colors) |
| 199 | || this.isAttackedByWildebeest(sq, colors); |
| 200 | } |
| 201 | |
| 202 | isAttackedByCamel(sq, colors) |
| 203 | { |
| 204 | return this.isAttackedBySlideNJump(sq, colors, |
| 205 | V.CAMEL, V.steps[V.CAMEL], "oneStep"); |
| 206 | } |
| 207 | |
| 208 | isAttackedByWildebeest(sq, colors) |
| 209 | { |
| 210 | return this.isAttackedBySlideNJump(sq, colors, V.WILDEBEEST, |
| 211 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), "oneStep"); |
| 212 | } |
| 213 | |
| 214 | checkGameEnd() |
| 215 | { |
| 216 | // No valid move: game is lost (stalemate is a win) |
| 217 | return this.turn == "w" ? "0-1" : "1-0"; |
| 218 | } |
| 219 | |
| 220 | static get VALUES() { |
| 221 | return Object.assign( |
| 222 | ChessRules.VALUES, |
| 223 | {'c': 3, 'w': 7} //experimental |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | static get SEARCH_DEPTH() { return 2; } |
| 228 | |
| 229 | static GenRandInitFen() |
| 230 | { |
| 231 | let pieces = { "w": new Array(10), "b": new Array(10) }; |
| 232 | for (let c of ["w","b"]) |
| 233 | { |
| 234 | let positions = _.range(11); |
| 235 | |
| 236 | // Get random squares for bishops + camels (different colors) |
| 237 | let randIndexes = _.sample(_.range(6), 2).map(i => { return 2*i; }); |
| 238 | let bishop1Pos = positions[randIndexes[0]]; |
| 239 | let camel1Pos = positions[randIndexes[1]]; |
| 240 | // The second bishop (camel) must be on a square of different color |
| 241 | let randIndexes_tmp = _.sample(_.range(5), 2).map(i => { return 2*i+1; }); |
| 242 | let bishop2Pos = positions[randIndexes_tmp[0]]; |
| 243 | let camel2Pos = positions[randIndexes_tmp[1]]; |
| 244 | for (let idx of randIndexes.concat(randIndexes_tmp) |
| 245 | .sort((a,b) => { return b-a; })) //largest indices first |
| 246 | { |
| 247 | positions.splice(idx, 1); |
| 248 | } |
| 249 | |
| 250 | let randIndex = _.random(6); |
| 251 | let knight1Pos = positions[randIndex]; |
| 252 | positions.splice(randIndex, 1); |
| 253 | randIndex = _.random(5); |
| 254 | let knight2Pos = positions[randIndex]; |
| 255 | positions.splice(randIndex, 1); |
| 256 | |
| 257 | randIndex = _.random(4); |
| 258 | let queenPos = positions[randIndex]; |
| 259 | positions.splice(randIndex, 1); |
| 260 | |
| 261 | // Random square for wildebeest |
| 262 | randIndex = _.random(3); |
| 263 | let wildebeestPos = positions[randIndex]; |
| 264 | positions.splice(randIndex, 1); |
| 265 | |
| 266 | let rook1Pos = positions[0]; |
| 267 | let kingPos = positions[1]; |
| 268 | let rook2Pos = positions[2]; |
| 269 | |
| 270 | pieces[c][rook1Pos] = 'r'; |
| 271 | pieces[c][knight1Pos] = 'n'; |
| 272 | pieces[c][bishop1Pos] = 'b'; |
| 273 | pieces[c][queenPos] = 'q'; |
| 274 | pieces[c][camel1Pos] = 'c'; |
| 275 | pieces[c][camel2Pos] = 'c'; |
| 276 | pieces[c][wildebeestPos] = 'w'; |
| 277 | pieces[c][kingPos] = 'k'; |
| 278 | pieces[c][bishop2Pos] = 'b'; |
| 279 | pieces[c][knight2Pos] = 'n'; |
| 280 | pieces[c][rook2Pos] = 'r'; |
| 281 | } |
| 282 | return pieces["b"].join("") + |
| 283 | "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" + |
| 284 | pieces["w"].join("").toUpperCase() + |
| 285 | " w 1111 -"; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | const VariantRules = WildebeestRules; |