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