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