Fix Koopa promotions with captures, and Balakhlava: pawns move forward
[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 81 super.setOtherVariables(fen);
dac39588 82 // Also init reserves (used by the interface to show landable pieces)
e50a8025
BA
83 const reserve =
84 V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
6808d7a1
BA
85 this.reserve = {
86 w: {
e50a8025
BA
87 [V.PAWN]: reserve[0],
88 [V.ROOK]: reserve[1],
89 [V.KNIGHT]: reserve[2],
90 [V.BISHOP]: reserve[3],
91 [V.QUEEN]: reserve[4]
dac39588 92 },
6808d7a1 93 b: {
e50a8025
BA
94 [V.PAWN]: reserve[5],
95 [V.ROOK]: reserve[6],
96 [V.KNIGHT]: reserve[7],
97 [V.BISHOP]: reserve[8],
98 [V.QUEEN]: reserve[9]
dac39588
BA
99 }
100 };
101 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
6808d7a1
BA
102 if (fenParsed.promoted != "-") {
103 for (let square of fenParsed.promoted.split(",")) {
c292ebb2
BA
104 const coords = V.SquareToCoords(square);
105 this.promoted[coords.x][coords.y] = true;
dac39588
BA
106 }
107 }
108 }
5c42c64e 109
6808d7a1
BA
110 getColor(i, j) {
111 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
dac39588
BA
112 return this.board[i][j].charAt(0);
113 }
2d7194bd 114
6808d7a1
BA
115 getPiece(i, j) {
116 if (i >= V.size.x) return V.RESERVE_PIECES[j];
dac39588
BA
117 return this.board[i][j].charAt(1);
118 }
a6abf094 119
dac39588 120 // Used by the interface:
241bf8f2 121 getReservePpath(index, color) {
dac39588
BA
122 return color + V.RESERVE_PIECES[index];
123 }
9d4a0218
BA
124// // Version if some day I have pieces with numbers printed on it:
125// getReservePpath(index, color) {
126// return (
127// "Crazyhouse/" +
128// color + V.RESERVE_PIECES[index] +
129// "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
130// );
131// }
a6abf094 132
dac39588 133 // Ordering on reserve pieces
6808d7a1
BA
134 static get RESERVE_PIECES() {
135 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
dac39588 136 }
1221ac47 137
6808d7a1 138 getReserveMoves([x, y]) {
dac39588
BA
139 const color = this.turn;
140 const p = V.RESERVE_PIECES[y];
6808d7a1 141 if (this.reserve[color][p] == 0) return [];
dac39588 142 let moves = [];
6808d7a1
BA
143 const pawnShift = p == V.PAWN ? 1 : 0;
144 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
145 for (let j = 0; j < V.size.y; j++) {
146 if (this.board[i][j] == V.EMPTY) {
dac39588
BA
147 let mv = new Move({
148 appear: [
149 new PiPo({
150 x: i,
151 y: j,
152 c: color,
153 p: p
154 })
155 ],
156 vanish: [],
6808d7a1
BA
157 start: { x: x, y: y }, //a bit artificial...
158 end: { x: i, y: j }
dac39588
BA
159 });
160 moves.push(mv);
161 }
162 }
163 }
164 return moves;
165 }
a6abf094 166
6808d7a1
BA
167 getPotentialMovesFrom([x, y]) {
168 if (x >= V.size.x) {
dac39588 169 // Reserves, outside of board: x == sizeX(+1)
6808d7a1 170 return this.getReserveMoves([x, y]);
dac39588
BA
171 }
172 // Standard moves
6808d7a1 173 return super.getPotentialMovesFrom([x, y]);
dac39588 174 }
a6abf094 175
6808d7a1 176 getAllValidMoves() {
0e001022 177 let moves = super.getAllPotentialMoves();
dac39588 178 const color = this.turn;
0e001022 179 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
6808d7a1
BA
180 moves = moves.concat(
181 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
182 );
0e001022 183 }
dac39588
BA
184 return this.filterValid(moves);
185 }
a6abf094 186
6808d7a1
BA
187 atLeastOneMove() {
188 if (!super.atLeastOneMove()) {
dac39588 189 // Search one reserve move
6808d7a1 190 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588 191 let moves = this.filterValid(
6808d7a1
BA
192 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
193 );
194 if (moves.length > 0) return true;
dac39588
BA
195 }
196 return false;
197 }
198 return true;
199 }
a6abf094 200
3a2a7b5f
BA
201 postPlay(move) {
202 super.postPlay(move);
2c5d7b20
BA
203 // Skip castle:
204 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 205 const color = move.appear[0].c;
6808d7a1 206 if (move.vanish.length == 0) {
dac39588
BA
207 this.reserve[color][move.appear[0].p]--;
208 return;
209 }
210 move.movePromoted = this.promoted[move.start.x][move.start.y];
6808d7a1 211 move.capturePromoted = this.promoted[move.end.x][move.end.y];
dac39588 212 this.promoted[move.start.x][move.start.y] = false;
6808d7a1
BA
213 this.promoted[move.end.x][move.end.y] =
214 move.movePromoted ||
215 (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN);
216 if (move.capturePromoted) this.reserve[color][V.PAWN]++;
217 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]++;
dac39588 218 }
1221ac47 219
3a2a7b5f
BA
220 postUndo(move) {
221 super.postUndo(move);
6808d7a1 222 if (move.vanish.length == 2 && move.appear.length == 2) return;
dac39588 223 const color = this.turn;
6808d7a1 224 if (move.vanish.length == 0) {
dac39588
BA
225 this.reserve[color][move.appear[0].p]++;
226 return;
227 }
6808d7a1 228 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
dac39588 229 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
6808d7a1
BA
230 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
231 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
dac39588 232 }
a6abf094 233
6808d7a1
BA
234 static get SEARCH_DEPTH() {
235 return 2;
78d64531 236 }
a6abf094 237
6808d7a1 238 evalPosition() {
dac39588
BA
239 let evaluation = super.evalPosition();
240 // Add reserves:
6808d7a1 241 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
dac39588
BA
242 const p = V.RESERVE_PIECES[i];
243 evaluation += this.reserve["w"][p] * V.VALUES[p];
244 evaluation -= this.reserve["b"][p] * V.VALUES[p];
245 }
246 return evaluation;
247 }
6752407b 248
6808d7a1
BA
249 getNotation(move) {
250 if (move.vanish.length > 0) return super.getNotation(move);
dac39588
BA
251 // Rebirth:
252 const piece =
6808d7a1 253 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
dac39588
BA
254 return piece + "@" + V.CoordsToSquare(move.end);
255 }
6808d7a1 256};