| 1 | import ChessRules from "/base_rules.js"; |
| 2 | import {FenUtil} from "/utils/setupPieces.js"; |
| 3 | |
| 4 | export default class AmbiguousRules extends ChessRules { |
| 5 | |
| 6 | static get Options() { |
| 7 | return { |
| 8 | select: C.Options.select, |
| 9 | styles: ["cylinder"] |
| 10 | }; |
| 11 | } |
| 12 | |
| 13 | get hasFlags() { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | setOtherVariables(fenParsed) { |
| 18 | super.setOtherVariables(fenParsed); |
| 19 | if (this.movesCount == 0) |
| 20 | this.subTurn = 2; |
| 21 | else |
| 22 | this.subTurn = 1; |
| 23 | } |
| 24 | |
| 25 | genRandInitBaseFen() { |
| 26 | const s = FenUtil.setupPieces( |
| 27 | ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], |
| 28 | { |
| 29 | randomness: this.options["randomness"], |
| 30 | diffCol: ['b'] |
| 31 | } |
| 32 | ); |
| 33 | return { |
| 34 | fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + |
| 35 | s.w.join("").toUpperCase(), |
| 36 | o: {} |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | canStepOver(x, y) { |
| 41 | return this.board[x][y] == "" || this.getPiece(x, y) == V.GOAL; |
| 42 | } |
| 43 | |
| 44 | // Subturn 1: play a move for the opponent on the designated square. |
| 45 | // Subturn 2: play a move for me (which just indicate a square). |
| 46 | getPotentialMovesFrom([x, y]) { |
| 47 | const color = this.turn; |
| 48 | const oppCol = C.GetOppTurn(color); |
| 49 | if (this.subTurn == 2) { |
| 50 | // Just play a normal move (which in fact only indicate a square) |
| 51 | let movesHash = {}; |
| 52 | return ( |
| 53 | super.getPotentialMovesFrom([x, y]) |
| 54 | .filter(m => { |
| 55 | // Filter promotions: keep only one, since no choice for now. |
| 56 | if (m.appear[0].p != m.vanish[0].p) { |
| 57 | const hash = C.CoordsToSquare(m.start) + C.CoordsToSquare(m.end); |
| 58 | if (!movesHash[hash]) { |
| 59 | movesHash[hash] = true; |
| 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | }) |
| 66 | .map(m => { |
| 67 | if (m.vanish.length == 1) |
| 68 | m.appear[0].p = V.GOAL; |
| 69 | else { |
| 70 | m.appear[0].p = V.TARGET_CODE[m.vanish[1].p]; |
| 71 | m.appear[0].c = m.vanish[1].c; |
| 72 | } |
| 73 | m.vanish.shift(); |
| 74 | return m; |
| 75 | }) |
| 76 | ); |
| 77 | } |
| 78 | // At subTurn == 1, play a targeted move for the opponent. |
| 79 | // Search for target: |
| 80 | let target = {x: -1, y: -1}; |
| 81 | outerLoop: for (let i = 0; i < this.size.x; i++) { |
| 82 | for (let j = 0; j < this.size.y; j++) { |
| 83 | if (this.board[i][j] != "") { |
| 84 | const piece = this.getPiece(i, j); |
| 85 | if ( |
| 86 | piece == V.GOAL || |
| 87 | Object.keys(V.TARGET_DECODE).includes(piece) |
| 88 | ) { |
| 89 | target = {x: i, y:j}; |
| 90 | break outerLoop; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | const moves = super.getPotentialMovesFrom([x, y], oppCol); |
| 96 | return moves.filter(m => m.end.x == target.x && m.end.y == target.y); |
| 97 | } |
| 98 | |
| 99 | canIplay(x, y) { |
| 100 | const color = this.getColor(x, y); |
| 101 | return ( |
| 102 | (this.subTurn == 1 && ![this.turn, this.playerColor].includes(color)) || |
| 103 | (this.subTurn == 2 && super.canIplay(x, y)) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | // Code for empty square target |
| 108 | static get GOAL() { |
| 109 | return 'g'; |
| 110 | } |
| 111 | |
| 112 | static get TARGET_DECODE() { |
| 113 | return { |
| 114 | 's': 'p', |
| 115 | 't': 'q', |
| 116 | 'u': 'r', |
| 117 | 'o': 'n', |
| 118 | 'c': 'b', |
| 119 | 'l': 'k' |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | static get TARGET_CODE() { |
| 124 | return { |
| 125 | 'p': 's', |
| 126 | 'q': 't', |
| 127 | 'r': 'u', |
| 128 | 'n': 'o', |
| 129 | 'b': 'c', |
| 130 | 'k': 'l' |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | pieces(color, x, y) { |
| 135 | const targets = { |
| 136 | 's': {"class": "target-pawn"}, |
| 137 | 'u': {"class": "target-rook"}, |
| 138 | 'o': {"class": "target-knight"}, |
| 139 | 'c': {"class": "target-bishop"}, |
| 140 | 't': {"class": "target-queen"}, |
| 141 | 'l': {"class": "target-king"} |
| 142 | }; |
| 143 | return Object.assign({ 'g': {"class": "target"} }, |
| 144 | targets, super.pieces(color, x, y)); |
| 145 | } |
| 146 | |
| 147 | atLeastOneMove() { |
| 148 | // Since there are no checks this seems true (same as for Magnetic...) |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | filterValid(moves) { |
| 153 | return moves; |
| 154 | } |
| 155 | |
| 156 | isKing(x, y, p) { |
| 157 | if (!p) |
| 158 | p = this.getPiece(x, y); |
| 159 | return ['k', 'l'].includes(p); |
| 160 | } |
| 161 | |
| 162 | getCurrentScore() { |
| 163 | // This function is only called at subTurn 1 |
| 164 | const color = C.GetOppTurn(this.turn); |
| 165 | if (this.searchKingPos(color).length == 0) |
| 166 | return (color == 'w' ? "0-1" : "1-0"); |
| 167 | return "*"; |
| 168 | } |
| 169 | |
| 170 | postPlay(move) { |
| 171 | const color = this.turn; |
| 172 | if (this.subTurn == 2 || this.searchKingPos(color).length == 0) { |
| 173 | this.turn = C.GetOppTurn(color); |
| 174 | this.movesCount++; |
| 175 | } |
| 176 | this.subTurn = 3 - this.subTurn; |
| 177 | } |
| 178 | |
| 179 | }; |