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