Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / variants / Alice.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3
4 // NOTE: alternative implementation, probably cleaner = use only 1 board
5 // TODO? atLeastOneMove() would be more efficient if rewritten here
6 // (less sideBoard computations)
7 export class AliceRules extends ChessRules {
8 static get ALICE_PIECES() {
9 return {
10 s: "p",
11 t: "q",
12 u: "r",
13 c: "b",
14 o: "n",
15 l: "k"
16 };
17 }
18 static get ALICE_CODES() {
19 return {
20 p: "s",
21 q: "t",
22 r: "u",
23 b: "c",
24 n: "o",
25 k: "l"
26 };
27 }
28
29 static get PIECES() {
30 return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES));
31 }
32
33 getPpath(b) {
34 return (Object.keys(V.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
35 }
36
37 setOtherVariables(fen) {
38 super.setOtherVariables(fen);
39 const rows = V.ParseFen(fen).position.split("/");
40 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) {
41 // INIT_COL_XXX won't be required if Alice kings are found
42 // (it means 'king moved')
43 for (let i = 0; i < rows.length; i++) {
44 let k = 0; //column index on board
45 for (let j = 0; j < rows[i].length; j++) {
46 switch (rows[i].charAt(j)) {
47 case "l":
48 this.kingPos["b"] = [i, k];
49 break;
50 case "L":
51 this.kingPos["w"] = [i, k];
52 break;
53 default: {
54 const num = parseInt(rows[i].charAt(j));
55 if (!isNaN(num)) k += num - 1;
56 }
57 }
58 k++;
59 }
60 }
61 }
62 }
63
64 // Return the (standard) color+piece notation at a square for a board
65 getSquareOccupation(i, j, mirrorSide) {
66 const piece = this.getPiece(i, j);
67 if (mirrorSide == 1 && Object.keys(V.ALICE_CODES).includes(piece))
68 return this.board[i][j];
69 if (mirrorSide == 2 && Object.keys(V.ALICE_PIECES).includes(piece))
70 return this.getColor(i, j) + V.ALICE_PIECES[piece];
71 return "";
72 }
73
74 // Build board of the given (mirror)side
75 getSideBoard(mirrorSide) {
76 // Build corresponding board from complete board
77 let sideBoard = ArrayFun.init(V.size.x, V.size.y, "");
78 for (let i = 0; i < V.size.x; i++) {
79 for (let j = 0; j < V.size.y; j++)
80 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
81 }
82 return sideBoard;
83 }
84
85 // NOTE: castle & enPassant
86 // https://www.chessvariants.com/other.dir/alice.html
87 getPotentialMovesFrom([x, y], sideBoard) {
88 const pieces = Object.keys(V.ALICE_CODES);
89 const codes = Object.keys(V.ALICE_PIECES);
90 const mirrorSide = pieces.includes(this.getPiece(x, y)) ? 1 : 2;
91 if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
92 const color = this.getColor(x, y);
93
94 // Search valid moves on sideBoard
95 const saveBoard = this.board;
96 this.board = sideBoard[mirrorSide - 1];
97 const moves = super.getPotentialMovesFrom([x, y]).filter(m => {
98 // Filter out king moves which result in under-check position on
99 // current board (before mirror traversing)
100 let aprioriValid = true;
101 if (m.appear[0].p == V.KING) {
102 this.play(m);
103 if (this.underCheck(color, sideBoard)) aprioriValid = false;
104 this.undo(m);
105 }
106 return aprioriValid;
107 });
108 this.board = saveBoard;
109
110 // Finally filter impossible moves
111 const res = moves.filter(m => {
112 if (m.appear.length == 2) {
113 // Castle: appear[i] must be an empty square on the other board
114 for (let psq of m.appear) {
115 if (
116 this.getSquareOccupation(psq.x, psq.y, 3 - mirrorSide) != V.EMPTY
117 ) {
118 return false;
119 }
120 }
121 } else if (this.board[m.end.x][m.end.y] != V.EMPTY) {
122 // Attempt to capture
123 const piece = this.getPiece(m.end.x, m.end.y);
124 if (
125 (mirrorSide == 1 && codes.includes(piece)) ||
126 (mirrorSide == 2 && pieces.includes(piece))
127 ) {
128 return false;
129 }
130 }
131 // If the move is computed on board1, m.appear change for Alice pieces.
132 if (mirrorSide == 1) {
133 m.appear.forEach(psq => {
134 // forEach: castling taken into account
135 psq.p = V.ALICE_CODES[psq.p]; //goto board2
136 });
137 }
138 else {
139 // Move on board2: mark vanishing pieces as Alice
140 m.vanish.forEach(psq => {
141 psq.p = V.ALICE_CODES[psq.p];
142 });
143 }
144 // Fix en-passant captures
145 if (
146 m.vanish[0].p == V.PAWN &&
147 m.vanish.length == 2 &&
148 this.board[m.end.x][m.end.y] == V.EMPTY
149 ) {
150 m.vanish[1].c = V.GetOppCol(this.getColor(x, y));
151 // In the special case of en-passant, if
152 // - board1 takes board2 : vanish[1] --> Alice
153 // - board2 takes board1 : vanish[1] --> normal
154 let van = m.vanish[1];
155 if (mirrorSide == 1 && codes.includes(this.getPiece(van.x, van.y)))
156 van.p = V.ALICE_CODES[van.p];
157 else if (
158 mirrorSide == 2 &&
159 pieces.includes(this.getPiece(van.x, van.y))
160 )
161 van.p = V.ALICE_PIECES[van.p];
162 }
163 return true;
164 });
165 return res;
166 }
167
168 filterValid(moves, sideBoard) {
169 if (moves.length == 0) return [];
170 if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
171 const color = this.turn;
172 return moves.filter(m => {
173 this.playSide(m, sideBoard); //no need to track flags
174 const res = !this.underCheck(color, sideBoard);
175 this.undoSide(m, sideBoard);
176 return res;
177 });
178 }
179
180 getAllValidMoves() {
181 const color = this.turn;
182 let potentialMoves = [];
183 const sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
184 for (var i = 0; i < V.size.x; i++) {
185 for (var j = 0; j < V.size.y; j++) {
186 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
187 Array.prototype.push.apply(
188 potentialMoves,
189 this.getPotentialMovesFrom([i, j], sideBoard)
190 );
191 }
192 }
193 }
194 return this.filterValid(potentialMoves, sideBoard);
195 }
196
197 // Play on sideboards [TODO: only one sideBoard required]
198 playSide(move, sideBoard) {
199 const pieces = Object.keys(V.ALICE_CODES);
200 move.vanish.forEach(psq => {
201 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
202 sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
203 });
204 move.appear.forEach(psq => {
205 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
206 const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
207 sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
208 if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
209 });
210 }
211
212 // Undo on sideboards
213 undoSide(move, sideBoard) {
214 const pieces = Object.keys(V.ALICE_CODES);
215 move.appear.forEach(psq => {
216 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
217 sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
218 });
219 move.vanish.forEach(psq => {
220 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
221 const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
222 sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
223 if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
224 });
225 }
226
227 // sideBoard: arg containing both boards (see getAllValidMoves())
228 underCheck(color, sideBoard) {
229 const kp = this.kingPos[color];
230 const mirrorSide = sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2;
231 let saveBoard = this.board;
232 this.board = sideBoard[mirrorSide - 1];
233 let res = this.isAttacked(kp, [V.GetOppCol(color)]);
234 this.board = saveBoard;
235 return res;
236 }
237
238 getCheckSquares(color) {
239 const pieces = Object.keys(V.ALICE_CODES);
240 const kp = this.kingPos[color];
241 const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2;
242 let sideBoard = this.getSideBoard(mirrorSide);
243 let saveBoard = this.board;
244 this.board = sideBoard;
245 let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])
246 ? [JSON.parse(JSON.stringify(this.kingPos[color]))]
247 : [];
248 this.board = saveBoard;
249 return res;
250 }
251
252 postPlay(move) {
253 super.postPlay(move); //standard king
254 const piece = move.vanish[0].p;
255 const c = move.vanish[0].c;
256 // "l" = Alice king
257 if (piece == "l") {
258 this.kingPos[c][0] = move.appear[0].x;
259 this.kingPos[c][1] = move.appear[0].y;
260 this.castleFlags[c] = [8, 8];
261 }
262 }
263
264 postUndo(move) {
265 super.postUndo(move);
266 const c = move.vanish[0].c;
267 if (move.vanish[0].p == "l")
268 this.kingPos[c] = [move.start.x, move.start.y];
269 }
270
271 getCurrentScore() {
272 if (this.atLeastOneMove()) return "*";
273 const pieces = Object.keys(V.ALICE_CODES);
274 const color = this.turn;
275 const kp = this.kingPos[color];
276 const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2;
277 let sideBoard = this.getSideBoard(mirrorSide);
278 let saveBoard = this.board;
279 this.board = sideBoard;
280 let res = "*";
281 if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
282 res = "1/2";
283 else res = color == "w" ? "0-1" : "1-0";
284 this.board = saveBoard;
285 return res;
286 }
287
288 static get VALUES() {
289 return Object.assign(
290 {
291 s: 1,
292 u: 5,
293 o: 3,
294 c: 3,
295 t: 9,
296 l: 1000
297 },
298 ChessRules.VALUES
299 );
300 }
301
302 static get SEARCH_DEPTH() {
303 return 2;
304 }
305
306 getNotation(move) {
307 if (move.appear.length == 2 && move.appear[0].p == V.KING) {
308 if (move.end.y < move.start.y) return "0-0-0";
309 return "0-0";
310 }
311
312 const finalSquare = V.CoordsToSquare(move.end);
313 const piece = this.getPiece(move.start.x, move.start.y);
314
315 const captureMark = move.vanish.length > move.appear.length ? "x" : "";
316 let pawnMark = "";
317 if (["p", "s"].includes(piece) && captureMark.length == 1)
318 pawnMark = V.CoordToColumn(move.start.y); //start column
319
320 // Piece or pawn movement
321 let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
322 if (["s", "p"].includes(piece) && !["s", "p"].includes(move.appear[0].p)) {
323 // Promotion
324 notation += "=" + move.appear[0].p.toUpperCase();
325 }
326 return notation;
327 }
328 };