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 { |
7e8a7ea1 | 6 | |
a97bdbda BA |
7 | // Analyse in Hiddenqueen mode makes no sense |
8 | static get CanAnalyze() { | |
9 | return false; | |
10 | } | |
11 | ||
12 | static get HIDDEN_QUEEN() { | |
13 | return 't'; | |
14 | } | |
15 | ||
00eef1ca BA |
16 | static get SomeHiddenMoves() { |
17 | return true; | |
18 | } | |
19 | ||
a97bdbda | 20 | static get PIECES() { |
6f2f9437 | 21 | return ChessRules.PIECES.concat([V.HIDDEN_QUEEN]); |
a97bdbda BA |
22 | } |
23 | ||
24 | getPiece(i, j) { | |
25 | const piece = this.board[i][j].charAt(1); | |
26 | if ( | |
27 | piece != V.HIDDEN_QUEEN || | |
28 | // 'side' is used to determine what I see: a pawn or a (hidden)queen? | |
29 | this.getColor(i, j) == this.side | |
30 | ) { | |
31 | return piece; | |
32 | } | |
33 | return V.PAWN; | |
34 | } | |
35 | ||
36 | getPpath(b, color, score) { | |
37 | if (b[1] == V.HIDDEN_QUEEN) { | |
38 | // Supposed to be hidden. | |
39 | if (score == "*" && (!color || color != b[0])) | |
40 | return b[0] + "p"; | |
41 | return "Hiddenqueen/" + b[0] + "t"; | |
42 | } | |
43 | return b; | |
44 | } | |
45 | ||
63d6223e BA |
46 | getEpSquare(moveOrSquare) { |
47 | if (!moveOrSquare) return undefined; | |
48 | if (typeof moveOrSquare === "string") { | |
49 | const square = moveOrSquare; | |
50 | if (square == "-") return undefined; | |
51 | return V.SquareToCoords(square); | |
52 | } | |
53 | const move = moveOrSquare; | |
54 | const s = move.start, | |
55 | e = move.end; | |
4eb0915a | 56 | const color = move.vanish[0].c; |
63d6223e BA |
57 | if ( |
58 | s.y == e.y && | |
59 | Math.abs(s.x - e.x) == 2 && | |
4eb0915a | 60 | ((color == 'w' && s.x == 6) || (color == 'b' && s.x == 1)) && |
63d6223e BA |
61 | [V.PAWN, V.HIDDEN_QUEEN].includes(move.vanish[0].p) |
62 | ) { | |
63 | return { | |
64 | x: (s.x + e.x) / 2, | |
65 | y: s.y | |
66 | }; | |
67 | } | |
68 | return undefined; //default | |
69 | } | |
70 | ||
a97bdbda BA |
71 | isValidPawnMove(move) { |
72 | const color = move.vanish[0].c; | |
73 | const pawnShift = color == "w" ? -1 : 1; | |
74 | const startRank = color == "w" ? V.size.x - 2 : 1; | |
a97bdbda | 75 | return ( |
a97bdbda | 76 | ( |
3a2a7b5f | 77 | move.end.x - move.start.x == pawnShift && |
a97bdbda | 78 | ( |
a97bdbda | 79 | ( |
3a2a7b5f BA |
80 | // Normal move |
81 | move.end.y == move.start.y && | |
82 | this.board[move.end.x][move.end.y] == V.EMPTY | |
83 | ) | |
84 | || | |
85 | ( | |
86 | // Capture | |
87 | Math.abs(move.end.y - move.start.y) == 1 && | |
88 | this.board[move.end.x][move.end.y] != V.EMPTY | |
a97bdbda BA |
89 | ) |
90 | ) | |
3a2a7b5f BA |
91 | ) |
92 | || | |
93 | ( | |
94 | // Two-spaces initial jump | |
95 | move.start.x == startRank && | |
96 | move.end.y == move.start.y && | |
97 | move.end.x - move.start.x == 2 * pawnShift && | |
98 | this.board[move.end.x][move.end.y] == V.EMPTY | |
a97bdbda BA |
99 | ) |
100 | ); | |
101 | } | |
102 | ||
103 | getPotentialMovesFrom([x, y]) { | |
104 | if (this.getPiece(x, y) == V.HIDDEN_QUEEN) { | |
b627d118 | 105 | const pawnMoves = this.getPotentialPawnMoves([x, y]); |
a97bdbda BA |
106 | let queenMoves = super.getPotentialQueenMoves([x, y]); |
107 | // Remove from queen moves those corresponding to a pawn move: | |
108 | queenMoves = queenMoves | |
109 | .filter(m => !this.isValidPawnMove(m)) | |
110 | // Hidden queen is revealed if moving like a queen: | |
111 | .map(m => { | |
112 | m.appear[0].p = V.QUEEN; | |
113 | return m; | |
114 | }); | |
115 | return pawnMoves.concat(queenMoves); | |
116 | } | |
117 | return super.getPotentialMovesFrom([x, y]); | |
118 | } | |
119 | ||
63d6223e BA |
120 | getEnpassantCaptures([x, y], shiftX) { |
121 | const Lep = this.epSquares.length; | |
122 | const epSquare = this.epSquares[Lep - 1]; | |
123 | let enpassantMove = null; | |
124 | if ( | |
125 | !!epSquare && | |
126 | epSquare.x == x + shiftX && | |
127 | Math.abs(epSquare.y - y) == 1 | |
128 | ) { | |
129 | enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); | |
130 | enpassantMove.vanish.push({ | |
131 | x: x, | |
132 | y: epSquare.y, | |
133 | // Captured piece may be a hidden queen | |
134 | p: this.board[x][epSquare.y][1], | |
135 | c: this.getColor(x, epSquare.y) | |
136 | }); | |
137 | } | |
138 | return !!enpassantMove ? [enpassantMove] : []; | |
139 | } | |
140 | ||
b627d118 | 141 | getPotentialPawnMoves([x, y]) { |
b627d118 | 142 | const piece = this.getPiece(x, y); |
32f6285e BA |
143 | const promotions = |
144 | piece == V.PAWN | |
145 | ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] | |
146 | : [V.QUEEN]; //hidden queen revealed | |
147 | return super.getPotentialPawnMoves([x, y], promotions); | |
b627d118 BA |
148 | } |
149 | ||
a97bdbda BA |
150 | getPossibleMovesFrom(sq) { |
151 | this.side = this.turn; | |
152 | return this.filterValid(this.getPotentialMovesFrom(sq)); | |
153 | } | |
154 | ||
7ba4a5bc BA |
155 | static GenRandInitFen(randomness) { |
156 | let fen = ChessRules.GenRandInitFen(randomness); | |
157 | // Place hidden queens at random (always): | |
a97bdbda BA |
158 | let hiddenQueenPos = randInt(8); |
159 | let pawnRank = "PPPPPPPP".split(""); | |
160 | pawnRank[hiddenQueenPos] = "T"; | |
161 | fen = fen.replace("PPPPPPPP", pawnRank.join("")); | |
162 | hiddenQueenPos = randInt(8); | |
163 | pawnRank = "pppppppp".split(""); | |
164 | pawnRank[hiddenQueenPos] = "t"; | |
165 | fen = fen.replace("pppppppp", pawnRank.join("")); | |
166 | return fen; | |
167 | } | |
168 | ||
3a2a7b5f BA |
169 | postPlay(move) { |
170 | super.postPlay(move); | |
a97bdbda BA |
171 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) |
172 | // We took opponent king | |
173 | this.kingPos[this.turn] = [-1, -1]; | |
174 | } | |
175 | ||
3a2a7b5f BA |
176 | preUndo(move) { |
177 | super.preUndo(move); | |
178 | const oppCol = this.turn; | |
a97bdbda | 179 | if (this.kingPos[oppCol][0] < 0) |
3a2a7b5f | 180 | // Move takes opponent's king: |
a97bdbda BA |
181 | this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y]; |
182 | } | |
183 | ||
165530a5 BA |
184 | underCheck(color) { |
185 | if (this.kingPos[color][0] < 0) return false; | |
186 | return super.underCheck(color); | |
187 | } | |
188 | ||
a97bdbda BA |
189 | getCurrentScore() { |
190 | const color = this.turn; | |
191 | if (this.kingPos[color][0] < 0) | |
192 | // King disappeared | |
193 | return color == "w" ? "0-1" : "1-0"; | |
194 | return super.getCurrentScore(); | |
195 | } | |
196 | ||
197 | // Search is biased, so not really needed to explore deeply | |
198 | static get SEARCH_DEPTH() { | |
199 | return 2; | |
200 | } | |
201 | ||
202 | static get VALUES() { | |
203 | return Object.assign( | |
204 | { t: 9 }, | |
205 | ChessRules.VALUES | |
206 | ); | |
207 | } | |
208 | ||
209 | getComputerMove() { | |
210 | this.side = this.turn; | |
211 | return super.getComputerMove(); | |
212 | } | |
b627d118 BA |
213 | |
214 | getNotation(move) { | |
63d6223e BA |
215 | // Not using getPiece() method because it would transform HQ into pawn: |
216 | if (this.board[move.start.x][move.start.y][1] != V.HIDDEN_QUEEN) | |
90df90bc BA |
217 | return super.getNotation(move); |
218 | const finalSquare = V.CoordsToSquare(move.end); | |
219 | if (move.appear[0].p == V.QUEEN) { | |
220 | return ( | |
221 | "Q" + | |
222 | (move.vanish.length > move.appear.length ? "x" : "") + | |
223 | finalSquare | |
224 | ); | |
225 | } | |
226 | // Do not reveal hidden queens playing as pawns | |
227 | let notation = ""; | |
228 | if (move.vanish.length == 2) | |
229 | // Capture | |
230 | notation = V.CoordToColumn(move.start.y) + "x" + finalSquare; | |
231 | else notation = finalSquare; | |
b627d118 BA |
232 | return notation; |
233 | } | |
7e8a7ea1 | 234 | |
a97bdbda | 235 | }; |