1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { SuicideRules
} from "@/variants/Suicide";
3 import { ArrayFun
} from "@/utils/array";
4 import { randInt
} from "@/utils/alea";
6 export class ChakartRules
extends ChessRules
{
8 static get PawnSpecs() {
9 return SuicideRules
.PawnSpecs
;
12 static get HasCastle() {
16 static get HasEnpassant() {
20 static get CorrConfirm() {
21 // Because of bonus effects
25 static get CanAnalyze() {
29 static get SomeHiddenMoves() {
33 hoverHighlight(x
, y
) {
34 if (this.subTurn
== 1) return false;
35 const L
= this.firstMove
.length
;
36 const fm
= this.firstMove
[L
-1];
37 if (fm
.end
.effect
!= 0) return false;
38 const deltaX
= Math
.abs(fm
.appear
[0].x
- x
);
39 const deltaY
= Math
.abs(fm
.appear
[0].y
- y
);
41 (this.board
[x
][y
] == V
.EMPTY
|| this.getColor(x
, y
) == 'a') &&
43 (fm
.vanish
[0].p
== V
.ROOK
&& deltaX
== 1 && deltaY
== 1) ||
44 (fm
.vanish
[0].p
== V
.BISHOP
&& deltaX
+ deltaY
== 1)
49 static get IMMOBILIZE_CODE() {
60 static get IMMOBILIZE_DECODE() {
71 static get INVISIBLE_QUEEN() {
75 // Fictive color 'a', bomb banana mushroom egg
80 return 'd'; //"Donkey"
85 static get MUSHROOM() {
92 ? "w" + f
.toLowerCase()
93 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
99 ChessRules
.PIECES
.concat(
100 Object
.keys(V
.IMMOBILIZE_DECODE
)).concat(
101 [V
.BANANA
, V
.BOMB
, V
.EGG
, V
.MUSHROOM
, V
.INVISIBLE_QUEEN
])
109 b
[1] == V
.INVISIBLE_QUEEN
||
110 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(b
[1])
118 if (!!m
.promoteInto
) return m
.promoteInto
;
119 let piece
= m
.appear
[0].p
;
120 if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
))
121 piece
= V
.IMMOBILIZE_DECODE
[piece
];
122 return this.getPpath(m
.appear
[0].c
+ piece
);
125 static ParseFen(fen
) {
126 const fenParts
= fen
.split(" ");
127 return Object
.assign(
128 ChessRules
.ParseFen(fen
),
129 { captured: fenParts
[4] }
133 static IsGoodFen(fen
) {
134 if (!ChessRules
.IsGoodFen(fen
)) return false;
135 const captured
= V
.ParseFen(fen
).captured
;
136 if (!captured
|| !captured
.match(/^[0-9]{12,12}$/)) return false;
140 // King can be l or L (immobilized) --> similar to Alice variant
141 static IsGoodPosition(position
) {
142 if (position
.length
== 0) return false;
143 const rows
= position
.split("/");
144 if (rows
.length
!= V
.size
.x
) return false;
145 let kings
= { "k": 0, "K": 0, 'l': 0, 'L': 0 };
146 for (let row
of rows
) {
148 for (let i
= 0; i
< row
.length
; i
++) {
149 if (['K', 'k', 'L', 'l'].includes(row
[i
])) kings
[row
[i
]]++;
150 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
152 const num
= parseInt(row
[i
], 10);
153 if (isNaN(num
)) return false;
157 if (sumElts
!= V
.size
.y
) return false;
159 if (kings
['k'] + kings
['l'] == 0 || kings
['K'] + kings
['L'] == 0)
164 static IsGoodFlags(flags
) {
165 // 4 for Peach + Mario w, b
166 return !!flags
.match(/^[01]{4,4}$/);
170 // King can send shell? Queen can be invisible?
172 w: { 'k': false, 'q': false },
173 b: { 'k': false, 'q': false }
175 for (let c
of ["w", "b"]) {
176 for (let p
of ['k', 'q']) {
177 this.powerFlags
[c
][p
] =
178 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
184 return this.powerFlags
;
187 disaggregateFlags(flags
) {
188 this.powerFlags
= flags
;
192 return super.getFen() + " " + this.getCapturedFen();
196 return super.getFenForRepeat() + "_" + this.getCapturedFen();
200 let counts
= [...Array(12).fill(0)];
202 for (let p
of V
.RESERVE_PIECES
) {
203 counts
[i
] = this.captured
["w"][p
];
204 counts
[6 + i
] = this.captured
["b"][p
];
207 return counts
.join("");
212 setOtherVariables(fen
) {
213 super.setOtherVariables(fen
);
214 // Initialize captured pieces' counts from FEN
216 V
.ParseFen(fen
).captured
.split("").map(x
=> parseInt(x
, 10));
219 [V
.PAWN
]: captured
[0],
220 [V
.ROOK
]: captured
[1],
221 [V
.KNIGHT
]: captured
[2],
222 [V
.BISHOP
]: captured
[3],
223 [V
.QUEEN
]: captured
[4],
224 [V
.KING
]: captured
[5]
227 [V
.PAWN
]: captured
[6],
228 [V
.ROOK
]: captured
[7],
229 [V
.KNIGHT
]: captured
[8],
230 [V
.BISHOP
]: captured
[9],
231 [V
.QUEEN
]: captured
[10],
232 [V
.KING
]: captured
[11]
242 for (let c
of ["w", "b"])
243 for (let p
of ['k', 'q']) fen
+= (this.powerFlags
[c
][p
] ? "1" : "0");
248 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
249 return this.board
[i
][j
].charAt(0);
253 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
254 return this.board
[i
][j
].charAt(1);
257 getReservePpath(index
, color
) {
258 return color
+ V
.RESERVE_PIECES
[index
];
261 static get RESERVE_PIECES() {
262 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
265 getReserveMoves([x
, y
]) {
266 const color
= this.turn
;
267 const p
= V
.RESERVE_PIECES
[y
];
268 if (this.reserve
[color
][p
] == 0) return [];
270 const start
= (color
== 'w' && p
== V
.PAWN
? 1 : 0);
271 const end
= (color
== 'b' && p
== V
.PAWN
? 7 : 8);
272 for (let i
= start
; i
< end
; i
++) {
273 for (let j
= 0; j
< V
.size
.y
; j
++) {
275 this.board
[i
][j
] == V
.EMPTY
||
276 this.getColor(i
, j
) == 'a' ||
277 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
279 let m
= this.getBasicMove({ p: p
, x: i
, y: j
});
280 m
.start
= { x: x
, y: y
};
288 getPotentialMovesFrom([x
, y
]) {
290 if (this.subTurn
== 1) {
291 moves
= super.getPotentialMovesFrom([x
, y
]);
292 const finalPieces
= V
.PawnSpecs
.promotions
;
293 const color
= this.turn
;
294 const lastRank
= (color
== "w" ? 0 : 7);
298 m
.appear
.length
> 0 &&
299 ['p', 's'].includes(m
.appear
[0].p
) &&
300 m
.appear
[0].x
== lastRank
302 for (let i
= 1; i
< finalPieces
.length
; i
++) {
303 const piece
= finalPieces
[i
];
304 let otherM
= JSON
.parse(JSON
.stringify(m
));
306 m
.appear
[0].p
== V
.PAWN
308 : V
.IMMOBILIZE_CODE
[finalPieces
[i
]];
311 // Finally alter m itself:
313 m
.appear
[0].p
== V
.PAWN
315 : V
.IMMOBILIZE_CODE
[finalPieces
[0]];
318 Array
.prototype.push
.apply(moves
, pMoves
);
322 const L
= this.firstMove
.length
;
323 const fm
= this.firstMove
[L
-1];
324 switch (fm
.end
.effect
) {
326 // Exchange position with any piece,
327 // except pawns if arriving on last rank.
328 const lastRank
= { 'w': 0, 'b': 7 };
329 const color
= this.turn
;
330 const allowLastRank
= (this.getPiece(x
, y
) != V
.PAWN
);
331 for (let i
=0; i
<8; i
++) {
332 for (let j
=0; j
<8; j
++) {
333 const colIJ
= this.getColor(i
, j
);
335 (i
!= x
|| j
!= y
) &&
336 this.board
[i
][j
] != V
.EMPTY
&&
339 const pieceIJ
= this.getPiece(i
, j
);
341 (pieceIJ
!= V
.PAWN
|| x
!= lastRank
[colIJ
]) &&
342 (allowLastRank
|| i
!= lastRank
[color
])
344 const movedUnit
= new PiPo({
348 p: this.getPiece(i
, j
)
350 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
351 mMove
.appear
.push(movedUnit
);
359 // Resurrect a captured piece
360 if (x
>= V
.size
.x
) moves
= this.getReserveMoves([x
, y
]);
363 // Play again with the same piece
364 if (fm
.appear
[0].x
== x
&& fm
.appear
[0].y
== y
)
365 moves
= super.getPotentialMovesFrom([x
, y
]);
372 // Helper for getBasicMove()
373 getRandomSquare([x
, y
], steps
) {
374 const color
= this.turn
;
375 const validSteps
= steps
.filter(s
=> {
376 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
379 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) != color
)
382 if (validSteps
.length
== 0)
383 // Can happen after mushroom jump
385 const step
= validSteps
[randInt(validSteps
.length
)];
386 return [x
+ step
[0], y
+ step
[1]];
389 canMove([x
, y
], piece
) {
390 const color
= this.getColor(x
, y
);
391 const oppCol
= V
.GetOppCol(color
);
392 piece
= piece
|| this.getPiece(x
, y
);
393 if (piece
== V
.PAWN
) {
394 const forward
= (color
== 'w' ? -1 : 1);
396 V
.OnBoard(x
+ forward
, y
) &&
398 this.board
[x
+ forward
][y
] == V
.EMPTY
||
400 V
.OnBoard(x
+ forward
, y
+ 1) &&
401 this.board
[x
+ forward
][y
+ 1] != V
.EMPTY
&&
402 this.getColor
[x
+ forward
, y
+ 1] == oppCol
405 V
.OnBoard(x
+ forward
, y
- 1) &&
406 this.board
[x
+ forward
][y
- 1] != V
.EMPTY
&&
407 this.getColor
[x
+ forward
, y
- 1] == oppCol
412 // Checking one step is enough:
414 [V
.KING
, V
.QUEEN
].includes(piece
)
415 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
417 for (let step
of steps
) {
418 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
421 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) != color
)
429 // Apply mushroom, bomb or banana effect (hidden to the player).
430 // Determine egg effect, too, and apply its first part if possible.
431 getBasicMove_aux(psq1
, sq2
, tr
, initMove
) {
432 const [x1
, y1
] = [psq1
.x
, psq1
.y
];
433 const color1
= this.turn
;
434 const piece1
= (!!tr
? tr
.p : (psq1
.p
|| this.getPiece(x1
, y1
)));
435 const oppCol
= V
.GetOppCol(color1
);
441 // banana or bomb defines next square, or the move ends there
450 if (this.board
[x1
][y1
] != V
.EMPTY
) {
451 const initP1
= this.getPiece(x1
, y1
);
456 c: this.getColor(x1
, y1
),
460 if ([V
.BANANA
, V
.BOMB
].includes(initP1
)) {
461 const steps
= V
.steps
[initP1
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
462 move.next
= this.getRandomSquare([x1
, y1
], steps
);
465 move.end
= { x: x1
, y: y1
};
468 const [x2
, y2
] = [sq2
[0], sq2
[1]];
469 // The move starts normally, on board:
470 let move = super.getBasicMove([x1
, y1
], [x2
, y2
], tr
);
471 if (!!tr
) move.promoteInto
= tr
.c
+ tr
.p
; //in case of (chomped...)
472 const L
= this.firstMove
.length
;
474 [V
.PAWN
, V
.KNIGHT
].includes(piece1
) &&
476 (this.subTurn
== 1 || this.firstMove
[L
-1].end
.effect
== "daisy")
480 const twoSquaresMove
= (Math
.abs(x2
- x1
) == 2);
481 const mushroomX
= x1
+ (twoSquaresMove
? (x2
- x1
) / 2 : 0);
490 if (this.getColor(mushroomX
, y1
) == 'a') {
496 p: this.getPiece(mushroomX
, y1
)
503 const deltaX
= Math
.abs(x2
- x1
);
504 const deltaY
= Math
.abs(y2
- y1
);
506 x1
+ (deltaX
== 2 ? (x2
- x1
) / 2 : 0),
507 y1
+ (deltaY
== 2 ? (y2
- y1
) / 2 : 0)
510 this.board
[eggSquare
[0]][eggSquare
[1]] != V
.EMPTY
&&
511 this.getColor(eggSquare
[0], eggSquare
[1]) != 'a'
524 if (this.getColor(eggSquare
[0], eggSquare
[1]) == 'a') {
530 p: this.getPiece(eggSquare
[0], eggSquare
[1])
538 // For (wa)luigi effect:
539 const changePieceColor
= (color
) => {
541 const oppLastRank
= (color
== 'w' ? 7 : 0);
542 for (let i
=0; i
<8; i
++) {
543 for (let j
=0; j
<8; j
++) {
545 (i
!= move.vanish
[0].x
|| j
!= move.vanish
[0].y
) &&
546 this.board
[i
][j
] != V
.EMPTY
&&
547 this.getColor(i
, j
) == color
549 const piece
= this.getPiece(i
, j
);
550 if (piece
!= V
.KING
&& (piece
!= V
.PAWN
|| i
!= oppLastRank
))
551 pieces
.push({ x: i
, y: j
, p: piece
});
555 // Special case of the current piece (still at its initial position)
557 pieces
.push({ x: move.appear
[0].x
, y: move.appear
[0].y
, p: piece1
});
558 const cp
= pieces
[randInt(pieces
.length
)];
559 if (move.appear
[0].x
!= cp
.x
|| move.appear
[0].y
!= cp
.y
) {
569 else move.appear
.shift();
574 c: V
.GetOppCol(color
),
579 const applyEggEffect
= () => {
580 if (this.subTurn
== 2)
581 // No egg effects at subTurn 2
583 // 1) Determine the effect (some may be impossible)
584 let effects
= ["kingboo", "koopa", "chomp", "bowser"];
585 if (Object
.values(this.captured
[color1
]).some(c
=> c
>= 1))
586 effects
.push("toadette");
587 const lastRank
= { 'w': 0, 'b': 7 };
588 let canPlayAgain
= undefined;
590 move.appear
[0].p
== V
.PAWN
&&
591 move.appear
[0].x
== lastRank
[color1
]
593 // Always possible: promote into a queen, rook or king
597 move.end
.effect
= "daisy";
598 V
.PlayOnBoard(this.board
, move);
599 const square
= [move.appear
[0].x
, move.appear
[0].y
];
600 canPlayAgain
= this.canMove(square
, piece1
);
601 V
.UndoOnBoard(this.board
, move);
602 delete move.end
["effect"];
604 if (canPlayAgain
) effects
.push("daisy");
606 this.board
.some((b
,i
) =>
611 (cell
[1] != V
.PAWN
|| i
!= lastRank
[color1
])
616 effects
.push("luigi");
621 (piece1
!= V
.PAWN
|| move.appear
[0].x
!= lastRank
[oppCol
])
623 this.board
.some((b
,i
) =>
628 (cell
[1] != V
.PAWN
|| i
!= lastRank
[oppCol
])
633 effects
.push("waluigi");
635 const effect
= effects
[randInt(effects
.length
)];
636 move.end
.effect
= effect
;
637 // 2) Apply it if possible
638 if (!(["kingboo", "toadette", "daisy"].includes(effect
))) {
642 // Maybe egg effect was applied after others,
643 // so just shift vanish array:
650 move.appear
[0].p
= V
.IMMOBILIZE_CODE
[piece1
];
653 changePieceColor(oppCol
);
656 changePieceColor(color1
);
661 const applyMushroomEffect
= () => {
662 if ([V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece1
)) {
663 // Just make another similar step, if possible (non-capturing)
665 move.appear
[0].x
+ (x2
- x1
),
666 move.appear
[0].y
+ (y2
- y1
)
670 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
672 move.appear
[0].x
= i
;
673 move.appear
[0].y
= j
;
674 if (this.board
[i
][j
] != V
.EMPTY
) {
675 const object
= this.getPiece(i
, j
);
687 const steps
= V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
688 move.next
= this.getRandomSquare([i
, j
], steps
);
694 applyMushroomEffect();
701 // Queen, bishop or rook:
703 (x2
- x1
) / Math
.abs(x2
- x1
) || 0,
704 (y2
- y1
) / Math
.abs(y2
- y1
) || 0
706 const next
= [move.appear
[0].x
+ step
[0], move.appear
[0].y
+ step
[1]];
708 V
.OnBoard(next
[0], next
[1]) &&
709 this.board
[next
[0]][next
[1]] != V
.EMPTY
&&
710 this.getColor(next
[0], next
[1]) != 'a'
712 const afterNext
= [next
[0] + step
[0], next
[1] + step
[1]];
713 if (V
.OnBoard(afterNext
[0], afterNext
[1])) {
714 const afterColor
= this.getColor(afterNext
[0], afterNext
[1])
716 this.board
[afterNext
[0]][afterNext
[1]] == V
.EMPTY
||
719 move.appear
[0].x
= afterNext
[0];
720 move.appear
[0].y
= afterNext
[1];
721 if (this.board
[afterNext
[0]][afterNext
[1]] != V
.EMPTY
) {
722 // The "object" could also be an opponent's piece
723 const object
= this.getPiece(afterNext
[0], afterNext
[1]);
736 V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
737 move.next
= this.getRandomSquare(
738 [afterNext
[0], afterNext
[1]], steps
);
744 applyMushroomEffect();
753 const color2
= this.getColor(x2
, y2
);
754 const piece2
= this.getPiece(x2
, y2
);
759 const steps
= V
.steps
[piece2
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
760 move.next
= this.getRandomSquare([x2
, y2
], steps
);
763 applyMushroomEffect();
766 if (this.subTurn
== 1)
767 // No egg effect at subTurn 2
775 move.appear
.length
> 0 &&
776 [V
.ROOK
, V
.BISHOP
].includes(piece1
)
778 const finalSquare
= [move.appear
[0].x
, move.appear
[0].y
];
781 this.getColor(finalSquare
[0], finalSquare
[1]) != 'a' ||
782 this.getPiece(finalSquare
[0], finalSquare
[1]) != V
.EGG
785 V
.steps
[piece1
== V
.ROOK
? V
.BISHOP : V
.ROOK
].filter(s
=> {
786 const [i
, j
] = [finalSquare
[0] + s
[0], finalSquare
[1] + s
[1]];
789 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
792 if (validSteps
.length
>= 1) {
794 finalSquare
[0] + validSteps
[0][0],
795 finalSquare
[1] + validSteps
[0][1]
802 p: (piece1
== V
.ROOK
? V
.BANANA : V
.BOMB
)
805 if (this.board
[x
][y
] != V
.EMPTY
) {
807 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
815 getBasicMove(psq1
, sq2
, tr
) {
817 if (Array
.isArray(psq1
)) psq1
= { x: psq1
[0], y: psq1
[1] };
818 let m
= this.getBasicMove_aux(psq1
, sq2
, tr
, "initMove");
820 // Last move ended on bomb or banana, direction change
821 V
.PlayOnBoard(this.board
, m
);
823 m
= this.getBasicMove_aux(
824 { x: m
.appear
[0].x
, y: m
.appear
[0].y
}, m
.next
);
826 for (let i
=moves
.length
-1; i
>=0; i
--) V
.UndoOnBoard(this.board
, moves
[i
]);
828 // Now merge moves into one
830 // start is wrong for Toadette moves --> it's fixed later
831 move.start
= { x: psq1
.x
, y: psq1
.y
};
832 move.end
= !!sq2
? { x: sq2
[0], y: sq2
[1] } : { x: psq1
.x
, y: psq1
.y
};
833 if (!!tr
) move.promoteInto
= moves
[0].promoteInto
;
834 let lm
= moves
[moves
.length
-1];
835 if (this.subTurn
== 1 && !!lm
.end
.effect
)
836 move.end
.effect
= lm
.end
.effect
;
837 if (moves
.length
== 1) {
838 move.appear
= moves
[0].appear
;
839 move.vanish
= moves
[0].vanish
;
842 // Keep first vanish and last appear (if any)
843 move.appear
= lm
.appear
;
844 move.vanish
= moves
[0].vanish
;
846 move.vanish
.length
>= 1 &&
847 move.appear
.length
>= 1 &&
848 move.vanish
[0].x
== move.appear
[0].x
&&
849 move.vanish
[0].y
== move.appear
[0].y
851 // Loopback on initial square:
855 for (let i
=1; i
< moves
.length
- 1; i
++) {
856 for (let v
of moves
[i
].vanish
) {
857 // Only vanishing objects, not appearing at init move
861 moves
[0].appear
.length
== 1 ||
862 moves
[0].appear
[1].x
!= v
.x
||
863 moves
[0].appear
[1].y
!= v
.y
870 // Final vanish is our piece, but others might be relevant
871 // (for some egg bonuses at least).
872 for (let i
=1; i
< lm
.vanish
.length
; i
++) {
874 lm
.vanish
[i
].c
!= 'a' ||
875 moves
[0].appear
.length
== 1 ||
876 moves
[0].appear
[1].x
!= lm
.vanish
[i
].x
||
877 moves
[0].appear
[1].y
!= lm
.vanish
[i
].y
879 move.vanish
.push(lm
.vanish
[i
]);
886 getPotentialPawnMoves([x
, y
]) {
887 const color
= this.turn
;
888 const oppCol
= V
.GetOppCol(color
);
889 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
890 const shiftX
= V
.PawnSpecs
.directions
[color
];
891 const firstRank
= (color
== "w" ? sizeX
- 1 : 0);
894 this.board
[x
+ shiftX
][y
] == V
.EMPTY
||
895 this.getColor(x
+ shiftX
, y
) == 'a'
897 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
899 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
901 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
||
902 this.getColor(x
+ 2 * shiftX
, y
) == 'a'
905 moves
.push(this.getBasicMove({ x: x
, y: y
}, [x
+ 2 * shiftX
, y
]));
908 for (let shiftY
of [-1, 1]) {
911 y
+ shiftY
< sizeY
&&
912 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
913 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, y
+ shiftY
))
915 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
921 getPotentialQueenMoves(sq
) {
922 const normalMoves
= super.getPotentialQueenMoves(sq
);
923 // If flag allows it, add 'invisible movements'
924 let invisibleMoves
= [];
925 if (this.powerFlags
[this.turn
][V
.QUEEN
]) {
926 normalMoves
.forEach(m
=> {
928 m
.appear
.length
== 1 &&
929 m
.vanish
.length
== 1 &&
930 // Only simple non-capturing moves:
933 let im
= JSON
.parse(JSON
.stringify(m
));
934 im
.appear
[0].p
= V
.INVISIBLE_QUEEN
;
935 im
.end
.noHighlight
= true;
936 invisibleMoves
.push(im
);
940 return normalMoves
.concat(invisibleMoves
);
943 getPotentialKingMoves([x
, y
]) {
944 let moves
= super.getPotentialKingMoves([x
, y
]);
945 const color
= this.turn
;
946 // If flag allows it, add 'remote shell captures'
947 if (this.powerFlags
[this.turn
][V
.KING
]) {
948 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
949 const [nextX
, nextY
] = [x
+ step
[0], y
+ step
[1]];
951 V
.OnBoard(nextX
, nextY
) &&
953 this.board
[nextX
][nextY
] == V
.EMPTY
||
955 this.getColor(nextX
, nextY
) == 'a' &&
956 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(nextX
, nextY
))
960 let [i
, j
] = [x
+ 2 * step
[0], y
+ 2 * step
[1]];
964 this.board
[i
][j
] == V
.EMPTY
||
966 this.getColor(i
, j
) == 'a' &&
967 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
974 if (V
.OnBoard(i
, j
)) {
975 const colIJ
= this.getColor(i
, j
);
976 if (colIJ
!= color
) {
977 // May just destroy a bomb or banana:
980 start: { x: x
, y: y
},
985 x: i
, y: j
, c: colIJ
, p: this.getPiece(i
, j
)
998 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
1000 outerLoop: for (let step
of steps
) {
1001 let i
= x
+ step
[0];
1002 let j
= y
+ step
[1];
1006 this.board
[i
][j
] == V
.EMPTY
||
1007 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
1009 this.getColor(i
, j
) == 'a' &&
1010 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
1014 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1015 if (oneStep
) continue outerLoop
;
1019 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
1020 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1025 getAllPotentialMoves() {
1026 if (this.subTurn
== 1) return super.getAllPotentialMoves();
1028 const L
= this.firstMove
.length
;
1029 const fm
= this.firstMove
[L
-1];
1030 switch (fm
.end
.effect
) {
1033 start: { x: -1, y: -1 },
1034 end: { x: -1, y: -1 },
1040 (fm
.vanish
[0].p
== V
.ROOK
? V
.steps
[V
.BISHOP
] : V
.steps
[V
.ROOK
])
1042 const [i
, j
] = [fm
.appear
[0].x
+ step
[0], fm
.appear
[0].y
+ step
[1]];
1045 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
1048 start: { x: -1, y: -1 },
1049 end: { x: i
, y: j
},
1055 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1060 if (this.board
[i
][j
] != V
.EMPTY
) {
1062 new PiPo({ x: i
, y: j
, c: 'a', p: this.getPiece(i
, j
) }));
1069 const [x
, y
] = [fm
.appear
[0].x
, fm
.appear
[0].y
];
1070 for (let i
=0; i
<8; i
++) {
1071 for (let j
=0; j
<8; j
++) {
1072 const colIJ
= this.getColor(i
, j
);
1076 this.board
[i
][j
] != V
.EMPTY
&&
1079 const movedUnit
= new PiPo({
1083 p: this.getPiece(i
, j
)
1085 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
1086 mMove
.appear
.push(movedUnit
);
1094 const x
= V
.size
.x
+ (this.turn
== 'w' ? 0 : 1);
1095 for (let y
= 0; y
< 8; y
++)
1096 Array
.prototype.push
.apply(moves
, this.getReserveMoves([x
, y
]));
1100 moves
= super.getPotentialMovesFrom([fm
.appear
[0].x
, fm
.appear
[0].y
]);
1107 const L
= this.firstMove
.length
;
1108 const fm
= (L
> 0 ? this.firstMove
[L
-1] : null);
1111 this.subTurn
== 1 ||
1112 !([0, "daisy"].includes(fm
.end
.effect
))
1116 const [x
, y
] = [square
[0], square
[1]];
1117 const deltaX
= Math
.abs(fm
.appear
[0].x
- x
);
1118 const deltaY
= Math
.abs(fm
.appear
[0].y
- y
);
1120 fm
.end
.effect
== 0 &&
1121 (this.board
[x
][y
] == V
.EMPTY
|| this.getColor(x
, y
) == 'a') &&
1123 (fm
.vanish
[0].p
== V
.ROOK
&& deltaX
== 1 && deltaY
== 1) ||
1124 (fm
.vanish
[0].p
== V
.BISHOP
&& deltaX
+ deltaY
== 1)
1128 start: { x: -1, y: -1 },
1129 end: { x: x
, y: y
},
1135 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1140 if (this.board
[x
][y
] != V
.EMPTY
) {
1142 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
1147 fm
.end
.effect
== "daisy" &&
1148 deltaX
== 0 && deltaY
== 0 &&
1149 !this.canMove([x
, y
])
1151 // No possible move: return empty move
1153 start: { x: -1, y: -1 },
1154 end: { x: -1, y: -1 },
1163 // if (!this.states) this.states = [];
1164 // const stateFen = this.getFen();
1165 // this.states.push(stateFen);
1167 move.flags
= JSON
.stringify(this.aggregateFlags());
1168 V
.PlayOnBoard(this.board
, move);
1169 move.turn
= [this.turn
, this.subTurn
];
1170 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
)) {
1171 this.firstMove
.push(move);
1175 this.turn
= V
.GetOppCol(this.turn
);
1179 this.postPlay(move);
1183 if (move.end
.effect
== "toadette") this.reserve
= this.captured
;
1184 else this.reserve
= undefined;
1185 const color
= move.turn
[0];
1186 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1187 // Capture: update this.captured
1188 let capturedPiece
= move.vanish
[1].p
;
1189 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1190 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1191 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1192 this.captured
[move.vanish
[1].c
][capturedPiece
]++;
1194 else if (move.vanish
.length
== 0) {
1195 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1196 // A piece is back on board
1197 this.captured
[move.appear
[0].c
][move.appear
[0].p
]--;
1199 if (move.appear
.length
== 0) {
1200 // Three cases: king "shell capture", Chomp or Koopa
1201 if (this.getPiece(move.start
.x
, move.start
.y
) == V
.KING
)
1202 // King remote capture:
1203 this.powerFlags
[color
][V
.KING
] = false;
1204 else if (move.end
.effect
== "chomp")
1205 this.captured
[color
][move.vanish
[0].p
]++;
1207 else if (move.appear
[0].p
== V
.INVISIBLE_QUEEN
)
1208 this.powerFlags
[move.appear
[0].c
][V
.QUEEN
] = false;
1209 if (this.subTurn
== 2) return;
1211 move.turn
[1] == 1 &&
1212 move.appear
.length
== 0 ||
1213 !(Object
.keys(V
.IMMOBILIZE_DECODE
).includes(move.appear
[0].p
))
1215 // Look for an immobilized piece of my color: it can now move
1216 for (let i
=0; i
<8; i
++) {
1217 for (let j
=0; j
<8; j
++) {
1218 if (this.board
[i
][j
] != V
.EMPTY
) {
1219 const piece
= this.getPiece(i
, j
);
1221 this.getColor(i
, j
) == color
&&
1222 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
1224 this.board
[i
][j
] = color
+ V
.IMMOBILIZE_DECODE
[piece
];
1225 move.wasImmobilized
= [i
, j
];
1231 // Also make opponent invisible queen visible again, if any
1232 const oppCol
= V
.GetOppCol(color
);
1233 for (let i
=0; i
<8; i
++) {
1234 for (let j
=0; j
<8; j
++) {
1236 this.board
[i
][j
] != V
.EMPTY
&&
1237 this.getColor(i
, j
) == oppCol
&&
1238 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
1240 this.board
[i
][j
] = oppCol
+ V
.QUEEN
;
1241 move.wasInvisible
= [i
, j
];
1248 this.disaggregateFlags(JSON
.parse(move.flags
));
1249 V
.UndoOnBoard(this.board
, move);
1250 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
))
1251 this.firstMove
.pop();
1252 else this.movesCount
--;
1253 this.turn
= move.turn
[0];
1254 this.subTurn
= move.turn
[1];
1255 this.postUndo(move);
1257 // const stateFen = this.getFen();
1258 // if (stateFen != this.states[this.states.length-1]) debugger;
1259 // this.states.pop();
1263 if (!!move.wasImmobilized
) {
1264 const [i
, j
] = move.wasImmobilized
;
1266 this.getColor(i
, j
) + V
.IMMOBILIZE_CODE
[this.getPiece(i
, j
)];
1268 if (!!move.wasInvisible
) {
1269 const [i
, j
] = move.wasInvisible
;
1270 this.board
[i
][j
] = this.getColor(i
, j
) + V
.INVISIBLE_QUEEN
;
1272 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1273 let capturedPiece
= move.vanish
[1].p
;
1274 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1275 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1276 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1277 this.captured
[move.vanish
[1].c
][capturedPiece
]--;
1279 else if (move.vanish
.length
== 0) {
1280 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1281 // A piece was back on board
1282 this.captured
[move.appear
[0].c
][move.appear
[0].p
]++;
1284 else if (move.appear
.length
== 0 && move.end
.effect
== "chomp")
1285 this.captured
[move.vanish
[0].c
][move.vanish
[0].p
]--;
1286 if (move.vanish
.length
== 0) this.reserve
= this.captured
;
1287 else this.reserve
= undefined;
1295 // Find kings (not tracked in this variant)
1296 let kingThere
= { w: false, b: false };
1297 for (let i
=0; i
<8; i
++) {
1298 for (let j
=0; j
<8; j
++) {
1300 this.board
[i
][j
] != V
.EMPTY
&&
1301 ['k', 'l'].includes(this.getPiece(i
, j
))
1303 kingThere
[this.getColor(i
, j
)] = true;
1307 if (!kingThere
['w']) return "0-1";
1308 if (!kingThere
['b']) return "1-0";
1309 if (!this.atLeastOneMove()) return (this.turn
== 'w' ? "0-1" : "1-0");
1313 static GenRandInitFen(randomness
) {
1315 SuicideRules
.GenRandInitFen(randomness
).slice(0, -1) +
1316 // Add Peach + Mario flags + capture counts
1321 filterValid(moves
) {
1325 static get VALUES() {
1326 return Object
.assign(
1344 static get SEARCH_DEPTH() {
1349 const moves
= this.getAllValidMoves();
1350 // Split into "normal" and "random" moves:
1351 // (Next splitting condition is OK because cannot take self object
1352 // without a banana or bomb on the way).
1353 const deterministicMoves
= moves
.filter(m
=> {
1354 return m
.vanish
.every(a
=> a
.c
!= 'a' || a
.p
== V
.MUSHROOM
);
1356 const randomMoves
= moves
.filter(m
=> {
1357 return m
.vanish
.some(a
=> a
.c
== 'a' && a
.p
!= V
.MUSHROOM
);
1359 if (Math
.random() < deterministicMoves
.length
/ randomMoves
.length
)
1360 // Play a deterministic one: capture king or material if possible
1361 return super.getComputerMove(deterministicMoves
);
1362 // Play a random effect move, at random:
1363 let move1
= randomMoves
[randInt(randomMoves
.length
)];
1365 let move2
= undefined;
1366 if (this.subTurn
== 2) {
1367 const moves2
= this.getAllValidMoves();
1368 move2
= moves2
[randInt(moves2
.length
)];
1371 if (!move2
) return move1
;
1372 return [move1
, move2
];
1376 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return "-";
1379 move.appear
.length
> 0 &&
1380 move.appear
[0].p
== V
.INVISIBLE_QUEEN
1384 const finalSquare
= V
.CoordsToSquare(move.end
);
1385 // Next condition also includes Toadette placements:
1386 if (move.appear
.length
> 0 && move.vanish
.every(a
=> a
.c
== 'a')) {
1388 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
1389 return piece
+ "@" + finalSquare
;
1391 else if (move.appear
.length
== 0) {
1392 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
1393 if (piece
== V
.KING
&& !move.end
.effect
)
1394 // King remote capture
1395 return "Kx" + finalSquare
;
1396 // Koopa or Chomp, or loopback after bananas, bombs & mushrooms:
1398 piece
.toUpperCase() + "x" + finalSquare
+
1401 ? "*" + (move.end
.effect
== "koopa" ? "K" : "C")
1406 if (move.appear
.length
== 1 && move.vanish
.length
== 1) {
1407 const moveStart
= move.appear
[0].p
.toUpperCase() + "@";
1408 if (move.appear
[0].c
== 'a' && move.vanish
[0].c
== 'a')
1409 // Bonus replacement:
1410 return moveStart
+ finalSquare
;
1412 move.vanish
[0].p
== V
.INVISIBLE_QUEEN
&&
1413 move.appear
[0].x
== move.vanish
[0].x
&&
1414 move.appear
[0].y
== move.vanish
[0].y
1416 // Toadette takes invisible queen
1417 return moveStart
+ "Q" + finalSquare
;
1421 move.appear
.length
== 2 &&
1422 move.vanish
.length
== 2 &&
1423 move.appear
.every(a
=> a
.c
!= 'a') &&
1424 move.vanish
.every(v
=> v
.c
!= 'a')
1426 // King Boo exchange
1427 return move.vanish
[1].p
.toUpperCase() + finalSquare
;
1429 const piece
= move.vanish
[0].p
;
1430 let notation
= undefined;
1431 if (piece
== V
.PAWN
) {
1433 if (move.vanish
.length
>= 2) {
1435 const startColumn
= V
.CoordToColumn(move.start
.y
);
1436 notation
= startColumn
+ "x" + finalSquare
;
1438 else notation
= finalSquare
;
1439 if (move.appear
[0].p
!= V
.PAWN
)
1441 notation
+= "=" + move.appear
[0].p
.toUpperCase();
1445 piece
.toUpperCase() +
1446 (move.vanish
.length
>= 2 ? "x" : "") +
1449 if (!!move.end
.effect
) {
1450 switch (move.end
.effect
) {
1465 const lastAppear
= move.appear
[move.appear
.length
- 1];
1467 V
.CoordsToSquare({ x: lastAppear
.x
, y : lastAppear
.y
});
1468 notation
+= "*" + move.end
.effect
[0].toUpperCase() + effectOn
;