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