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