Commit | Line | Data |
---|---|---|
b967d5ba | 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
5d75c82c | 2 | import { SuicideRules } from "@/variants/Suicide"; |
00eef1ca | 3 | import { ArrayFun } from "@/utils/array"; |
15d69043 | 4 | import { randInt } from "@/utils/alea"; |
6c7cbfed BA |
5 | |
6 | export class ChakartRules extends ChessRules { | |
5d75c82c BA |
7 | static get PawnSpecs() { |
8 | return SuicideRules.PawnSpecs; | |
9 | } | |
10 | ||
11 | static get HasCastle() { | |
12 | return false; | |
13 | } | |
14 | ||
596e24d0 BA |
15 | static get HasEnpassant() { |
16 | return false; | |
17 | } | |
18 | ||
ad030c7d BA |
19 | static get CorrConfirm() { |
20 | // Because of bonus effects | |
21 | return false; | |
22 | } | |
90df90bc | 23 | |
ad030c7d | 24 | static get CanAnalyze() { |
77fc0c65 | 25 | return false; |
00eef1ca BA |
26 | } |
27 | ||
28 | static get SomeHiddenMoves() { | |
29 | return true; | |
ad030c7d | 30 | } |
6c7cbfed | 31 | |
90df90bc | 32 | hoverHighlight(x, y) { |
82820616 BA |
33 | if (this.subTurn == 1) return false; |
34 | const L = this.firstMove.length; | |
35 | const fm = this.firstMove[L-1]; | |
36 | if (fm.end.effect != 0) return false; | |
00eef1ca BA |
37 | const deltaX = Math.abs(fm.appear[0].x - x); |
38 | const deltaY = Math.abs(fm.appear[0].y - y); | |
ad030c7d | 39 | return ( |
596e24d0 | 40 | (this.board[x][y] == V.EMPTY || this.getColor(x, y) == 'a') && |
ad030c7d | 41 | ( |
596e24d0 BA |
42 | (fm.vanish[0].p == V.ROOK && deltaX == 1 && deltaY == 1) || |
43 | (fm.vanish[0].p == V.BISHOP && deltaX + deltaY == 1) | |
ad030c7d BA |
44 | ) |
45 | ); | |
90df90bc BA |
46 | } |
47 | ||
b9ce3d0f BA |
48 | static get IMMOBILIZE_CODE() { |
49 | return { | |
50 | 'p': 's', | |
51 | 'r': 'u', | |
52 | 'n': 'o', | |
53 | 'b': 'c', | |
54 | 'q': 't', | |
55 | 'k': 'l' | |
56 | }; | |
57 | } | |
58 | ||
59 | static get IMMOBILIZE_DECODE() { | |
60 | return { | |
61 | 's': 'p', | |
62 | 'u': 'r', | |
63 | 'o': 'n', | |
64 | 'c': 'b', | |
65 | 't': 'q', | |
66 | 'l': 'k' | |
67 | }; | |
68 | } | |
69 | ||
70 | static get INVISIBLE_QUEEN() { | |
71 | return 'i'; | |
72 | } | |
73 | ||
ad030c7d BA |
74 | // Fictive color 'a', bomb banana mushroom egg |
75 | static get BOMB() { | |
00eef1ca | 76 | return 'w'; //"Wario" |
ad030c7d BA |
77 | } |
78 | static get BANANA() { | |
00eef1ca | 79 | return 'd'; //"Donkey" |
ad030c7d BA |
80 | } |
81 | static get EGG() { | |
82 | return 'e'; | |
83 | } | |
84 | static get MUSHROOM() { | |
85 | return 'm'; | |
86 | } | |
87 | ||
00eef1ca BA |
88 | static fen2board(f) { |
89 | return ( | |
90 | f.charCodeAt() <= 90 | |
91 | ? "w" + f.toLowerCase() | |
92 | : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f | |
93 | ); | |
94 | } | |
95 | ||
ad030c7d BA |
96 | static get PIECES() { |
97 | return ( | |
98 | ChessRules.PIECES.concat( | |
99 | Object.keys(V.IMMOBILIZE_DECODE)).concat( | |
100 | [V.BANANA, V.BOMB, V.EGG, V.MUSHROOM, V.INVISIBLE_QUEEN]) | |
101 | ); | |
102 | } | |
103 | ||
b9ce3d0f BA |
104 | getPpath(b) { |
105 | let prefix = ""; | |
106 | if ( | |
ad030c7d | 107 | b[0] == 'a' || |
b9ce3d0f BA |
108 | b[1] == V.INVISIBLE_QUEEN || |
109 | Object.keys(V.IMMOBILIZE_DECODE).includes(b[1]) | |
110 | ) { | |
111 | prefix = "Chakart/"; | |
112 | } | |
113 | return prefix + b; | |
114 | } | |
115 | ||
00eef1ca | 116 | getPPpath(m) { |
b36db525 | 117 | if (!!m.promoteInto) return m.promoteInto; |
00eef1ca BA |
118 | let piece = m.appear[0].p; |
119 | if (Object.keys(V.IMMOBILIZE_DECODE).includes(piece)) | |
120 | piece = V.IMMOBILIZE_DECODE[piece]; | |
121 | return this.getPpath(m.appear[0].c + piece); | |
122 | } | |
123 | ||
b9ce3d0f BA |
124 | static ParseFen(fen) { |
125 | const fenParts = fen.split(" "); | |
126 | return Object.assign( | |
127 | ChessRules.ParseFen(fen), | |
596e24d0 | 128 | { captured: fenParts[4] } |
b9ce3d0f BA |
129 | ); |
130 | } | |
131 | ||
596e24d0 BA |
132 | static IsGoodFen(fen) { |
133 | if (!ChessRules.IsGoodFen(fen)) return false; | |
134 | const captured = V.ParseFen(fen).captured; | |
135 | if (!captured || !captured.match(/^[0-9]{12,12}$/)) return false; | |
136 | return true; | |
137 | } | |
138 | ||
b9ce3d0f | 139 | // King can be l or L (immobilized) --> similar to Alice variant |
90df90bc BA |
140 | static IsGoodPosition(position) { |
141 | if (position.length == 0) return false; | |
142 | const rows = position.split("/"); | |
143 | if (rows.length != V.size.x) return false; | |
b9ce3d0f | 144 | let kings = { "k": 0, "K": 0, 'l': 0, 'L': 0 }; |
90df90bc BA |
145 | for (let row of rows) { |
146 | let sumElts = 0; | |
147 | for (let i = 0; i < row.length; i++) { | |
00eef1ca | 148 | if (['K', 'k', 'L', 'l'].includes(row[i])) kings[row[i]]++; |
90df90bc BA |
149 | if (V.PIECES.includes(row[i].toLowerCase())) sumElts++; |
150 | else { | |
e50a8025 | 151 | const num = parseInt(row[i], 10); |
90df90bc BA |
152 | if (isNaN(num)) return false; |
153 | sumElts += num; | |
154 | } | |
155 | } | |
156 | if (sumElts != V.size.y) return false; | |
157 | } | |
978fa11c | 158 | if (kings['k'] + kings['l'] == 0 || kings['K'] + kings['L'] == 0) |
b9ce3d0f | 159 | return false; |
90df90bc BA |
160 | return true; |
161 | } | |
162 | ||
b9ce3d0f | 163 | static IsGoodFlags(flags) { |
5d75c82c BA |
164 | // 4 for Peach + Mario w, b |
165 | return !!flags.match(/^[01]{4,4}$/); | |
b9ce3d0f BA |
166 | } |
167 | ||
168 | setFlags(fenflags) { | |
b967d5ba | 169 | // King can send shell? Queen can be invisible? |
b9ce3d0f | 170 | this.powerFlags = { |
00eef1ca BA |
171 | w: { 'k': false, 'q': false }, |
172 | b: { 'k': false, 'q': false } | |
b9ce3d0f | 173 | }; |
b9ce3d0f | 174 | for (let c of ["w", "b"]) { |
b967d5ba BA |
175 | for (let p of ['k', 'q']) { |
176 | this.powerFlags[c][p] = | |
15d69043 | 177 | fenflags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1"; |
b967d5ba | 178 | } |
b9ce3d0f BA |
179 | } |
180 | } | |
181 | ||
182 | aggregateFlags() { | |
5d75c82c | 183 | return this.powerFlags; |
b9ce3d0f BA |
184 | } |
185 | ||
186 | disaggregateFlags(flags) { | |
5d75c82c | 187 | this.powerFlags = flags; |
b9ce3d0f BA |
188 | } |
189 | ||
190 | getFen() { | |
191 | return super.getFen() + " " + this.getCapturedFen(); | |
192 | } | |
193 | ||
194 | getFenForRepeat() { | |
195 | return super.getFenForRepeat() + "_" + this.getCapturedFen(); | |
196 | } | |
197 | ||
198 | getCapturedFen() { | |
596e24d0 | 199 | let counts = [...Array(12).fill(0)]; |
b9ce3d0f | 200 | let i = 0; |
596e24d0 | 201 | for (let p of V.RESERVE_PIECES) { |
b9ce3d0f | 202 | counts[i] = this.captured["w"][p]; |
596e24d0 | 203 | counts[6 + i] = this.captured["b"][p]; |
b9ce3d0f BA |
204 | i++; |
205 | } | |
206 | return counts.join(""); | |
207 | } | |
208 | ||
15d69043 BA |
209 | scanKings() {} |
210 | ||
6c7cbfed | 211 | setOtherVariables(fen) { |
15d69043 | 212 | super.setOtherVariables(fen); |
b9ce3d0f | 213 | // Initialize captured pieces' counts from FEN |
e50a8025 BA |
214 | const captured = |
215 | V.ParseFen(fen).captured.split("").map(x => parseInt(x, 10)); | |
b9ce3d0f BA |
216 | this.captured = { |
217 | w: { | |
e50a8025 BA |
218 | [V.PAWN]: captured[0], |
219 | [V.ROOK]: captured[1], | |
220 | [V.KNIGHT]: captured[2], | |
221 | [V.BISHOP]: captured[3], | |
222 | [V.QUEEN]: captured[4], | |
223 | [V.KING]: captured[5] | |
b9ce3d0f BA |
224 | }, |
225 | b: { | |
e50a8025 BA |
226 | [V.PAWN]: captured[6], |
227 | [V.ROOK]: captured[7], | |
228 | [V.KNIGHT]: captured[8], | |
229 | [V.BISHOP]: captured[9], | |
230 | [V.QUEEN]: captured[10], | |
231 | [V.KING]: captured[11] | |
b9ce3d0f BA |
232 | } |
233 | }; | |
b967d5ba | 234 | this.firstMove = []; |
6c7cbfed BA |
235 | this.subTurn = 1; |
236 | } | |
237 | ||
b9ce3d0f | 238 | getFlagsFen() { |
5d75c82c | 239 | let fen = ""; |
b9ce3d0f BA |
240 | // Add power flags |
241 | for (let c of ["w", "b"]) | |
b967d5ba | 242 | for (let p of ['k', 'q']) fen += (this.powerFlags[c][p] ? "1" : "0"); |
b9ce3d0f BA |
243 | return fen; |
244 | } | |
245 | ||
596e24d0 BA |
246 | getColor(i, j) { |
247 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; | |
248 | return this.board[i][j].charAt(0); | |
249 | } | |
250 | ||
251 | getPiece(i, j) { | |
252 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; | |
253 | return this.board[i][j].charAt(1); | |
254 | } | |
255 | ||
00eef1ca BA |
256 | getReservePpath(index, color) { |
257 | return color + V.RESERVE_PIECES[index]; | |
258 | } | |
259 | ||
b967d5ba | 260 | static get RESERVE_PIECES() { |
596e24d0 | 261 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING]; |
b967d5ba BA |
262 | } |
263 | ||
264 | getReserveMoves([x, y]) { | |
265 | const color = this.turn; | |
266 | const p = V.RESERVE_PIECES[y]; | |
267 | if (this.reserve[color][p] == 0) return []; | |
268 | let moves = []; | |
269 | const start = (color == 'w' && p == V.PAWN ? 1 : 0); | |
270 | const end = (color == 'b' && p == V.PAWN ? 7 : 8); | |
271 | for (let i = start; i < end; i++) { | |
272 | for (let j = 0; j < V.size.y; j++) { | |
42e37983 BA |
273 | if ( |
274 | this.board[i][j] == V.EMPTY || | |
275 | this.getColor(i, j) == 'a' || | |
276 | this.getPiece(i, j) == V.INVISIBLE_QUEEN | |
277 | ) { | |
77fc0c65 BA |
278 | let m = this.getBasicMove({ p: p, x: i, y: j}); |
279 | m.start = { x: x, y: y }; | |
280 | moves.push(m); | |
b967d5ba BA |
281 | } |
282 | } | |
283 | } | |
284 | return moves; | |
285 | } | |
286 | ||
287 | getPotentialMovesFrom([x, y]) { | |
00eef1ca BA |
288 | let moves = []; |
289 | if (this.subTurn == 1) { | |
290 | moves = super.getPotentialMovesFrom([x, y]); | |
291 | const finalPieces = V.PawnSpecs.promotions; | |
292 | const color = this.turn; | |
293 | const lastRank = (color == "w" ? 0 : 7); | |
294 | let pMoves = []; | |
295 | moves.forEach(m => { | |
296 | if ( | |
297 | m.appear.length > 0 && | |
298 | ['p', 's'].includes(m.appear[0].p) && | |
299 | m.appear[0].x == lastRank | |
300 | ) { | |
301 | for (let i = 1; i < finalPieces.length; i++) { | |
302 | const piece = finalPieces[i]; | |
303 | let otherM = JSON.parse(JSON.stringify(m)); | |
304 | otherM.appear[0].p = | |
305 | m.appear[0].p == V.PAWN | |
306 | ? finalPieces[i] | |
307 | : V.IMMOBILIZE_CODE[finalPieces[i]]; | |
308 | pMoves.push(otherM); | |
309 | } | |
310 | // Finally alter m itself: | |
311 | m.appear[0].p = | |
312 | m.appear[0].p == V.PAWN | |
313 | ? finalPieces[0] | |
314 | : V.IMMOBILIZE_CODE[finalPieces[0]]; | |
315 | } | |
316 | }); | |
317 | Array.prototype.push.apply(moves, pMoves); | |
318 | } | |
319 | else { | |
320 | // Subturn == 2 | |
b967d5ba BA |
321 | const L = this.firstMove.length; |
322 | const fm = this.firstMove[L-1]; | |
323 | switch (fm.end.effect) { | |
00eef1ca BA |
324 | case "kingboo": |
325 | // Exchange position with any piece, | |
326 | // except pawns if arriving on last rank. | |
327 | const lastRank = { 'w': 0, 'b': 7 }; | |
328 | const color = this.turn; | |
329 | const allowLastRank = (this.getPiece(x, y) != V.PAWN); | |
b967d5ba BA |
330 | for (let i=0; i<8; i++) { |
331 | for (let j=0; j<8; j++) { | |
332 | const colIJ = this.getColor(i, j); | |
333 | if ( | |
00eef1ca | 334 | (i != x || j != y) && |
b967d5ba BA |
335 | this.board[i][j] != V.EMPTY && |
336 | colIJ != 'a' | |
337 | ) { | |
00eef1ca BA |
338 | const pieceIJ = this.getPiece(i, j); |
339 | if ( | |
340 | (pieceIJ != V.PAWN || x != lastRank[colIJ]) && | |
341 | (allowLastRank || i != lastRank[color]) | |
342 | ) { | |
343 | const movedUnit = new PiPo({ | |
344 | x: x, | |
345 | y: y, | |
346 | c: colIJ, | |
347 | p: this.getPiece(i, j) | |
348 | }); | |
77fc0c65 | 349 | let mMove = this.getBasicMove({ x: x, y: y }, [i, j]); |
00eef1ca BA |
350 | mMove.appear.push(movedUnit); |
351 | moves.push(mMove); | |
352 | } | |
b967d5ba BA |
353 | } |
354 | } | |
355 | } | |
356 | break; | |
00eef1ca | 357 | case "toadette": |
b967d5ba BA |
358 | // Resurrect a captured piece |
359 | if (x >= V.size.x) moves = this.getReserveMoves([x, y]); | |
360 | break; | |
00eef1ca | 361 | case "daisy": |
b967d5ba | 362 | // Play again with the same piece |
00eef1ca | 363 | if (fm.appear[0].x == x && fm.appear[0].y == y) |
b967d5ba BA |
364 | moves = super.getPotentialMovesFrom([x, y]); |
365 | break; | |
366 | } | |
6c7cbfed | 367 | } |
00eef1ca | 368 | return moves; |
6c7cbfed BA |
369 | } |
370 | ||
15d69043 BA |
371 | // Helper for getBasicMove() |
372 | getRandomSquare([x, y], steps) { | |
7c8d0174 | 373 | const color = this.turn; |
15d69043 BA |
374 | const validSteps = steps.filter(s => { |
375 | const [i, j] = [x + s[0], y + s[1]]; | |
376 | return ( | |
377 | V.OnBoard(i, j) && | |
7c8d0174 | 378 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) != color) |
15d69043 BA |
379 | ); |
380 | }); | |
381 | if (validSteps.length == 0) | |
382 | // Can happen after mushroom jump | |
383 | return [x, y]; | |
384 | const step = validSteps[randInt(validSteps.length)]; | |
385 | return [x + step[0], y + step[1]]; | |
386 | } | |
387 | ||
596e24d0 BA |
388 | canMove([x, y], piece) { |
389 | const color = this.getColor(x, y); | |
390 | const oppCol = V.GetOppCol(color); | |
391 | piece = piece || this.getPiece(x, y); | |
392 | if (piece == V.PAWN) { | |
393 | const forward = (color == 'w' ? -1 : 1); | |
394 | return ( | |
b36db525 | 395 | V.OnBoard(x + forward, y) && |
596e24d0 | 396 | ( |
b36db525 BA |
397 | this.board[x + forward][y] != oppCol || |
398 | ( | |
399 | V.OnBoard(x + forward, y + 1) && | |
400 | this.board[x + forward][y + 1] != V.EMPTY && | |
401 | this.getColor[x + forward, y + 1] == oppCol | |
402 | ) || | |
403 | ( | |
404 | V.OnBoard(x + forward, y - 1) && | |
405 | this.board[x + forward][y - 1] != V.EMPTY && | |
406 | this.getColor[x + forward, y - 1] == oppCol | |
407 | ) | |
596e24d0 BA |
408 | ) |
409 | ); | |
410 | } | |
411 | // Checking one step is enough: | |
412 | const steps = | |
413 | [V.KING, V.QUEEN].includes(piece) | |
414 | ? V.steps[V.ROOK].concat(V.steps[V.BISHOP]) | |
415 | : V.steps[piece]; | |
596e24d0 BA |
416 | for (let step of steps) { |
417 | const [i, j] = [x + step[0], y + step[1]]; | |
418 | if ( | |
419 | V.OnBoard(i, j) && | |
420 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) != color) | |
421 | ) { | |
422 | return true; | |
423 | } | |
424 | } | |
425 | return false; | |
426 | } | |
427 | ||
77fc0c65 BA |
428 | // Apply mushroom, bomb or banana effect (hidden to the player). |
429 | // Determine egg effect, too, and apply its first part if possible. | |
430 | getBasicMove_aux(psq1, sq2, tr, initMove) { | |
431 | const [x1, y1] = [psq1.x, psq1.y]; | |
432 | const color1 = this.turn; | |
b36db525 | 433 | const piece1 = (!!tr ? tr.p : (psq1.p || this.getPiece(x1, y1))); |
15d69043 | 434 | const oppCol = V.GetOppCol(color1); |
77fc0c65 BA |
435 | if (!sq2) { |
436 | let move = { | |
437 | appear: [], | |
438 | vanish: [] | |
439 | }; | |
440 | // banana or bomb defines next square, or the move ends there | |
441 | move.appear = [ | |
442 | new PiPo({ | |
443 | x: x1, | |
444 | y: y1, | |
445 | c: color1, | |
446 | p: piece1 | |
447 | }) | |
448 | ]; | |
449 | if (this.board[x1][y1] != V.EMPTY) { | |
450 | const initP1 = this.getPiece(x1, y1); | |
451 | move.vanish = [ | |
452 | new PiPo({ | |
453 | x: x1, | |
454 | y: y1, | |
455 | c: this.getColor(x1, y1), | |
456 | p: initP1 | |
457 | }) | |
458 | ]; | |
459 | if ([V.BANANA, V.BOMB].includes(initP1)) { | |
460 | const steps = V.steps[initP1 == V.BANANA ? V.ROOK : V.BISHOP]; | |
461 | move.next = this.getRandomSquare([x1, y1], steps); | |
462 | } | |
463 | } | |
464 | move.end = { x: x1, y: y1 }; | |
465 | return move; | |
466 | } | |
467 | const [x2, y2] = [sq2[0], sq2[1]]; | |
468 | // The move starts normally, on board: | |
469 | let move = super.getBasicMove([x1, y1], [x2, y2], tr); | |
b36db525 | 470 | if (!!tr) move.promoteInto = tr.c + tr.p; //in case of (chomped...) |
77fc0c65 BA |
471 | const L = this.firstMove.length; |
472 | if ( | |
473 | [V.PAWN, V.KNIGHT].includes(piece1) && | |
474 | !!initMove && | |
475 | (this.subTurn == 1 || this.firstMove[L-1].end.effect == "daisy") | |
476 | ) { | |
15d69043 BA |
477 | switch (piece1) { |
478 | case V.PAWN: { | |
479 | const twoSquaresMove = (Math.abs(x2 - x1) == 2); | |
480 | const mushroomX = x1 + (twoSquaresMove ? (x2 - x1) / 2 : 0); | |
481 | move.appear.push( | |
482 | new PiPo({ | |
483 | x: mushroomX, | |
484 | y: y1, | |
485 | c: 'a', | |
486 | p: V.MUSHROOM | |
487 | }) | |
488 | ); | |
489 | if (this.getColor(mushroomX, y1) == 'a') { | |
490 | move.vanish.push( | |
491 | new PiPo({ | |
492 | x: mushroomX, | |
493 | y: y1, | |
494 | c: 'a', | |
495 | p: this.getPiece(mushroomX, y1) | |
496 | }) | |
497 | ); | |
498 | } | |
499 | break; | |
500 | } | |
501 | case V.KNIGHT: { | |
502 | const deltaX = Math.abs(x2 - x1); | |
503 | const deltaY = Math.abs(y2 - y1); | |
00eef1ca | 504 | let eggSquare = [ |
15d69043 BA |
505 | x1 + (deltaX == 2 ? (x2 - x1) / 2 : 0), |
506 | y1 + (deltaY == 2 ? (y2 - y1) / 2 : 0) | |
507 | ]; | |
508 | if ( | |
00eef1ca BA |
509 | this.board[eggSquare[0]][eggSquare[1]] != V.EMPTY && |
510 | this.getColor(eggSquare[0], eggSquare[1]) != 'a' | |
15d69043 | 511 | ) { |
00eef1ca BA |
512 | eggSquare[0] = x1; |
513 | eggSquare[1] = y1; | |
514 | } | |
515 | move.appear.push( | |
516 | new PiPo({ | |
517 | x: eggSquare[0], | |
518 | y: eggSquare[1], | |
519 | c: 'a', | |
520 | p: V.EGG | |
521 | }) | |
522 | ); | |
523 | if (this.getColor(eggSquare[0], eggSquare[1]) == 'a') { | |
524 | move.vanish.push( | |
15d69043 BA |
525 | new PiPo({ |
526 | x: eggSquare[0], | |
527 | y: eggSquare[1], | |
528 | c: 'a', | |
00eef1ca | 529 | p: this.getPiece(eggSquare[0], eggSquare[1]) |
15d69043 BA |
530 | }) |
531 | ); | |
15d69043 BA |
532 | } |
533 | break; | |
534 | } | |
535 | } | |
536 | } | |
15d69043 BA |
537 | // For (wa)luigi effect: |
538 | const changePieceColor = (color) => { | |
539 | let pieces = []; | |
00eef1ca | 540 | const oppLastRank = (color == 'w' ? 7 : 0); |
15d69043 BA |
541 | for (let i=0; i<8; i++) { |
542 | for (let j=0; j<8; j++) { | |
77fc0c65 BA |
543 | if ( |
544 | (i != move.vanish[0].x || j != move.vanish[0].y) && | |
545 | this.board[i][j] != V.EMPTY && | |
546 | this.getColor(i, j) == color | |
547 | ) { | |
15d69043 | 548 | const piece = this.getPiece(i, j); |
00eef1ca | 549 | if (piece != V.KING && (piece != V.PAWN || i != oppLastRank)) |
15d69043 BA |
550 | pieces.push({ x: i, y: j, p: piece }); |
551 | } | |
552 | } | |
553 | } | |
77fc0c65 | 554 | // Special case of the current piece (still at its initial position) |
596e24d0 BA |
555 | if (color == color1) |
556 | pieces.push({ x: move.appear[0].x, y: move.appear[0].y, p: piece1 }); | |
15d69043 | 557 | const cp = pieces[randInt(pieces.length)]; |
596e24d0 BA |
558 | if (move.appear[0].x != cp.x || move.appear[0].y != cp.y) { |
559 | move.vanish.push( | |
560 | new PiPo({ | |
561 | x: cp.x, | |
562 | y: cp.y, | |
563 | c: color, | |
564 | p: cp.p | |
565 | }) | |
566 | ); | |
567 | } | |
77fc0c65 | 568 | else move.appear.shift(); |
15d69043 BA |
569 | move.appear.push( |
570 | new PiPo({ | |
571 | x: cp.x, | |
572 | y: cp.y, | |
573 | c: V.GetOppCol(color), | |
574 | p: cp.p | |
575 | }) | |
576 | ); | |
577 | }; | |
578 | const applyEggEffect = () => { | |
00eef1ca BA |
579 | if (this.subTurn == 2) |
580 | // No egg effects at subTurn 2 | |
581 | return; | |
582 | // 1) Determine the effect (some may be impossible) | |
583 | let effects = ["kingboo", "koopa", "chomp", "bowser"]; | |
584 | if (Object.values(this.captured[color1]).some(c => c >= 1)) | |
585 | effects.push("toadette"); | |
586 | const lastRank = { 'w': 0, 'b': 7 }; | |
587 | let canPlayAgain = undefined; | |
588 | if ( | |
589 | move.appear[0].p == V.PAWN && | |
590 | move.appear[0].x == lastRank[color1] | |
591 | ) { | |
77fc0c65 | 592 | // Always possible: promote into a queen, rook or king |
00eef1ca BA |
593 | canPlayAgain = true; |
594 | } | |
595 | else { | |
596e24d0 | 596 | move.end.effect = "daisy"; |
596e24d0 BA |
597 | V.PlayOnBoard(this.board, move); |
598 | const square = [move.appear[0].x, move.appear[0].y]; | |
599 | canPlayAgain = this.canMove(square, piece1); | |
600 | V.UndoOnBoard(this.board, move); | |
00eef1ca BA |
601 | delete move.end["effect"]; |
602 | } | |
603 | if (canPlayAgain) effects.push("daisy"); | |
604 | if ( | |
605 | this.board.some((b,i) => | |
606 | b.some(cell => { | |
607 | return ( | |
608 | cell[0] == oppCol && | |
609 | cell[1] != V.KING && | |
596e24d0 | 610 | (cell[1] != V.PAWN || i != lastRank[color1]) |
00eef1ca BA |
611 | ); |
612 | }) | |
613 | ) | |
614 | ) { | |
615 | effects.push("luigi"); | |
616 | } | |
617 | if ( | |
596e24d0 BA |
618 | ( |
619 | piece1 != V.KING && | |
620 | (piece1 != V.PAWN || move.appear[0].x != lastRank[oppCol]) | |
621 | ) || | |
00eef1ca BA |
622 | this.board.some((b,i) => |
623 | b.some(cell => { | |
624 | return ( | |
625 | cell[0] == color1 && | |
626 | cell[1] != V.KING && | |
596e24d0 | 627 | (cell[1] != V.PAWN || i != lastRank[oppCol]) |
00eef1ca BA |
628 | ); |
629 | }) | |
630 | ) | |
631 | ) { | |
632 | effects.push("waluigi"); | |
633 | } | |
634 | const effect = effects[randInt(effects.length)]; | |
635 | move.end.effect = effect; | |
636 | // 2) Apply it if possible | |
637 | if (!(["kingboo", "toadette", "daisy"].includes(effect))) { | |
638 | switch (effect) { | |
639 | case "koopa": | |
640 | move.appear = []; | |
641 | // Maybe egg effect was applied after others, | |
642 | // so just shift vanish array: | |
643 | move.vanish.shift(); | |
644 | break; | |
645 | case "chomp": | |
646 | move.appear = []; | |
647 | break; | |
648 | case "bowser": | |
649 | move.appear[0].p = V.IMMOBILIZE_CODE[piece1]; | |
650 | break; | |
651 | case "luigi": | |
652 | changePieceColor(oppCol); | |
653 | break; | |
654 | case "waluigi": | |
655 | changePieceColor(color1); | |
656 | break; | |
15d69043 BA |
657 | } |
658 | } | |
659 | }; | |
660 | const applyMushroomEffect = () => { | |
661 | if ([V.PAWN, V.KING, V.KNIGHT].includes(piece1)) { | |
662 | // Just make another similar step, if possible (non-capturing) | |
663 | const [i, j] = [ | |
00eef1ca BA |
664 | move.appear[0].x + (x2 - x1), |
665 | move.appear[0].y + (y2 - y1) | |
15d69043 BA |
666 | ]; |
667 | if ( | |
668 | V.OnBoard(i, j) && | |
669 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a') | |
670 | ) { | |
671 | move.appear[0].x = i; | |
672 | move.appear[0].y = j; | |
673 | if (this.board[i][j] != V.EMPTY) { | |
674 | const object = this.getPiece(i, j); | |
675 | move.vanish.push( | |
676 | new PiPo({ | |
677 | x: i, | |
678 | y: j, | |
679 | c: 'a', | |
680 | p: object | |
681 | }) | |
682 | ); | |
683 | switch (object) { | |
684 | case V.BANANA: | |
15d69043 | 685 | case V.BOMB: |
77fc0c65 BA |
686 | const steps = V.steps[object == V.BANANA ? V.ROOK : V.BISHOP]; |
687 | move.next = this.getRandomSquare([i, j], steps); | |
15d69043 BA |
688 | break; |
689 | case V.EGG: | |
690 | applyEggEffect(); | |
691 | break; | |
692 | case V.MUSHROOM: | |
693 | applyMushroomEffect(); | |
694 | break; | |
695 | } | |
696 | } | |
697 | } | |
698 | } | |
699 | else { | |
700 | // Queen, bishop or rook: | |
701 | const step = [ | |
702 | (x2 - x1) / Math.abs(x2 - x1) || 0, | |
703 | (y2 - y1) / Math.abs(y2 - y1) || 0 | |
704 | ]; | |
705 | const next = [move.appear[0].x + step[0], move.appear[0].y + step[1]]; | |
706 | if ( | |
707 | V.OnBoard(next[0], next[1]) && | |
708 | this.board[next[0]][next[1]] != V.EMPTY && | |
709 | this.getColor(next[0], next[1]) != 'a' | |
710 | ) { | |
711 | const afterNext = [next[0] + step[0], next[1] + step[1]]; | |
596e24d0 BA |
712 | if (V.OnBoard(afterNext[0], afterNext[1])) { |
713 | const afterColor = this.getColor(afterNext[0], afterNext[1]) | |
714 | if ( | |
15d69043 | 715 | this.board[afterNext[0]][afterNext[1]] == V.EMPTY || |
596e24d0 BA |
716 | afterColor != color1 |
717 | ) { | |
718 | move.appear[0].x = afterNext[0]; | |
719 | move.appear[0].y = afterNext[1]; | |
720 | if (this.board[afterNext[0]][afterNext[1]] != V.EMPTY) { | |
721 | // The "object" could also be an opponent's piece | |
722 | const object = this.getPiece(afterNext[0], afterNext[1]); | |
723 | move.vanish.push( | |
724 | new PiPo({ | |
725 | x: afterNext[0], | |
726 | y: afterNext[1], | |
727 | c: afterColor, | |
728 | p: object | |
729 | }) | |
730 | ); | |
731 | switch (object) { | |
732 | case V.BANANA: | |
596e24d0 | 733 | case V.BOMB: |
77fc0c65 BA |
734 | const steps = |
735 | V.steps[object == V.BANANA ? V.ROOK : V.BISHOP]; | |
736 | move.next = this.getRandomSquare( | |
737 | [afterNext[0], afterNext[1]], steps); | |
596e24d0 BA |
738 | break; |
739 | case V.EGG: | |
740 | applyEggEffect(); | |
741 | break; | |
742 | case V.MUSHROOM: | |
743 | applyMushroomEffect(); | |
744 | break; | |
745 | } | |
15d69043 BA |
746 | } |
747 | } | |
748 | } | |
749 | } | |
750 | } | |
751 | }; | |
596e24d0 BA |
752 | const color2 = this.getColor(x2, y2); |
753 | const piece2 = this.getPiece(x2, y2); | |
15d69043 BA |
754 | if (color2 == 'a') { |
755 | switch (piece2) { | |
756 | case V.BANANA: | |
15d69043 | 757 | case V.BOMB: |
77fc0c65 BA |
758 | const steps = V.steps[piece2 == V.BANANA ? V.ROOK : V.BISHOP]; |
759 | move.next = this.getRandomSquare([x2, y2], steps); | |
15d69043 BA |
760 | break; |
761 | case V.MUSHROOM: | |
762 | applyMushroomEffect(); | |
763 | break; | |
764 | case V.EGG: | |
596e24d0 | 765 | if (this.subTurn == 1) |
15d69043 | 766 | // No egg effect at subTurn 2 |
15d69043 | 767 | applyEggEffect(); |
15d69043 BA |
768 | break; |
769 | } | |
770 | } | |
00eef1ca | 771 | if ( |
15d69043 | 772 | this.subTurn == 1 && |
77fc0c65 | 773 | !move.next && |
596e24d0 | 774 | move.appear.length > 0 && |
15d69043 BA |
775 | [V.ROOK, V.BISHOP].includes(piece1) |
776 | ) { | |
596e24d0 BA |
777 | const finalSquare = [move.appear[0].x, move.appear[0].y]; |
778 | if ( | |
779 | color2 != 'a' || | |
780 | this.getColor(finalSquare[0], finalSquare[1]) != 'a' || | |
781 | this.getPiece(finalSquare[0], finalSquare[1]) != V.EGG | |
782 | ) { | |
783 | const validSteps = | |
784 | V.steps[piece1 == V.ROOK ? V.BISHOP : V.ROOK].filter(s => { | |
785 | const [i, j] = [finalSquare[0] + s[0], finalSquare[1] + s[1]]; | |
786 | return ( | |
787 | V.OnBoard(i, j) && | |
788 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a') | |
789 | ); | |
790 | }); | |
77fc0c65 | 791 | if (validSteps.length >= 1) { |
596e24d0 BA |
792 | const [x, y] = [ |
793 | finalSquare[0] + validSteps[0][0], | |
794 | finalSquare[1] + validSteps[0][1] | |
795 | ]; | |
796 | move.appear.push( | |
797 | new PiPo({ | |
798 | x: x, | |
799 | y: y, | |
800 | c: 'a', | |
801 | p: (piece1 == V.ROOK ? V.BANANA : V.BOMB) | |
802 | }) | |
803 | ); | |
804 | if (this.board[x][y] != V.EMPTY) { | |
805 | move.vanish.push( | |
806 | new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) })); | |
807 | } | |
808 | } | |
809 | } | |
15d69043 | 810 | } |
77fc0c65 BA |
811 | return move; |
812 | } | |
813 | ||
814 | getBasicMove(psq1, sq2, tr) { | |
815 | let moves = []; | |
816 | if (Array.isArray(psq1)) psq1 = { x: psq1[0], y: psq1[1] }; | |
817 | let m = this.getBasicMove_aux(psq1, sq2, tr, "initMove"); | |
818 | while (!!m.next) { | |
819 | // Last move ended on bomb or banana, direction change | |
820 | V.PlayOnBoard(this.board, m); | |
821 | moves.push(m); | |
822 | m = this.getBasicMove_aux( | |
823 | { x: m.appear[0].x, y: m.appear[0].y }, m.next); | |
824 | } | |
825 | for (let i=moves.length-1; i>=0; i--) V.UndoOnBoard(this.board, moves[i]); | |
826 | moves.push(m); | |
827 | // Now merge moves into one | |
828 | let move = {}; | |
829 | // start is wrong for Toadette moves --> it's fixed later | |
830 | move.start = { x: psq1.x, y: psq1.y }; | |
831 | move.end = !!sq2 ? { x: sq2[0], y: sq2[1] } : { x: psq1.x, y: psq1.y }; | |
b36db525 | 832 | if (!!tr) move.promoteInto = moves[0].promoteInto; |
77fc0c65 BA |
833 | let lm = moves[moves.length-1]; |
834 | if (this.subTurn == 1 && !!lm.end.effect) | |
835 | move.end.effect = lm.end.effect; | |
836 | if (moves.length == 1) { | |
837 | move.appear = moves[0].appear; | |
838 | move.vanish = moves[0].vanish; | |
839 | } | |
840 | else { | |
841 | // Keep first vanish and last appear (if any) | |
842 | move.appear = lm.appear; | |
843 | move.vanish = moves[0].vanish; | |
844 | if ( | |
845 | move.vanish.length >= 1 && | |
846 | move.appear.length >= 1 && | |
847 | move.vanish[0].x == move.appear[0].x && | |
848 | move.vanish[0].y == move.appear[0].y | |
849 | ) { | |
850 | // Loopback on initial square: | |
851 | move.vanish.shift(); | |
852 | move.appear.shift(); | |
853 | } | |
854 | for (let i=1; i < moves.length - 1; i++) { | |
855 | for (let v of moves[i].vanish) { | |
856 | // Only vanishing objects, not appearing at init move | |
857 | if ( | |
858 | v.c == 'a' && | |
859 | ( | |
860 | moves[0].appear.length == 1 || | |
861 | moves[0].appear[1].x != v.x || | |
862 | moves[0].appear[1].y != v.y | |
863 | ) | |
864 | ) { | |
865 | move.vanish.push(v); | |
866 | } | |
867 | } | |
868 | } | |
869 | // Final vanish is our piece, but others might be relevant | |
870 | // (for some egg bonuses at least). | |
871 | for (let i=1; i < lm.vanish.length; i++) { | |
872 | if ( | |
873 | lm.vanish[i].c != 'a' || | |
874 | moves[0].appear.length == 1 || | |
875 | moves[0].appear[1].x != lm.vanish[i].x || | |
876 | moves[0].appear[1].y != lm.vanish[i].y | |
877 | ) { | |
878 | move.vanish.push(lm.vanish[i]); | |
879 | } | |
880 | } | |
b967d5ba | 881 | } |
596e24d0 | 882 | return move; |
b967d5ba BA |
883 | } |
884 | ||
15d69043 BA |
885 | getPotentialPawnMoves([x, y]) { |
886 | const color = this.turn; | |
887 | const oppCol = V.GetOppCol(color); | |
888 | const [sizeX, sizeY] = [V.size.x, V.size.y]; | |
889 | const shiftX = V.PawnSpecs.directions[color]; | |
890 | const firstRank = (color == "w" ? sizeX - 1 : 0); | |
891 | let moves = []; | |
892 | if ( | |
893 | this.board[x + shiftX][y] == V.EMPTY || | |
894 | this.getColor(x + shiftX, y) == 'a' | |
895 | ) { | |
896 | this.addPawnMoves([x, y], [x + shiftX, y], moves); | |
897 | if ( | |
898 | [firstRank, firstRank + shiftX].includes(x) && | |
00eef1ca BA |
899 | ( |
900 | this.board[x + 2 * shiftX][y] == V.EMPTY || | |
901 | this.getColor(x + 2 * shiftX, y) == 'a' | |
902 | ) | |
15d69043 | 903 | ) { |
77fc0c65 | 904 | moves.push(this.getBasicMove({ x: x, y: y }, [x + 2 * shiftX, y])); |
15d69043 BA |
905 | } |
906 | } | |
907 | for (let shiftY of [-1, 1]) { | |
908 | if ( | |
909 | y + shiftY >= 0 && | |
910 | y + shiftY < sizeY && | |
911 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
596e24d0 | 912 | ['a', oppCol].includes(this.getColor(x + shiftX, y + shiftY)) |
15d69043 BA |
913 | ) { |
914 | this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves); | |
915 | } | |
916 | } | |
15d69043 BA |
917 | return moves; |
918 | } | |
919 | ||
b967d5ba BA |
920 | getPotentialQueenMoves(sq) { |
921 | const normalMoves = super.getPotentialQueenMoves(sq); | |
922 | // If flag allows it, add 'invisible movements' | |
923 | let invisibleMoves = []; | |
924 | if (this.powerFlags[this.turn][V.QUEEN]) { | |
925 | normalMoves.forEach(m => { | |
596e24d0 BA |
926 | if ( |
927 | m.appear.length == 1 && | |
928 | m.vanish.length == 1 && | |
929 | // Only simple non-capturing moves: | |
930 | m.vanish[0].c != 'a' | |
931 | ) { | |
b967d5ba | 932 | let im = JSON.parse(JSON.stringify(m)); |
00eef1ca BA |
933 | im.appear[0].p = V.INVISIBLE_QUEEN; |
934 | im.end.noHighlight = true; | |
b967d5ba BA |
935 | invisibleMoves.push(im); |
936 | } | |
937 | }); | |
938 | } | |
939 | return normalMoves.concat(invisibleMoves); | |
940 | } | |
941 | ||
5d75c82c BA |
942 | getPotentialKingMoves([x, y]) { |
943 | let moves = super.getPotentialKingMoves([x, y]); | |
b967d5ba BA |
944 | const color = this.turn; |
945 | // If flag allows it, add 'remote shell captures' | |
946 | if (this.powerFlags[this.turn][V.KING]) { | |
947 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(step => { | |
596e24d0 | 948 | const [nextX, nextY] = [x + step[0], y + step[1]]; |
00eef1ca | 949 | if ( |
596e24d0 BA |
950 | V.OnBoard(nextX, nextY) && |
951 | ( | |
952 | this.board[nextX][nextY] == V.EMPTY || | |
953 | ( | |
954 | this.getColor(nextX, nextY) == 'a' && | |
955 | [V.EGG, V.MUSHROOM].includes(this.getPiece(nextX, nextY)) | |
956 | ) | |
957 | ) | |
00eef1ca BA |
958 | ) { |
959 | let [i, j] = [x + 2 * step[0], y + 2 * step[1]]; | |
960 | while ( | |
961 | V.OnBoard(i, j) && | |
b967d5ba | 962 | ( |
00eef1ca BA |
963 | this.board[i][j] == V.EMPTY || |
964 | ( | |
965 | this.getColor(i, j) == 'a' && | |
966 | [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j)) | |
967 | ) | |
b967d5ba | 968 | ) |
00eef1ca BA |
969 | ) { |
970 | i += step[0]; | |
971 | j += step[1]; | |
972 | } | |
973 | if (V.OnBoard(i, j)) { | |
974 | const colIJ = this.getColor(i, j); | |
975 | if (colIJ != color) { | |
976 | // May just destroy a bomb or banana: | |
977 | moves.push( | |
978 | new Move({ | |
979 | start: { x: x, y: y}, | |
980 | end: { x: i, y: j }, | |
981 | appear: [], | |
982 | vanish: [ | |
983 | new PiPo({ | |
984 | x: i, y: j, c: colIJ, p: this.getPiece(i, j) | |
985 | }) | |
986 | ] | |
987 | }) | |
988 | ); | |
989 | } | |
990 | } | |
b967d5ba | 991 | } |
b967d5ba BA |
992 | }); |
993 | } | |
5d75c82c BA |
994 | return moves; |
995 | } | |
996 | ||
997 | getSlideNJumpMoves([x, y], steps, oneStep) { | |
998 | let moves = []; | |
999 | outerLoop: for (let step of steps) { | |
1000 | let i = x + step[0]; | |
1001 | let j = y + step[1]; | |
1002 | while ( | |
1003 | V.OnBoard(i, j) && | |
1004 | ( | |
1005 | this.board[i][j] == V.EMPTY || | |
77fc0c65 | 1006 | this.getPiece(i, j) == V.INVISIBLE_QUEEN || |
5d75c82c BA |
1007 | ( |
1008 | this.getColor(i, j) == 'a' && | |
1009 | [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j)) | |
1010 | ) | |
1011 | ) | |
1012 | ) { | |
77fc0c65 | 1013 | moves.push(this.getBasicMove({ x: x, y: y }, [i, j])); |
5d75c82c BA |
1014 | if (oneStep) continue outerLoop; |
1015 | i += step[0]; | |
1016 | j += step[1]; | |
1017 | } | |
1018 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j])) | |
77fc0c65 | 1019 | moves.push(this.getBasicMove({ x: x, y: y }, [i, j])); |
5d75c82c BA |
1020 | } |
1021 | return moves; | |
90df90bc BA |
1022 | } |
1023 | ||
ad030c7d | 1024 | getAllPotentialMoves() { |
5d75c82c | 1025 | if (this.subTurn == 1) return super.getAllPotentialMoves(); |
b967d5ba BA |
1026 | let moves = []; |
1027 | const L = this.firstMove.length; | |
1028 | const fm = this.firstMove[L-1]; | |
15d69043 BA |
1029 | switch (fm.end.effect) { |
1030 | case 0: | |
1031 | moves.push({ | |
1032 | start: { x: -1, y: -1 }, | |
1033 | end: { x: -1, y: -1 }, | |
1034 | appear: [], | |
1035 | vanish: [] | |
1036 | }); | |
1037 | for ( | |
1038 | let step of | |
1039 | (fm.vanish[0].p == V.ROOK ? V.steps[V.BISHOP] : V.steps[V.ROOK]) | |
1040 | ) { | |
00eef1ca | 1041 | const [i, j] = [fm.appear[0].x + step[0], fm.appear[0].y + step[1]]; |
15d69043 BA |
1042 | if ( |
1043 | V.OnBoard(i, j) && | |
1044 | (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a') | |
1045 | ) { | |
1046 | let m = new Move({ | |
1047 | start: { x: -1, y: -1 }, | |
1048 | end: { x: i, y: j }, | |
1049 | appear: [ | |
1050 | new PiPo({ | |
1051 | x: i, | |
1052 | y: j, | |
1053 | c: 'a', | |
1054 | p: (fm.vanish[0].p == V.ROOK ? V.BANANA : V.BOMB) | |
1055 | }) | |
1056 | ], | |
1057 | vanish: [] | |
1058 | }); | |
1059 | if (this.board[i][j] != V.EMPTY) { | |
1060 | m.vanish.push( | |
1061 | new PiPo({ x: i, y: j, c: 'a', p: this.getPiece(i, j) })); | |
1062 | } | |
1063 | moves.push(m); | |
1064 | } | |
1065 | } | |
1066 | break; | |
00eef1ca BA |
1067 | case "kingboo": { |
1068 | const [x, y] = [fm.appear[0].x, fm.appear[0].y]; | |
15d69043 BA |
1069 | for (let i=0; i<8; i++) { |
1070 | for (let j=0; j<8; j++) { | |
1071 | const colIJ = this.getColor(i, j); | |
1072 | if ( | |
1073 | i != x && | |
1074 | j != y && | |
1075 | this.board[i][j] != V.EMPTY && | |
1076 | colIJ != 'a' | |
1077 | ) { | |
1078 | const movedUnit = new PiPo({ | |
1079 | x: x, | |
1080 | y: y, | |
1081 | c: colIJ, | |
1082 | p: this.getPiece(i, j) | |
1083 | }); | |
77fc0c65 | 1084 | let mMove = this.getBasicMove({ x: x, y: y }, [i, j]); |
15d69043 BA |
1085 | mMove.appear.push(movedUnit); |
1086 | moves.push(mMove); | |
1087 | } | |
1088 | } | |
1089 | } | |
1090 | break; | |
1091 | } | |
00eef1ca | 1092 | case "toadette": { |
15d69043 BA |
1093 | const x = V.size.x + (this.turn == 'w' ? 0 : 1); |
1094 | for (let y = 0; y < 8; y++) | |
1095 | Array.prototype.push.apply(moves, this.getReserveMoves([x, y])); | |
1096 | break; | |
1097 | } | |
00eef1ca BA |
1098 | case "daisy": |
1099 | moves = super.getPotentialMovesFrom([fm.appear[0].x, fm.appear[0].y]); | |
15d69043 BA |
1100 | break; |
1101 | } | |
1102 | return moves; | |
6c7cbfed BA |
1103 | } |
1104 | ||
1c15969e | 1105 | doClick(square) { |
82820616 | 1106 | const L = this.firstMove.length; |
596e24d0 BA |
1107 | const fm = (L > 0 ? this.firstMove[L-1] : null); |
1108 | if ( | |
1109 | isNaN(square[0]) || | |
1110 | this.subTurn == 1 || | |
1111 | !([0, "daisy"].includes(fm.end.effect)) | |
1112 | ) { | |
1113 | return null; | |
1114 | } | |
82820616 | 1115 | const [x, y] = [square[0], square[1]]; |
00eef1ca BA |
1116 | const deltaX = Math.abs(fm.appear[0].x - x); |
1117 | const deltaY = Math.abs(fm.appear[0].y - y); | |
82820616 | 1118 | if ( |
596e24d0 | 1119 | fm.end.effect == 0 && |
15d69043 | 1120 | (this.board[x][y] == V.EMPTY || this.getColor(x, y) == 'a') && |
82820616 BA |
1121 | ( |
1122 | (fm.vanish[0].p == V.ROOK && deltaX == 1 && deltaY == 1) || | |
1123 | (fm.vanish[0].p == V.BISHOP && deltaX + deltaY == 1) | |
1124 | ) | |
1125 | ) { | |
15d69043 | 1126 | let m = new Move({ |
82820616 BA |
1127 | start: { x: -1, y: -1 }, |
1128 | end: { x: x, y: y }, | |
1129 | appear: [ | |
1130 | new PiPo({ | |
1131 | x: x, | |
1132 | y: y, | |
1133 | c: 'a', | |
1134 | p: (fm.vanish[0].p == V.ROOK ? V.BANANA : V.BOMB) | |
1135 | }) | |
1136 | ], | |
1137 | vanish: [] | |
1138 | }); | |
15d69043 BA |
1139 | if (this.board[x][y] != V.EMPTY) { |
1140 | m.vanish.push( | |
1141 | new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) })); | |
1142 | } | |
1143 | return m; | |
1c15969e | 1144 | } |
596e24d0 BA |
1145 | else if ( |
1146 | fm.end.effect == "daisy" && | |
1147 | deltaX == 0 && deltaY == 0 && | |
1148 | !this.canMove([x, y]) | |
1149 | ) { | |
1150 | // No possible move: return empty move | |
1151 | return { | |
1152 | start: { x: -1, y: -1 }, | |
1153 | end: { x: -1, y: -1 }, | |
1154 | appear: [], | |
1155 | vanish: [] | |
1156 | }; | |
1157 | } | |
1c15969e BA |
1158 | return null; |
1159 | } | |
1160 | ||
5d75c82c | 1161 | play(move) { |
596e24d0 BA |
1162 | // if (!this.states) this.states = []; |
1163 | // const stateFen = this.getFen(); | |
1164 | // this.states.push(stateFen); | |
1165 | ||
b967d5ba | 1166 | move.flags = JSON.stringify(this.aggregateFlags()); |
b967d5ba | 1167 | V.PlayOnBoard(this.board, move); |
15d69043 | 1168 | move.turn = [this.turn, this.subTurn]; |
00eef1ca | 1169 | if ([0, "kingboo", "toadette", "daisy"].includes(move.end.effect)) { |
b967d5ba BA |
1170 | this.firstMove.push(move); |
1171 | this.subTurn = 2; | |
b967d5ba BA |
1172 | } |
1173 | else { | |
1174 | this.turn = V.GetOppCol(this.turn); | |
15d69043 | 1175 | this.movesCount++; |
b967d5ba | 1176 | this.subTurn = 1; |
b967d5ba | 1177 | } |
15d69043 | 1178 | this.postPlay(move); |
1c15969e | 1179 | } |
5d75c82c | 1180 | |
b9ce3d0f | 1181 | postPlay(move) { |
00eef1ca BA |
1182 | if (move.end.effect == "toadette") this.reserve = this.captured; |
1183 | else this.reserve = undefined; | |
596e24d0 | 1184 | const color = move.turn[0]; |
77fc0c65 | 1185 | if (move.vanish.length == 2 && move.vanish[1].c != 'a') { |
b9ce3d0f | 1186 | // Capture: update this.captured |
77fc0c65 BA |
1187 | let capturedPiece = move.vanish[1].p; |
1188 | if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN; | |
1189 | else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece)) | |
1190 | capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece]; | |
1191 | this.captured[move.vanish[1].c][capturedPiece]++; | |
1192 | } | |
5d75c82c | 1193 | else if (move.vanish.length == 0) { |
00eef1ca | 1194 | if (move.appear.length == 0 || move.appear[0].c == 'a') return; |
5d75c82c | 1195 | // A piece is back on board |
00eef1ca | 1196 | this.captured[move.appear[0].c][move.appear[0].p]--; |
5d75c82c | 1197 | } |
596e24d0 BA |
1198 | if (move.appear.length == 0) { |
1199 | // Three cases: king "shell capture", Chomp or Koopa | |
1200 | if (this.getPiece(move.start.x, move.start.y) == V.KING) | |
1201 | // King remote capture: | |
1202 | this.powerFlags[color][V.KING] = false; | |
1203 | else if (move.end.effect == "chomp") | |
1204 | this.captured[color][move.vanish[0].p]++; | |
1205 | } | |
1206 | else if (move.appear[0].p == V.INVISIBLE_QUEEN) | |
1207 | this.powerFlags[move.appear[0].c][V.QUEEN] = false; | |
b36db525 | 1208 | if (this.subTurn == 2) return; |
596e24d0 | 1209 | if ( |
b36db525 | 1210 | move.turn[1] == 1 && |
596e24d0 BA |
1211 | move.appear.length == 0 || |
1212 | !(Object.keys(V.IMMOBILIZE_DECODE).includes(move.appear[0].p)) | |
1213 | ) { | |
1214 | // Look for an immobilized piece of my color: it can now move | |
596e24d0 BA |
1215 | for (let i=0; i<8; i++) { |
1216 | for (let j=0; j<8; j++) { | |
1217 | if (this.board[i][j] != V.EMPTY) { | |
596e24d0 BA |
1218 | const piece = this.getPiece(i, j); |
1219 | if ( | |
b36db525 | 1220 | this.getColor(i, j) == color && |
596e24d0 BA |
1221 | Object.keys(V.IMMOBILIZE_DECODE).includes(piece) |
1222 | ) { | |
1223 | this.board[i][j] = color + V.IMMOBILIZE_DECODE[piece]; | |
1224 | move.wasImmobilized = [i, j]; | |
1225 | } | |
15d69043 BA |
1226 | } |
1227 | } | |
1228 | } | |
1229 | } | |
b36db525 BA |
1230 | // Also make opponent invisible queen visible again, if any |
1231 | const oppCol = V.GetOppCol(color); | |
1232 | for (let i=0; i<8; i++) { | |
1233 | for (let j=0; j<8; j++) { | |
1234 | if ( | |
1235 | this.board[i][j] != V.EMPTY && | |
1236 | this.getColor(i, j) == oppCol && | |
1237 | this.getPiece(i, j) == V.INVISIBLE_QUEEN | |
1238 | ) { | |
1239 | this.board[i][j] = oppCol + V.QUEEN; | |
1240 | move.wasInvisible = [i, j]; | |
1241 | } | |
1242 | } | |
1243 | } | |
5d75c82c BA |
1244 | } |
1245 | ||
1246 | undo(move) { | |
15d69043 BA |
1247 | this.disaggregateFlags(JSON.parse(move.flags)); |
1248 | V.UndoOnBoard(this.board, move); | |
00eef1ca BA |
1249 | if ([0, "kingboo", "toadette", "daisy"].includes(move.end.effect)) |
1250 | this.firstMove.pop(); | |
15d69043 | 1251 | else this.movesCount--; |
15d69043 BA |
1252 | this.turn = move.turn[0]; |
1253 | this.subTurn = move.turn[1]; | |
1254 | this.postUndo(move); | |
596e24d0 BA |
1255 | |
1256 | // const stateFen = this.getFen(); | |
1257 | // if (stateFen != this.states[this.states.length-1]) debugger; | |
1258 | // this.states.pop(); | |
b9ce3d0f BA |
1259 | } |
1260 | ||
1261 | postUndo(move) { | |
15d69043 BA |
1262 | if (!!move.wasImmobilized) { |
1263 | const [i, j] = move.wasImmobilized; | |
1264 | this.board[i][j] = | |
1265 | this.getColor(i, j) + V.IMMOBILIZE_CODE[this.getPiece(i, j)]; | |
1266 | } | |
1267 | if (!!move.wasInvisible) { | |
1268 | const [i, j] = move.wasInvisible; | |
596e24d0 | 1269 | this.board[i][j] = this.getColor(i, j) + V.INVISIBLE_QUEEN; |
15d69043 | 1270 | } |
77fc0c65 BA |
1271 | if (move.vanish.length == 2 && move.vanish[1].c != 'a') { |
1272 | let capturedPiece = move.vanish[1].p; | |
1273 | if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN; | |
1274 | else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece)) | |
1275 | capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece]; | |
1276 | this.captured[move.vanish[1].c][capturedPiece]--; | |
1277 | } | |
15d69043 | 1278 | else if (move.vanish.length == 0) { |
00eef1ca BA |
1279 | if (move.appear.length == 0 || move.appear[0].c == 'a') return; |
1280 | // A piece was back on board | |
1281 | this.captured[move.appear[0].c][move.appear[0].p]++; | |
15d69043 | 1282 | } |
596e24d0 BA |
1283 | else if (move.appear.length == 0 && move.end.effect == "chomp") |
1284 | this.captured[move.vanish[0].c][move.vanish[0].p]--; | |
15d69043 | 1285 | if (move.vanish.length == 0) this.reserve = this.captured; |
00eef1ca | 1286 | else this.reserve = undefined; |
b9ce3d0f | 1287 | } |
1c15969e | 1288 | |
5d75c82c BA |
1289 | getCheckSquares() { |
1290 | return []; | |
1291 | } | |
1292 | ||
6c7cbfed | 1293 | getCurrentScore() { |
5d75c82c BA |
1294 | // Find kings (not tracked in this variant) |
1295 | let kingThere = { w: false, b: false }; | |
1296 | for (let i=0; i<8; i++) { | |
1297 | for (let j=0; j<8; j++) { | |
00eef1ca BA |
1298 | if ( |
1299 | this.board[i][j] != V.EMPTY && | |
1300 | ['k', 'l'].includes(this.getPiece(i, j)) | |
1301 | ) { | |
5d75c82c | 1302 | kingThere[this.getColor(i, j)] = true; |
00eef1ca | 1303 | } |
5d75c82c BA |
1304 | } |
1305 | } | |
1306 | if (!kingThere['w']) return "0-1"; | |
1307 | if (!kingThere['b']) return "1-0"; | |
00eef1ca | 1308 | if (!this.atLeastOneMove()) return (this.turn == 'w' ? "0-1" : "1-0"); |
5d75c82c | 1309 | return "*"; |
6c7cbfed BA |
1310 | } |
1311 | ||
b9ce3d0f BA |
1312 | static GenRandInitFen(randomness) { |
1313 | return ( | |
5d75c82c | 1314 | SuicideRules.GenRandInitFen(randomness).slice(0, -1) + |
596e24d0 BA |
1315 | // Add Peach + Mario flags + capture counts |
1316 | "1111 000000000000" | |
b9ce3d0f BA |
1317 | ); |
1318 | } | |
1319 | ||
5d75c82c BA |
1320 | filterValid(moves) { |
1321 | return moves; | |
1322 | } | |
1323 | ||
13ce1e9b BA |
1324 | static get VALUES() { |
1325 | return Object.assign( | |
1326 | {}, | |
1327 | ChessRules.VALUES, | |
1328 | { | |
1329 | s: 1, | |
1330 | u: 5, | |
1331 | o: 3, | |
1332 | c: 3, | |
1333 | t: 9, | |
1334 | l: 1000, | |
1335 | e: 0, | |
1336 | d: 0, | |
1337 | w: 0, | |
1338 | m: 0 | |
1339 | } | |
1340 | ); | |
1341 | } | |
1342 | ||
1343 | static get SEARCH_DEPTH() { | |
1344 | return 1; | |
1345 | } | |
1346 | ||
1c15969e | 1347 | getComputerMove() { |
5d75c82c | 1348 | const moves = this.getAllValidMoves(); |
13ce1e9b BA |
1349 | // Split into "normal" and "random" moves: |
1350 | // (Next splitting condition is OK because cannot take self object | |
1351 | // without a banana or bomb on the way). | |
1352 | const deterministicMoves = moves.filter(m => { | |
1353 | return m.vanish.every(a => a.c != 'a' || a.p == V.MUSHROOM); | |
1354 | }); | |
1355 | const randomMoves = moves.filter(m => { | |
1356 | return m.vanish.some(a => a.c == 'a' && a.p != V.MUSHROOM); | |
1357 | }); | |
1358 | if (Math.random() < deterministicMoves.length / randomMoves.length) | |
1359 | // Play a deterministic one: capture king or material if possible | |
1360 | return super.getComputerMove(deterministicMoves); | |
1361 | // Play a random effect move, at random: | |
032ecd58 | 1362 | let move1 = randomMoves[randInt(randomMoves.length)]; |
b967d5ba BA |
1363 | this.play(move1); |
1364 | let move2 = undefined; | |
1365 | if (this.subTurn == 2) { | |
1366 | const moves2 = this.getAllValidMoves(); | |
1367 | move2 = moves2[randInt(moves2.length)]; | |
1368 | } | |
1369 | this.undo(move1); | |
1370 | if (!move2) return move1; | |
1371 | return [move1, move2]; | |
1c15969e | 1372 | } |
b9ce3d0f BA |
1373 | |
1374 | getNotation(move) { | |
77fc0c65 | 1375 | if (move.vanish.length == 0 && move.appear.length == 0) return "-"; |
00eef1ca BA |
1376 | if ( |
1377 | !move.end.effect && | |
1378 | move.appear.length > 0 && | |
1379 | move.appear[0].p == V.INVISIBLE_QUEEN | |
1380 | ) { | |
1381 | return "Q??"; | |
1382 | } | |
1383 | const finalSquare = V.CoordsToSquare(move.end); | |
77fc0c65 BA |
1384 | // Next condition also includes Toadette placements: |
1385 | if (move.appear.length > 0 && move.vanish.every(a => a.c == 'a')) { | |
1386 | const piece = | |
1387 | move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : ""; | |
1388 | return piece + "@" + finalSquare; | |
1389 | } | |
1390 | else if (move.appear.length == 0) { | |
1391 | const piece = this.getPiece(move.start.x, move.start.y); | |
1392 | if (piece == V.KING && !move.end.effect) | |
1393 | // King remote capture | |
1394 | return "Kx" + finalSquare; | |
1395 | // Koopa or Chomp, or loopback after bananas, bombs & mushrooms: | |
00eef1ca BA |
1396 | return ( |
1397 | piece.toUpperCase() + "x" + finalSquare + | |
77fc0c65 BA |
1398 | ( |
1399 | !!move.end.effect | |
1400 | ? "*" + (move.end.effect == "koopa" ? "K" : "C") | |
1401 | : "" | |
1402 | ) | |
00eef1ca BA |
1403 | ); |
1404 | } | |
42e37983 BA |
1405 | if (move.appear.length == 1 && move.vanish.length == 1) { |
1406 | const moveStart = move.appear[0].p.toUpperCase() + "@"; | |
1407 | if (move.appear[0].c == 'a' && move.vanish[0].c == 'a') | |
1408 | // Bonus replacement: | |
1409 | return moveStart + finalSquare; | |
1410 | if ( | |
1411 | move.vanish[0].p == V.INVISIBLE_QUEEN && | |
1412 | move.appear[0].x == move.vanish[0].x && | |
1413 | move.appear[0].y == move.vanish[0].y | |
1414 | ) { | |
1415 | // Toadette takes invisible queen | |
1416 | return moveStart + "Q" + finalSquare; | |
1417 | } | |
596e24d0 | 1418 | } |
77fc0c65 BA |
1419 | if ( |
1420 | move.appear.length == 2 && | |
1421 | move.vanish.length == 2 && | |
1422 | move.appear.every(a => a.c != 'a') && | |
1423 | move.vanish.every(v => v.c != 'a') | |
1424 | ) { | |
1425 | // King Boo exchange | |
1426 | return move.vanish[1].p.toUpperCase() + finalSquare; | |
1427 | } | |
1428 | const piece = move.vanish[0].p; | |
00eef1ca BA |
1429 | let notation = undefined; |
1430 | if (piece == V.PAWN) { | |
1431 | // Pawn move | |
1432 | if (move.vanish.length >= 2) { | |
1433 | // Capture | |
1434 | const startColumn = V.CoordToColumn(move.start.y); | |
1435 | notation = startColumn + "x" + finalSquare; | |
1436 | } | |
1437 | else notation = finalSquare; | |
1438 | if (move.appear[0].p != V.PAWN) | |
1439 | // Promotion | |
1440 | notation += "=" + move.appear[0].p.toUpperCase(); | |
1441 | } | |
1442 | else { | |
1443 | notation = | |
1444 | piece.toUpperCase() + | |
1445 | (move.vanish.length >= 2 ? "x" : "") + | |
1446 | finalSquare; | |
1447 | } | |
1448 | if (!!move.end.effect) { | |
1449 | switch (move.end.effect) { | |
1450 | case "kingboo": | |
1451 | notation += "*B"; | |
1452 | break; | |
1453 | case "toadette": | |
1454 | notation += "*T"; | |
1455 | break; | |
1456 | case "daisy": | |
1457 | notation += "*D"; | |
1458 | break; | |
1459 | case "bowser": | |
1460 | notation += "*M"; | |
1461 | break; | |
1462 | case "luigi": | |
00eef1ca | 1463 | case "waluigi": |
77fc0c65 BA |
1464 | const lastAppear = move.appear[move.appear.length - 1]; |
1465 | const effectOn = | |
1466 | V.CoordsToSquare({ x: lastAppear.x, y : lastAppear.y }); | |
1467 | notation += "*" + move.end.effect[0].toUpperCase() + effectOn; | |
00eef1ca BA |
1468 | break; |
1469 | } | |
1470 | } | |
00eef1ca | 1471 | return notation; |
b9ce3d0f | 1472 | } |
6c7cbfed | 1473 | }; |