Update TODO, improve style for Crazyhouse variant, fix an introduced bug in example...
[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
5export const VariantRules = 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 const lastRank = color == "w" ? 0 : V.size.x - 1;
46 return (
47 // The queen is discovered if she reaches the 8th rank,
48 // even if this would be a technically valid pawn move.
49 move.end.x != lastRank &&
50 (
51 (
52 move.end.x - move.start.x == pawnShift &&
53 (
54 (
55 // Normal move
56 move.end.y == move.start.y &&
57 this.board[move.end.x][move.end.y] == V.EMPTY
58 )
59 ||
60 (
61 // Capture
62 Math.abs(move.end.y - move.start.y) == 1 &&
63 this.board[move.end.x][move.end.y] != V.EMPTY
64 )
65 )
66 )
67 ||
68 (
69 // Two-spaces initial jump
70 move.start.x == startRank &&
71 move.end.y == move.start.y &&
72 move.end.x - move.start.x == 2 * pawnShift &&
73 this.board[move.end.x][move.end.y] == V.EMPTY
74 )
75 )
76 );
77 }
78
79 getPotentialMovesFrom([x, y]) {
80 if (this.getPiece(x, y) == V.HIDDEN_QUEEN) {
b627d118 81 const pawnMoves = this.getPotentialPawnMoves([x, y]);
a97bdbda
BA
82 let queenMoves = super.getPotentialQueenMoves([x, y]);
83 // Remove from queen moves those corresponding to a pawn move:
84 queenMoves = queenMoves
85 .filter(m => !this.isValidPawnMove(m))
86 // Hidden queen is revealed if moving like a queen:
87 .map(m => {
88 m.appear[0].p = V.QUEEN;
89 return m;
90 });
91 return pawnMoves.concat(queenMoves);
92 }
93 return super.getPotentialMovesFrom([x, y]);
94 }
95
b627d118
BA
96 // TODO: find a more general way to describe pawn movements to avoid
97 // re-writing almost the same function for several variants.
98 getPotentialPawnMoves([x, y]) {
99 const color = this.turn;
100 const piece = this.getPiece(x, y);
101 let moves = [];
102 const [sizeX, sizeY] = [V.size.x, V.size.y];
103 const shiftX = color == "w" ? -1 : 1;
104 const startRank = color == "w" ? sizeX - 2 : 1;
105 const lastRank = color == "w" ? 0 : sizeX - 1;
106
107 const finalPieces =
108 x + shiftX == lastRank
109 ? piece == V.PAWN
110 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
111 : [V.QUEEN] //hidden queen revealed
112 : piece;
113 if (this.board[x + shiftX][y] == V.EMPTY) {
114 // One square forward
115 for (let p of finalPieces) {
116 moves.push(
117 this.getBasicMove([x, y], [x + shiftX, y], {
118 c: color,
119 p: p
120 })
121 );
122 }
123 if (
124 x == startRank &&
125 this.board[x + 2 * shiftX][y] == V.EMPTY
126 ) {
127 // Two squares jump
128 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
129 }
130 }
131 // Captures
132 for (let shiftY of [-1, 1]) {
133 if (
134 y + shiftY >= 0 &&
135 y + shiftY < sizeY &&
136 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
137 this.canTake([x, y], [x + shiftX, y + shiftY])
138 ) {
139 for (let p of finalPieces) {
140 moves.push(
141 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
142 c: color,
143 p: p
144 })
145 );
146 }
147 }
148 }
149
150 if (V.HasEnpassant) {
151 // En passant
152 const Lep = this.epSquares.length;
153 const epSquare = this.epSquares[Lep - 1]; //always at least one element
154 if (
155 !!epSquare &&
156 epSquare.x == x + shiftX &&
157 Math.abs(epSquare.y - y) == 1
158 ) {
159 let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]);
160 enpassantMove.vanish.push({
161 x: x,
162 y: epSquare.y,
163 p: "p",
164 c: this.getColor(x, epSquare.y)
165 });
166 moves.push(enpassantMove);
167 }
168 }
169
170 return moves;
171 }
172
a97bdbda
BA
173 getPossibleMovesFrom(sq) {
174 this.side = this.turn;
175 return this.filterValid(this.getPotentialMovesFrom(sq));
176 }
177
7ba4a5bc
BA
178 static GenRandInitFen(randomness) {
179 let fen = ChessRules.GenRandInitFen(randomness);
180 // Place hidden queens at random (always):
a97bdbda
BA
181 let hiddenQueenPos = randInt(8);
182 let pawnRank = "PPPPPPPP".split("");
183 pawnRank[hiddenQueenPos] = "T";
184 fen = fen.replace("PPPPPPPP", pawnRank.join(""));
185 hiddenQueenPos = randInt(8);
186 pawnRank = "pppppppp".split("");
187 pawnRank[hiddenQueenPos] = "t";
188 fen = fen.replace("pppppppp", pawnRank.join(""));
189 return fen;
190 }
191
192 updateVariables(move) {
193 super.updateVariables(move);
194 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
195 // We took opponent king
196 this.kingPos[this.turn] = [-1, -1];
197 }
198
199 unupdateVariables(move) {
200 super.unupdateVariables(move);
201 const c = move.vanish[0].c;
202 const oppCol = V.GetOppCol(c);
203 if (this.kingPos[oppCol][0] < 0)
204 // Last move took opponent's king:
205 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
206 }
207
208 getCurrentScore() {
209 const color = this.turn;
210 if (this.kingPos[color][0] < 0)
211 // King disappeared
212 return color == "w" ? "0-1" : "1-0";
213 return super.getCurrentScore();
214 }
215
216 // Search is biased, so not really needed to explore deeply
217 static get SEARCH_DEPTH() {
218 return 2;
219 }
220
221 static get VALUES() {
222 return Object.assign(
223 { t: 9 },
224 ChessRules.VALUES
225 );
226 }
227
228 getComputerMove() {
229 this.side = this.turn;
230 return super.getComputerMove();
231 }
b627d118
BA
232
233 getNotation(move) {
234 const notation = super.getNotation(move);
235 if (notation.charAt(0) == 'T')
236 // Do not reveal hidden queens
237 return notation.substr(1);
238 return notation;
239 }
a97bdbda 240};