Fix Parachute, allow 3 squares in Ball variant
[vchess.git] / client / src / variants / Parachute.js
CommitLineData
0d5335de
BA
1import { ChessRules, PiPo, Move } from "@/base_rules";
2
3export class ParachuteRules extends ChessRules {
4 static get HasFlags() {
5 return false;
6 }
7
8 static IsGoodFen(fen) {
9 if (!ChessRules.IsGoodFen(fen)) return false;
10 const fenParsed = V.ParseFen(fen);
11 // 5) Check reserves
12 if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{12,12}$/))
13 return false;
14 return true;
15 }
16
94b9fcef
BA
17 static IsGoodPosition(position) {
18 if (position.length == 0) return false;
19 const rows = position.split("/");
20 if (rows.length != V.size.x) return false;
21 for (let row of rows) {
22 let sumElts = 0;
23 for (let i = 0; i < row.length; i++) {
24 if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
25 else {
26 const num = parseInt(row[i]);
27 if (isNaN(num)) return false;
28 sumElts += num;
29 }
30 }
31 if (sumElts != V.size.y) return false;
32 }
33 return true;
34 }
35
0d5335de
BA
36 static ParseFen(fen) {
37 const fenParts = fen.split(" ");
38 return Object.assign(
39 ChessRules.ParseFen(fen),
40 { reserve: fenParts[4] }
41 );
42 }
43
44 static GenRandInitFen() {
45 // ChessRules.PIECES order is P, R, N, B, Q, K:
46 return "8/8/8/8/8/8/8/8 w 0 - 822211822211";
47 }
48
49 getFen() {
50 return super.getFen() + " " + this.getReserveFen();
51 }
52
53 getFenForRepeat() {
54 return super.getFenForRepeat() + "_" + this.getReserveFen();
55 }
56
57 getReserveFen() {
58 let counts = new Array(12);
59 for (let i = 0; i < V.PIECES.length; i++) {
60 counts[i] = this.reserve["w"][V.PIECES[i]];
61 counts[6 + i] = this.reserve["b"][V.PIECES[i]];
62 }
63 return counts.join("");
64 }
65
66 setOtherVariables(fen) {
67 super.setOtherVariables(fen);
68 const fenParsed = V.ParseFen(fen);
69 // Also init reserves (used by the interface to show landable pieces)
70 this.reserve = {
71 w: {
72 [V.PAWN]: parseInt(fenParsed.reserve[0]),
73 [V.ROOK]: parseInt(fenParsed.reserve[1]),
74 [V.KNIGHT]: parseInt(fenParsed.reserve[2]),
75 [V.BISHOP]: parseInt(fenParsed.reserve[3]),
76 [V.QUEEN]: parseInt(fenParsed.reserve[4]),
77 [V.KING]: parseInt(fenParsed.reserve[5])
78 },
79 b: {
80 [V.PAWN]: parseInt(fenParsed.reserve[6]),
81 [V.ROOK]: parseInt(fenParsed.reserve[7]),
82 [V.KNIGHT]: parseInt(fenParsed.reserve[8]),
83 [V.BISHOP]: parseInt(fenParsed.reserve[9]),
84 [V.QUEEN]: parseInt(fenParsed.reserve[10]),
85 [V.KING]: parseInt(fenParsed.reserve[11])
86 }
87 };
88 }
89
90 getColor(i, j) {
91 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
92 return this.board[i][j].charAt(0);
93 }
94
95 getPiece(i, j) {
96 if (i >= V.size.x) return V.RESERVE_PIECES[j];
97 return this.board[i][j].charAt(1);
98 }
99
100 // Used by the interface:
101 getReservePpath(index, color) {
102 return color + V.RESERVE_PIECES[index];
103 }
104
105 // Ordering on reserve pieces (matching V.PIECES order)
106 static get RESERVE_PIECES() {
107 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING];
108 }
109
110 getReserveMoves([x, y]) {
111 const color = this.turn;
112 const oppCol = V.GetOppCol(color);
113 const p = V.RESERVE_PIECES[y];
114 if (this.reserve[color][p] == 0) return [];
115 let moves = [];
116 let boundary =
117 p == V.PAWN
118 // Pawns can land only on 4 first ranks:
119 ? (color == 'w' ? [4, 8] : [0, 4])
120 : [0, 8];
121 for (let i = boundary[0]; i < boundary[1]; i++) {
122 for (let j = 0; j < 8; j++) {
123 if (this.board[i][j] == V.EMPTY) {
124 let mv = new Move({
125 appear: [
126 new PiPo({
127 x: i,
128 y: j,
129 c: color,
130 p: p
131 })
132 ],
133 vanish: [],
134 start: { x: x, y: y }, //a bit artificial...
135 end: { x: i, y: j }
136 });
137 this.play(mv);
138 // Landing with check is forbidden:
139 if (!this.underCheck(oppCol)) moves.push(mv);
140 this.undo(mv);
141 }
142 }
143 }
144 return moves;
145 }
146
147 getPotentialMovesFrom([x, y]) {
148 let moves =
149 x >= 8
150 ? this.getReserveMoves([x, y])
151 : super.getPotentialMovesFrom([x, y]);
152 // Forbid captures if king not landed yet:
153 if (x < 8 && moves.length > 0 && this.kingPos[moves[0].appear[0].c][0] < 0)
154 moves = moves.filter(m => m.vanish.length == 1);
155 return moves;
156 }
157
158 getAllValidMoves() {
159 let moves = super.getAllValidMoves();
160 const color = this.turn;
161 for (let i = 0; i < V.RESERVE_PIECES.length; i++)
162 moves = moves.concat(
163 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
164 );
165 return this.filterValid(moves);
166 }
167
168 isAttacked(sq, color) {
169 // While the king hasn't landed, nothing is attacked:
170 if (this.kingPos[color][0] < 0) return false;
171 return super.isAttacked(sq, color);
172 }
173
174 atLeastOneMove() {
175 if (!super.atLeastOneMove()) {
176 // Search one reserve move
177 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
178 let moves = this.filterValid(
179 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
180 );
181 if (moves.length > 0) return true;
182 }
183 return false;
184 }
185 return true;
186 }
187
94b9fcef
BA
188 underCheck(color) {
189 if (this.kingPos[color][0] < 0)
190 // A king outside the board isn't under check
191 return false;
192 return this.isAttacked(this.kingPos[color], V.GetOppCol(color));
193 }
194
0d5335de
BA
195 prePlay(move) {
196 super.prePlay(move);
197 if (move.vanish.length == 0) this.reserve[this.turn][move.appear[0].p]--;
198 }
199
200 postUndo(move) {
201 if (move.vanish.length == 0) this.reserve[this.turn][move.appear[0].p]++;
202 // (Potentially) Reset king position
203 if (move.appear[0].p == V.KING) {
204 const c = move.appear[0].c;
205 if (move.vanish.length == 0)
206 // Landing king
207 this.kingPos[c] = [-1, -1];
208 else
209 // King movement
210 this.kingPos[c] = [move.start.x, move.start.y];
211 }
212 }
213
214 static get SEARCH_DEPTH() {
215 return 1;
216 }
217
218 evalPosition() {
219 let evaluation = super.evalPosition();
220 // Add reserves:
221 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
222 const p = V.RESERVE_PIECES[i];
223 evaluation += this.reserve["w"][p] * V.VALUES[p];
224 evaluation -= this.reserve["b"][p] * V.VALUES[p];
225 }
226 return evaluation;
227 }
228
229 getNotation(move) {
230 if (move.vanish.length > 0) return super.getNotation(move);
231 // Parachutage:
232 const piece =
233 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
234 return piece + "@" + V.CoordsToSquare(move.end);
235 }
236};