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