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 static get IMMOBILIZE_CODE() {
44 static get IMMOBILIZE_DECODE() {
55 static get INVISIBLE_QUEEN() {
59 // Fictive color 'a', bomb banana mushroom egg
64 return 'd'; //"Donkey"
69 static get MUSHROOM() {
76 ? "w" + f
.toLowerCase()
77 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
83 ChessRules
.PIECES
.concat(
84 Object
.keys(V
.IMMOBILIZE_DECODE
)).concat(
85 [V
.BANANA
, V
.BOMB
, V
.EGG
, V
.MUSHROOM
, V
.INVISIBLE_QUEEN
])
93 b
[1] == V
.INVISIBLE_QUEEN
||
94 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(b
[1])
102 if (!!m
.promoteInto
) return m
.promoteInto
;
103 if (m
.appear
.length
== 0 && m
.vanish
.length
== 1)
104 // King 'remote shell capture', on an adjacent square:
105 return this.getPpath(m
.vanish
[0].c
+ m
.vanish
[0].p
);
106 let piece
= m
.appear
[0].p
;
107 if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
))
108 // Promotion by capture into immobilized piece: do not reveal!
109 piece
= V
.IMMOBILIZE_DECODE
[piece
];
110 return this.getPpath(m
.appear
[0].c
+ piece
);
113 static ParseFen(fen
) {
114 const fenParts
= fen
.split(" ");
115 return Object
.assign(
116 ChessRules
.ParseFen(fen
),
117 { captured: fenParts
[4] }
121 static IsGoodFen(fen
) {
122 if (!ChessRules
.IsGoodFen(fen
)) return false;
123 const captured
= V
.ParseFen(fen
).captured
;
124 if (!captured
|| !captured
.match(/^[0-9]{12,12}$/)) return false;
128 // King can be l or L (immobilized) --> similar to Alice variant
129 static IsGoodPosition(position
) {
130 if (position
.length
== 0) return false;
131 const rows
= position
.split("/");
132 if (rows
.length
!= V
.size
.x
) return false;
133 let kings
= { "k": 0, "K": 0, 'l': 0, 'L': 0 };
134 for (let row
of rows
) {
136 for (let i
= 0; i
< row
.length
; i
++) {
137 if (['K', 'k', 'L', 'l'].includes(row
[i
])) kings
[row
[i
]]++;
138 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
140 const num
= parseInt(row
[i
], 10);
141 if (isNaN(num
)) return false;
145 if (sumElts
!= V
.size
.y
) return false;
147 if (kings
['k'] + kings
['l'] == 0 || kings
['K'] + kings
['L'] == 0)
152 static IsGoodFlags(flags
) {
153 // 4 for Peach + Mario w, b
154 return !!flags
.match(/^[01]{4,4}$/);
158 // King can send shell? Queen can be invisible?
160 w: { 'k': false, 'q': false },
161 b: { 'k': false, 'q': false }
163 for (let c
of ["w", "b"]) {
164 for (let p
of ['k', 'q']) {
165 this.powerFlags
[c
][p
] =
166 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
172 return this.powerFlags
;
175 disaggregateFlags(flags
) {
176 this.powerFlags
= flags
;
180 return super.getFen() + " " + this.getCapturedFen();
184 return super.getFenForRepeat() + "_" + this.getCapturedFen();
188 let counts
= [...Array(12).fill(0)];
190 for (let p
of V
.RESERVE_PIECES
) {
191 counts
[i
] = this.captured
["w"][p
];
192 counts
[6 + i
] = this.captured
["b"][p
];
195 return counts
.join("");
200 setOtherVariables(fen
) {
201 super.setOtherVariables(fen
);
202 // Initialize captured pieces' counts from FEN
204 V
.ParseFen(fen
).captured
.split("").map(x
=> parseInt(x
, 10));
207 [V
.PAWN
]: captured
[0],
208 [V
.ROOK
]: captured
[1],
209 [V
.KNIGHT
]: captured
[2],
210 [V
.BISHOP
]: captured
[3],
211 [V
.QUEEN
]: captured
[4],
212 [V
.KING
]: captured
[5]
215 [V
.PAWN
]: captured
[6],
216 [V
.ROOK
]: captured
[7],
217 [V
.KNIGHT
]: captured
[8],
218 [V
.BISHOP
]: captured
[9],
219 [V
.QUEEN
]: captured
[10],
220 [V
.KING
]: captured
[11]
230 for (let c
of ["w", "b"])
231 for (let p
of ['k', 'q']) fen
+= (this.powerFlags
[c
][p
] ? "1" : "0");
236 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
237 return this.board
[i
][j
].charAt(0);
241 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
242 return this.board
[i
][j
].charAt(1);
245 getReservePpath(index
, color
) {
246 return color
+ V
.RESERVE_PIECES
[index
];
249 static get RESERVE_PIECES() {
250 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
253 getReserveMoves([x
, y
]) {
254 const color
= this.turn
;
255 const p
= V
.RESERVE_PIECES
[y
];
256 if (this.reserve
[color
][p
] == 0) return [];
258 const start
= (color
== 'w' && p
== V
.PAWN
? 1 : 0);
259 const end
= (color
== 'b' && p
== V
.PAWN
? 7 : 8);
260 for (let i
= start
; i
< end
; i
++) {
261 for (let j
= 0; j
< V
.size
.y
; j
++) {
263 this.board
[i
][j
] == V
.EMPTY
||
264 this.getColor(i
, j
) == 'a' ||
265 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
267 let m
= this.getBasicMove({ p: p
, x: i
, y: j
});
268 m
.start
= { x: x
, y: y
};
276 getPotentialMovesFrom([x
, y
]) {
278 if (this.subTurn
== 1) {
279 moves
= super.getPotentialMovesFrom([x
, y
]);
280 const finalPieces
= V
.PawnSpecs
.promotions
;
281 const color
= this.turn
;
282 const lastRank
= (color
== "w" ? 0 : 7);
286 m
.appear
.length
> 0 &&
287 ['p', 's'].includes(m
.appear
[0].p
) &&
288 m
.appear
[0].x
== lastRank
290 for (let i
= 1; i
< finalPieces
.length
; i
++) {
291 const piece
= finalPieces
[i
];
292 let otherM
= JSON
.parse(JSON
.stringify(m
));
294 m
.appear
[0].p
== V
.PAWN
296 : V
.IMMOBILIZE_CODE
[finalPieces
[i
]];
299 // Finally alter m itself:
301 m
.appear
[0].p
== V
.PAWN
303 : V
.IMMOBILIZE_CODE
[finalPieces
[0]];
306 Array
.prototype.push
.apply(moves
, pMoves
);
310 const L
= this.effects
.length
;
311 switch (this.effects
[L
-1]) {
313 // Exchange position with any visible piece,
314 // except pawns if arriving on last rank.
315 const lastRank
= { 'w': 0, 'b': 7 };
316 const color
= this.turn
;
317 const allowLastRank
= (this.getPiece(x
, y
) != V
.PAWN
);
318 for (let i
=0; i
<8; i
++) {
319 for (let j
=0; j
<8; j
++) {
320 const colIJ
= this.getColor(i
, j
);
321 const pieceIJ
= this.getPiece(i
, j
);
323 (i
!= x
|| j
!= y
) &&
324 this.board
[i
][j
] != V
.EMPTY
&&
325 pieceIJ
!= V
.INVISIBLE_QUEEN
&&
329 (pieceIJ
!= V
.PAWN
|| x
!= lastRank
[colIJ
]) &&
330 (allowLastRank
|| i
!= lastRank
[color
])
332 const movedUnit
= new PiPo({
336 p: this.getPiece(i
, j
)
338 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
339 mMove
.appear
.push(movedUnit
);
347 // Resurrect a captured piece
348 if (x
>= V
.size
.x
) moves
= this.getReserveMoves([x
, y
]);
351 // Play again with any piece
352 moves
= super.getPotentialMovesFrom([x
, y
]);
359 // Helper for getBasicMove(): banana/bomb effect
360 getRandomSquare([x
, y
], steps
) {
361 const validSteps
= steps
.filter(s
=> V
.OnBoard(x
+ s
[0], y
+ s
[1]));
362 const step
= validSteps
[randInt(validSteps
.length
)];
363 return [x
+ step
[0], y
+ step
[1]];
366 // Apply mushroom, bomb or banana effect (hidden to the player).
367 // Determine egg effect, too, and apply its first part if possible.
368 getBasicMove_aux(psq1
, sq2
, tr
, initMove
) {
369 const [x1
, y1
] = [psq1
.x
, psq1
.y
];
370 const color1
= this.turn
;
371 const piece1
= (!!tr
? tr
.p : (psq1
.p
|| this.getPiece(x1
, y1
)));
372 const oppCol
= V
.GetOppCol(color1
);
378 // banana or bomb defines next square, or the move ends there
387 if (this.board
[x1
][y1
] != V
.EMPTY
) {
388 const initP1
= this.getPiece(x1
, y1
);
393 c: this.getColor(x1
, y1
),
397 if ([V
.BANANA
, V
.BOMB
].includes(initP1
)) {
398 const steps
= V
.steps
[initP1
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
399 move.next
= this.getRandomSquare([x1
, y1
], steps
);
402 move.end
= { x: x1
, y: y1
};
405 const [x2
, y2
] = [sq2
[0], sq2
[1]];
406 // The move starts normally, on board:
407 let move = super.getBasicMove([x1
, y1
], [x2
, y2
], tr
);
408 if (!!tr
) move.promoteInto
= tr
.c
+ tr
.p
; //in case of (chomped...)
409 const L
= this.effects
.length
;
411 [V
.PAWN
, V
.KNIGHT
].includes(piece1
) &&
413 (this.subTurn
== 1 || this.effects
[L
-1] == "daisy")
417 const twoSquaresMove
= (Math
.abs(x2
- x1
) == 2);
418 const mushroomX
= x1
+ (twoSquaresMove
? (x2
- x1
) / 2 : 0);
427 if (this.getColor(mushroomX
, y1
) == 'a') {
433 p: this.getPiece(mushroomX
, y1
)
440 const deltaX
= Math
.abs(x2
- x1
);
441 const deltaY
= Math
.abs(y2
- y1
);
443 x1
+ (deltaX
== 2 ? (x2
- x1
) / 2 : 0),
444 y1
+ (deltaY
== 2 ? (y2
- y1
) / 2 : 0)
447 this.board
[eggSquare
[0]][eggSquare
[1]] != V
.EMPTY
&&
448 this.getColor(eggSquare
[0], eggSquare
[1]) != 'a'
461 if (this.getColor(eggSquare
[0], eggSquare
[1]) == 'a') {
467 p: this.getPiece(eggSquare
[0], eggSquare
[1])
475 // For (wa)luigi effect:
476 const changePieceColor
= (color
) => {
478 const oppLastRank
= (color
== 'w' ? 7 : 0);
479 for (let i
=0; i
<8; i
++) {
480 for (let j
=0; j
<8; j
++) {
481 const piece
= this.getPiece(i
, j
);
483 (i
!= move.vanish
[0].x
|| j
!= move.vanish
[0].y
) &&
484 this.board
[i
][j
] != V
.EMPTY
&&
485 piece
!= V
.INVISIBLE_QUEEN
&&
486 this.getColor(i
, j
) == color
488 if (piece
!= V
.KING
&& (piece
!= V
.PAWN
|| i
!= oppLastRank
))
489 pieces
.push({ x: i
, y: j
, p: piece
});
493 // Special case of the current piece (still at its initial position)
495 pieces
.push({ x: move.appear
[0].x
, y: move.appear
[0].y
, p: piece1
});
496 const cp
= pieces
[randInt(pieces
.length
)];
497 if (move.appear
[0].x
!= cp
.x
|| move.appear
[0].y
!= cp
.y
) {
507 else move.appear
.shift();
512 c: V
.GetOppCol(color
),
517 const applyEggEffect
= () => {
518 if (this.subTurn
== 2)
519 // No egg effects at subTurn 2
521 // 1) Determine the effect (some may be impossible)
522 let effects
= ["kingboo", "koopa", "chomp", "bowser", "daisy"];
523 if (Object
.values(this.captured
[color1
]).some(c
=> c
>= 1))
524 effects
.push("toadette");
525 const lastRank
= { 'w': 0, 'b': 7 };
527 this.board
.some((b
,i
) =>
532 (cell
[1] != V
.PAWN
|| i
!= lastRank
[color1
])
537 effects
.push("luigi");
542 (piece1
!= V
.PAWN
|| move.appear
[0].x
!= lastRank
[oppCol
])
544 this.board
.some((b
,i
) =>
549 (cell
[1] != V
.PAWN
|| i
!= lastRank
[oppCol
])
554 effects
.push("waluigi");
556 const effect
= effects
[randInt(effects
.length
)];
557 move.end
.effect
= effect
;
558 // 2) Apply it if possible
559 if (!(["kingboo", "toadette", "daisy"].includes(effect
))) {
563 // Maybe egg effect was applied after others,
564 // so just shift vanish array:
571 move.appear
[0].p
= V
.IMMOBILIZE_CODE
[piece1
];
574 changePieceColor(oppCol
);
577 changePieceColor(color1
);
582 const applyMushroomEffect
= () => {
583 if ([V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece1
)) {
584 // Just make another similar step, if possible (non-capturing)
586 move.appear
[0].x
+ (x2
- x1
),
587 move.appear
[0].y
+ (y2
- y1
)
592 this.board
[i
][j
] == V
.EMPTY
||
593 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
594 this.getColor(i
, j
) == 'a'
597 move.appear
[0].x
= i
;
598 move.appear
[0].y
= j
;
599 if (this.board
[i
][j
] != V
.EMPTY
) {
600 const object
= this.getPiece(i
, j
);
601 const color
= this.getColor(i
, j
);
613 const steps
= V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
614 move.next
= this.getRandomSquare([i
, j
], steps
);
620 applyMushroomEffect();
627 // Queen, bishop or rook:
629 (x2
- x1
) / Math
.abs(x2
- x1
) || 0,
630 (y2
- y1
) / Math
.abs(y2
- y1
) || 0
632 const next
= [move.appear
[0].x
+ step
[0], move.appear
[0].y
+ step
[1]];
634 V
.OnBoard(next
[0], next
[1]) &&
635 this.board
[next
[0]][next
[1]] != V
.EMPTY
&&
636 this.getPiece(next
[0], next
[1]) != V
.INVISIBLE_QUEEN
&&
637 this.getColor(next
[0], next
[1]) != 'a'
639 const afterNext
= [next
[0] + step
[0], next
[1] + step
[1]];
640 if (V
.OnBoard(afterNext
[0], afterNext
[1])) {
641 const afterColor
= this.getColor(afterNext
[0], afterNext
[1]);
643 this.board
[afterNext
[0]][afterNext
[1]] == V
.EMPTY
||
646 move.appear
[0].x
= afterNext
[0];
647 move.appear
[0].y
= afterNext
[1];
648 if (this.board
[afterNext
[0]][afterNext
[1]] != V
.EMPTY
) {
649 // The "object" could also be an opponent's piece
650 const object
= this.getPiece(afterNext
[0], afterNext
[1]);
663 V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
664 move.next
= this.getRandomSquare(
665 [afterNext
[0], afterNext
[1]], steps
);
671 applyMushroomEffect();
680 const color2
= this.getColor(x2
, y2
);
681 const piece2
= this.getPiece(x2
, y2
);
686 const steps
= V
.steps
[piece2
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
687 move.next
= this.getRandomSquare([x2
, y2
], steps
);
690 applyMushroomEffect();
693 if (this.subTurn
== 1)
694 // No egg effect at subTurn 2
702 move.appear
.length
> 0 &&
703 [V
.ROOK
, V
.BISHOP
].includes(piece1
)
705 const finalSquare
= [move.appear
[0].x
, move.appear
[0].y
];
708 this.getColor(finalSquare
[0], finalSquare
[1]) != 'a' ||
709 this.getPiece(finalSquare
[0], finalSquare
[1]) != V
.EGG
712 V
.steps
[piece1
== V
.ROOK
? V
.BISHOP : V
.ROOK
].filter(s
=> {
713 const [i
, j
] = [finalSquare
[0] + s
[0], finalSquare
[1] + s
[1]];
716 // NOTE: do not place a bomb or banana on the invisible queen!
717 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
720 if (validSteps
.length
>= 1) {
721 const randIdx
= randInt(validSteps
.length
);
723 finalSquare
[0] + validSteps
[randIdx
][0],
724 finalSquare
[1] + validSteps
[randIdx
][1]
731 p: (piece1
== V
.ROOK
? V
.BANANA : V
.BOMB
)
734 if (this.board
[x
][y
] != V
.EMPTY
) {
736 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
744 getBasicMove(psq1
, sq2
, tr
) {
746 if (Array
.isArray(psq1
)) psq1
= { x: psq1
[0], y: psq1
[1] };
747 let m
= this.getBasicMove_aux(psq1
, sq2
, tr
, "initMove");
749 // Last move ended on bomb or banana, direction change
750 V
.PlayOnBoard(this.board
, m
);
752 m
= this.getBasicMove_aux(
753 { x: m
.appear
[0].x
, y: m
.appear
[0].y
}, m
.next
);
755 for (let i
=moves
.length
-1; i
>=0; i
--) V
.UndoOnBoard(this.board
, moves
[i
]);
757 // Now merge moves into one
759 // start is wrong for Toadette moves --> it's fixed later
760 move.start
= { x: psq1
.x
, y: psq1
.y
};
761 move.end
= !!sq2
? { x: sq2
[0], y: sq2
[1] } : { x: psq1
.x
, y: psq1
.y
};
762 if (!!tr
) move.promoteInto
= moves
[0].promoteInto
;
763 let lm
= moves
[moves
.length
-1];
764 if (this.subTurn
== 1 && !!lm
.end
.effect
)
765 move.end
.effect
= lm
.end
.effect
;
766 if (moves
.length
== 1) {
767 move.appear
= moves
[0].appear
;
768 move.vanish
= moves
[0].vanish
;
771 // Keep first vanish and last appear (if any)
772 move.appear
= lm
.appear
;
773 move.vanish
= moves
[0].vanish
;
775 move.vanish
.length
>= 1 &&
776 move.appear
.length
>= 1 &&
777 move.vanish
[0].x
== move.appear
[0].x
&&
778 move.vanish
[0].y
== move.appear
[0].y
780 // Loopback on initial square:
784 for (let i
=1; i
< moves
.length
- 1; i
++) {
785 for (let v
of moves
[i
].vanish
) {
786 // Only vanishing objects, not appearing at init move
790 moves
[0].appear
.length
== 1 ||
791 moves
[0].appear
[1].x
!= v
.x
||
792 moves
[0].appear
[1].y
!= v
.y
799 // Final vanish is our piece, but others might be relevant
800 // (for some egg bonuses at least).
801 for (let i
=1; i
< lm
.vanish
.length
; i
++) {
803 lm
.vanish
[i
].c
!= 'a' ||
804 moves
[0].appear
.length
== 1 ||
805 moves
[0].appear
[1].x
!= lm
.vanish
[i
].x
||
806 moves
[0].appear
[1].y
!= lm
.vanish
[i
].y
808 move.vanish
.push(lm
.vanish
[i
]);
815 getPotentialPawnMoves([x
, y
]) {
816 const color
= this.turn
;
817 const oppCol
= V
.GetOppCol(color
);
818 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
819 const shiftX
= V
.PawnSpecs
.directions
[color
];
820 const firstRank
= (color
== "w" ? sizeX
- 1 : 0);
823 this.board
[x
+ shiftX
][y
] == V
.EMPTY
||
824 this.getColor(x
+ shiftX
, y
) == 'a' ||
825 this.getPiece(x
+ shiftX
, y
) == V
.INVISIBLE_QUEEN
827 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
829 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
831 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
||
832 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
833 this.getPiece(x
+ 2 * shiftX
, y
) == V
.INVISIBLE_QUEEN
836 moves
.push(this.getBasicMove({ x: x
, y: y
}, [x
+ 2 * shiftX
, y
]));
839 for (let shiftY
of [-1, 1]) {
842 y
+ shiftY
< sizeY
&&
843 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
844 // Pawns cannot capture invisible queen this way!
845 this.getPiece(x
+ shiftX
, y
+ shiftY
) != V
.INVISIBLE_QUEEN
&&
846 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, y
+ shiftY
))
848 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
854 getPotentialQueenMoves(sq
) {
855 const normalMoves
= super.getPotentialQueenMoves(sq
);
856 // If flag allows it, add 'invisible movements'
857 let invisibleMoves
= [];
858 if (this.powerFlags
[this.turn
][V
.QUEEN
]) {
859 normalMoves
.forEach(m
=> {
861 m
.appear
.length
== 1 &&
862 m
.vanish
.length
== 1 &&
863 // Only simple non-capturing moves:
866 let im
= JSON
.parse(JSON
.stringify(m
));
867 im
.appear
[0].p
= V
.INVISIBLE_QUEEN
;
868 im
.end
.noHighlight
= true;
869 invisibleMoves
.push(im
);
873 return normalMoves
.concat(invisibleMoves
);
876 getPotentialKingMoves([x
, y
]) {
877 let moves
= super.getPotentialKingMoves([x
, y
]);
878 const color
= this.turn
;
879 // If flag allows it, add 'remote shell captures'
880 if (this.powerFlags
[this.turn
][V
.KING
]) {
881 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
882 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
886 this.board
[i
][j
] == V
.EMPTY
||
887 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
889 this.getColor(i
, j
) == 'a' &&
890 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
897 if (V
.OnBoard(i
, j
)) {
898 const colIJ
= this.getColor(i
, j
);
899 if (colIJ
!= color
) {
900 // May just destroy a bomb or banana:
903 start: { x: x
, y: y
},
908 x: i
, y: j
, c: colIJ
, p: this.getPiece(i
, j
)
920 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
922 outerLoop: for (let step
of steps
) {
928 this.board
[i
][j
] == V
.EMPTY
||
929 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
931 this.getColor(i
, j
) == 'a' &&
932 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
936 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
937 if (oneStep
) continue outerLoop
;
941 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
942 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
947 getAllPotentialMoves() {
948 if (this.subTurn
== 1) return super.getAllPotentialMoves();
950 const color
= this.turn
;
951 const L
= this.effects
.length
;
952 switch (this.effects
[L
-1]) {
955 for (let i
=0; i
<8; i
++) {
956 for (let j
=0; j
<8; j
++) {
957 const colIJ
= this.getColor(i
, j
);
958 const pieceIJ
= this.getPiece(i
, j
);
961 this.board
[i
][j
] != V
.EMPTY
&&
963 pieceIJ
!= V
.INVISIBLE_QUEEN
965 allPieces
.push({ x: i
, y: j
, c: colIJ
, p: pieceIJ
});
969 for (let x
=0; x
<8; x
++) {
970 for (let y
=0; y
<8; y
++) {
971 if (this.getColor(i
, j
) == color
) {
972 // Add exchange with something
973 allPieces
.forEach(pp
=> {
974 if (pp
.x
!= i
|| pp
.y
!= j
) {
975 const movedUnit
= new PiPo({
981 let mMove
= this.getBasicMove({ x: x
, y: y
}, [pp
.x
, pp
.y
]);
982 mMove
.appear
.push(movedUnit
);
992 const x
= V
.size
.x
+ (this.turn
== 'w' ? 0 : 1);
993 for (let y
= 0; y
< 8; y
++)
994 Array
.prototype.push
.apply(moves
, this.getReserveMoves([x
, y
]));
998 moves
= super.getAllPotentialMoves();
1005 // if (!this.states) this.states = [];
1006 // const stateFen = this.getFen();
1007 // this.states.push(stateFen);
1009 move.flags
= JSON
.stringify(this.aggregateFlags());
1010 V
.PlayOnBoard(this.board
, move);
1011 move.turn
= [this.turn
, this.subTurn
];
1012 if (["kingboo", "toadette", "daisy"].includes(move.end
.effect
)) {
1013 this.effects
.push(move.end
.effect
);
1017 this.turn
= V
.GetOppCol(this.turn
);
1021 this.postPlay(move);
1025 if (move.end
.effect
== "toadette") this.reserve
= this.captured
;
1026 else this.reserve
= undefined;
1027 const color
= move.turn
[0];
1029 move.vanish
.length
== 2 &&
1030 move.vanish
[1].c
!= 'a' &&
1031 move.appear
.length
== 1 //avoid king Boo!
1033 // Capture: update this.captured
1034 let capturedPiece
= move.vanish
[1].p
;
1035 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1036 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1037 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1038 this.captured
[move.vanish
[1].c
][capturedPiece
]++;
1040 else if (move.vanish
.length
== 0) {
1041 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1042 // A piece is back on board
1043 this.captured
[move.appear
[0].c
][move.appear
[0].p
]--;
1045 if (move.appear
.length
== 0) {
1046 // Three cases: king "shell capture", Chomp or Koopa
1047 if (this.getPiece(move.start
.x
, move.start
.y
) == V
.KING
)
1048 // King remote capture:
1049 this.powerFlags
[color
][V
.KING
] = false;
1050 else if (move.end
.effect
== "chomp")
1051 this.captured
[color
][move.vanish
[0].p
]++;
1053 else if (move.appear
[0].p
== V
.INVISIBLE_QUEEN
)
1054 this.powerFlags
[move.appear
[0].c
][V
.QUEEN
] = false;
1055 if (this.subTurn
== 2) return;
1057 move.turn
[1] == 1 &&
1058 move.appear
.length
== 0 ||
1059 !(Object
.keys(V
.IMMOBILIZE_DECODE
).includes(move.appear
[0].p
))
1061 // Look for an immobilized piece of my color: it can now move
1062 for (let i
=0; i
<8; i
++) {
1063 for (let j
=0; j
<8; j
++) {
1064 if (this.board
[i
][j
] != V
.EMPTY
) {
1065 const piece
= this.getPiece(i
, j
);
1067 this.getColor(i
, j
) == color
&&
1068 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
1070 this.board
[i
][j
] = color
+ V
.IMMOBILIZE_DECODE
[piece
];
1071 move.wasImmobilized
= [i
, j
];
1077 // Also make opponent invisible queen visible again, if any
1078 const oppCol
= V
.GetOppCol(color
);
1079 for (let i
=0; i
<8; i
++) {
1080 for (let j
=0; j
<8; j
++) {
1082 this.board
[i
][j
] != V
.EMPTY
&&
1083 this.getColor(i
, j
) == oppCol
&&
1084 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
1086 this.board
[i
][j
] = oppCol
+ V
.QUEEN
;
1087 move.wasInvisible
= [i
, j
];
1094 this.disaggregateFlags(JSON
.parse(move.flags
));
1095 V
.UndoOnBoard(this.board
, move);
1096 if (["kingboo", "toadette", "daisy"].includes(move.end
.effect
))
1098 else this.movesCount
--;
1099 this.turn
= move.turn
[0];
1100 this.subTurn
= move.turn
[1];
1101 this.postUndo(move);
1103 // const stateFen = this.getFen();
1104 // if (stateFen != this.states[this.states.length-1]) debugger;
1105 // this.states.pop();
1109 if (!!move.wasImmobilized
) {
1110 const [i
, j
] = move.wasImmobilized
;
1112 this.getColor(i
, j
) + V
.IMMOBILIZE_CODE
[this.getPiece(i
, j
)];
1114 if (!!move.wasInvisible
) {
1115 const [i
, j
] = move.wasInvisible
;
1116 this.board
[i
][j
] = this.getColor(i
, j
) + V
.INVISIBLE_QUEEN
;
1118 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1119 let capturedPiece
= move.vanish
[1].p
;
1120 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1121 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1122 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1123 this.captured
[move.vanish
[1].c
][capturedPiece
]--;
1125 else if (move.vanish
.length
== 0) {
1126 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1127 // A piece was back on board
1128 this.captured
[move.appear
[0].c
][move.appear
[0].p
]++;
1130 else if (move.appear
.length
== 0 && move.end
.effect
== "chomp")
1131 this.captured
[move.vanish
[0].c
][move.vanish
[0].p
]--;
1132 if (move.vanish
.length
== 0) this.reserve
= this.captured
;
1133 else this.reserve
= undefined;
1141 // Find kings (not tracked in this variant)
1142 let kingThere
= { w: false, b: false };
1143 for (let i
=0; i
<8; i
++) {
1144 for (let j
=0; j
<8; j
++) {
1146 this.board
[i
][j
] != V
.EMPTY
&&
1147 ['k', 'l'].includes(this.getPiece(i
, j
))
1149 kingThere
[this.getColor(i
, j
)] = true;
1153 if (!kingThere
['w']) return "0-1";
1154 if (!kingThere
['b']) return "1-0";
1155 if (!this.atLeastOneMove()) return (this.turn
== 'w' ? "0-1" : "1-0");
1159 static GenRandInitFen(options
) {
1161 SuicideRules
.GenRandInitFen(options
).slice(0, -1) +
1162 // Add Peach + Mario flags + capture counts
1167 filterValid(moves
) {
1171 static get VALUES() {
1172 return Object
.assign(
1190 static get SEARCH_DEPTH() {
1195 const moves
= this.getAllValidMoves();
1196 // Split into "normal" and "random" moves:
1197 // (Next splitting condition is OK because cannot take self object
1198 // without a banana or bomb on the way).
1199 const deterministicMoves
= moves
.filter(m
=> {
1200 return m
.vanish
.every(a
=> a
.c
!= 'a' || a
.p
== V
.MUSHROOM
);
1202 const randomMoves
= moves
.filter(m
=> {
1203 return m
.vanish
.some(a
=> a
.c
== 'a' && a
.p
!= V
.MUSHROOM
);
1205 if (Math
.random() < deterministicMoves
.length
/ randomMoves
.length
)
1206 // Play a deterministic one: capture king or material if possible
1207 return super.getComputerMove(deterministicMoves
);
1208 // Play a random effect move, at random:
1209 let move1
= randomMoves
[randInt(randomMoves
.length
)];
1211 let move2
= undefined;
1212 if (this.subTurn
== 2) {
1213 const moves2
= this.getAllValidMoves();
1214 move2
= moves2
[randInt(moves2
.length
)];
1217 if (!move2
) return move1
;
1218 return [move1
, move2
];
1222 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return "-";
1225 move.appear
.length
> 0 &&
1226 move.appear
[0].p
== V
.INVISIBLE_QUEEN
1230 const finalSquare
= V
.CoordsToSquare(move.end
);
1231 // Next condition also includes Toadette placements:
1232 if (move.appear
.length
> 0 && move.vanish
.every(a
=> a
.c
== 'a')) {
1234 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
1235 return piece
+ "@" + finalSquare
;
1237 else if (move.appear
.length
== 0) {
1238 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
1239 if (piece
== V
.KING
&& !move.end
.effect
)
1240 // King remote capture
1241 return "Kx" + finalSquare
;
1242 // Koopa or Chomp, or loopback after bananas, bombs & mushrooms:
1244 piece
.toUpperCase() + "x" + finalSquare
+
1247 ? "*" + (move.end
.effect
== "koopa" ? "K" : "C")
1252 if (move.appear
.length
== 1 && move.vanish
.length
== 1) {
1253 const moveStart
= move.appear
[0].p
.toUpperCase() + "@";
1254 if (move.appear
[0].c
== 'a' && move.vanish
[0].c
== 'a')
1255 // Bonus replacement:
1256 return moveStart
+ finalSquare
;
1258 move.vanish
[0].p
== V
.INVISIBLE_QUEEN
&&
1259 move.appear
[0].x
== move.vanish
[0].x
&&
1260 move.appear
[0].y
== move.vanish
[0].y
1262 // Toadette takes invisible queen
1263 return moveStart
+ "Q" + finalSquare
;
1267 move.appear
.length
== 2 &&
1268 move.vanish
.length
== 2 &&
1269 move.appear
.every(a
=> a
.c
!= 'a') &&
1270 move.vanish
.every(v
=> v
.c
!= 'a')
1272 // King Boo exchange
1273 return V
.CoordsToSquare(move.start
) + finalSquare
;
1275 const piece
= move.vanish
[0].p
;
1276 let notation
= undefined;
1277 if (piece
== V
.PAWN
) {
1279 if (this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
) {
1281 const startColumn
= V
.CoordToColumn(move.start
.y
);
1282 notation
= startColumn
+ "x" + finalSquare
;
1284 else notation
= finalSquare
;
1285 if (move.appear
[0].p
!= V
.PAWN
)
1287 notation
+= "=" + move.appear
[0].p
.toUpperCase();
1291 piece
.toUpperCase() +
1292 (this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
? "x" : "") +
1295 if (!!move.end
.effect
) {
1296 switch (move.end
.effect
) {
1311 const lastAppear
= move.appear
[move.appear
.length
- 1];
1313 V
.CoordsToSquare({ x: lastAppear
.x
, y : lastAppear
.y
});
1314 notation
+= "*" + move.end
.effect
[0].toUpperCase() + effectOn
;