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
{
7 static get PawnSpecs() {
8 return SuicideRules
.PawnSpecs
;
11 static get HasCastle() {
15 static get HasEnpassant() {
19 static get CorrConfirm() {
20 // Because of bonus effects
24 static get CanAnalyze() {
28 static get SomeHiddenMoves() {
32 hoverHighlight(x
, y
) {
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;
37 const deltaX
= Math
.abs(fm
.appear
[0].x
- x
);
38 const deltaY
= Math
.abs(fm
.appear
[0].y
- y
);
40 (this.board
[x
][y
] == V
.EMPTY
|| this.getColor(x
, y
) == 'a') &&
42 (fm
.vanish
[0].p
== V
.ROOK
&& deltaX
== 1 && deltaY
== 1) ||
43 (fm
.vanish
[0].p
== V
.BISHOP
&& deltaX
+ deltaY
== 1)
48 static get IMMOBILIZE_CODE() {
59 static get IMMOBILIZE_DECODE() {
70 static get INVISIBLE_QUEEN() {
74 // Fictive color 'a', bomb banana mushroom egg
79 return 'd'; //"Donkey"
84 static get MUSHROOM() {
91 ? "w" + f
.toLowerCase()
92 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
98 ChessRules
.PIECES
.concat(
99 Object
.keys(V
.IMMOBILIZE_DECODE
)).concat(
100 [V
.BANANA
, V
.BOMB
, V
.EGG
, V
.MUSHROOM
, V
.INVISIBLE_QUEEN
])
108 b
[1] == V
.INVISIBLE_QUEEN
||
109 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(b
[1])
117 if (!!m
.promoteInto
) return m
.promoteInto
;
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
);
124 static ParseFen(fen
) {
125 const fenParts
= fen
.split(" ");
126 return Object
.assign(
127 ChessRules
.ParseFen(fen
),
128 { captured: fenParts
[4] }
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;
139 // King can be l or L (immobilized) --> similar to Alice variant
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;
144 let kings
= { "k": 0, "K": 0, 'l': 0, 'L': 0 };
145 for (let row
of rows
) {
147 for (let i
= 0; i
< row
.length
; i
++) {
148 if (['K', 'k', 'L', 'l'].includes(row
[i
])) kings
[row
[i
]]++;
149 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
151 const num
= parseInt(row
[i
]);
152 if (isNaN(num
)) return false;
156 if (sumElts
!= V
.size
.y
) return false;
158 if (kings
['k'] + kings
['l'] == 0 || kings
['K'] + kings
['L'] == 0)
163 static IsGoodFlags(flags
) {
164 // 4 for Peach + Mario w, b
165 return !!flags
.match(/^[01]{4,4}$/);
169 // King can send shell? Queen can be invisible?
171 w: { 'k': false, 'q': false },
172 b: { 'k': false, 'q': false }
174 for (let c
of ["w", "b"]) {
175 for (let p
of ['k', 'q']) {
176 this.powerFlags
[c
][p
] =
177 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
183 return this.powerFlags
;
186 disaggregateFlags(flags
) {
187 this.powerFlags
= flags
;
191 return super.getFen() + " " + this.getCapturedFen();
195 return super.getFenForRepeat() + "_" + this.getCapturedFen();
199 let counts
= [...Array(12).fill(0)];
201 for (let p
of V
.RESERVE_PIECES
) {
202 counts
[i
] = this.captured
["w"][p
];
203 counts
[6 + i
] = this.captured
["b"][p
];
206 return counts
.join("");
211 setOtherVariables(fen
) {
212 super.setOtherVariables(fen
);
213 const fenParsed
= V
.ParseFen(fen
);
214 // Initialize captured pieces' counts from FEN
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])
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])
240 for (let c
of ["w", "b"])
241 for (let p
of ['k', 'q']) fen
+= (this.powerFlags
[c
][p
] ? "1" : "0");
246 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
247 return this.board
[i
][j
].charAt(0);
251 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
252 return this.board
[i
][j
].charAt(1);
255 getReservePpath(index
, color
) {
256 return color
+ V
.RESERVE_PIECES
[index
];
259 static get RESERVE_PIECES() {
260 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
263 getReserveMoves([x
, y
]) {
264 const color
= this.turn
;
265 const p
= V
.RESERVE_PIECES
[y
];
266 if (this.reserve
[color
][p
] == 0) return [];
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
++) {
273 this.board
[i
][j
] == V
.EMPTY
||
274 this.getColor(i
, j
) == 'a' ||
275 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
277 let m
= this.getBasicMove({ p: p
, x: i
, y: j
});
278 m
.start
= { x: x
, y: y
};
286 getPotentialMovesFrom([x
, y
]) {
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);
296 m
.appear
.length
> 0 &&
297 ['p', 's'].includes(m
.appear
[0].p
) &&
298 m
.appear
[0].x
== lastRank
300 for (let i
= 1; i
< finalPieces
.length
; i
++) {
301 const piece
= finalPieces
[i
];
302 let otherM
= JSON
.parse(JSON
.stringify(m
));
304 m
.appear
[0].p
== V
.PAWN
306 : V
.IMMOBILIZE_CODE
[finalPieces
[i
]];
309 // Finally alter m itself:
311 m
.appear
[0].p
== V
.PAWN
313 : V
.IMMOBILIZE_CODE
[finalPieces
[0]];
316 Array
.prototype.push
.apply(moves
, pMoves
);
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)
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
);
330 for (let i
=0; i
<8; i
++) {
331 for (let j
=0; j
<8; j
++) {
332 const colIJ
= this.getColor(i
, j
);
334 (i
!= x
|| j
!= y
) &&
335 this.board
[i
][j
] != V
.EMPTY
&&
338 const pieceIJ
= this.getPiece(i
, j
);
340 (pieceIJ
!= V
.PAWN
|| x
!= lastRank
[colIJ
]) &&
341 (allowLastRank
|| i
!= lastRank
[color
])
343 const movedUnit
= new PiPo({
347 p: this.getPiece(i
, j
)
349 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
350 mMove
.appear
.push(movedUnit
);
358 // Resurrect a captured piece
359 if (x
>= V
.size
.x
) moves
= this.getReserveMoves([x
, y
]);
362 // Play again with the same piece
363 if (fm
.appear
[0].x
== x
&& fm
.appear
[0].y
== y
)
364 moves
= super.getPotentialMovesFrom([x
, y
]);
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]];
377 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
380 if (validSteps
.length
== 0)
381 // Can happen after mushroom jump
383 const step
= validSteps
[randInt(validSteps
.length
)];
384 return [x
+ step
[0], y
+ step
[1]];
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);
394 V
.OnBoard(x
+ forward
, y
) &&
396 this.board
[x
+ forward
][y
] != oppCol
||
398 V
.OnBoard(x
+ forward
, y
+ 1) &&
399 this.board
[x
+ forward
][y
+ 1] != V
.EMPTY
&&
400 this.getColor
[x
+ forward
, y
+ 1] == oppCol
403 V
.OnBoard(x
+ forward
, y
- 1) &&
404 this.board
[x
+ forward
][y
- 1] != V
.EMPTY
&&
405 this.getColor
[x
+ forward
, y
- 1] == oppCol
410 // Checking one step is enough:
412 [V
.KING
, V
.QUEEN
].includes(piece
)
413 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
415 for (let step
of steps
) {
416 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
419 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) != color
)
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
;
432 const piece1
= (!!tr
? tr
.p : (psq1
.p
|| this.getPiece(x1
, y1
)));
433 const oppCol
= V
.GetOppCol(color1
);
439 // banana or bomb defines next square, or the move ends there
448 if (this.board
[x1
][y1
] != V
.EMPTY
) {
449 const initP1
= this.getPiece(x1
, y1
);
454 c: this.getColor(x1
, y1
),
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
);
463 move.end
= { x: x1
, y: y1
};
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
);
469 if (!!tr
) move.promoteInto
= tr
.c
+ tr
.p
; //in case of (chomped...)
470 const L
= this.firstMove
.length
;
472 [V
.PAWN
, V
.KNIGHT
].includes(piece1
) &&
474 (this.subTurn
== 1 || this.firstMove
[L
-1].end
.effect
== "daisy")
478 const twoSquaresMove
= (Math
.abs(x2
- x1
) == 2);
479 const mushroomX
= x1
+ (twoSquaresMove
? (x2
- x1
) / 2 : 0);
488 if (this.getColor(mushroomX
, y1
) == 'a') {
494 p: this.getPiece(mushroomX
, y1
)
501 const deltaX
= Math
.abs(x2
- x1
);
502 const deltaY
= Math
.abs(y2
- y1
);
504 x1
+ (deltaX
== 2 ? (x2
- x1
) / 2 : 0),
505 y1
+ (deltaY
== 2 ? (y2
- y1
) / 2 : 0)
508 this.board
[eggSquare
[0]][eggSquare
[1]] != V
.EMPTY
&&
509 this.getColor(eggSquare
[0], eggSquare
[1]) != 'a'
522 if (this.getColor(eggSquare
[0], eggSquare
[1]) == 'a') {
528 p: this.getPiece(eggSquare
[0], eggSquare
[1])
536 // For (wa)luigi effect:
537 const changePieceColor
= (color
) => {
539 const oppLastRank
= (color
== 'w' ? 7 : 0);
540 for (let i
=0; i
<8; i
++) {
541 for (let j
=0; j
<8; j
++) {
543 (i
!= move.vanish
[0].x
|| j
!= move.vanish
[0].y
) &&
544 this.board
[i
][j
] != V
.EMPTY
&&
545 this.getColor(i
, j
) == color
547 const piece
= this.getPiece(i
, j
);
548 if (piece
!= V
.KING
&& (piece
!= V
.PAWN
|| i
!= oppLastRank
))
549 pieces
.push({ x: i
, y: j
, p: piece
});
553 // Special case of the current piece (still at its initial position)
555 pieces
.push({ x: move.appear
[0].x
, y: move.appear
[0].y
, p: piece1
});
556 const cp
= pieces
[randInt(pieces
.length
)];
557 if (move.appear
[0].x
!= cp
.x
|| move.appear
[0].y
!= cp
.y
) {
567 else move.appear
.shift();
572 c: V
.GetOppCol(color
),
577 const applyEggEffect
= () => {
578 if (this.subTurn
== 2)
579 // No egg effects at subTurn 2
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;
588 move.appear
[0].p
== V
.PAWN
&&
589 move.appear
[0].x
== lastRank
[color1
]
591 // Always possible: promote into a queen, rook or king
595 move.end
.effect
= "daisy";
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);
600 delete move.end
["effect"];
602 if (canPlayAgain
) effects
.push("daisy");
604 this.board
.some((b
,i
) =>
609 (cell
[1] != V
.PAWN
|| i
!= lastRank
[color1
])
614 effects
.push("luigi");
619 (piece1
!= V
.PAWN
|| move.appear
[0].x
!= lastRank
[oppCol
])
621 this.board
.some((b
,i
) =>
626 (cell
[1] != V
.PAWN
|| i
!= lastRank
[oppCol
])
631 effects
.push("waluigi");
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
))) {
640 // Maybe egg effect was applied after others,
641 // so just shift vanish array:
648 move.appear
[0].p
= V
.IMMOBILIZE_CODE
[piece1
];
651 changePieceColor(oppCol
);
654 changePieceColor(color1
);
659 const applyMushroomEffect
= () => {
660 if ([V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece1
)) {
661 // Just make another similar step, if possible (non-capturing)
663 move.appear
[0].x
+ (x2
- x1
),
664 move.appear
[0].y
+ (y2
- y1
)
668 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
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
);
685 const steps
= V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
686 move.next
= this.getRandomSquare([i
, j
], steps
);
692 applyMushroomEffect();
699 // Queen, bishop or rook:
701 (x2
- x1
) / Math
.abs(x2
- x1
) || 0,
702 (y2
- y1
) / Math
.abs(y2
- y1
) || 0
704 const next
= [move.appear
[0].x
+ step
[0], move.appear
[0].y
+ step
[1]];
706 V
.OnBoard(next
[0], next
[1]) &&
707 this.board
[next
[0]][next
[1]] != V
.EMPTY
&&
708 this.getColor(next
[0], next
[1]) != 'a'
710 const afterNext
= [next
[0] + step
[0], next
[1] + step
[1]];
711 if (V
.OnBoard(afterNext
[0], afterNext
[1])) {
712 const afterColor
= this.getColor(afterNext
[0], afterNext
[1])
714 this.board
[afterNext
[0]][afterNext
[1]] == V
.EMPTY
||
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]);
734 V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
735 move.next
= this.getRandomSquare(
736 [afterNext
[0], afterNext
[1]], steps
);
742 applyMushroomEffect();
751 const color2
= this.getColor(x2
, y2
);
752 const piece2
= this.getPiece(x2
, y2
);
757 const steps
= V
.steps
[piece2
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
758 move.next
= this.getRandomSquare([x2
, y2
], steps
);
761 applyMushroomEffect();
764 if (this.subTurn
== 1)
765 // No egg effect at subTurn 2
773 move.appear
.length
> 0 &&
774 [V
.ROOK
, V
.BISHOP
].includes(piece1
)
776 const finalSquare
= [move.appear
[0].x
, move.appear
[0].y
];
779 this.getColor(finalSquare
[0], finalSquare
[1]) != 'a' ||
780 this.getPiece(finalSquare
[0], finalSquare
[1]) != V
.EGG
783 V
.steps
[piece1
== V
.ROOK
? V
.BISHOP : V
.ROOK
].filter(s
=> {
784 const [i
, j
] = [finalSquare
[0] + s
[0], finalSquare
[1] + s
[1]];
787 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
790 if (validSteps
.length
>= 1) {
792 finalSquare
[0] + validSteps
[0][0],
793 finalSquare
[1] + validSteps
[0][1]
800 p: (piece1
== V
.ROOK
? V
.BANANA : V
.BOMB
)
803 if (this.board
[x
][y
] != V
.EMPTY
) {
805 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
813 getBasicMove(psq1
, sq2
, tr
) {
815 if (Array
.isArray(psq1
)) psq1
= { x: psq1
[0], y: psq1
[1] };
816 let m
= this.getBasicMove_aux(psq1
, sq2
, tr
, "initMove");
818 // Last move ended on bomb or banana, direction change
819 V
.PlayOnBoard(this.board
, m
);
821 m
= this.getBasicMove_aux(
822 { x: m
.appear
[0].x
, y: m
.appear
[0].y
}, m
.next
);
824 for (let i
=moves
.length
-1; i
>=0; i
--) V
.UndoOnBoard(this.board
, moves
[i
]);
826 // Now merge moves into one
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
};
831 if (!!tr
) move.promoteInto
= moves
[0].promoteInto
;
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
;
840 // Keep first vanish and last appear (if any)
841 move.appear
= lm
.appear
;
842 move.vanish
= moves
[0].vanish
;
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
849 // Loopback on initial square:
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
859 moves
[0].appear
.length
== 1 ||
860 moves
[0].appear
[1].x
!= v
.x
||
861 moves
[0].appear
[1].y
!= v
.y
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
++) {
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
877 move.vanish
.push(lm
.vanish
[i
]);
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);
892 this.board
[x
+ shiftX
][y
] == V
.EMPTY
||
893 this.getColor(x
+ shiftX
, y
) == 'a'
895 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
897 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
899 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
||
900 this.getColor(x
+ 2 * shiftX
, y
) == 'a'
903 moves
.push(this.getBasicMove({ x: x
, y: y
}, [x
+ 2 * shiftX
, y
]));
906 for (let shiftY
of [-1, 1]) {
909 y
+ shiftY
< sizeY
&&
910 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
911 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, y
+ shiftY
))
913 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
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
=> {
926 m
.appear
.length
== 1 &&
927 m
.vanish
.length
== 1 &&
928 // Only simple non-capturing moves:
931 let im
= JSON
.parse(JSON
.stringify(m
));
932 im
.appear
[0].p
= V
.INVISIBLE_QUEEN
;
933 im
.end
.noHighlight
= true;
934 invisibleMoves
.push(im
);
938 return normalMoves
.concat(invisibleMoves
);
941 getPotentialKingMoves([x
, y
]) {
942 let moves
= super.getPotentialKingMoves([x
, y
]);
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
=> {
947 const [nextX
, nextY
] = [x
+ step
[0], y
+ step
[1]];
949 V
.OnBoard(nextX
, nextY
) &&
951 this.board
[nextX
][nextY
] == V
.EMPTY
||
953 this.getColor(nextX
, nextY
) == 'a' &&
954 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(nextX
, nextY
))
958 let [i
, j
] = [x
+ 2 * step
[0], y
+ 2 * step
[1]];
962 this.board
[i
][j
] == V
.EMPTY
||
964 this.getColor(i
, j
) == 'a' &&
965 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
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:
978 start: { x: x
, y: y
},
983 x: i
, y: j
, c: colIJ
, p: this.getPiece(i
, j
)
996 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
998 outerLoop: for (let step
of steps
) {
1000 let j
= y
+ step
[1];
1004 this.board
[i
][j
] == V
.EMPTY
||
1005 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
1007 this.getColor(i
, j
) == 'a' &&
1008 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
1012 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1013 if (oneStep
) continue outerLoop
;
1017 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
1018 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1023 getAllPotentialMoves() {
1024 if (this.subTurn
== 1) return super.getAllPotentialMoves();
1026 const L
= this.firstMove
.length
;
1027 const fm
= this.firstMove
[L
-1];
1028 switch (fm
.end
.effect
) {
1031 start: { x: -1, y: -1 },
1032 end: { x: -1, y: -1 },
1038 (fm
.vanish
[0].p
== V
.ROOK
? V
.steps
[V
.BISHOP
] : V
.steps
[V
.ROOK
])
1040 const [i
, j
] = [fm
.appear
[0].x
+ step
[0], fm
.appear
[0].y
+ step
[1]];
1043 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
1046 start: { x: -1, y: -1 },
1047 end: { x: i
, y: j
},
1053 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1058 if (this.board
[i
][j
] != V
.EMPTY
) {
1060 new PiPo({ x: i
, y: j
, c: 'a', p: this.getPiece(i
, j
) }));
1067 const [x
, y
] = [fm
.appear
[0].x
, fm
.appear
[0].y
];
1068 for (let i
=0; i
<8; i
++) {
1069 for (let j
=0; j
<8; j
++) {
1070 const colIJ
= this.getColor(i
, j
);
1074 this.board
[i
][j
] != V
.EMPTY
&&
1077 const movedUnit
= new PiPo({
1081 p: this.getPiece(i
, j
)
1083 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
1084 mMove
.appear
.push(movedUnit
);
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
]));
1098 moves
= super.getPotentialMovesFrom([fm
.appear
[0].x
, fm
.appear
[0].y
]);
1105 const L
= this.firstMove
.length
;
1106 const fm
= (L
> 0 ? this.firstMove
[L
-1] : null);
1109 this.subTurn
== 1 ||
1110 !([0, "daisy"].includes(fm
.end
.effect
))
1114 const [x
, y
] = [square
[0], square
[1]];
1115 const deltaX
= Math
.abs(fm
.appear
[0].x
- x
);
1116 const deltaY
= Math
.abs(fm
.appear
[0].y
- y
);
1118 fm
.end
.effect
== 0 &&
1119 (this.board
[x
][y
] == V
.EMPTY
|| this.getColor(x
, y
) == 'a') &&
1121 (fm
.vanish
[0].p
== V
.ROOK
&& deltaX
== 1 && deltaY
== 1) ||
1122 (fm
.vanish
[0].p
== V
.BISHOP
&& deltaX
+ deltaY
== 1)
1126 start: { x: -1, y: -1 },
1127 end: { x: x
, y: y
},
1133 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1138 if (this.board
[x
][y
] != V
.EMPTY
) {
1140 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
1145 fm
.end
.effect
== "daisy" &&
1146 deltaX
== 0 && deltaY
== 0 &&
1147 !this.canMove([x
, y
])
1149 // No possible move: return empty move
1151 start: { x: -1, y: -1 },
1152 end: { x: -1, y: -1 },
1161 // if (!this.states) this.states = [];
1162 // const stateFen = this.getFen();
1163 // this.states.push(stateFen);
1165 move.flags
= JSON
.stringify(this.aggregateFlags());
1166 V
.PlayOnBoard(this.board
, move);
1167 move.turn
= [this.turn
, this.subTurn
];
1168 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
)) {
1169 this.firstMove
.push(move);
1173 this.turn
= V
.GetOppCol(this.turn
);
1177 this.postPlay(move);
1181 if (move.end
.effect
== "toadette") this.reserve
= this.captured
;
1182 else this.reserve
= undefined;
1183 const color
= move.turn
[0];
1184 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1185 // Capture: update this.captured
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
]++;
1192 else if (move.vanish
.length
== 0) {
1193 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1194 // A piece is back on board
1195 this.captured
[move.appear
[0].c
][move.appear
[0].p
]--;
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
]++;
1205 else if (move.appear
[0].p
== V
.INVISIBLE_QUEEN
)
1206 this.powerFlags
[move.appear
[0].c
][V
.QUEEN
] = false;
1207 if (this.subTurn
== 2) return;
1209 move.turn
[1] == 1 &&
1210 move.appear
.length
== 0 ||
1211 !(Object
.keys(V
.IMMOBILIZE_DECODE
).includes(move.appear
[0].p
))
1213 // Look for an immobilized piece of my color: it can now move
1214 for (let i
=0; i
<8; i
++) {
1215 for (let j
=0; j
<8; j
++) {
1216 if (this.board
[i
][j
] != V
.EMPTY
) {
1217 const piece
= this.getPiece(i
, j
);
1219 this.getColor(i
, j
) == color
&&
1220 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
1222 this.board
[i
][j
] = color
+ V
.IMMOBILIZE_DECODE
[piece
];
1223 move.wasImmobilized
= [i
, j
];
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
++) {
1234 this.board
[i
][j
] != V
.EMPTY
&&
1235 this.getColor(i
, j
) == oppCol
&&
1236 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
1238 this.board
[i
][j
] = oppCol
+ V
.QUEEN
;
1239 move.wasInvisible
= [i
, j
];
1246 this.disaggregateFlags(JSON
.parse(move.flags
));
1247 V
.UndoOnBoard(this.board
, move);
1248 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
))
1249 this.firstMove
.pop();
1250 else this.movesCount
--;
1251 this.turn
= move.turn
[0];
1252 this.subTurn
= move.turn
[1];
1253 this.postUndo(move);
1255 // const stateFen = this.getFen();
1256 // if (stateFen != this.states[this.states.length-1]) debugger;
1257 // this.states.pop();
1261 if (!!move.wasImmobilized
) {
1262 const [i
, j
] = move.wasImmobilized
;
1264 this.getColor(i
, j
) + V
.IMMOBILIZE_CODE
[this.getPiece(i
, j
)];
1266 if (!!move.wasInvisible
) {
1267 const [i
, j
] = move.wasInvisible
;
1268 this.board
[i
][j
] = this.getColor(i
, j
) + V
.INVISIBLE_QUEEN
;
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
]--;
1277 else if (move.vanish
.length
== 0) {
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
]++;
1282 else if (move.appear
.length
== 0 && move.end
.effect
== "chomp")
1283 this.captured
[move.vanish
[0].c
][move.vanish
[0].p
]--;
1284 if (move.vanish
.length
== 0) this.reserve
= this.captured
;
1285 else this.reserve
= undefined;
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
++) {
1298 this.board
[i
][j
] != V
.EMPTY
&&
1299 ['k', 'l'].includes(this.getPiece(i
, j
))
1301 kingThere
[this.getColor(i
, j
)] = true;
1305 if (!kingThere
['w']) return "0-1";
1306 if (!kingThere
['b']) return "1-0";
1307 if (!this.atLeastOneMove()) return (this.turn
== 'w' ? "0-1" : "1-0");
1311 static GenRandInitFen(randomness
) {
1313 SuicideRules
.GenRandInitFen(randomness
).slice(0, -1) +
1314 // Add Peach + Mario flags + capture counts
1319 filterValid(moves
) {
1323 static get VALUES() {
1324 return Object
.assign(
1342 static get SEARCH_DEPTH() {
1347 const moves
= this.getAllValidMoves();
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
);
1354 const randomMoves
= moves
.filter(m
=> {
1355 return m
.vanish
.some(a
=> a
.c
== 'a' && a
.p
!= V
.MUSHROOM
);
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:
1361 let move1
= randomMoves
[randInt(randomMoves
.length
)];
1363 let move2
= undefined;
1364 if (this.subTurn
== 2) {
1365 const moves2
= this.getAllValidMoves();
1366 move2
= moves2
[randInt(moves2
.length
)];
1369 if (!move2
) return move1
;
1370 return [move1
, move2
];
1374 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return "-";
1377 move.appear
.length
> 0 &&
1378 move.appear
[0].p
== V
.INVISIBLE_QUEEN
1382 const finalSquare
= V
.CoordsToSquare(move.end
);
1383 // Next condition also includes Toadette placements:
1384 if (move.appear
.length
> 0 && move.vanish
.every(a
=> a
.c
== 'a')) {
1386 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
1387 return piece
+ "@" + finalSquare
;
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:
1396 piece
.toUpperCase() + "x" + finalSquare
+
1399 ? "*" + (move.end
.effect
== "koopa" ? "K" : "C")
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
;
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
1414 // Toadette takes invisible queen
1415 return moveStart
+ "Q" + finalSquare
;
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')
1424 // King Boo exchange
1425 return move.vanish
[1].p
.toUpperCase() + finalSquare
;
1427 const piece
= move.vanish
[0].p
;
1428 let notation
= undefined;
1429 if (piece
== V
.PAWN
) {
1431 if (move.vanish
.length
>= 2) {
1433 const startColumn
= V
.CoordToColumn(move.start
.y
);
1434 notation
= startColumn
+ "x" + finalSquare
;
1436 else notation
= finalSquare
;
1437 if (move.appear
[0].p
!= V
.PAWN
)
1439 notation
+= "=" + move.appear
[0].p
.toUpperCase();
1443 piece
.toUpperCase() +
1444 (move.vanish
.length
>= 2 ? "x" : "") +
1447 if (!!move.end
.effect
) {
1448 switch (move.end
.effect
) {
1463 const lastAppear
= move.appear
[move.appear
.length
- 1];
1465 V
.CoordsToSquare({ x: lastAppear
.x
, y : lastAppear
.y
});
1466 notation
+= "*" + move.end
.effect
[0].toUpperCase() + effectOn
;