| 1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
| 2 | import { ArrayFun } from "@/utils/array"; |
| 3 | |
| 4 | export class RecycleRules extends ChessRules { |
| 5 | static get PawnSpecs() { |
| 6 | return Object.assign( |
| 7 | {}, |
| 8 | ChessRules.PawnSpecs, |
| 9 | { promotions: [V.PAWN] } //in fact, none |
| 10 | ); |
| 11 | } |
| 12 | |
| 13 | static IsGoodFen(fen) { |
| 14 | if (!ChessRules.IsGoodFen(fen)) return false; |
| 15 | const fenParsed = V.ParseFen(fen); |
| 16 | // 5) Check reserves |
| 17 | if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{10,10}$/)) |
| 18 | return false; |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | static ParseFen(fen) { |
| 23 | const fenParts = fen.split(" "); |
| 24 | return Object.assign( |
| 25 | ChessRules.ParseFen(fen), |
| 26 | { reserve: fenParts[5] } |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | static GenRandInitFen(randomness) { |
| 31 | return ChessRules.GenRandInitFen(randomness) + " 0000000000"; |
| 32 | } |
| 33 | |
| 34 | getFen() { |
| 35 | return super.getFen() + " " + this.getReserveFen(); |
| 36 | } |
| 37 | |
| 38 | getFenForRepeat() { |
| 39 | return super.getFenForRepeat() + "_" + this.getReserveFen(); |
| 40 | } |
| 41 | |
| 42 | getReserveFen() { |
| 43 | let counts = new Array(10); |
| 44 | for ( |
| 45 | let i = 0; |
| 46 | i < V.PIECES.length - 1; |
| 47 | i++ //-1: no king reserve |
| 48 | ) { |
| 49 | counts[i] = this.reserve["w"][V.PIECES[i]]; |
| 50 | counts[5 + i] = this.reserve["b"][V.PIECES[i]]; |
| 51 | } |
| 52 | return counts.join(""); |
| 53 | } |
| 54 | |
| 55 | setOtherVariables(fen) { |
| 56 | super.setOtherVariables(fen); |
| 57 | // Also init reserves (used by the interface to show landable pieces) |
| 58 | const reserve = |
| 59 | V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10)); |
| 60 | this.reserve = { |
| 61 | w: { |
| 62 | [V.PAWN]: reserve[0], |
| 63 | [V.ROOK]: reserve[1], |
| 64 | [V.KNIGHT]: reserve[2], |
| 65 | [V.BISHOP]: reserve[3], |
| 66 | [V.QUEEN]: reserve[4] |
| 67 | }, |
| 68 | b: { |
| 69 | [V.PAWN]: reserve[5], |
| 70 | [V.ROOK]: reserve[6], |
| 71 | [V.KNIGHT]: reserve[7], |
| 72 | [V.BISHOP]: reserve[8], |
| 73 | [V.QUEEN]: reserve[9] |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | getColor(i, j) { |
| 79 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; |
| 80 | return this.board[i][j].charAt(0); |
| 81 | } |
| 82 | |
| 83 | getPiece(i, j) { |
| 84 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; |
| 85 | return this.board[i][j].charAt(1); |
| 86 | } |
| 87 | |
| 88 | // Used by the interface: |
| 89 | getReservePpath(index, color) { |
| 90 | return color + V.RESERVE_PIECES[index]; |
| 91 | } |
| 92 | |
| 93 | // Ordering on reserve pieces |
| 94 | static get RESERVE_PIECES() { |
| 95 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]; |
| 96 | } |
| 97 | |
| 98 | getReserveMoves([x, y]) { |
| 99 | const color = this.turn; |
| 100 | const p = V.RESERVE_PIECES[y]; |
| 101 | if (this.reserve[color][p] == 0) return []; |
| 102 | let moves = []; |
| 103 | const pawnShift = p == V.PAWN ? 1 : 0; |
| 104 | for (let i = pawnShift; i < V.size.x - pawnShift; i++) { |
| 105 | for (let j = 0; j < V.size.y; j++) { |
| 106 | if (this.board[i][j] == V.EMPTY) { |
| 107 | let mv = new Move({ |
| 108 | appear: [ |
| 109 | new PiPo({ |
| 110 | x: i, |
| 111 | y: j, |
| 112 | c: color, |
| 113 | p: p |
| 114 | }) |
| 115 | ], |
| 116 | vanish: [], |
| 117 | start: { x: x, y: y }, //a bit artificial... |
| 118 | end: { x: i, y: j } |
| 119 | }); |
| 120 | moves.push(mv); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return moves; |
| 125 | } |
| 126 | |
| 127 | getPotentialMovesFrom([x, y]) { |
| 128 | if (x >= V.size.x) |
| 129 | // Reserves, outside of board: x == sizeX(+1) |
| 130 | return this.getReserveMoves([x, y]); |
| 131 | // Standard moves |
| 132 | return super.getPotentialMovesFrom([x, y]); |
| 133 | } |
| 134 | |
| 135 | getPotentialPawnMoves([x, y]) { |
| 136 | let moves = super.getPotentialPawnMoves([x, y]); |
| 137 | // Remove pawns on 8th rank ("fallen"): |
| 138 | const color = this.turn; |
| 139 | const lastRank = (color == "w" ? 0 : V.size.x - 1); |
| 140 | moves.forEach(m => { |
| 141 | if (m.appear[0].x == lastRank) m.appear.pop(); |
| 142 | }); |
| 143 | return moves; |
| 144 | } |
| 145 | |
| 146 | getAllValidMoves() { |
| 147 | let moves = super.getAllPotentialMoves(); |
| 148 | const color = this.turn; |
| 149 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
| 150 | moves = moves.concat( |
| 151 | this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i]) |
| 152 | ); |
| 153 | } |
| 154 | return this.filterValid(moves); |
| 155 | } |
| 156 | |
| 157 | atLeastOneMove() { |
| 158 | if (!super.atLeastOneMove()) { |
| 159 | // Search one reserve move |
| 160 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
| 161 | let moves = this.filterValid( |
| 162 | this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) |
| 163 | ); |
| 164 | if (moves.length > 0) return true; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | canTake([x1, y1], [x2, y2]) { |
| 172 | // Self-captures allowed, except for the king: |
| 173 | return this.getPiece(x2, y2) != V.KING; |
| 174 | } |
| 175 | |
| 176 | prePlay(move) { |
| 177 | super.prePlay(move); |
| 178 | // Skip castle: |
| 179 | if (move.vanish.length == 2 && move.appear.length == 2) return; |
| 180 | const color = this.turn; |
| 181 | if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]--; |
| 182 | else if (move.vanish.length == 2 && move.vanish[1].c == color) |
| 183 | // Self-capture |
| 184 | this.reserve[color][move.vanish[1].p]++; |
| 185 | } |
| 186 | |
| 187 | postUndo(move) { |
| 188 | super.postUndo(move); |
| 189 | if (move.vanish.length == 2 && move.appear.length == 2) return; |
| 190 | const color = this.turn; |
| 191 | if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]++; |
| 192 | else if (move.vanish.length == 2 && move.vanish[1].c == color) |
| 193 | this.reserve[color][move.vanish[1].p]--; |
| 194 | } |
| 195 | |
| 196 | static get SEARCH_DEPTH() { |
| 197 | return 2; |
| 198 | } |
| 199 | |
| 200 | evalPosition() { |
| 201 | let evaluation = super.evalPosition(); |
| 202 | // Add reserves: |
| 203 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
| 204 | const p = V.RESERVE_PIECES[i]; |
| 205 | evaluation += this.reserve["w"][p] * V.VALUES[p]; |
| 206 | evaluation -= this.reserve["b"][p] * V.VALUES[p]; |
| 207 | } |
| 208 | return evaluation; |
| 209 | } |
| 210 | |
| 211 | getNotation(move) { |
| 212 | const finalSquare = V.CoordsToSquare(move.end); |
| 213 | if (move.vanish.length > 0) { |
| 214 | if (move.appear.length > 0) { |
| 215 | // Standard move |
| 216 | return super.getNotation(move); |
| 217 | } else { |
| 218 | // Pawn fallen: capturing or not |
| 219 | let res = ""; |
| 220 | if (move.vanish.length == 2) |
| 221 | res += V.CoordToColumn(move.start.y) + "x"; |
| 222 | return res + finalSquare; |
| 223 | } |
| 224 | } |
| 225 | // Rebirth: |
| 226 | const piece = |
| 227 | move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : ""; |
| 228 | return piece + "@" + V.CoordsToSquare(move.end); |
| 229 | } |
| 230 | }; |