Fix Antiking 1 notation
[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;
a97bdbda 45 return (
a97bdbda 46 (
3a2a7b5f 47 move.end.x - move.start.x == pawnShift &&
a97bdbda 48 (
a97bdbda 49 (
3a2a7b5f
BA
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
a97bdbda
BA
59 )
60 )
3a2a7b5f
BA
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
a97bdbda
BA
69 )
70 );
71 }
72
73 getPotentialMovesFrom([x, y]) {
74 if (this.getPiece(x, y) == V.HIDDEN_QUEEN) {
b627d118 75 const pawnMoves = this.getPotentialPawnMoves([x, y]);
a97bdbda
BA
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
b627d118
BA
90 // TODO: find a more general way to describe pawn movements to avoid
91 // re-writing almost the same function for several variants.
92 getPotentialPawnMoves([x, y]) {
93 const color = this.turn;
94 const piece = this.getPiece(x, y);
95 let moves = [];
96 const [sizeX, sizeY] = [V.size.x, V.size.y];
97 const shiftX = color == "w" ? -1 : 1;
98 const startRank = color == "w" ? sizeX - 2 : 1;
99 const lastRank = color == "w" ? 0 : sizeX - 1;
100
101 const finalPieces =
102 x + shiftX == lastRank
103 ? piece == V.PAWN
104 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
105 : [V.QUEEN] //hidden queen revealed
3a2a7b5f 106 : [piece]; //V.PAWN
b627d118
BA
107 if (this.board[x + shiftX][y] == V.EMPTY) {
108 // One square forward
109 for (let p of finalPieces) {
110 moves.push(
111 this.getBasicMove([x, y], [x + shiftX, y], {
112 c: color,
113 p: p
114 })
115 );
116 }
117 if (
118 x == startRank &&
119 this.board[x + 2 * shiftX][y] == V.EMPTY
120 ) {
121 // Two squares jump
122 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
123 }
124 }
125 // Captures
126 for (let shiftY of [-1, 1]) {
127 if (
128 y + shiftY >= 0 &&
129 y + shiftY < sizeY &&
130 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
131 this.canTake([x, y], [x + shiftX, y + shiftY])
132 ) {
133 for (let p of finalPieces) {
134 moves.push(
135 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
136 c: color,
137 p: p
138 })
139 );
140 }
141 }
142 }
143
3a2a7b5f
BA
144 // En passant
145 const Lep = this.epSquares.length;
146 const epSquare = this.epSquares[Lep - 1]; //always at least one element
147 if (
148 !!epSquare &&
149 epSquare.x == x + shiftX &&
150 Math.abs(epSquare.y - y) == 1
151 ) {
152 let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]);
153 enpassantMove.vanish.push({
154 x: x,
155 y: epSquare.y,
156 p: "p",
157 c: this.getColor(x, epSquare.y)
158 });
159 moves.push(enpassantMove);
b627d118
BA
160 }
161
162 return moves;
163 }
164
a97bdbda
BA
165 getPossibleMovesFrom(sq) {
166 this.side = this.turn;
167 return this.filterValid(this.getPotentialMovesFrom(sq));
168 }
169
7ba4a5bc
BA
170 static GenRandInitFen(randomness) {
171 let fen = ChessRules.GenRandInitFen(randomness);
172 // Place hidden queens at random (always):
a97bdbda
BA
173 let hiddenQueenPos = randInt(8);
174 let pawnRank = "PPPPPPPP".split("");
175 pawnRank[hiddenQueenPos] = "T";
176 fen = fen.replace("PPPPPPPP", pawnRank.join(""));
177 hiddenQueenPos = randInt(8);
178 pawnRank = "pppppppp".split("");
179 pawnRank[hiddenQueenPos] = "t";
180 fen = fen.replace("pppppppp", pawnRank.join(""));
181 return fen;
182 }
183
3a2a7b5f
BA
184 postPlay(move) {
185 super.postPlay(move);
a97bdbda
BA
186 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
187 // We took opponent king
188 this.kingPos[this.turn] = [-1, -1];
189 }
190
3a2a7b5f
BA
191 preUndo(move) {
192 super.preUndo(move);
193 const oppCol = this.turn;
a97bdbda 194 if (this.kingPos[oppCol][0] < 0)
3a2a7b5f 195 // Move takes opponent's king:
a97bdbda
BA
196 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
197 }
198
199 getCurrentScore() {
200 const color = this.turn;
201 if (this.kingPos[color][0] < 0)
202 // King disappeared
203 return color == "w" ? "0-1" : "1-0";
204 return super.getCurrentScore();
205 }
206
207 // Search is biased, so not really needed to explore deeply
208 static get SEARCH_DEPTH() {
209 return 2;
210 }
211
212 static get VALUES() {
213 return Object.assign(
214 { t: 9 },
215 ChessRules.VALUES
216 );
217 }
218
219 getComputerMove() {
220 this.side = this.turn;
221 return super.getComputerMove();
222 }
b627d118
BA
223
224 getNotation(move) {
225 const notation = super.getNotation(move);
226 if (notation.charAt(0) == 'T')
227 // Do not reveal hidden queens
228 return notation.substr(1);
229 return notation;
230 }
a97bdbda 231};