| 1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
| 2 | import { ArrayFun } from "@/utils/array"; |
| 3 | import { randInt } from "@/utils/alea"; |
| 4 | |
| 5 | export class HiddenqueenRules extends ChessRules { |
| 6 | // Analyse in Hiddenqueen mode makes no sense |
| 7 | static get CanAnalyze() { |
| 8 | return false; |
| 9 | } |
| 10 | |
| 11 | static get HIDDEN_QUEEN() { |
| 12 | return 't'; |
| 13 | } |
| 14 | |
| 15 | static get PIECES() { |
| 16 | return ChessRules.PIECES.concat(Object.values(V.HIDDEN_CODE)); |
| 17 | } |
| 18 | |
| 19 | getPiece(i, j) { |
| 20 | const piece = this.board[i][j].charAt(1); |
| 21 | if ( |
| 22 | piece != V.HIDDEN_QUEEN || |
| 23 | // 'side' is used to determine what I see: a pawn or a (hidden)queen? |
| 24 | this.getColor(i, j) == this.side |
| 25 | ) { |
| 26 | return piece; |
| 27 | } |
| 28 | return V.PAWN; |
| 29 | } |
| 30 | |
| 31 | getPpath(b, color, score) { |
| 32 | if (b[1] == V.HIDDEN_QUEEN) { |
| 33 | // Supposed to be hidden. |
| 34 | if (score == "*" && (!color || color != b[0])) |
| 35 | return b[0] + "p"; |
| 36 | return "Hiddenqueen/" + b[0] + "t"; |
| 37 | } |
| 38 | return b; |
| 39 | } |
| 40 | |
| 41 | isValidPawnMove(move) { |
| 42 | const color = move.vanish[0].c; |
| 43 | const pawnShift = color == "w" ? -1 : 1; |
| 44 | const startRank = color == "w" ? V.size.x - 2 : 1; |
| 45 | return ( |
| 46 | ( |
| 47 | move.end.x - move.start.x == pawnShift && |
| 48 | ( |
| 49 | ( |
| 50 | // Normal move |
| 51 | move.end.y == move.start.y && |
| 52 | this.board[move.end.x][move.end.y] == V.EMPTY |
| 53 | ) |
| 54 | || |
| 55 | ( |
| 56 | // Capture |
| 57 | Math.abs(move.end.y - move.start.y) == 1 && |
| 58 | this.board[move.end.x][move.end.y] != V.EMPTY |
| 59 | ) |
| 60 | ) |
| 61 | ) |
| 62 | || |
| 63 | ( |
| 64 | // Two-spaces initial jump |
| 65 | move.start.x == startRank && |
| 66 | move.end.y == move.start.y && |
| 67 | move.end.x - move.start.x == 2 * pawnShift && |
| 68 | this.board[move.end.x][move.end.y] == V.EMPTY |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | getPotentialMovesFrom([x, y]) { |
| 74 | if (this.getPiece(x, y) == V.HIDDEN_QUEEN) { |
| 75 | const pawnMoves = this.getPotentialPawnMoves([x, y]); |
| 76 | let queenMoves = super.getPotentialQueenMoves([x, y]); |
| 77 | // Remove from queen moves those corresponding to a pawn move: |
| 78 | queenMoves = queenMoves |
| 79 | .filter(m => !this.isValidPawnMove(m)) |
| 80 | // Hidden queen is revealed if moving like a queen: |
| 81 | .map(m => { |
| 82 | m.appear[0].p = V.QUEEN; |
| 83 | return m; |
| 84 | }); |
| 85 | return pawnMoves.concat(queenMoves); |
| 86 | } |
| 87 | return super.getPotentialMovesFrom([x, y]); |
| 88 | } |
| 89 | |
| 90 | getPotentialPawnMoves([x, y]) { |
| 91 | const piece = this.getPiece(x, y); |
| 92 | const promotions = |
| 93 | piece == V.PAWN |
| 94 | ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] |
| 95 | : [V.QUEEN]; //hidden queen revealed |
| 96 | return super.getPotentialPawnMoves([x, y], promotions); |
| 97 | } |
| 98 | |
| 99 | getPossibleMovesFrom(sq) { |
| 100 | this.side = this.turn; |
| 101 | return this.filterValid(this.getPotentialMovesFrom(sq)); |
| 102 | } |
| 103 | |
| 104 | static GenRandInitFen(randomness) { |
| 105 | let fen = ChessRules.GenRandInitFen(randomness); |
| 106 | // Place hidden queens at random (always): |
| 107 | let hiddenQueenPos = randInt(8); |
| 108 | let pawnRank = "PPPPPPPP".split(""); |
| 109 | pawnRank[hiddenQueenPos] = "T"; |
| 110 | fen = fen.replace("PPPPPPPP", pawnRank.join("")); |
| 111 | hiddenQueenPos = randInt(8); |
| 112 | pawnRank = "pppppppp".split(""); |
| 113 | pawnRank[hiddenQueenPos] = "t"; |
| 114 | fen = fen.replace("pppppppp", pawnRank.join("")); |
| 115 | return fen; |
| 116 | } |
| 117 | |
| 118 | postPlay(move) { |
| 119 | super.postPlay(move); |
| 120 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) |
| 121 | // We took opponent king |
| 122 | this.kingPos[this.turn] = [-1, -1]; |
| 123 | } |
| 124 | |
| 125 | preUndo(move) { |
| 126 | super.preUndo(move); |
| 127 | const oppCol = this.turn; |
| 128 | if (this.kingPos[oppCol][0] < 0) |
| 129 | // Move takes opponent's king: |
| 130 | this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y]; |
| 131 | } |
| 132 | |
| 133 | getCurrentScore() { |
| 134 | const color = this.turn; |
| 135 | if (this.kingPos[color][0] < 0) |
| 136 | // King disappeared |
| 137 | return color == "w" ? "0-1" : "1-0"; |
| 138 | return super.getCurrentScore(); |
| 139 | } |
| 140 | |
| 141 | // Search is biased, so not really needed to explore deeply |
| 142 | static get SEARCH_DEPTH() { |
| 143 | return 2; |
| 144 | } |
| 145 | |
| 146 | static get VALUES() { |
| 147 | return Object.assign( |
| 148 | { t: 9 }, |
| 149 | ChessRules.VALUES |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | getComputerMove() { |
| 154 | this.side = this.turn; |
| 155 | return super.getComputerMove(); |
| 156 | } |
| 157 | |
| 158 | getNotation(move) { |
| 159 | const notation = super.getNotation(move); |
| 160 | if (notation.charAt(0) == 'T') |
| 161 | // Do not reveal hidden queens |
| 162 | return notation.substr(1); |
| 163 | return notation; |
| 164 | } |
| 165 | }; |