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