| 1 | // NOTE: initial setup differs from the original; see |
| 2 | // https://www.chessvariants.com/large.dir/freeling.html |
| 3 | class GrandRules extends ChessRules |
| 4 | { |
| 5 | static getPpath(b) |
| 6 | { |
| 7 | return ([V.MARSHALL,V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b; |
| 8 | } |
| 9 | |
| 10 | initVariables(fen) |
| 11 | { |
| 12 | super.initVariables(fen); |
| 13 | this.captures = { "w": {}, "b": {} }; //for promotions |
| 14 | } |
| 15 | |
| 16 | static get size() { return {x:10,y:10}; } |
| 17 | |
| 18 | static get MARSHALL() { return 'm'; } //rook+knight |
| 19 | static get CARDINAL() { return 'c'; } //bishop+knight |
| 20 | |
| 21 | static get PIECES() { |
| 22 | return ChessRules.PIECES.concat([V.MARSHALL,V.CARDINAL]); |
| 23 | } |
| 24 | |
| 25 | // En-passant after 2-sq or 3-sq jumps |
| 26 | getEpSquare(move) |
| 27 | { |
| 28 | const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x]; |
| 29 | if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2) |
| 30 | { |
| 31 | const step = (ex-sx) / Math.abs(ex-sx); |
| 32 | let res = [{ |
| 33 | x: sx + step, |
| 34 | y: sy |
| 35 | }]; |
| 36 | if (sx + 2*step != ex) //3-squares move |
| 37 | { |
| 38 | res.push({ |
| 39 | x: sx + 2*step, |
| 40 | y: sy |
| 41 | }); |
| 42 | } |
| 43 | return res; |
| 44 | } |
| 45 | return undefined; //default |
| 46 | } |
| 47 | |
| 48 | getPotentialMovesFrom([x,y]) |
| 49 | { |
| 50 | switch (this.getPiece(x,y)) |
| 51 | { |
| 52 | case V.MARSHALL: |
| 53 | return this.getPotentialMarshallMoves([x,y]); |
| 54 | case V.CARDINAL: |
| 55 | return this.getPotentialCardinalMoves([x,y]); |
| 56 | default: |
| 57 | return super.getPotentialMovesFrom([x,y]) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Special pawn rules: promotions to captured friendly pieces, |
| 62 | // optional on ranks 8-9 and mandatory on rank 10. |
| 63 | getPotentialPawnMoves([x,y]) |
| 64 | { |
| 65 | const color = this.turn; |
| 66 | let moves = []; |
| 67 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
| 68 | const shift = (color == "w" ? -1 : 1); |
| 69 | const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]); |
| 70 | const lastRanks = (color == "w" ? [0,1,2] : [sizeX-1,sizeX-2,sizeX-3]); |
| 71 | |
| 72 | if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRanks[0]) |
| 73 | { |
| 74 | // Normal moves |
| 75 | if (this.board[x+shift][y] == V.EMPTY) |
| 76 | { |
| 77 | moves.push(this.getBasicMove([x,y], [x+shift,y])); |
| 78 | if (startRanks.includes(x) && this.board[x+2*shift][y] == V.EMPTY) |
| 79 | { |
| 80 | // Two squares jump |
| 81 | moves.push(this.getBasicMove([x,y], [x+2*shift,y])); |
| 82 | if (x == startRanks[0] && this.board[x+3*shift][y] == V.EMPTY) |
| 83 | { |
| 84 | // 3-squares jump |
| 85 | moves.push(this.getBasicMove([x,y], [x+3*shift,y])); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | // Captures |
| 90 | if (y>0 && this.canTake([x,y], [x+shift,y-1]) |
| 91 | && this.board[x+shift][y-1] != V.EMPTY) |
| 92 | { |
| 93 | moves.push(this.getBasicMove([x,y], [x+shift,y-1])); |
| 94 | } |
| 95 | if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) |
| 96 | && this.board[x+shift][y+1] != V.EMPTY) |
| 97 | { |
| 98 | moves.push(this.getBasicMove([x,y], [x+shift,y+1])); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (lastRanks.includes(x+shift)) |
| 103 | { |
| 104 | // Promotion |
| 105 | let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN,V.MARSHALL,V.CARDINAL]; |
| 106 | promotionPieces.forEach(p => { |
| 107 | if (!this.captures[color][p] || this.captures[color][p]==0) |
| 108 | return; |
| 109 | // Normal move |
| 110 | if (this.board[x+shift][y] == V.EMPTY) |
| 111 | moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); |
| 112 | // Captures |
| 113 | if (y>0 && this.canTake([x,y], [x+shift,y-1]) |
| 114 | && this.board[x+shift][y-1] != V.EMPTY) |
| 115 | { |
| 116 | moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p})); |
| 117 | } |
| 118 | if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) |
| 119 | && this.board[x+shift][y+1] != V.EMPTY) |
| 120 | { |
| 121 | moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p})); |
| 122 | } |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | // En passant |
| 127 | const Lep = this.epSquares.length; |
| 128 | const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined; |
| 129 | if (!!epSquare) |
| 130 | { |
| 131 | for (let epsq of epSquare) |
| 132 | { |
| 133 | // TODO: some redundant checks |
| 134 | if (epsq.x == x+shift && Math.abs(epsq.y - y) == 1) |
| 135 | { |
| 136 | let epStep = epsq.y - y; |
| 137 | var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]); |
| 138 | enpassantMove.vanish.push({ |
| 139 | x: x, |
| 140 | y: y+epStep, |
| 141 | p: 'p', |
| 142 | c: this.getColor(x,y+epStep) |
| 143 | }); |
| 144 | moves.push(enpassantMove); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return moves; |
| 150 | } |
| 151 | |
| 152 | // TODO: different castle? |
| 153 | |
| 154 | getPotentialMarshallMoves(sq) |
| 155 | { |
| 156 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( |
| 157 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep")); |
| 158 | } |
| 159 | |
| 160 | getPotentialCardinalMoves(sq) |
| 161 | { |
| 162 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( |
| 163 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep")); |
| 164 | } |
| 165 | |
| 166 | isAttacked(sq, colors) |
| 167 | { |
| 168 | return super.isAttacked(sq, colors) |
| 169 | || this.isAttackedByMarshall(sq, colors) |
| 170 | || this.isAttackedByCardinal(sq, colors); |
| 171 | } |
| 172 | |
| 173 | isAttackedByMarshall(sq, colors) |
| 174 | { |
| 175 | return this.isAttackedBySlideNJump(sq, colors, V.MARSHALL, V.steps[V.ROOK]) |
| 176 | || this.isAttackedBySlideNJump( |
| 177 | sq, colors, V.MARSHALL, V.steps[V.KNIGHT], "oneStep"); |
| 178 | } |
| 179 | |
| 180 | isAttackedByCardinal(sq, colors) |
| 181 | { |
| 182 | return this.isAttackedBySlideNJump(sq, colors, V.CARDINAL, V.steps[V.BISHOP]) |
| 183 | || this.isAttackedBySlideNJump( |
| 184 | sq, colors, V.CARDINAL, V.steps[V.KNIGHT], "oneStep"); |
| 185 | } |
| 186 | |
| 187 | updateVariables(move) |
| 188 | { |
| 189 | super.updateVariables(move); |
| 190 | if (move.vanish.length==2 && move.appear.length==1 && move.vanish[1].p != V.PAWN) |
| 191 | { |
| 192 | // Capture: update this.captures |
| 193 | if (!this.captures[move.vanish[1].c][move.vanish[1].p]) |
| 194 | this.captures[move.vanish[1].c][move.vanish[1].p] = 1; |
| 195 | else |
| 196 | this.captures[move.vanish[1].c][move.vanish[1].p]++; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | unupdateVariables(move) |
| 201 | { |
| 202 | super.unupdateVariables(move); |
| 203 | if (move.vanish.length==2 && move.appear.length==1 && move.vanish[1].p != V.PAWN) |
| 204 | { |
| 205 | this.captures[move.vanish[1].c][move.vanish[1].p] = |
| 206 | Math.max(0, this.captures[move.vanish[1].c][move.vanish[1].p]-1); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static get VALUES() { |
| 211 | return Object.assign( |
| 212 | ChessRules.VALUES, |
| 213 | {'c': 5, 'm': 7} //experimental |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | static get SEARCH_DEPTH() { return 2; } |
| 218 | |
| 219 | // TODO: this function could be generalized and shared better |
| 220 | static GenRandInitFen() |
| 221 | { |
| 222 | let pieces = { "w": new Array(10), "b": new Array(10) }; |
| 223 | // Shuffle pieces on first and last rank |
| 224 | for (let c of ["w","b"]) |
| 225 | { |
| 226 | let positions = _.range(10); |
| 227 | |
| 228 | // Get random squares for bishops |
| 229 | let randIndex = 2 * _.random(4); |
| 230 | let bishop1Pos = positions[randIndex]; |
| 231 | // The second bishop must be on a square of different color |
| 232 | let randIndex_tmp = 2 * _.random(4) + 1; |
| 233 | let bishop2Pos = positions[randIndex_tmp]; |
| 234 | // Remove chosen squares |
| 235 | positions.splice(Math.max(randIndex,randIndex_tmp), 1); |
| 236 | positions.splice(Math.min(randIndex,randIndex_tmp), 1); |
| 237 | |
| 238 | // Get random squares for knights |
| 239 | randIndex = _.random(7); |
| 240 | let knight1Pos = positions[randIndex]; |
| 241 | positions.splice(randIndex, 1); |
| 242 | randIndex = _.random(6); |
| 243 | let knight2Pos = positions[randIndex]; |
| 244 | positions.splice(randIndex, 1); |
| 245 | |
| 246 | // Get random square for queen |
| 247 | randIndex = _.random(5); |
| 248 | let queenPos = positions[randIndex]; |
| 249 | positions.splice(randIndex, 1); |
| 250 | |
| 251 | // ...random square for marshall |
| 252 | randIndex = _.random(4); |
| 253 | let marshallPos = positions[randIndex]; |
| 254 | positions.splice(randIndex, 1); |
| 255 | |
| 256 | // ...random square for cardinal |
| 257 | randIndex = _.random(3); |
| 258 | let cardinalPos = positions[randIndex]; |
| 259 | positions.splice(randIndex, 1); |
| 260 | |
| 261 | // Rooks and king positions are now fixed, because of the ordering rook-king-rook |
| 262 | let rook1Pos = positions[0]; |
| 263 | let kingPos = positions[1]; |
| 264 | let rook2Pos = positions[2]; |
| 265 | |
| 266 | // Finally put the shuffled pieces in the board array |
| 267 | pieces[c][rook1Pos] = 'r'; |
| 268 | pieces[c][knight1Pos] = 'n'; |
| 269 | pieces[c][bishop1Pos] = 'b'; |
| 270 | pieces[c][queenPos] = 'q'; |
| 271 | pieces[c][marshallPos] = 'm'; |
| 272 | pieces[c][cardinalPos] = 'c'; |
| 273 | pieces[c][kingPos] = 'k'; |
| 274 | pieces[c][bishop2Pos] = 'b'; |
| 275 | pieces[c][knight2Pos] = 'n'; |
| 276 | pieces[c][rook2Pos] = 'r'; |
| 277 | } |
| 278 | let fen = pieces["b"].join("") + |
| 279 | "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" + |
| 280 | pieces["w"].join("").toUpperCase() + |
| 281 | " 1111"; |
| 282 | return fen; |
| 283 | } |
| 284 | } |