Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / variants / Recycle.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3
4 export class RecycleRules extends ChessRules {
5 static get PawnSpecs() {
6 return Object.assign(
7 {},
8 ChessRules.PawnSpecs,
9 { promotions: [V.PAWN] } //in fact, none
10 );
11 }
12
13 static IsGoodFen(fen) {
14 if (!ChessRules.IsGoodFen(fen)) return false;
15 const fenParsed = V.ParseFen(fen);
16 // 5) Check reserves
17 if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{10,10}$/))
18 return false;
19 return true;
20 }
21
22 static ParseFen(fen) {
23 const fenParts = fen.split(" ");
24 return Object.assign(
25 ChessRules.ParseFen(fen),
26 { reserve: fenParts[5] }
27 );
28 }
29
30 static GenRandInitFen(randomness) {
31 return ChessRules.GenRandInitFen(randomness) + " 0000000000";
32 }
33
34 getFen() {
35 return super.getFen() + " " + this.getReserveFen();
36 }
37
38 getFenForRepeat() {
39 return super.getFenForRepeat() + "_" + this.getReserveFen();
40 }
41
42 getReserveFen() {
43 let counts = new Array(10);
44 for (
45 let i = 0;
46 i < V.PIECES.length - 1;
47 i++ //-1: no king reserve
48 ) {
49 counts[i] = this.reserve["w"][V.PIECES[i]];
50 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
51 }
52 return counts.join("");
53 }
54
55 setOtherVariables(fen) {
56 super.setOtherVariables(fen);
57 const fenParsed = V.ParseFen(fen);
58 // Also init reserves (used by the interface to show landable pieces)
59 this.reserve = {
60 w: {
61 [V.PAWN]: parseInt(fenParsed.reserve[0]),
62 [V.ROOK]: parseInt(fenParsed.reserve[1]),
63 [V.KNIGHT]: parseInt(fenParsed.reserve[2]),
64 [V.BISHOP]: parseInt(fenParsed.reserve[3]),
65 [V.QUEEN]: parseInt(fenParsed.reserve[4])
66 },
67 b: {
68 [V.PAWN]: parseInt(fenParsed.reserve[5]),
69 [V.ROOK]: parseInt(fenParsed.reserve[6]),
70 [V.KNIGHT]: parseInt(fenParsed.reserve[7]),
71 [V.BISHOP]: parseInt(fenParsed.reserve[8]),
72 [V.QUEEN]: parseInt(fenParsed.reserve[9])
73 }
74 };
75 }
76
77 getColor(i, j) {
78 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
79 return this.board[i][j].charAt(0);
80 }
81
82 getPiece(i, j) {
83 if (i >= V.size.x) return V.RESERVE_PIECES[j];
84 return this.board[i][j].charAt(1);
85 }
86
87 // Used by the interface:
88 getReservePpath(index, color) {
89 return color + V.RESERVE_PIECES[index];
90 }
91
92 // Ordering on reserve pieces
93 static get RESERVE_PIECES() {
94 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
95 }
96
97 getReserveMoves([x, y]) {
98 const color = this.turn;
99 const p = V.RESERVE_PIECES[y];
100 if (this.reserve[color][p] == 0) return [];
101 let moves = [];
102 const pawnShift = p == V.PAWN ? 1 : 0;
103 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
104 for (let j = 0; j < V.size.y; j++) {
105 if (this.board[i][j] == V.EMPTY) {
106 let mv = new Move({
107 appear: [
108 new PiPo({
109 x: i,
110 y: j,
111 c: color,
112 p: p
113 })
114 ],
115 vanish: [],
116 start: { x: x, y: y }, //a bit artificial...
117 end: { x: i, y: j }
118 });
119 moves.push(mv);
120 }
121 }
122 }
123 return moves;
124 }
125
126 getPotentialMovesFrom([x, y]) {
127 if (x >= V.size.x) {
128 // Reserves, outside of board: x == sizeX(+1)
129 return this.getReserveMoves([x, y]);
130 }
131 // Standard moves
132 return super.getPotentialMovesFrom([x, y]);
133 }
134
135 getPotentialPawnMoves([x, y]) {
136 let moves = super.getPotentialPawnMoves([x, y]);
137 // Remove pawns on 8th rank ("fallen"):
138 const color = this.turn;
139 const lastRank = (color == "w" ? 0 : V.size.x - 1);
140 moves.forEach(m => {
141 if (m.appear[0].x == lastRank) m.appear.pop();
142 });
143 return moves;
144 }
145
146 getAllValidMoves() {
147 let moves = super.getAllValidMoves();
148 const color = this.turn;
149 for (let i = 0; i < V.RESERVE_PIECES.length; i++)
150 moves = moves.concat(
151 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
152 );
153 return this.filterValid(moves);
154 }
155
156 atLeastOneMove() {
157 if (!super.atLeastOneMove()) {
158 // Search one reserve move
159 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
160 let moves = this.filterValid(
161 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
162 );
163 if (moves.length > 0) return true;
164 }
165 return false;
166 }
167 return true;
168 }
169
170 canTake([x1, y1], [x2, y2]) {
171 // Self-captures allowed, except for the king:
172 return this.getPiece(x2, y2) != V.KING;
173 }
174
175 prePlay(move) {
176 super.prePlay(move);
177 // Skip castle:
178 if (move.vanish.length == 2 && move.appear.length == 2) return;
179 const color = this.turn;
180 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]--;
181 else if (move.vanish.length == 2 && move.vanish[1].c == color)
182 // Self-capture
183 this.reserve[color][move.vanish[1].p]++;
184 }
185
186 postUndo(move) {
187 super.postUndo(move);
188 if (move.vanish.length == 2 && move.appear.length == 2) return;
189 const color = this.turn;
190 if (move.vanish.length == 0) this.reserve[color][move.appear[0].p]++;
191 else if (move.vanish.length == 2 && move.vanish[1].c == color)
192 this.reserve[color][move.vanish[1].p]--;
193 }
194
195 static get SEARCH_DEPTH() {
196 return 2;
197 }
198
199 evalPosition() {
200 let evaluation = super.evalPosition();
201 // Add reserves:
202 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
203 const p = V.RESERVE_PIECES[i];
204 evaluation += this.reserve["w"][p] * V.VALUES[p];
205 evaluation -= this.reserve["b"][p] * V.VALUES[p];
206 }
207 return evaluation;
208 }
209
210 getNotation(move) {
211 const finalSquare = V.CoordsToSquare(move.end);
212 if (move.vanish.length > 0) {
213 if (move.appear.length > 0) {
214 // Standard move
215 return super.getNotation(move);
216 } else {
217 // Pawn fallen: capturing or not
218 let res = "";
219 if (move.vanish.length == 2)
220 res += V.CoordToColumn(move.start.y) + "x";
221 return res + finalSquare;
222 }
223 }
224 // Rebirth:
225 const piece =
226 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
227 return piece + "@" + V.CoordsToSquare(move.end);
228 }
229 };