Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / variants / Hidden.js
CommitLineData
b9139251 1import { ChessRules, PiPo, Move } from "@/base_rules";
241bf8f2
BA
2import { ArrayFun } from "@/utils/array";
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class HiddenRules extends ChessRules {
241bf8f2
BA
6 static get HasFlags() {
7 return false;
8 }
9
10 static get HasEnpassant() {
11 return false;
12 }
13
14 // Analyse in Hidden mode makes no sense
15 static get CanAnalyze() {
16 return false;
17 }
18
57eb158f 19 // Moves are revealed only when game ends, but are highlighted on board
241bf8f2 20 static get ShowMoves() {
57eb158f 21 return "highlight";
241bf8f2
BA
22 }
23
24 static get HIDDEN_DECODE() {
25 return {
26 s: "p",
27 t: "q",
28 u: "r",
29 c: "b",
30 o: "n",
31 l: "k"
32 };
33 }
34 static get HIDDEN_CODE() {
35 return {
36 p: "s",
37 q: "t",
38 r: "u",
39 b: "c",
40 n: "o",
41 k: "l"
42 };
43 }
44
1cd3e362
BA
45 // Turn a hidden piece or revealed piece into revealed piece:
46 static Decode(p) {
47 if (Object.keys(V.HIDDEN_DECODE).includes(p))
48 return V.HIDDEN_DECODE[p];
49 return p;
50 }
51
241bf8f2 52 static get PIECES() {
6f2f9437 53 return ChessRules.PIECES.concat(Object.keys(V.HIDDEN_DECODE));
241bf8f2
BA
54 }
55
b9139251
BA
56 // Pieces can be hidden :)
57 getPiece(i, j) {
58 const piece = this.board[i][j].charAt(1);
59 if (Object.keys(V.HIDDEN_DECODE).includes(piece))
60 return V.HIDDEN_DECODE[piece];
61 return piece;
62 }
63
e71161fb
BA
64 getPpath(b, color, score) {
65 if (Object.keys(V.HIDDEN_DECODE).includes(b[1])) {
66 // Supposed to be hidden.
67 if (score == "*" && (!color || color != b[0]))
68 return "Hidden/" + b[0] + "p";
69 // Else: condition OK to show the piece
70 return b[0] + V.HIDDEN_DECODE[b[1]];
71 }
72 // The piece is already not supposed to be hidden:
73 return b;
74 }
75
241bf8f2 76 // Scan board for kings positions (no castling)
3a2a7b5f 77 scanKings(fen) {
241bf8f2
BA
78 this.kingPos = { w: [-1, -1], b: [-1, -1] };
79 const fenRows = V.ParseFen(fen).position.split("/");
80 for (let i = 0; i < fenRows.length; i++) {
81 let k = 0; //column index on board
82 for (let j = 0; j < fenRows[i].length; j++) {
83 switch (fenRows[i].charAt(j)) {
84 case "k":
85 case "l":
86 this.kingPos["b"] = [i, k];
87 break;
88 case "K":
89 case "L":
90 this.kingPos["w"] = [i, k];
91 break;
92 default: {
93 const num = parseInt(fenRows[i].charAt(j));
94 if (!isNaN(num)) k += num - 1;
95 }
96 }
97 k++;
98 }
99 }
100 }
101
b9139251 102 getBasicMove([sx, sy], [ex, ey], tr) {
1cd3e362
BA
103 if (
104 tr &&
105 Object.keys(V.HIDDEN_DECODE).includes(this.board[sx][sy].charAt(1))
106 ) {
107 // The transformed piece is a priori hidden
108 tr.p = V.HIDDEN_CODE[tr.p];
109 }
b9139251
BA
110 let mv = new Move({
111 appear: [
112 new PiPo({
113 x: ex,
114 y: ey,
115 c: tr ? tr.c : this.getColor(sx, sy),
116 p: tr ? tr.p : this.board[sx][sy].charAt(1)
117 })
118 ],
119 vanish: [
120 new PiPo({
121 x: sx,
122 y: sy,
123 c: this.getColor(sx, sy),
124 p: this.board[sx][sy].charAt(1)
125 })
126 ]
127 });
128
129 // The opponent piece disappears if we take it
130 if (this.board[ex][ey] != V.EMPTY) {
131 mv.vanish.push(
132 new PiPo({
133 x: ex,
134 y: ey,
135 c: this.getColor(ex, ey),
136 p: this.board[ex][ey].charAt(1)
137 })
138 );
139 // Pieces are revealed when they capture
1cd3e362 140 mv.appear[0].p = V.Decode(mv.appear[0].p);
b9139251 141 }
1cd3e362 142
b9139251
BA
143 return mv;
144 }
145
1cd3e362
BA
146 filterValid(moves) {
147 return moves;
148 }
149
7ba4a5bc 150 // Ignore randomness here: placement is always random asymmetric
241bf8f2
BA
151 static GenRandInitFen() {
152 let pieces = { w: new Array(8), b: new Array(8) };
153 // Shuffle pieces + pawns on two first ranks
154 for (let c of ["w", "b"]) {
155 let positions = ArrayFun.range(16);
156
157 // Get random squares for bishops
158 let randIndex = 2 * randInt(8);
159 const bishop1Pos = positions[randIndex];
160 // The second bishop must be on a square of different color
161 let randIndex_tmp = 2 * randInt(8) + 1;
162 const bishop2Pos = positions[randIndex_tmp];
163 // Remove chosen squares
164 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
165 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
166
167 // Get random squares for knights
168 randIndex = randInt(14);
169 const knight1Pos = positions[randIndex];
170 positions.splice(randIndex, 1);
171 randIndex = randInt(13);
172 const knight2Pos = positions[randIndex];
173 positions.splice(randIndex, 1);
174
b9139251 175 // Get random squares for rooks
241bf8f2 176 randIndex = randInt(12);
b9139251
BA
177 const rook1Pos = positions[randIndex];
178 positions.splice(randIndex, 1);
179 randIndex = randInt(11);
180 const rook2Pos = positions[randIndex];
181 positions.splice(randIndex, 1);
182
183 // Get random square for queen
184 randIndex = randInt(10);
241bf8f2
BA
185 const queenPos = positions[randIndex];
186 positions.splice(randIndex, 1);
187
1cd3e362 188 // Get random square for king
b9139251
BA
189 randIndex = randInt(9);
190 const kingPos = positions[randIndex];
191 positions.splice(randIndex, 1);
241bf8f2 192
b9139251
BA
193 // Pawns position are all remaining slots:
194 for (let p of positions)
195 pieces[c][p] = "s";
241bf8f2
BA
196
197 // Finally put the shuffled pieces in the board array
b9139251
BA
198 pieces[c][rook1Pos] = "u";
199 pieces[c][knight1Pos] = "o";
200 pieces[c][bishop1Pos] = "c";
201 pieces[c][queenPos] = "t";
202 pieces[c][kingPos] = "l";
203 pieces[c][bishop2Pos] = "c";
204 pieces[c][knight2Pos] = "o";
205 pieces[c][rook2Pos] = "u";
241bf8f2 206 }
b9139251 207 let upFen = pieces["b"].join("");
2c5d7b20
BA
208 upFen = upFen.substr(0,8) + "/" +
209 upFen.substr(8).split("").reverse().join("");
b9139251 210 let downFen = pieces["b"].join("").toUpperCase();
2c5d7b20
BA
211 downFen = downFen.substr(0,8) + "/" +
212 downFen.substr(8).split("").reverse().join("");
b9139251 213 return upFen + "/8/8/8/8/" + downFen + " w 0";
241bf8f2
BA
214 }
215
216 getCheckSquares() {
217 return [];
218 }
219
3a2a7b5f
BA
220 postPlay(move) {
221 super.postPlay(move);
241bf8f2
BA
222 if (
223 move.vanish.length >= 2 &&
224 [V.KING,V.HIDDEN_CODE[V.KING]].includes(move.vanish[1].p)
225 ) {
226 // We took opponent king
227 this.kingPos[this.turn] = [-1, -1];
228 }
229 }
230
3a2a7b5f
BA
231 postUndo(move) {
232 super.postUndo(move);
241bf8f2
BA
233 const c = move.vanish[0].c;
234 const oppCol = V.GetOppCol(c);
235 if (this.kingPos[oppCol][0] < 0)
236 // Last move took opponent's king:
237 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
238 }
239
240 getCurrentScore() {
241 const color = this.turn;
242 const kp = this.kingPos[color];
243 if (kp[0] < 0)
244 // King disappeared
245 return color == "w" ? "0-1" : "1-0";
246 // Assume that stalemate is impossible:
247 return "*";
248 }
249
250 getComputerMove() {
71ef1664
BA
251 const color = this.turn;
252 let moves = this.getAllValidMoves();
253 for (let move of moves) {
254 move.eval = 0; //a priori...
255
256 // Can I take something ? If yes, do it with some probability
257 if (move.vanish.length == 2 && move.vanish[1].c != color) {
258 // OK this isn't a castling move
259 const myPieceVal = V.VALUES[move.appear[0].p];
2c5d7b20
BA
260 const hisPieceVal =
261 Object.keys(V.HIDDEN_DECODE).includes(move.vanish[1].p)
262 ? undefined
263 : V.VALUES[move.vanish[1].p];
71ef1664
BA
264 if (!hisPieceVal) {
265 // Opponent's piece is unknown: do not take too much risk
266 move.eval = -myPieceVal + 1.5; //so that pawns always take
267 }
268 // Favor captures
269 else if (myPieceVal <= hisPieceVal)
270 move.eval = hisPieceVal - myPieceVal + 1;
271 else {
272 // Taking a pawn with minor piece,
273 // or minor piece or pawn with a rook,
274 // or anything but a queen with a queen,
275 // or anything with a king.
276 move.eval = hisPieceVal - myPieceVal;
277 }
278 } else {
279 // If no capture, favor small step moves,
280 // but sometimes move the knight anyway
281 const penalty = V.Decode(move.vanish[0].p) != V.KNIGHT
2c5d7b20
BA
282 ? Math.abs(move.end.x - move.start.x) +
283 Math.abs(move.end.y - move.start.y)
71ef1664
BA
284 : (Math.random() < 0.5 ? 3 : 1);
285 move.eval -= penalty / (V.size.x + V.size.y - 1);
286 }
287
288 // TODO: also favor movements toward the center?
289 }
290
291 moves.sort((a, b) => b.eval - a.eval);
292 let candidates = [0];
293 for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++)
294 candidates.push(j);
295 return moves[candidates[randInt(candidates.length)]];
241bf8f2 296 }
1cd3e362
BA
297
298 getNotation(move) {
299 // Translate final square
300 const finalSquare = V.CoordsToSquare(move.end);
301
302 const piece = this.getPiece(move.start.x, move.start.y);
303 if (piece == V.PAWN) {
304 // Pawn move
305 let notation = "";
306 if (move.vanish.length > move.appear.length) {
307 // Capture
308 const startColumn = V.CoordToColumn(move.start.y);
309 notation = startColumn + "x" + finalSquare;
310 }
311 else notation = finalSquare;
312 if (move.appear.length > 0 && !["p","s"].includes(move.appear[0].p)) {
313 // Promotion
314 const appearPiece = V.Decode(move.appear[0].p);
315 notation += "=" + appearPiece.toUpperCase();
316 }
317 return notation;
318 }
319 // Piece movement
320 return (
321 piece.toUpperCase() +
322 (move.vanish.length > move.appear.length ? "x" : "") +
323 finalSquare
324 );
325 }
241bf8f2 326};