A few bugs fixes
[vchess.git] / client / src / variants / Hiddenqueen.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt } from "@/utils/alea";
4
5 export 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 SomeHiddenMoves() {
16 return true;
17 }
18
19 static get PIECES() {
20 return ChessRules.PIECES.concat([V.HIDDEN_QUEEN]);
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 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;
55 const color = move.vanish[0].c;
56 if (
57 s.y == e.y &&
58 Math.abs(s.x - e.x) == 2 &&
59 ((color == 'w' && s.x == 6) || (color == 'b' && s.x == 1)) &&
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
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;
74 return (
75 (
76 move.end.x - move.start.x == pawnShift &&
77 (
78 (
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
88 )
89 )
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
98 )
99 );
100 }
101
102 getPotentialMovesFrom([x, y]) {
103 if (this.getPiece(x, y) == V.HIDDEN_QUEEN) {
104 const pawnMoves = this.getPotentialPawnMoves([x, y]);
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
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
140 getPotentialPawnMoves([x, y]) {
141 const piece = this.getPiece(x, y);
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);
147 }
148
149 getPossibleMovesFrom(sq) {
150 this.side = this.turn;
151 return this.filterValid(this.getPotentialMovesFrom(sq));
152 }
153
154 static GenRandInitFen(randomness) {
155 let fen = ChessRules.GenRandInitFen(randomness);
156 // Place hidden queens at random (always):
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
168 postPlay(move) {
169 super.postPlay(move);
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
175 preUndo(move) {
176 super.preUndo(move);
177 const oppCol = this.turn;
178 if (this.kingPos[oppCol][0] < 0)
179 // Move takes opponent's king:
180 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
181 }
182
183 underCheck(color) {
184 if (this.kingPos[color][0] < 0) return false;
185 return super.underCheck(color);
186 }
187
188 getCurrentScore() {
189 const color = this.turn;
190 if (this.kingPos[color][0] < 0)
191 // King disappeared
192 return color == "w" ? "0-1" : "1-0";
193 return super.getCurrentScore();
194 }
195
196 // Search is biased, so not really needed to explore deeply
197 static get SEARCH_DEPTH() {
198 return 2;
199 }
200
201 static get VALUES() {
202 return Object.assign(
203 { t: 9 },
204 ChessRules.VALUES
205 );
206 }
207
208 getComputerMove() {
209 this.side = this.turn;
210 return super.getComputerMove();
211 }
212
213 getNotation(move) {
214 // Not using getPiece() method because it would transform HQ into pawn:
215 if (this.board[move.start.x][move.start.y][1] != V.HIDDEN_QUEEN)
216 return super.getNotation(move);
217 const finalSquare = V.CoordsToSquare(move.end);
218 if (move.appear[0].p == V.QUEEN) {
219 return (
220 "Q" +
221 (move.vanish.length > move.appear.length ? "x" : "") +
222 finalSquare
223 );
224 }
225 // Do not reveal hidden queens playing as pawns
226 let notation = "";
227 if (move.vanish.length == 2)
228 // Capture
229 notation = V.CoordToColumn(move.start.y) + "x" + finalSquare;
230 else notation = finalSquare;
231 return notation;
232 }
233 };