| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | import { ArrayFun } from "@/utils/array"; |
| 3 | import { randInt } from "@/utils/alea"; |
| 4 | |
| 5 | export class OrdaRules extends ChessRules { |
| 6 | static get PawnSpecs() { |
| 7 | return Object.assign( |
| 8 | {}, |
| 9 | ChessRules.PawnSpecs, |
| 10 | { promotions: [V.QUEEN, V.KHESHIG] } |
| 11 | ); |
| 12 | } |
| 13 | |
| 14 | static IsGoodFlags(flags) { |
| 15 | // Only white can castle |
| 16 | return !!flags.match(/^[a-z]{2,2}$/); |
| 17 | } |
| 18 | |
| 19 | getPpath(b) { |
| 20 | if (b[0] == 'b' || b[1] == 'h') |
| 21 | // Horde piece or white promoted pawn in kheshig |
| 22 | return "Orda/" + b; |
| 23 | return b; |
| 24 | } |
| 25 | |
| 26 | static GenRandInitFen(randomness) { |
| 27 | if (randomness == 0) |
| 28 | return "lhaykahl/8/pppppppp/8/8/8/PPPPPPPP/RNBQKBNR w 0 ah -"; |
| 29 | |
| 30 | // Mapping kingdom --> horde: |
| 31 | const piecesMap = { |
| 32 | 'r': 'l', |
| 33 | 'n': 'h', |
| 34 | 'b': 'a', |
| 35 | 'q': 'y', |
| 36 | 'k': 'k' |
| 37 | }; |
| 38 | |
| 39 | let pieces = { w: new Array(8), b: new Array(8) }; |
| 40 | let flags = ""; |
| 41 | // Shuffle pieces on first (and last rank if randomness == 2) |
| 42 | for (let c of ["w", "b"]) { |
| 43 | if (c == 'b' && randomness == 1) { |
| 44 | pieces['b'] = pieces['w'].map(p => piecesMap[p]); |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | // TODO: same code as in base_rules. Should extract and factorize? |
| 49 | |
| 50 | let positions = ArrayFun.range(8); |
| 51 | |
| 52 | let randIndex = 2 * randInt(4); |
| 53 | const bishop1Pos = positions[randIndex]; |
| 54 | let randIndex_tmp = 2 * randInt(4) + 1; |
| 55 | const bishop2Pos = positions[randIndex_tmp]; |
| 56 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
| 57 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); |
| 58 | |
| 59 | randIndex = randInt(6); |
| 60 | const knight1Pos = positions[randIndex]; |
| 61 | positions.splice(randIndex, 1); |
| 62 | randIndex = randInt(5); |
| 63 | const knight2Pos = positions[randIndex]; |
| 64 | positions.splice(randIndex, 1); |
| 65 | |
| 66 | randIndex = randInt(4); |
| 67 | const queenPos = positions[randIndex]; |
| 68 | positions.splice(randIndex, 1); |
| 69 | |
| 70 | const rook1Pos = positions[0]; |
| 71 | const kingPos = positions[1]; |
| 72 | const rook2Pos = positions[2]; |
| 73 | |
| 74 | pieces[c][rook1Pos] = "r"; |
| 75 | pieces[c][knight1Pos] = "n"; |
| 76 | pieces[c][bishop1Pos] = "b"; |
| 77 | pieces[c][queenPos] = "q"; |
| 78 | pieces[c][kingPos] = "k"; |
| 79 | pieces[c][bishop2Pos] = "b"; |
| 80 | pieces[c][knight2Pos] = "n"; |
| 81 | pieces[c][rook2Pos] = "r"; |
| 82 | if (c == 'b') pieces[c] = pieces[c].map(p => piecesMap[p]); |
| 83 | else flags = V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
| 84 | } |
| 85 | // Add turn + flags + enpassant |
| 86 | return ( |
| 87 | pieces["b"].join("") + |
| 88 | "/8/pppppppp/8/8/8/PPPPPPPP/" + |
| 89 | pieces["w"].join("").toUpperCase() + |
| 90 | " w 0 " + flags + " -" |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | getFlagsFen() { |
| 95 | return this.castleFlags['w'].map(V.CoordToColumn).join(""); |
| 96 | } |
| 97 | |
| 98 | setFlags(fenflags) { |
| 99 | this.castleFlags = { 'w': [-1, -1] }; |
| 100 | for (let i = 0; i < 2; i++) |
| 101 | this.castleFlags['w'][i] = V.ColumnToCoord(fenflags.charAt(i)); |
| 102 | } |
| 103 | |
| 104 | static get LANCER() { |
| 105 | return 'l'; |
| 106 | } |
| 107 | static get ARCHER() { |
| 108 | return 'a'; |
| 109 | } |
| 110 | static get KHESHIG() { |
| 111 | return 'h'; |
| 112 | } |
| 113 | static get YURT() { |
| 114 | return 'y'; |
| 115 | } |
| 116 | // Khan is technically a King, so let's keep things simple. |
| 117 | |
| 118 | static get PIECES() { |
| 119 | return ChessRules.PIECES.concat([V.LANCER, V.ARCHER, V.KHESHIG, V.YURT]); |
| 120 | } |
| 121 | |
| 122 | getPotentialMovesFrom([x, y]) { |
| 123 | switch (this.getPiece(x, y)) { |
| 124 | case V.LANCER: |
| 125 | return this.getPotentialLancerMoves([x, y]); |
| 126 | case V.ARCHER: |
| 127 | return this.getPotentialArcherMoves([x, y]); |
| 128 | case V.KHESHIG: |
| 129 | return this.getPotentialKheshigMoves([x, y]); |
| 130 | case V.YURT: |
| 131 | return this.getPotentialYurtMoves([x, y]); |
| 132 | default: |
| 133 | return super.getPotentialMovesFrom([x, y]); |
| 134 | } |
| 135 | return []; |
| 136 | } |
| 137 | |
| 138 | getSlideNJumpMoves([x, y], steps, oneStep, options) { |
| 139 | options = options || {}; |
| 140 | let moves = []; |
| 141 | outerLoop: for (let step of steps) { |
| 142 | let i = x + step[0]; |
| 143 | let j = y + step[1]; |
| 144 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 145 | if (!options.onlyTake) moves.push(this.getBasicMove([x, y], [i, j])); |
| 146 | if (oneStep) continue outerLoop; |
| 147 | i += step[0]; |
| 148 | j += step[1]; |
| 149 | } |
| 150 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]) && !options.onlyMove) |
| 151 | moves.push(this.getBasicMove([x, y], [i, j])); |
| 152 | } |
| 153 | return moves; |
| 154 | } |
| 155 | |
| 156 | getPotentialLancerMoves(sq) { |
| 157 | const onlyMoves = this.getSlideNJumpMoves( |
| 158 | sq, |
| 159 | V.steps[V.KNIGHT], |
| 160 | "oneStep", |
| 161 | { onlyMove: true } |
| 162 | ); |
| 163 | const onlyTakes = this.getSlideNJumpMoves( |
| 164 | sq, |
| 165 | V.steps[V.ROOK], |
| 166 | null, |
| 167 | { onlyTake: true } |
| 168 | ); |
| 169 | return onlyMoves.concat(onlyTakes); |
| 170 | } |
| 171 | |
| 172 | getPotentialArcherMoves(sq) { |
| 173 | const onlyMoves = this.getSlideNJumpMoves( |
| 174 | sq, |
| 175 | V.steps[V.KNIGHT], |
| 176 | "oneStep", |
| 177 | { onlyMove: true } |
| 178 | ); |
| 179 | const onlyTakes = this.getSlideNJumpMoves( |
| 180 | sq, |
| 181 | V.steps[V.BISHOP], |
| 182 | null, |
| 183 | { onlyTake: true } |
| 184 | ); |
| 185 | return onlyMoves.concat(onlyTakes); |
| 186 | } |
| 187 | |
| 188 | getPotentialLancerMoves(sq) { |
| 189 | const onlyMoves = this.getSlideNJumpMoves( |
| 190 | sq, |
| 191 | V.steps[V.KNIGHT], |
| 192 | "oneStep", |
| 193 | { onlyMove: true } |
| 194 | ); |
| 195 | const onlyTakes = this.getSlideNJumpMoves( |
| 196 | sq, |
| 197 | V.steps[V.ROOK], |
| 198 | null, |
| 199 | { onlyTake: true } |
| 200 | ); |
| 201 | return onlyMoves.concat(onlyTakes); |
| 202 | } |
| 203 | |
| 204 | getPotentialKheshigMoves(sq) { |
| 205 | return this.getSlideNJumpMoves( |
| 206 | sq, |
| 207 | V.steps[V.KNIGHT].concat(V.steps[V.ROOK]).concat(V.steps[V.BISHOP]), |
| 208 | "oneStep" |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | getPotentialYurtMoves(sq) { |
| 213 | return this.getSlideNJumpMoves( |
| 214 | sq, |
| 215 | V.steps[V.BISHOP].concat([ [1, 0] ]), |
| 216 | "oneStep" |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | getPotentialKingMoves([x, y]) { |
| 221 | if (this.getColor(x, y) == 'w') return super.getPotentialKingMoves([x, y]); |
| 222 | // Horde doesn't castle: |
| 223 | return this.getSlideNJumpMoves( |
| 224 | [x, y], |
| 225 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), |
| 226 | "oneStep" |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | isAttacked(sq, color) { |
| 231 | if (color == 'w') { |
| 232 | return ( |
| 233 | super.isAttacked(sq, color) || |
| 234 | this.isAttackedByKheshig(sq, color) |
| 235 | ); |
| 236 | } |
| 237 | // Horde: only pawn and queen (if promotions) in common: |
| 238 | return ( |
| 239 | super.isAttackedByPawn(sq, color) || |
| 240 | this.isAttackedByLancer(sq, color) || |
| 241 | this.isAttackedByKheshig(sq, color) || |
| 242 | this.isAttackedByArcher(sq, color) || |
| 243 | this.isAttackedByYurt(sq, color) || |
| 244 | super.isAttackedByQueen(sq, color) |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | isAttackedByLancer(sq, color) { |
| 249 | return this.isAttackedBySlideNJump(sq, color, V.LANCER, V.steps[V.ROOK]); |
| 250 | } |
| 251 | |
| 252 | isAttackedByArcher(sq, color) { |
| 253 | return this.isAttackedBySlideNJump(sq, color, V.ARCHER, V.steps[V.BISHOP]); |
| 254 | } |
| 255 | |
| 256 | isAttackedByKheshig(sq, color) { |
| 257 | return super.isAttackedBySlideNJump( |
| 258 | sq, |
| 259 | color, |
| 260 | V.KHESHIG, |
| 261 | V.steps[V.KNIGHT].concat(V.steps[V.ROOK]).concat(V.steps[V.BISHOP]), |
| 262 | "oneStep" |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | isAttackedByYurt(sq, color) { |
| 267 | return super.isAttackedBySlideNJump( |
| 268 | sq, |
| 269 | color, |
| 270 | V.YURT, |
| 271 | V.steps[V.BISHOP].concat([ [1, 0] ]), |
| 272 | "oneStep" |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | updateCastleFlags(move, piece) { |
| 277 | // Only white can castle: |
| 278 | const firstRank = 7; |
| 279 | if (piece == V.KING && move.appear[0].c == 'w') |
| 280 | this.castleFlags['w'] = [8, 8]; |
| 281 | else if ( |
| 282 | move.start.x == firstRank && |
| 283 | this.castleFlags['w'].includes(move.start.y) |
| 284 | ) { |
| 285 | const flagIdx = (move.start.y == this.castleFlags['w'][0] ? 0 : 1); |
| 286 | this.castleFlags['w'][flagIdx] = 8; |
| 287 | } |
| 288 | else if ( |
| 289 | move.end.x == firstRank && |
| 290 | this.castleFlags['w'].includes(move.end.y) |
| 291 | ) { |
| 292 | const flagIdx = (move.end.y == this.castleFlags['w'][0] ? 0 : 1); |
| 293 | this.castleFlags['w'][flagIdx] = 8; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | getCurrentScore() { |
| 298 | // Turn has changed: |
| 299 | const color = V.GetOppCol(this.turn); |
| 300 | const lastRank = (color == 'w' ? 0 : 7); |
| 301 | if (this.kingPos[color][0] == lastRank) |
| 302 | // The opposing edge is reached! |
| 303 | return color == "w" ? "1-0" : "0-1"; |
| 304 | if (this.atLeastOneMove()) return "*"; |
| 305 | // Game over |
| 306 | const oppCol = this.turn; |
| 307 | if (!this.underCheck(oppCol)) return "1/2"; |
| 308 | return (oppCol == "w" ? "0-1" : "1-0"); |
| 309 | } |
| 310 | |
| 311 | static get VALUES() { |
| 312 | return Object.assign( |
| 313 | {}, |
| 314 | ChessRules.VALUES, |
| 315 | { |
| 316 | y: 2, |
| 317 | a: 4, |
| 318 | h: 7, |
| 319 | l: 4 |
| 320 | } |
| 321 | ); |
| 322 | } |
| 323 | }; |