Fix issues with preset challenges
[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
e727fe31 31 getEpSquare(moveOrSquare) {
bbf66837 32 if (typeof moveOrSquare !== "object" || moveOrSquare.vanish.length > 0)
e727fe31
BA
33 return super.getEpSquare(moveOrSquare);
34 // Landing move: no en-passant
35 return undefined;
36 }
37
7ba4a5bc
BA
38 static GenRandInitFen(randomness) {
39 return ChessRules.GenRandInitFen(randomness) + " 0000000000 -";
dac39588 40 }
2d7194bd 41
6808d7a1
BA
42 getFen() {
43 return (
44 super.getFen() + " " + this.getReserveFen() + " " + this.getPromotedFen()
45 );
dac39588 46 }
2d7194bd 47
f9c36b2d
BA
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
6808d7a1 59 getReserveFen() {
dac39588 60 let counts = new Array(10);
6808d7a1
BA
61 for (
62 let i = 0;
63 i < V.PIECES.length - 1;
64 i++ //-1: no king reserve
65 ) {
dac39588 66 counts[i] = this.reserve["w"][V.PIECES[i]];
6808d7a1 67 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
dac39588
BA
68 }
69 return counts.join("");
70 }
2d7194bd 71
6808d7a1 72 getPromotedFen() {
dac39588 73 let res = "";
6808d7a1
BA
74 for (let i = 0; i < V.size.x; i++) {
75 for (let j = 0; j < V.size.y; j++) {
c292ebb2 76 if (this.promoted[i][j]) res += V.CoordsToSquare({ x: i, y: j }) + ",";
dac39588
BA
77 }
78 }
c292ebb2 79 // Remove last comma:
6808d7a1 80 if (res.length > 0) res = res.slice(0, -1);
6808d7a1 81 else res = "-";
dac39588
BA
82 return res;
83 }
2d7194bd 84
6808d7a1 85 setOtherVariables(fen) {
dac39588
BA
86 super.setOtherVariables(fen);
87 const fenParsed = V.ParseFen(fen);
88 // Also init reserves (used by the interface to show landable pieces)
6808d7a1
BA
89 this.reserve = {
90 w: {
dac39588
BA
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]),
6808d7a1 95 [V.QUEEN]: parseInt(fenParsed.reserve[4])
dac39588 96 },
6808d7a1 97 b: {
dac39588
BA
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]),
6808d7a1 102 [V.QUEEN]: parseInt(fenParsed.reserve[9])
dac39588
BA
103 }
104 };
105 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
6808d7a1
BA
106 if (fenParsed.promoted != "-") {
107 for (let square of fenParsed.promoted.split(",")) {
c292ebb2
BA
108 const coords = V.SquareToCoords(square);
109 this.promoted[coords.x][coords.y] = true;
dac39588
BA
110 }
111 }
112 }
5c42c64e 113
6808d7a1
BA
114 getColor(i, j) {
115 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
dac39588
BA
116 return this.board[i][j].charAt(0);
117 }
2d7194bd 118
6808d7a1
BA
119 getPiece(i, j) {
120 if (i >= V.size.x) return V.RESERVE_PIECES[j];
dac39588
BA
121 return this.board[i][j].charAt(1);
122 }
a6abf094 123
dac39588 124 // Used by the interface:
241bf8f2 125 getReservePpath(index, color) {
dac39588
BA
126 return color + V.RESERVE_PIECES[index];
127 }
9d4a0218
BA
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// }
a6abf094 136
dac39588 137 // Ordering on reserve pieces
6808d7a1
BA
138 static get RESERVE_PIECES() {
139 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
dac39588 140 }
1221ac47 141
6808d7a1 142 getReserveMoves([x, y]) {
dac39588
BA
143 const color = this.turn;
144 const p = V.RESERVE_PIECES[y];
6808d7a1 145 if (this.reserve[color][p] == 0) return [];
dac39588 146 let moves = [];
6808d7a1
BA
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) {
dac39588
BA
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: [],
6808d7a1
BA
161 start: { x: x, y: y }, //a bit artificial...
162 end: { x: i, y: j }
dac39588
BA
163 });
164 moves.push(mv);
165 }
166 }
167 }
168 return moves;
169 }
a6abf094 170
6808d7a1
BA
171 getPotentialMovesFrom([x, y]) {
172 if (x >= V.size.x) {
dac39588 173 // Reserves, outside of board: x == sizeX(+1)
6808d7a1 174 return this.getReserveMoves([x, y]);
dac39588
BA
175 }
176 // Standard moves
6808d7a1 177 return super.getPotentialMovesFrom([x, y]);
dac39588 178 }
a6abf094 179
6808d7a1 180 getAllValidMoves() {
dac39588
BA
181 let moves = super.getAllValidMoves();
182 const color = this.turn;
6808d7a1
BA
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 );
dac39588
BA
187 return this.filterValid(moves);
188 }
a6abf094 189
6808d7a1
BA
190 atLeastOneMove() {
191 if (!super.atLeastOneMove()) {
dac39588 192 // Search one reserve move
6808d7a1 193 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588 194 let moves = this.filterValid(
6808d7a1
BA
195 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
196 );
197 if (moves.length > 0) return true;
dac39588
BA
198 }
199 return false;
200 }
201 return true;
202 }
a6abf094 203
6808d7a1 204 updateVariables(move) {
dac39588 205 super.updateVariables(move);
6808d7a1 206 if (move.vanish.length == 2 && move.appear.length == 2) return; //skip castle
dac39588 207 const color = move.appear[0].c;
6808d7a1 208 if (move.vanish.length == 0) {
dac39588
BA
209 this.reserve[color][move.appear[0].p]--;
210 return;
211 }
212 move.movePromoted = this.promoted[move.start.x][move.start.y];
6808d7a1 213 move.capturePromoted = this.promoted[move.end.x][move.end.y];
dac39588 214 this.promoted[move.start.x][move.start.y] = false;
6808d7a1
BA
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]++;
dac39588 220 }
1221ac47 221
6808d7a1 222 unupdateVariables(move) {
dac39588 223 super.unupdateVariables(move);
6808d7a1 224 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 225 const color = this.turn;
6808d7a1 226 if (move.vanish.length == 0) {
dac39588
BA
227 this.reserve[color][move.appear[0].p]++;
228 return;
229 }
6808d7a1 230 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
dac39588 231 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
6808d7a1
BA
232 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
233 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
dac39588 234 }
a6abf094 235
6808d7a1
BA
236 static get SEARCH_DEPTH() {
237 return 2;
78d64531 238 }
a6abf094 239
6808d7a1 240 evalPosition() {
dac39588
BA
241 let evaluation = super.evalPosition();
242 // Add reserves:
6808d7a1 243 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588
BA
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 }
6752407b 250
6808d7a1
BA
251 getNotation(move) {
252 if (move.vanish.length > 0) return super.getNotation(move);
dac39588
BA
253 // Rebirth:
254 const piece =
6808d7a1 255 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
dac39588
BA
256 return piece + "@" + V.CoordsToSquare(move.end);
257 }
6808d7a1 258};