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