Flip knights for variants with knightriders (waiting for a better image)
[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 static IsGoodFen(fen) {
6 if (!ChessRules.IsGoodFen(fen)) return false;
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
12 if (!fenParsed.promoted) return false;
13 if (fenParsed.promoted == "-") return true; //no promoted piece on board
14 const squares = fenParsed.promoted.split(",");
15 for (let square of squares) {
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 }
22
23 static ParseFen(fen) {
24 const fenParts = fen.split(" ");
25 return Object.assign(
26 ChessRules.ParseFen(fen),
27 {
28 reserve: fenParts[5],
29 promoted: fenParts[6]
30 }
31 );
32 }
33
34 static GenRandInitFen(randomness) {
35 return ChessRules.GenRandInitFen(randomness) + " 0000000000 -";
36 }
37
38 getFen() {
39 return (
40 super.getFen() + " " +
41 this.getReserveFen() + " " +
42 this.getPromotedFen()
43 );
44 }
45
46 getFenForRepeat() {
47 return (
48 super.getFenForRepeat() + "_" +
49 this.getReserveFen() + "_" +
50 this.getPromotedFen()
51 );
52 }
53
54 getReserveFen() {
55 let counts = new Array(10);
56 for (
57 let i = 0;
58 i < V.PIECES.length - 1;
59 i++ //-1: no king reserve
60 ) {
61 counts[i] = this.reserve["w"][V.PIECES[i]];
62 counts[5 + i] = this.reserve["b"][V.PIECES[i]];
63 }
64 return counts.join("");
65 }
66
67 getPromotedFen() {
68 let res = "";
69 for (let i = 0; i < V.size.x; i++) {
70 for (let j = 0; j < V.size.y; j++) {
71 if (this.promoted[i][j]) res += V.CoordsToSquare({ x: i, y: j }) + ",";
72 }
73 }
74 // Remove last comma:
75 if (res.length > 0) res = res.slice(0, -1);
76 else res = "-";
77 return res;
78 }
79
80 setOtherVariables(fen) {
81 super.setOtherVariables(fen);
82 // Also init reserves (used by the interface to show landable pieces)
83 const reserve =
84 V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
85 this.reserve = {
86 w: {
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]
92 },
93 b: {
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]
99 }
100 };
101 this.promoted = ArrayFun.init(V.size.x, V.size.y, false);
102 if (fenParsed.promoted != "-") {
103 for (let square of fenParsed.promoted.split(",")) {
104 const coords = V.SquareToCoords(square);
105 this.promoted[coords.x][coords.y] = true;
106 }
107 }
108 }
109
110 getColor(i, j) {
111 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
112 return this.board[i][j].charAt(0);
113 }
114
115 getPiece(i, j) {
116 if (i >= V.size.x) return V.RESERVE_PIECES[j];
117 return this.board[i][j].charAt(1);
118 }
119
120 // Used by the interface:
121 getReservePpath(index, color) {
122 return color + V.RESERVE_PIECES[index];
123 }
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 // }
132
133 // Ordering on reserve pieces
134 static get RESERVE_PIECES() {
135 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
136 }
137
138 getReserveMoves([x, y]) {
139 const color = this.turn;
140 const p = V.RESERVE_PIECES[y];
141 if (this.reserve[color][p] == 0) return [];
142 let moves = [];
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) {
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: [],
157 start: { x: x, y: y }, //a bit artificial...
158 end: { x: i, y: j }
159 });
160 moves.push(mv);
161 }
162 }
163 }
164 return moves;
165 }
166
167 getPotentialMovesFrom([x, y]) {
168 if (x >= V.size.x) {
169 // Reserves, outside of board: x == sizeX(+1)
170 return this.getReserveMoves([x, y]);
171 }
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()) {
189 // Search one reserve move
190 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
191 let 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 return true;
199 }
200
201 postPlay(move) {
202 super.postPlay(move);
203 // Skip castle:
204 if (move.vanish.length == 2 && move.appear.length == 2) return;
205 const color = move.appear[0].c;
206 if (move.vanish.length == 0) {
207 this.reserve[color][move.appear[0].p]--;
208 return;
209 }
210 move.movePromoted = this.promoted[move.start.x][move.start.y];
211 move.capturePromoted = this.promoted[move.end.x][move.end.y];
212 this.promoted[move.start.x][move.start.y] = false;
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]++;
218 }
219
220 postUndo(move) {
221 super.postUndo(move);
222 if (move.vanish.length == 2 && move.appear.length == 2) return;
223 const color = this.turn;
224 if (move.vanish.length == 0) {
225 this.reserve[color][move.appear[0].p]++;
226 return;
227 }
228 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
229 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
230 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
231 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
232 }
233
234 static get SEARCH_DEPTH() {
235 return 2;
236 }
237
238 evalPosition() {
239 let evaluation = super.evalPosition();
240 // Add reserves:
241 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
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 }
248
249 getNotation(move) {
250 if (move.vanish.length > 0) return super.getNotation(move);
251 // Rebirth:
252 const piece =
253 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
254 return piece + "@" + V.CoordsToSquare(move.end);
255 }
256 };