Improve last move highlighting colors
[vchess.git] / client / src / variants / Koopa.js
CommitLineData
6e0f2842 1import { ChessRules, PiPo } from "@/base_rules";
e88d69a8
BA
2
3export class KoopaRules extends ChessRules {
4 static get HasEnpassant() {
5 return false;
6 }
7
6e0f2842 8 static get STUNNED() {
e88d69a8
BA
9 return ['s', 'u', 'o', 'c', 't', 'l'];
10 }
11
6e0f2842
BA
12 static get PIECES() {
13 return ChessRules.PIECES.concat(V.STUNNED);
e88d69a8
BA
14 }
15
6e0f2842
BA
16 static ParseFen(fen) {
17 let res = ChessRules.ParseFen(fen);
18 const fenParts = fen.split(" ");
19 res.stunned = fenParts[4];
20 return res;
21 }
22
23 static IsGoodFen(fen) {
24 if (!ChessRules.IsGoodFen(fen)) return false;
25 const fenParsed = V.ParseFen(fen);
26 // 5) Check "stunned"
27 if (
28 !fenParsed.stunned ||
29 (
30 fenParsed.stunned != "-" &&
31 !fenParsed.stunned.match(/^([a-h][1-8][1-4],?)*$/)
32 )
33 ) {
34 return false;
35 }
36 return true;
37 }
38
39 getPpath(b) {
40 return (V.STUNNED.includes(b[1]) ? "Koopa/" : "") + b;
41 }
42
43 getFen() {
44 return super.getFen() + " " + this.getStunnedFen();
45 }
46
47 getFenForRepeat() {
48 return super.getFenForRepeat() + "_" + this.getStunnedFen();
49 }
50
51 getStunnedFen() {
e50a8025
BA
52 const squares = Object.keys(this.stunned);
53 if (squares.length == 0) return "-";
54 return squares.map(square => square + this.stunned[square]).join(",");
6e0f2842
BA
55 }
56
57 // Base GenRandInitFen() is fine because en-passant indicator will
58 // stand for stunned indicator.
59
60 scanKings(fen) {
61 this.INIT_COL_KING = { w: -1, b: -1 };
62 // Squares of white and black king:
63 this.kingPos = { w: [-1, -1], b: [-1, -1] };
64 const fenRows = V.ParseFen(fen).position.split("/");
65 const startRow = { 'w': V.size.x - 1, 'b': 0 };
66 for (let i = 0; i < fenRows.length; i++) {
67 let k = 0; //column index on board
68 for (let j = 0; j < fenRows[i].length; j++) {
69 switch (fenRows[i].charAt(j)) {
70 case "k":
71 case "l":
72 this.kingPos["b"] = [i, k];
73 this.INIT_COL_KING["b"] = k;
74 break;
75 case "K":
76 case "L":
77 this.kingPos["w"] = [i, k];
78 this.INIT_COL_KING["w"] = k;
79 break;
80 default: {
e50a8025 81 const num = parseInt(fenRows[i].charAt(j), 10);
6e0f2842
BA
82 if (!isNaN(num)) k += num - 1;
83 }
84 }
85 k++;
86 }
87 }
88 }
89
90 setOtherVariables(fen) {
91 super.setOtherVariables(fen);
92 let stunnedArray = [];
93 const stunnedFen = V.ParseFen(fen).stunned;
94 if (stunnedFen != "-") {
95 stunnedArray =
96 stunnedFen
97 .split(",")
98 .map(s => {
99 return {
100 square: s.substr(0, 2),
e50a8025 101 state: parseInt(s[2], 10)
6e0f2842
BA
102 };
103 });
104 }
105 this.stunned = {};
106 stunnedArray.forEach(s => {
107 this.stunned[s.square] = s.state;
108 });
e88d69a8
BA
109 }
110
111 getNormalizedStep(step) {
112 const [deltaX, deltaY] = [Math.abs(step[0]), Math.abs(step[1])];
113 if (deltaX == 0 || deltaY == 0 || deltaX == deltaY)
114 return [step[0] / deltaX || 0, step[1] / deltaY || 0];
115 // Knight:
116 const divisor = Math.min(deltaX, deltaY)
117 return [step[0] / divisor, step[1] / divisor];
118 }
119
120 getPotentialMovesFrom([x, y]) {
121 let moves = super.getPotentialMovesFrom([x, y]);
122 // Complete moves: stuns & kicks
6e0f2842
BA
123 let promoteAfterStun = [];
124 const color = this.turn;
e88d69a8
BA
125 moves.forEach(m => {
126 if (m.vanish.length == 2 && m.appear.length == 1) {
127 const step =
128 this.getNormalizedStep([m.end.x - m.start.x, m.end.y - m.start.y]);
129 // "Capture" something: is target stunned?
6e0f2842 130 if (V.STUNNED.includes(m.vanish[1].p)) {
e88d69a8
BA
131 // Kick it: continue movement in the same direction,
132 // destroying all on its path.
133 let [i, j] = [m.end.x + step[0], m.end.y + step[1]];
134 while (V.OnBoard(i, j)) {
135 if (this.board[i][j] != V.EMPTY) {
136 m.vanish.push(
137 new PiPo({
138 x: i,
139 y: j,
140 c: this.getColor(i, j),
141 p: this.getPiece(i, j)
142 })
143 );
144 }
145 i += step[0];
146 j += step[1];
147 }
148 }
149 else {
150 // The piece is now stunned
6e0f2842 151 m.appear.push(JSON.parse(JSON.stringify(m.vanish[1])));
e88d69a8 152 const pIdx = ChessRules.PIECES.findIndex(p => p == m.appear[1].p);
6e0f2842 153 m.appear[1].p = V.STUNNED[pIdx];
e88d69a8
BA
154 // And the capturer continue in the same direction until an empty
155 // square or the edge of the board, maybe stunning other pieces.
156 let [i, j] = [m.end.x + step[0], m.end.y + step[1]];
157 while (V.OnBoard(i, j) && this.board[i][j] != V.EMPTY) {
158 const colIJ = this.getColor(i, j);
159 const pieceIJ = this.getPiece(i, j);
6e0f2842
BA
160 let pIdx = ChessRules.PIECES.findIndex(p => p == pieceIJ);
161 if (pIdx >= 0) {
162 // The piece isn't already stunned
163 m.vanish.push(
164 new PiPo({
165 x: i,
166 y: j,
167 c: colIJ,
168 p: pieceIJ
169 })
170 );
171 m.appear.push(
172 new PiPo({
173 x: i,
174 y: j,
175 c: colIJ,
176 p: V.STUNNED[pIdx]
177 })
178 );
179 }
e88d69a8
BA
180 i += step[0];
181 j += step[1];
182 }
183 if (V.OnBoard(i, j)) {
184 m.appear[0].x = i;
185 m.appear[0].y = j;
186 // Is it a pawn on last rank?
6e0f2842
BA
187 if ((color == 'w' && i == 0) || (color == 'b' && i == 7)) {
188 m.appear[0].p = V.ROOK;
189 for (let ppiece of [V.KNIGHT, V.BISHOP, V.QUEEN]) {
190 let mp = JSON.parse(JSON.stringify(m));
191 mp.appear[0].p = ppiece;
192 promoteAfterStun.push(mp);
193 }
194 }
e88d69a8 195 }
6e0f2842 196 else
e88d69a8
BA
197 // The piece is out
198 m.appear.shift();
e88d69a8
BA
199 }
200 }
201 });
6e0f2842 202 return moves.concat(promoteAfterStun);
e88d69a8
BA
203 }
204
205 filterValid(moves) {
206 // Forbid kicking own king out
207 const color = this.turn;
208 return moves.filter(m => {
6e0f2842
BA
209 const kingAppear = m.appear.some(a => a.c == color && a.p == V.KING);
210 return m.vanish.every(v => {
211 return (
212 v.c != color ||
213 !["k", "l"].includes(v.p) ||
214 (v.p == "k" && kingAppear)
215 );
216 });
e88d69a8
BA
217 });
218 }
219
220 getCheckSquares() {
221 return [];
222 }
223
224 getCurrentScore() {
225 if (this.kingPos['w'][0] < 0) return "0-1";
226 if (this.kingPos['b'][0] < 0) return "1-0";
227 if (!this.atLeastOneMove()) return "1/2";
228 return "*";
229 }
230
231 postPlay(move) {
6e0f2842
BA
232 // Base method is fine because a stunned king (which won't be detected)
233 // can still castle after going back to normal.
234 super.postPlay(move);
235 const kIdx = move.vanish.findIndex(v => v.p == "l");
236 if (kIdx >= 0)
237 // A stunned king vanish (game over)
238 this.kingPos[move.vanish[kIdx].c] = [-1, -1];
239 move.stunned = JSON.stringify(this.stunned);
240 // Array of stunned stage 1 pieces (just back to normal then)
241 Object.keys(this.stunned).forEach(square => {
242 // All (formerly) stunned pieces progress by 1 level, if still on board
243 const coords = V.SquareToCoords(square);
244 const [x, y] = [coords.x, coords.y];
245 if (V.STUNNED.includes(this.board[x][y][1])) {
246 // Stunned piece still on board
247 this.stunned[square]--;
248 if (this.stunned[square] == 0) {
249 delete this.stunned[square];
250 const color = this.getColor(x, y);
251 const piece = this.getPiece(x, y);
252 const pIdx = V.STUNNED.findIndex(p => p == piece);
253 this.board[x][y] = color + ChessRules.PIECES[pIdx];
254 }
255 }
256 else delete this.stunned[square];
257 });
258 // Any new stunned pieces?
259 move.appear.forEach(a => {
260 if (V.STUNNED.includes(a.p))
261 // Set to maximum stun level:
262 this.stunned[V.CoordsToSquare({ x: a.x, y: a.y })] = 4;
263 });
e88d69a8
BA
264 }
265
266 postUndo(move) {
6e0f2842
BA
267 super.postUndo(move);
268 const kIdx = move.vanish.findIndex(v => v.p == "l");
269 if (kIdx >= 0) {
270 // A stunned king vanished
271 this.kingPos[move.vanish[kIdx].c] =
272 [move.vanish[kIdx].x, move.vanish[kIdx].y];
273 }
274 this.stunned = JSON.parse(move.stunned);
275 for (let i=0; i<8; i++) {
276 for (let j=0; j<8; j++) {
277 const square = V.CoordsToSquare({ x: i, y: j });
278 const pieceIJ = this.getPiece(i, j);
279 if (!this.stunned[square]) {
280 const pIdx = V.STUNNED.findIndex(p => p == pieceIJ);
281 if (pIdx >= 0)
282 this.board[i][j] = this.getColor(i, j) + ChessRules.PIECES[pIdx];
283 }
284 else {
285 const pIdx = ChessRules.PIECES.findIndex(p => p == pieceIJ);
286 if (pIdx >= 0)
287 this.board[i][j] = this.getColor(i, j) + V.STUNNED[pIdx];
288 }
289 }
290 }
291 }
292
293 static get VALUES() {
294 return Object.assign(
295 {
296 s: 1,
297 u: 5,
298 o: 3,
299 c: 3,
300 t: 9,
301 l: 1000
302 },
303 ChessRules.VALUES
304 );
305 }
306
307 static get SEARCH_DEPTH() {
308 return 2;
309 }
310
311 getNotation(move) {
312 if (
313 move.appear.length == 2 &&
314 move.vanish.length == 2 &&
315 move.appear.concat(move.vanish).every(
316 av => ChessRules.PIECES.includes(av.p)) &&
317 move.appear[0].p == V.KING
318 ) {
319 if (move.end.y < move.start.y) return "0-0-0";
320 return "0-0";
321 }
322 const finalSquare = V.CoordsToSquare(move.end);
323 const piece = this.getPiece(move.start.x, move.start.y);
324 const captureMark = move.vanish.length >= 2 ? "x" : "";
325 let pawnMark = "";
326 if (piece == 'p' && captureMark.length == 1)
327 pawnMark = V.CoordToColumn(move.start.y); //start column
328 // Piece or pawn movement
329 let notation =
330 (piece == V.PAWN ? pawnMark : piece.toUpperCase()) +
331 captureMark + finalSquare;
332 if (
333 piece == 'p' &&
334 move.appear[0].c == move.vanish[0].c &&
335 move.appear[0].p != 'p'
336 ) {
337 // Promotion
338 notation += "=" + move.appear[0].p.toUpperCase();
339 }
340 return notation;
e88d69a8
BA
341 }
342};