Fix Chakart when landing piece on invisible queen + reserve pieces ordering
[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() {
0e001022 176 let moves = super.getAllPotentialMoves();
dac39588 177 const color = this.turn;
0e001022 178 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
6808d7a1
BA
179 moves = moves.concat(
180 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
181 );
0e001022 182 }
dac39588
BA
183 return this.filterValid(moves);
184 }
a6abf094 185
6808d7a1
BA
186 atLeastOneMove() {
187 if (!super.atLeastOneMove()) {
dac39588 188 // Search one reserve move
6808d7a1 189 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588 190 let moves = this.filterValid(
6808d7a1
BA
191 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
192 );
193 if (moves.length > 0) return true;
dac39588
BA
194 }
195 return false;
196 }
197 return true;
198 }
a6abf094 199
3a2a7b5f
BA
200 postPlay(move) {
201 super.postPlay(move);
2c5d7b20
BA
202 // Skip castle:
203 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 204 const color = move.appear[0].c;
6808d7a1 205 if (move.vanish.length == 0) {
dac39588
BA
206 this.reserve[color][move.appear[0].p]--;
207 return;
208 }
209 move.movePromoted = this.promoted[move.start.x][move.start.y];
6808d7a1 210 move.capturePromoted = this.promoted[move.end.x][move.end.y];
dac39588 211 this.promoted[move.start.x][move.start.y] = false;
6808d7a1
BA
212 this.promoted[move.end.x][move.end.y] =
213 move.movePromoted ||
214 (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN);
215 if (move.capturePromoted) this.reserve[color][V.PAWN]++;
216 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]++;
dac39588 217 }
1221ac47 218
3a2a7b5f
BA
219 postUndo(move) {
220 super.postUndo(move);
6808d7a1 221 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 222 const color = this.turn;
6808d7a1 223 if (move.vanish.length == 0) {
dac39588
BA
224 this.reserve[color][move.appear[0].p]++;
225 return;
226 }
6808d7a1 227 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
dac39588 228 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
6808d7a1
BA
229 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
230 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
dac39588 231 }
a6abf094 232
6808d7a1
BA
233 static get SEARCH_DEPTH() {
234 return 2;
78d64531 235 }
a6abf094 236
6808d7a1 237 evalPosition() {
dac39588
BA
238 let evaluation = super.evalPosition();
239 // Add reserves:
6808d7a1 240 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588
BA
241 const p = V.RESERVE_PIECES[i];
242 evaluation += this.reserve["w"][p] * V.VALUES[p];
243 evaluation -= this.reserve["b"][p] * V.VALUES[p];
244 }
245 return evaluation;
246 }
6752407b 247
6808d7a1
BA
248 getNotation(move) {
249 if (move.vanish.length > 0) return super.getNotation(move);
dac39588
BA
250 // Rebirth:
251 const piece =
6808d7a1 252 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
dac39588
BA
253 return piece + "@" + V.CoordsToSquare(move.end);
254 }
6808d7a1 255};