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