Improve Bario rules description about castling
[vchess.git] / client / src / variants / Crazyhouse.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3
4 export class CrazyhouseRules extends ChessRules {
5
6 static IsGoodFen(fen) {
7 if (!ChessRules.IsGoodFen(fen)) return false;
8 const fenParsed = V.ParseFen(fen);
9 // 5) Check reserves
10 if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{10,10}$/))
11 return false;
12 // 6) Check promoted array
13 if (!fenParsed.promoted) return false;
14 if (fenParsed.promoted == "-") return true; //no promoted piece on board
15 const squares = fenParsed.promoted.split(",");
16 for (let square of squares) {
17 const c = V.SquareToCoords(square);
18 if (c.y < 0 || c.y > V.size.y || isNaN(c.x) || c.x < 0 || c.x > V.size.x)
19 return false;
20 }
21 return true;
22 }
23
24 static ParseFen(fen) {
25 const fenParts = fen.split(" ");
26 return Object.assign(
27 ChessRules.ParseFen(fen),
28 {
29 reserve: fenParts[5],
30 promoted: fenParts[6]
31 }
32 );
33 }
34
35 static GenRandInitFen(randomness) {
36 return ChessRules.GenRandInitFen(randomness) + " 0000000000 -";
37 }
38
39 getFen() {
40 return (
41 super.getFen() + " " +
42 this.getReserveFen() + " " +
43 this.getPromotedFen()
44 );
45 }
46
47 getFenForRepeat() {
48 return (
49 super.getFenForRepeat() + "_" +
50 this.getReserveFen() + "_" +
51 this.getPromotedFen()
52 );
53 }
54
55 getReserveFen() {
56 let counts = new Array(10);
57 for (
58 let i = 0;
59 i < V.PIECES.length - 1;
60 i++ //-1: no king reserve
61 ) {
62 counts[i] = this.reserve["w"][V.PIECES[i]];
63 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
64 }
65 return counts.join("");
66 }
67
68 getPromotedFen() {
69 let res = "";
70 for (let i = 0; i < V.size.x; i++) {
71 for (let j = 0; j < V.size.y; j++) {
72 if (this.promoted[i][j]) res += V.CoordsToSquare({ x: i, y: j }) + ",";
73 }
74 }
75 // Remove last comma:
76 if (res.length > 0) res = res.slice(0, -1);
77 else res = "-";
78 return res;
79 }
80
81 setOtherVariables(fen) {
82 super.setOtherVariables(fen);
83 const fenParsed = V.ParseFen(fen);
84 // Also init reserves (used by the interface to show landable pieces)
85 const reserve = fenParsed.reserve.split("").map(x => parseInt(x, 10));
86 this.reserve = {
87 w: {
88 [V.PAWN]: reserve[0],
89 [V.ROOK]: reserve[1],
90 [V.KNIGHT]: reserve[2],
91 [V.BISHOP]: reserve[3],
92 [V.QUEEN]: reserve[4]
93 },
94 b: {
95 [V.PAWN]: reserve[5],
96 [V.ROOK]: reserve[6],
97 [V.KNIGHT]: reserve[7],
98 [V.BISHOP]: reserve[8],
99 [V.QUEEN]: reserve[9]
100 }
101 };
102 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
103 if (fenParsed.promoted != "-") {
104 for (let square of fenParsed.promoted.split(",")) {
105 const coords = V.SquareToCoords(square);
106 this.promoted[coords.x][coords.y] = true;
107 }
108 }
109 }
110
111 getColor(i, j) {
112 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
113 return this.board[i][j].charAt(0);
114 }
115
116 getPiece(i, j) {
117 if (i >= V.size.x) return V.RESERVE_PIECES[j];
118 return this.board[i][j].charAt(1);
119 }
120
121 // Used by the interface:
122 getReservePpath(index, color) {
123 return color + V.RESERVE_PIECES[index];
124 }
125 // // Version if some day I have pieces with numbers printed on it:
126 // getReservePpath(index, color) {
127 // return (
128 // "Crazyhouse/" +
129 // color + V.RESERVE_PIECES[index] +
130 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
131 // );
132 // }
133
134 // Ordering on reserve pieces
135 static get RESERVE_PIECES() {
136 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
137 }
138
139 getReserveMoves([x, y]) {
140 const color = this.turn;
141 const p = V.RESERVE_PIECES[y];
142 if (this.reserve[color][p] == 0) return [];
143 let moves = [];
144 const pawnShift = p == V.PAWN ? 1 : 0;
145 for (let i = pawnShift; i < V.size.x - pawnShift; i++) {
146 for (let j = 0; j < V.size.y; j++) {
147 if (this.board[i][j] == V.EMPTY) {
148 let mv = new Move({
149 appear: [
150 new PiPo({
151 x: i,
152 y: j,
153 c: color,
154 p: p
155 })
156 ],
157 vanish: [],
158 start: { x: x, y: y }, //a bit artificial...
159 end: { x: i, y: j }
160 });
161 moves.push(mv);
162 }
163 }
164 }
165 return moves;
166 }
167
168 getPotentialMovesFrom([x, y]) {
169 if (x >= V.size.x)
170 // Reserves, outside of board: x == sizeX(+1)
171 return this.getReserveMoves([x, y]);
172 // Standard moves
173 return super.getPotentialMovesFrom([x, y]);
174 }
175
176 getAllValidMoves() {
177 let moves = super.getAllPotentialMoves();
178 const color = this.turn;
179 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
180 moves = moves.concat(
181 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
182 );
183 }
184 return this.filterValid(moves);
185 }
186
187 atLeastOneMove() {
188 if (super.atLeastOneMove()) return true;
189 // Search one reserve move
190 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
191 const moves = this.filterValid(
192 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
193 );
194 if (moves.length > 0) return true;
195 }
196 return false;
197 }
198
199 postPlay(move) {
200 super.postPlay(move);
201 // Skip castle:
202 if (move.vanish.length == 2 && move.appear.length == 2) return;
203 const color = move.appear[0].c;
204 if (move.vanish.length == 0) {
205 this.reserve[color][move.appear[0].p]--;
206 return;
207 }
208 move.movePromoted = this.promoted[move.start.x][move.start.y];
209 move.capturePromoted = this.promoted[move.end.x][move.end.y];
210 this.promoted[move.start.x][move.start.y] = false;
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]++;
216 }
217
218 postUndo(move) {
219 super.postUndo(move);
220 if (move.vanish.length == 2 && move.appear.length == 2) return;
221 const color = this.turn;
222 if (move.vanish.length == 0) {
223 this.reserve[color][move.appear[0].p]++;
224 return;
225 }
226 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
227 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
228 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
229 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
230 }
231
232 static get SEARCH_DEPTH() {
233 return 2;
234 }
235
236 evalPosition() {
237 let evaluation = super.evalPosition();
238 // Add reserves:
239 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
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 }
246
247 getNotation(move) {
248 if (move.vanish.length > 0) return super.getNotation(move);
249 // Rebirth:
250 const piece =
251 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
252 return piece + "@" + V.CoordsToSquare(move.end);
253 }
254
255 };