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