Draft Suction Chess
[vchess.git] / client / src / variants / Crazyhouse.js
CommitLineData
0c3fe8a6 1import { ChessRules, PiPo, Move } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
0c3fe8a6 3
6808d7a1
BA
4export const VariantRules = class CrazyhouseRules extends ChessRules {
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(" ");
6808d7a1
BA
25 return Object.assign(ChessRules.ParseFen(fen), {
26 reserve: fenParts[5],
27 promoted: fenParts[6]
28 });
dac39588 29 }
fb6ceeff 30
6808d7a1 31 static GenRandInitFen() {
dac39588
BA
32 return ChessRules.GenRandInitFen() + " 0000000000 -";
33 }
2d7194bd 34
6808d7a1
BA
35 getFen() {
36 return (
37 super.getFen() + " " + this.getReserveFen() + " " + this.getPromotedFen()
38 );
dac39588 39 }
2d7194bd 40
6808d7a1 41 getReserveFen() {
dac39588 42 let counts = new Array(10);
6808d7a1
BA
43 for (
44 let i = 0;
45 i < V.PIECES.length - 1;
46 i++ //-1: no king reserve
47 ) {
dac39588 48 counts[i] = this.reserve["w"][V.PIECES[i]];
6808d7a1 49 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
dac39588
BA
50 }
51 return counts.join("");
52 }
2d7194bd 53
6808d7a1 54 getPromotedFen() {
dac39588 55 let res = "";
6808d7a1
BA
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 });
dac39588
BA
59 }
60 }
6808d7a1
BA
61 if (res.length > 0) res = res.slice(0, -1);
62 //remove last comma
63 else res = "-";
dac39588
BA
64 return res;
65 }
2d7194bd 66
6808d7a1 67 setOtherVariables(fen) {
dac39588
BA
68 super.setOtherVariables(fen);
69 const fenParsed = V.ParseFen(fen);
70 // Also init reserves (used by the interface to show landable pieces)
6808d7a1
BA
71 this.reserve = {
72 w: {
dac39588
BA
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]),
6808d7a1 77 [V.QUEEN]: parseInt(fenParsed.reserve[4])
dac39588 78 },
6808d7a1 79 b: {
dac39588
BA
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]),
6808d7a1 84 [V.QUEEN]: parseInt(fenParsed.reserve[9])
dac39588
BA
85 }
86 };
87 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
6808d7a1
BA
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;
dac39588
BA
92 }
93 }
94 }
5c42c64e 95
6808d7a1
BA
96 getColor(i, j) {
97 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
dac39588
BA
98 return this.board[i][j].charAt(0);
99 }
2d7194bd 100
6808d7a1
BA
101 getPiece(i, j) {
102 if (i >= V.size.x) return V.RESERVE_PIECES[j];
dac39588
BA
103 return this.board[i][j].charAt(1);
104 }
a6abf094 105
dac39588 106 // Used by the interface:
6808d7a1 107 getReservePpath(color, index) {
dac39588
BA
108 return color + V.RESERVE_PIECES[index];
109 }
a6abf094 110
dac39588 111 // Ordering on reserve pieces
6808d7a1
BA
112 static get RESERVE_PIECES() {
113 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
dac39588 114 }
1221ac47 115
6808d7a1 116 getReserveMoves([x, y]) {
dac39588
BA
117 const color = this.turn;
118 const p = V.RESERVE_PIECES[y];
6808d7a1 119 if (this.reserve[color][p] == 0) return [];
dac39588 120 let moves = [];
6808d7a1
BA
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) {
dac39588
BA
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: [],
6808d7a1
BA
135 start: { x: x, y: y }, //a bit artificial...
136 end: { x: i, y: j }
dac39588
BA
137 });
138 moves.push(mv);
139 }
140 }
141 }
142 return moves;
143 }
a6abf094 144
6808d7a1
BA
145 getPotentialMovesFrom([x, y]) {
146 if (x >= V.size.x) {
dac39588 147 // Reserves, outside of board: x == sizeX(+1)
6808d7a1 148 return this.getReserveMoves([x, y]);
dac39588
BA
149 }
150 // Standard moves
6808d7a1 151 return super.getPotentialMovesFrom([x, y]);
dac39588 152 }
a6abf094 153
6808d7a1 154 getAllValidMoves() {
dac39588
BA
155 let moves = super.getAllValidMoves();
156 const color = this.turn;
6808d7a1
BA
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 );
dac39588
BA
161 return this.filterValid(moves);
162 }
a6abf094 163
6808d7a1
BA
164 atLeastOneMove() {
165 if (!super.atLeastOneMove()) {
dac39588 166 // Search one reserve move
6808d7a1 167 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588 168 let moves = this.filterValid(
6808d7a1
BA
169 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
170 );
171 if (moves.length > 0) return true;
dac39588
BA
172 }
173 return false;
174 }
175 return true;
176 }
a6abf094 177
6808d7a1 178 updateVariables(move) {
dac39588 179 super.updateVariables(move);
6808d7a1 180 if (move.vanish.length == 2 && move.appear.length == 2) return; //skip castle
dac39588 181 const color = move.appear[0].c;
6808d7a1 182 if (move.vanish.length == 0) {
dac39588
BA
183 this.reserve[color][move.appear[0].p]--;
184 return;
185 }
186 move.movePromoted = this.promoted[move.start.x][move.start.y];
6808d7a1 187 move.capturePromoted = this.promoted[move.end.x][move.end.y];
dac39588 188 this.promoted[move.start.x][move.start.y] = false;
6808d7a1
BA
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]++;
dac39588 194 }
1221ac47 195
6808d7a1 196 unupdateVariables(move) {
dac39588 197 super.unupdateVariables(move);
6808d7a1 198 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 199 const color = this.turn;
6808d7a1 200 if (move.vanish.length == 0) {
dac39588
BA
201 this.reserve[color][move.appear[0].p]++;
202 return;
203 }
6808d7a1 204 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
dac39588 205 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
6808d7a1
BA
206 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
207 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
dac39588 208 }
a6abf094 209
6808d7a1 210 static get SEARCH_DEPTH() {
78d64531 211 // High branching factor
6808d7a1 212 return 2;
78d64531 213 }
a6abf094 214
6808d7a1 215 evalPosition() {
dac39588
BA
216 let evaluation = super.evalPosition();
217 // Add reserves:
6808d7a1 218 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588
BA
219 const p = V.RESERVE_PIECES[i];
220 evaluation += this.reserve["w"][p] * V.VALUES[p];
221 evaluation -= this.reserve["b"][p] * V.VALUES[p];
222 }
223 return evaluation;
224 }
6752407b 225
6808d7a1
BA
226 getNotation(move) {
227 if (move.vanish.length > 0) return super.getNotation(move);
dac39588
BA
228 // Rebirth:
229 const piece =
6808d7a1 230 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
dac39588
BA
231 return piece + "@" + V.CoordsToSquare(move.end);
232 }
6808d7a1 233};