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