Add first draft of 'Victor Chess'
[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 {
e50a8025 26 const num = parseInt(row[i], 10);
94b9fcef
BA
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);
0d5335de 68 // Also init reserves (used by the interface to show landable pieces)
e50a8025
BA
69 const reserve =
70 V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
0d5335de
BA
71 this.reserve = {
72 w: {
e50a8025
BA
73 [V.PAWN]: reserve[0],
74 [V.ROOK]: reserve[1],
75 [V.KNIGHT]: reserve[2],
76 [V.BISHOP]: reserve[3],
77 [V.QUEEN]: reserve[4],
78 [V.KING]: reserve[5]
0d5335de
BA
79 },
80 b: {
e50a8025
BA
81 [V.PAWN]: reserve[6],
82 [V.ROOK]: reserve[7],
83 [V.KNIGHT]: reserve[8],
84 [V.BISHOP]: reserve[9],
85 [V.QUEEN]: reserve[10],
86 [V.KING]: reserve[11]
0d5335de
BA
87 }
88 };
89 }
90
91 getColor(i, j) {
92 if (i >= V.size.x) return i == V.size.x ? "w" : "b";
93 return this.board[i][j].charAt(0);
94 }
95
96 getPiece(i, j) {
97 if (i >= V.size.x) return V.RESERVE_PIECES[j];
98 return this.board[i][j].charAt(1);
99 }
100
101 // Used by the interface:
102 getReservePpath(index, color) {
103 return color + V.RESERVE_PIECES[index];
104 }
105
106 // Ordering on reserve pieces (matching V.PIECES order)
107 static get RESERVE_PIECES() {
108 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING];
109 }
110
111 getReserveMoves([x, y]) {
112 const color = this.turn;
113 const oppCol = V.GetOppCol(color);
114 const p = V.RESERVE_PIECES[y];
115 if (this.reserve[color][p] == 0) return [];
116 let moves = [];
117 let boundary =
118 p == V.PAWN
119 // Pawns can land only on 4 first ranks:
120 ? (color == 'w' ? [4, 8] : [0, 4])
121 : [0, 8];
122 for (let i = boundary[0]; i < boundary[1]; i++) {
123 for (let j = 0; j < 8; j++) {
124 if (this.board[i][j] == V.EMPTY) {
125 let mv = new Move({
126 appear: [
127 new PiPo({
128 x: i,
129 y: j,
130 c: color,
131 p: p
132 })
133 ],
134 vanish: [],
135 start: { x: x, y: y }, //a bit artificial...
136 end: { x: i, y: j }
137 });
138 this.play(mv);
139 // Landing with check is forbidden:
140 if (!this.underCheck(oppCol)) moves.push(mv);
141 this.undo(mv);
142 }
143 }
144 }
145 return moves;
146 }
147
148 getPotentialMovesFrom([x, y]) {
149 let moves =
150 x >= 8
151 ? this.getReserveMoves([x, y])
152 : super.getPotentialMovesFrom([x, y]);
153 // Forbid captures if king not landed yet:
154 if (x < 8 && moves.length > 0 && this.kingPos[moves[0].appear[0].c][0] < 0)
155 moves = moves.filter(m => m.vanish.length == 1);
156 return moves;
157 }
158
159 getAllValidMoves() {
160 let moves = super.getAllValidMoves();
161 const color = this.turn;
162 for (let i = 0; i < V.RESERVE_PIECES.length; i++)
163 moves = moves.concat(
164 this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i])
165 );
166 return this.filterValid(moves);
167 }
168
169 isAttacked(sq, color) {
170 // While the king hasn't landed, nothing is attacked:
171 if (this.kingPos[color][0] < 0) return false;
172 return super.isAttacked(sq, color);
173 }
174
175 atLeastOneMove() {
176 if (!super.atLeastOneMove()) {
177 // Search one reserve move
178 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
179 let moves = this.filterValid(
180 this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i])
181 );
182 if (moves.length > 0) return true;
183 }
184 return false;
185 }
186 return true;
187 }
188
94b9fcef
BA
189 underCheck(color) {
190 if (this.kingPos[color][0] < 0)
191 // A king outside the board isn't under check
192 return false;
193 return this.isAttacked(this.kingPos[color], V.GetOppCol(color));
194 }
195
0d5335de
BA
196 prePlay(move) {
197 super.prePlay(move);
198 if (move.vanish.length == 0) this.reserve[this.turn][move.appear[0].p]--;
199 }
200
201 postUndo(move) {
202 if (move.vanish.length == 0) this.reserve[this.turn][move.appear[0].p]++;
203 // (Potentially) Reset king position
204 if (move.appear[0].p == V.KING) {
205 const c = move.appear[0].c;
206 if (move.vanish.length == 0)
207 // Landing king
208 this.kingPos[c] = [-1, -1];
209 else
210 // King movement
211 this.kingPos[c] = [move.start.x, move.start.y];
212 }
213 }
214
215 static get SEARCH_DEPTH() {
216 return 1;
217 }
218
219 evalPosition() {
220 let evaluation = super.evalPosition();
221 // Add reserves:
222 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
223 const p = V.RESERVE_PIECES[i];
224 evaluation += this.reserve["w"][p] * V.VALUES[p];
225 evaluation -= this.reserve["b"][p] * V.VALUES[p];
226 }
227 return evaluation;
228 }
229
230 getNotation(move) {
231 if (move.vanish.length > 0) return super.getNotation(move);
232 // Parachutage:
233 const piece =
234 move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
235 return piece + "@" + V.CoordsToSquare(move.end);
236 }
237};