Almost added TitanChess + EvolutionChess
[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 // Also init reserves (used by the interface to show landable pieces)
84 const reserve =
85 V.ParseFen(fen).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 }
173 // Standard moves
174 return super.getPotentialMovesFrom([x, y]);
175 }
176
177 getAllValidMoves() {
178 let moves = super.getAllPotentialMoves();
179 const color = this.turn;
180 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
181 moves = moves.concat(
182 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
183 );
184 }
185 return this.filterValid(moves);
186 }
187
188 atLeastOneMove() {
189 if (!super.atLeastOneMove()) {
190 // Search one reserve move
191 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
192 let moves = this.filterValid(
193 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
194 );
195 if (moves.length > 0) return true;
196 }
197 return false;
198 }
199 return true;
200 }
201
202 postPlay(move) {
203 super.postPlay(move);
204 // Skip castle:
205 if (move.vanish.length == 2 && move.appear.length == 2) return;
206 const color = move.appear[0].c;
207 if (move.vanish.length == 0) {
208 this.reserve[color][move.appear[0].p]--;
209 return;
210 }
211 move.movePromoted = this.promoted[move.start.x][move.start.y];
212 move.capturePromoted = this.promoted[move.end.x][move.end.y];
213 this.promoted[move.start.x][move.start.y] = false;
214 this.promoted[move.end.x][move.end.y] =
215 move.movePromoted ||
216 (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN);
217 if (move.capturePromoted) this.reserve[color][V.PAWN]++;
218 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]++;
219 }
220
221 postUndo(move) {
222 super.postUndo(move);
223 if (move.vanish.length == 2 && move.appear.length == 2) return;
224 const color = this.turn;
225 if (move.vanish.length == 0) {
226 this.reserve[color][move.appear[0].p]++;
227 return;
228 }
229 if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true;
230 this.promoted[move.end.x][move.end.y] = move.capturePromoted;
231 if (move.capturePromoted) this.reserve[color][V.PAWN]--;
232 else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--;
233 }
234
235 static get SEARCH_DEPTH() {
236 return 2;
237 }
238
239 evalPosition() {
240 let evaluation = super.evalPosition();
241 // Add reserves:
242 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
243 const p = V.RESERVE_PIECES[i];
244 evaluation += this.reserve["w"][p] * V.VALUES[p];
245 evaluation -= this.reserve["b"][p] * V.VALUES[p];
246 }
247 return evaluation;
248 }
249
250 getNotation(move) {
251 if (move.vanish.length > 0) return super.getNotation(move);
252 // Rebirth:
253 const piece =
254 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
255 return piece + "@" + V.CoordsToSquare(move.end);
256 }
257
258 };