Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[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
7ba4a5bc
BA
34 static GenRandInitFen(randomness) {
35 return ChessRules.GenRandInitFen(randomness) + " 0000000000 -";
dac39588 36 }
2d7194bd 37
6808d7a1
BA
38 getFen() {
39 return (
90e814b6
BA
40 super.getFen() + " " +
41 this.getReserveFen() + " " +
42 this.getPromotedFen()
6808d7a1 43 );
dac39588 44 }
2d7194bd 45
f9c36b2d
BA
46 getFenForRepeat() {
47 return (
90e814b6 48 super.getFenForRepeat() + "_" +
f9c36b2d
BA
49 this.getReserveFen() + "_" +
50 this.getPromotedFen()
51 );
52 }
53
6808d7a1 54 getReserveFen() {
dac39588 55 let counts = new Array(10);
6808d7a1
BA
56 for (
57 let i = 0;
58 i < V.PIECES.length - 1;
59 i++ //-1: no king reserve
60 ) {
dac39588 61 counts[i] = this.reserve["w"][V.PIECES[i]];
6808d7a1 62 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
dac39588
BA
63 }
64 return counts.join("");
65 }
2d7194bd 66
6808d7a1 67 getPromotedFen() {
dac39588 68 let res = "";
6808d7a1
BA
69 for (let i = 0; i < V.size.x; i++) {
70 for (let j = 0; j < V.size.y; j++) {
c292ebb2 71 if (this.promoted[i][j]) res += V.CoordsToSquare({ x: i, y: j }) + ",";
dac39588
BA
72 }
73 }
c292ebb2 74 // Remove last comma:
6808d7a1 75 if (res.length > 0) res = res.slice(0, -1);
6808d7a1 76 else res = "-";
dac39588
BA
77 return res;
78 }
2d7194bd 79
6808d7a1 80 setOtherVariables(fen) {
dac39588
BA
81 super.setOtherVariables(fen);
82 const fenParsed = V.ParseFen(fen);
83 // Also init reserves (used by the interface to show landable pieces)
6808d7a1
BA
84 this.reserve = {
85 w: {
dac39588
BA
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]),
6808d7a1 90 [V.QUEEN]: parseInt(fenParsed.reserve[4])
dac39588 91 },
6808d7a1 92 b: {
dac39588
BA
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]),
6808d7a1 97 [V.QUEEN]: parseInt(fenParsed.reserve[9])
dac39588
BA
98 }
99 };
100 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
6808d7a1
BA
101 if (fenParsed.promoted != "-") {
102 for (let square of fenParsed.promoted.split(",")) {
c292ebb2
BA
103 const coords = V.SquareToCoords(square);
104 this.promoted[coords.x][coords.y] = true;
dac39588
BA
105 }
106 }
107 }
5c42c64e 108
6808d7a1
BA
109 getColor(i, j) {
110 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
dac39588
BA
111 return this.board[i][j].charAt(0);
112 }
2d7194bd 113
6808d7a1
BA
114 getPiece(i, j) {
115 if (i >= V.size.x) return V.RESERVE_PIECES[j];
dac39588
BA
116 return this.board[i][j].charAt(1);
117 }
a6abf094 118
dac39588 119 // Used by the interface:
241bf8f2 120 getReservePpath(index, color) {
dac39588
BA
121 return color + V.RESERVE_PIECES[index];
122 }
9d4a0218
BA
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// }
a6abf094 131
dac39588 132 // Ordering on reserve pieces
6808d7a1
BA
133 static get RESERVE_PIECES() {
134 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
dac39588 135 }
1221ac47 136
6808d7a1 137 getReserveMoves([x, y]) {
dac39588
BA
138 const color = this.turn;
139 const p = V.RESERVE_PIECES[y];
6808d7a1 140 if (this.reserve[color][p] == 0) return [];
dac39588 141 let moves = [];
6808d7a1
BA
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) {
dac39588
BA
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: [],
6808d7a1
BA
156 start: { x: x, y: y }, //a bit artificial...
157 end: { x: i, y: j }
dac39588
BA
158 });
159 moves.push(mv);
160 }
161 }
162 }
163 return moves;
164 }
a6abf094 165
6808d7a1
BA
166 getPotentialMovesFrom([x, y]) {
167 if (x >= V.size.x) {
dac39588 168 // Reserves, outside of board: x == sizeX(+1)
6808d7a1 169 return this.getReserveMoves([x, y]);
dac39588
BA
170 }
171 // Standard moves
6808d7a1 172 return super.getPotentialMovesFrom([x, y]);
dac39588 173 }
a6abf094 174
6808d7a1 175 getAllValidMoves() {
dac39588
BA
176 let moves = super.getAllValidMoves();
177 const color = this.turn;
6808d7a1
BA
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 );
dac39588
BA
182 return this.filterValid(moves);
183 }
a6abf094 184
6808d7a1
BA
185 atLeastOneMove() {
186 if (!super.atLeastOneMove()) {
dac39588 187 // Search one reserve move
6808d7a1 188 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588 189 let moves = this.filterValid(
6808d7a1
BA
190 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
191 );
192 if (moves.length > 0) return true;
dac39588
BA
193 }
194 return false;
195 }
196 return true;
197 }
a6abf094 198
3a2a7b5f
BA
199 postPlay(move) {
200 super.postPlay(move);
2c5d7b20
BA
201 // Skip castle:
202 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 203 const color = move.appear[0].c;
6808d7a1 204 if (move.vanish.length == 0) {
dac39588
BA
205 this.reserve[color][move.appear[0].p]--;
206 return;
207 }
208 move.movePromoted = this.promoted[move.start.x][move.start.y];
6808d7a1 209 move.capturePromoted = this.promoted[move.end.x][move.end.y];
dac39588 210 this.promoted[move.start.x][move.start.y] = false;
6808d7a1
BA
211 this.promoted[move.end.x][move.end.y] =
212 move.movePromoted ||
213 (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN);
214 if (move.capturePromoted) this.reserve[color][V.PAWN]++;
215 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]++;
dac39588 216 }
1221ac47 217
3a2a7b5f
BA
218 postUndo(move) {
219 super.postUndo(move);
6808d7a1 220 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 221 const color = this.turn;
6808d7a1 222 if (move.vanish.length == 0) {
dac39588
BA
223 this.reserve[color][move.appear[0].p]++;
224 return;
225 }
6808d7a1 226 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
dac39588 227 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
6808d7a1
BA
228 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
229 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
dac39588 230 }
a6abf094 231
6808d7a1
BA
232 static get SEARCH_DEPTH() {
233 return 2;
78d64531 234 }
a6abf094 235
6808d7a1 236 evalPosition() {
dac39588
BA
237 let evaluation = super.evalPosition();
238 // Add reserves:
6808d7a1 239 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588
BA
240 const p = V.RESERVE_PIECES[i];
241 evaluation += this.reserve["w"][p] * V.VALUES[p];
242 evaluation -= this.reserve["b"][p] * V.VALUES[p];
243 }
244 return evaluation;
245 }
6752407b 246
6808d7a1
BA
247 getNotation(move) {
248 if (move.vanish.length > 0) return super.getNotation(move);
dac39588
BA
249 // Rebirth:
250 const piece =
6808d7a1 251 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
dac39588
BA
252 return piece + "@" + V.CoordsToSquare(move.end);
253 }
6808d7a1 254};