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