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
], 10);
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 // Initialize captured pieces' counts from FEN
215 V
.ParseFen(fen
).captured
.split("").map(x
=> parseInt(x
, 10));
218 [V
.PAWN
]: captured
[0],
219 [V
.ROOK
]: captured
[1],
220 [V
.KNIGHT
]: captured
[2],
221 [V
.BISHOP
]: captured
[3],
222 [V
.QUEEN
]: captured
[4],
223 [V
.KING
]: captured
[5]
226 [V
.PAWN
]: captured
[6],
227 [V
.ROOK
]: captured
[7],
228 [V
.KNIGHT
]: captured
[8],
229 [V
.BISHOP
]: captured
[9],
230 [V
.QUEEN
]: captured
[10],
231 [V
.KING
]: captured
[11]
241 for (let c
of ["w", "b"])
242 for (let p
of ['k', 'q']) fen
+= (this.powerFlags
[c
][p
] ? "1" : "0");
247 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
248 return this.board
[i
][j
].charAt(0);
252 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
253 return this.board
[i
][j
].charAt(1);
256 getReservePpath(index
, color
) {
257 return color
+ V
.RESERVE_PIECES
[index
];
260 static get RESERVE_PIECES() {
261 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
264 getReserveMoves([x
, y
]) {
265 const color
= this.turn
;
266 const p
= V
.RESERVE_PIECES
[y
];
267 if (this.reserve
[color
][p
] == 0) return [];
269 const start
= (color
== 'w' && p
== V
.PAWN
? 1 : 0);
270 const end
= (color
== 'b' && p
== V
.PAWN
? 7 : 8);
271 for (let i
= start
; i
< end
; i
++) {
272 for (let j
= 0; j
< V
.size
.y
; j
++) {
274 this.board
[i
][j
] == V
.EMPTY
||
275 this.getColor(i
, j
) == 'a' ||
276 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
278 let m
= this.getBasicMove({ p: p
, x: i
, y: j
});
279 m
.start
= { x: x
, y: y
};
287 getPotentialMovesFrom([x
, y
]) {
289 if (this.subTurn
== 1) {
290 moves
= super.getPotentialMovesFrom([x
, y
]);
291 const finalPieces
= V
.PawnSpecs
.promotions
;
292 const color
= this.turn
;
293 const lastRank
= (color
== "w" ? 0 : 7);
297 m
.appear
.length
> 0 &&
298 ['p', 's'].includes(m
.appear
[0].p
) &&
299 m
.appear
[0].x
== lastRank
301 for (let i
= 1; i
< finalPieces
.length
; i
++) {
302 const piece
= finalPieces
[i
];
303 let otherM
= JSON
.parse(JSON
.stringify(m
));
305 m
.appear
[0].p
== V
.PAWN
307 : V
.IMMOBILIZE_CODE
[finalPieces
[i
]];
310 // Finally alter m itself:
312 m
.appear
[0].p
== V
.PAWN
314 : V
.IMMOBILIZE_CODE
[finalPieces
[0]];
317 Array
.prototype.push
.apply(moves
, pMoves
);
321 const L
= this.firstMove
.length
;
322 const fm
= this.firstMove
[L
-1];
323 switch (fm
.end
.effect
) {
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 color
= this.turn
;
374 const validSteps
= steps
.filter(s
=> {
375 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
378 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) != color
)
381 if (validSteps
.length
== 0)
382 // Can happen after mushroom jump
384 const step
= validSteps
[randInt(validSteps
.length
)];
385 return [x
+ step
[0], y
+ step
[1]];
388 canMove([x
, y
], piece
) {
389 const color
= this.getColor(x
, y
);
390 const oppCol
= V
.GetOppCol(color
);
391 piece
= piece
|| this.getPiece(x
, y
);
392 if (piece
== V
.PAWN
) {
393 const forward
= (color
== 'w' ? -1 : 1);
395 V
.OnBoard(x
+ forward
, y
) &&
397 this.board
[x
+ forward
][y
] == V
.EMPTY
||
399 V
.OnBoard(x
+ forward
, y
+ 1) &&
400 this.board
[x
+ forward
][y
+ 1] != V
.EMPTY
&&
401 this.getColor
[x
+ forward
, y
+ 1] == oppCol
404 V
.OnBoard(x
+ forward
, y
- 1) &&
405 this.board
[x
+ forward
][y
- 1] != V
.EMPTY
&&
406 this.getColor
[x
+ forward
, y
- 1] == oppCol
411 // Checking one step is enough:
413 [V
.KING
, V
.QUEEN
].includes(piece
)
414 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
416 for (let step
of steps
) {
417 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
420 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) != color
)
428 // Apply mushroom, bomb or banana effect (hidden to the player).
429 // Determine egg effect, too, and apply its first part if possible.
430 getBasicMove_aux(psq1
, sq2
, tr
, initMove
) {
431 const [x1
, y1
] = [psq1
.x
, psq1
.y
];
432 const color1
= this.turn
;
433 const piece1
= (!!tr
? tr
.p : (psq1
.p
|| this.getPiece(x1
, y1
)));
434 const oppCol
= V
.GetOppCol(color1
);
440 // banana or bomb defines next square, or the move ends there
449 if (this.board
[x1
][y1
] != V
.EMPTY
) {
450 const initP1
= this.getPiece(x1
, y1
);
455 c: this.getColor(x1
, y1
),
459 if ([V
.BANANA
, V
.BOMB
].includes(initP1
)) {
460 const steps
= V
.steps
[initP1
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
461 move.next
= this.getRandomSquare([x1
, y1
], steps
);
464 move.end
= { x: x1
, y: y1
};
467 const [x2
, y2
] = [sq2
[0], sq2
[1]];
468 // The move starts normally, on board:
469 let move = super.getBasicMove([x1
, y1
], [x2
, y2
], tr
);
470 if (!!tr
) move.promoteInto
= tr
.c
+ tr
.p
; //in case of (chomped...)
471 const L
= this.firstMove
.length
;
473 [V
.PAWN
, V
.KNIGHT
].includes(piece1
) &&
475 (this.subTurn
== 1 || this.firstMove
[L
-1].end
.effect
== "daisy")
479 const twoSquaresMove
= (Math
.abs(x2
- x1
) == 2);
480 const mushroomX
= x1
+ (twoSquaresMove
? (x2
- x1
) / 2 : 0);
489 if (this.getColor(mushroomX
, y1
) == 'a') {
495 p: this.getPiece(mushroomX
, y1
)
502 const deltaX
= Math
.abs(x2
- x1
);
503 const deltaY
= Math
.abs(y2
- y1
);
505 x1
+ (deltaX
== 2 ? (x2
- x1
) / 2 : 0),
506 y1
+ (deltaY
== 2 ? (y2
- y1
) / 2 : 0)
509 this.board
[eggSquare
[0]][eggSquare
[1]] != V
.EMPTY
&&
510 this.getColor(eggSquare
[0], eggSquare
[1]) != 'a'
523 if (this.getColor(eggSquare
[0], eggSquare
[1]) == 'a') {
529 p: this.getPiece(eggSquare
[0], eggSquare
[1])
537 // For (wa)luigi effect:
538 const changePieceColor
= (color
) => {
540 const oppLastRank
= (color
== 'w' ? 7 : 0);
541 for (let i
=0; i
<8; i
++) {
542 for (let j
=0; j
<8; j
++) {
544 (i
!= move.vanish
[0].x
|| j
!= move.vanish
[0].y
) &&
545 this.board
[i
][j
] != V
.EMPTY
&&
546 this.getColor(i
, j
) == color
548 const piece
= this.getPiece(i
, j
);
549 if (piece
!= V
.KING
&& (piece
!= V
.PAWN
|| i
!= oppLastRank
))
550 pieces
.push({ x: i
, y: j
, p: piece
});
554 // Special case of the current piece (still at its initial position)
556 pieces
.push({ x: move.appear
[0].x
, y: move.appear
[0].y
, p: piece1
});
557 const cp
= pieces
[randInt(pieces
.length
)];
558 if (move.appear
[0].x
!= cp
.x
|| move.appear
[0].y
!= cp
.y
) {
568 else move.appear
.shift();
573 c: V
.GetOppCol(color
),
578 const applyEggEffect
= () => {
579 if (this.subTurn
== 2)
580 // No egg effects at subTurn 2
582 // 1) Determine the effect (some may be impossible)
583 let effects
= ["kingboo", "koopa", "chomp", "bowser"];
584 if (Object
.values(this.captured
[color1
]).some(c
=> c
>= 1))
585 effects
.push("toadette");
586 const lastRank
= { 'w': 0, 'b': 7 };
587 let canPlayAgain
= undefined;
589 move.appear
[0].p
== V
.PAWN
&&
590 move.appear
[0].x
== lastRank
[color1
]
592 // Always possible: promote into a queen, rook or king
596 move.end
.effect
= "daisy";
597 V
.PlayOnBoard(this.board
, move);
598 const square
= [move.appear
[0].x
, move.appear
[0].y
];
599 canPlayAgain
= this.canMove(square
, piece1
);
600 V
.UndoOnBoard(this.board
, move);
601 delete move.end
["effect"];
603 if (canPlayAgain
) effects
.push("daisy");
605 this.board
.some((b
,i
) =>
610 (cell
[1] != V
.PAWN
|| i
!= lastRank
[color1
])
615 effects
.push("luigi");
620 (piece1
!= V
.PAWN
|| move.appear
[0].x
!= lastRank
[oppCol
])
622 this.board
.some((b
,i
) =>
627 (cell
[1] != V
.PAWN
|| i
!= lastRank
[oppCol
])
632 effects
.push("waluigi");
634 const effect
= effects
[randInt(effects
.length
)];
635 move.end
.effect
= effect
;
636 // 2) Apply it if possible
637 if (!(["kingboo", "toadette", "daisy"].includes(effect
))) {
641 // Maybe egg effect was applied after others,
642 // so just shift vanish array:
649 move.appear
[0].p
= V
.IMMOBILIZE_CODE
[piece1
];
652 changePieceColor(oppCol
);
655 changePieceColor(color1
);
660 const applyMushroomEffect
= () => {
661 if ([V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece1
)) {
662 // Just make another similar step, if possible (non-capturing)
664 move.appear
[0].x
+ (x2
- x1
),
665 move.appear
[0].y
+ (y2
- y1
)
669 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
671 move.appear
[0].x
= i
;
672 move.appear
[0].y
= j
;
673 if (this.board
[i
][j
] != V
.EMPTY
) {
674 const object
= this.getPiece(i
, j
);
686 const steps
= V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
687 move.next
= this.getRandomSquare([i
, j
], steps
);
693 applyMushroomEffect();
700 // Queen, bishop or rook:
702 (x2
- x1
) / Math
.abs(x2
- x1
) || 0,
703 (y2
- y1
) / Math
.abs(y2
- y1
) || 0
705 const next
= [move.appear
[0].x
+ step
[0], move.appear
[0].y
+ step
[1]];
707 V
.OnBoard(next
[0], next
[1]) &&
708 this.board
[next
[0]][next
[1]] != V
.EMPTY
&&
709 this.getColor(next
[0], next
[1]) != 'a'
711 const afterNext
= [next
[0] + step
[0], next
[1] + step
[1]];
712 if (V
.OnBoard(afterNext
[0], afterNext
[1])) {
713 const afterColor
= this.getColor(afterNext
[0], afterNext
[1])
715 this.board
[afterNext
[0]][afterNext
[1]] == V
.EMPTY
||
718 move.appear
[0].x
= afterNext
[0];
719 move.appear
[0].y
= afterNext
[1];
720 if (this.board
[afterNext
[0]][afterNext
[1]] != V
.EMPTY
) {
721 // The "object" could also be an opponent's piece
722 const object
= this.getPiece(afterNext
[0], afterNext
[1]);
735 V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
736 move.next
= this.getRandomSquare(
737 [afterNext
[0], afterNext
[1]], steps
);
743 applyMushroomEffect();
752 const color2
= this.getColor(x2
, y2
);
753 const piece2
= this.getPiece(x2
, y2
);
758 const steps
= V
.steps
[piece2
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
759 move.next
= this.getRandomSquare([x2
, y2
], steps
);
762 applyMushroomEffect();
765 if (this.subTurn
== 1)
766 // No egg effect at subTurn 2
774 move.appear
.length
> 0 &&
775 [V
.ROOK
, V
.BISHOP
].includes(piece1
)
777 const finalSquare
= [move.appear
[0].x
, move.appear
[0].y
];
780 this.getColor(finalSquare
[0], finalSquare
[1]) != 'a' ||
781 this.getPiece(finalSquare
[0], finalSquare
[1]) != V
.EGG
784 V
.steps
[piece1
== V
.ROOK
? V
.BISHOP : V
.ROOK
].filter(s
=> {
785 const [i
, j
] = [finalSquare
[0] + s
[0], finalSquare
[1] + s
[1]];
788 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
791 if (validSteps
.length
>= 1) {
793 finalSquare
[0] + validSteps
[0][0],
794 finalSquare
[1] + validSteps
[0][1]
801 p: (piece1
== V
.ROOK
? V
.BANANA : V
.BOMB
)
804 if (this.board
[x
][y
] != V
.EMPTY
) {
806 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
814 getBasicMove(psq1
, sq2
, tr
) {
816 if (Array
.isArray(psq1
)) psq1
= { x: psq1
[0], y: psq1
[1] };
817 let m
= this.getBasicMove_aux(psq1
, sq2
, tr
, "initMove");
819 // Last move ended on bomb or banana, direction change
820 V
.PlayOnBoard(this.board
, m
);
822 m
= this.getBasicMove_aux(
823 { x: m
.appear
[0].x
, y: m
.appear
[0].y
}, m
.next
);
825 for (let i
=moves
.length
-1; i
>=0; i
--) V
.UndoOnBoard(this.board
, moves
[i
]);
827 // Now merge moves into one
829 // start is wrong for Toadette moves --> it's fixed later
830 move.start
= { x: psq1
.x
, y: psq1
.y
};
831 move.end
= !!sq2
? { x: sq2
[0], y: sq2
[1] } : { x: psq1
.x
, y: psq1
.y
};
832 if (!!tr
) move.promoteInto
= moves
[0].promoteInto
;
833 let lm
= moves
[moves
.length
-1];
834 if (this.subTurn
== 1 && !!lm
.end
.effect
)
835 move.end
.effect
= lm
.end
.effect
;
836 if (moves
.length
== 1) {
837 move.appear
= moves
[0].appear
;
838 move.vanish
= moves
[0].vanish
;
841 // Keep first vanish and last appear (if any)
842 move.appear
= lm
.appear
;
843 move.vanish
= moves
[0].vanish
;
845 move.vanish
.length
>= 1 &&
846 move.appear
.length
>= 1 &&
847 move.vanish
[0].x
== move.appear
[0].x
&&
848 move.vanish
[0].y
== move.appear
[0].y
850 // Loopback on initial square:
854 for (let i
=1; i
< moves
.length
- 1; i
++) {
855 for (let v
of moves
[i
].vanish
) {
856 // Only vanishing objects, not appearing at init move
860 moves
[0].appear
.length
== 1 ||
861 moves
[0].appear
[1].x
!= v
.x
||
862 moves
[0].appear
[1].y
!= v
.y
869 // Final vanish is our piece, but others might be relevant
870 // (for some egg bonuses at least).
871 for (let i
=1; i
< lm
.vanish
.length
; i
++) {
873 lm
.vanish
[i
].c
!= 'a' ||
874 moves
[0].appear
.length
== 1 ||
875 moves
[0].appear
[1].x
!= lm
.vanish
[i
].x
||
876 moves
[0].appear
[1].y
!= lm
.vanish
[i
].y
878 move.vanish
.push(lm
.vanish
[i
]);
885 getPotentialPawnMoves([x
, y
]) {
886 const color
= this.turn
;
887 const oppCol
= V
.GetOppCol(color
);
888 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
889 const shiftX
= V
.PawnSpecs
.directions
[color
];
890 const firstRank
= (color
== "w" ? sizeX
- 1 : 0);
893 this.board
[x
+ shiftX
][y
] == V
.EMPTY
||
894 this.getColor(x
+ shiftX
, y
) == 'a'
896 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
898 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
900 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
||
901 this.getColor(x
+ 2 * shiftX
, y
) == 'a'
904 moves
.push(this.getBasicMove({ x: x
, y: y
}, [x
+ 2 * shiftX
, y
]));
907 for (let shiftY
of [-1, 1]) {
910 y
+ shiftY
< sizeY
&&
911 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
912 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, y
+ shiftY
))
914 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
920 getPotentialQueenMoves(sq
) {
921 const normalMoves
= super.getPotentialQueenMoves(sq
);
922 // If flag allows it, add 'invisible movements'
923 let invisibleMoves
= [];
924 if (this.powerFlags
[this.turn
][V
.QUEEN
]) {
925 normalMoves
.forEach(m
=> {
927 m
.appear
.length
== 1 &&
928 m
.vanish
.length
== 1 &&
929 // Only simple non-capturing moves:
932 let im
= JSON
.parse(JSON
.stringify(m
));
933 im
.appear
[0].p
= V
.INVISIBLE_QUEEN
;
934 im
.end
.noHighlight
= true;
935 invisibleMoves
.push(im
);
939 return normalMoves
.concat(invisibleMoves
);
942 getPotentialKingMoves([x
, y
]) {
943 let moves
= super.getPotentialKingMoves([x
, y
]);
944 const color
= this.turn
;
945 // If flag allows it, add 'remote shell captures'
946 if (this.powerFlags
[this.turn
][V
.KING
]) {
947 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
948 const [nextX
, nextY
] = [x
+ step
[0], y
+ step
[1]];
950 V
.OnBoard(nextX
, nextY
) &&
952 this.board
[nextX
][nextY
] == V
.EMPTY
||
954 this.getColor(nextX
, nextY
) == 'a' &&
955 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(nextX
, nextY
))
959 let [i
, j
] = [x
+ 2 * step
[0], y
+ 2 * step
[1]];
963 this.board
[i
][j
] == V
.EMPTY
||
965 this.getColor(i
, j
) == 'a' &&
966 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
973 if (V
.OnBoard(i
, j
)) {
974 const colIJ
= this.getColor(i
, j
);
975 if (colIJ
!= color
) {
976 // May just destroy a bomb or banana:
979 start: { x: x
, y: y
},
984 x: i
, y: j
, c: colIJ
, p: this.getPiece(i
, j
)
997 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
999 outerLoop: for (let step
of steps
) {
1000 let i
= x
+ step
[0];
1001 let j
= y
+ step
[1];
1005 this.board
[i
][j
] == V
.EMPTY
||
1006 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
1008 this.getColor(i
, j
) == 'a' &&
1009 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
1013 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1014 if (oneStep
) continue outerLoop
;
1018 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
1019 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
1024 getAllPotentialMoves() {
1025 if (this.subTurn
== 1) return super.getAllPotentialMoves();
1027 const L
= this.firstMove
.length
;
1028 const fm
= this.firstMove
[L
-1];
1029 switch (fm
.end
.effect
) {
1032 start: { x: -1, y: -1 },
1033 end: { x: -1, y: -1 },
1039 (fm
.vanish
[0].p
== V
.ROOK
? V
.steps
[V
.BISHOP
] : V
.steps
[V
.ROOK
])
1041 const [i
, j
] = [fm
.appear
[0].x
+ step
[0], fm
.appear
[0].y
+ step
[1]];
1044 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
1047 start: { x: -1, y: -1 },
1048 end: { x: i
, y: j
},
1054 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1059 if (this.board
[i
][j
] != V
.EMPTY
) {
1061 new PiPo({ x: i
, y: j
, c: 'a', p: this.getPiece(i
, j
) }));
1068 const [x
, y
] = [fm
.appear
[0].x
, fm
.appear
[0].y
];
1069 for (let i
=0; i
<8; i
++) {
1070 for (let j
=0; j
<8; j
++) {
1071 const colIJ
= this.getColor(i
, j
);
1075 this.board
[i
][j
] != V
.EMPTY
&&
1078 const movedUnit
= new PiPo({
1082 p: this.getPiece(i
, j
)
1084 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
1085 mMove
.appear
.push(movedUnit
);
1093 const x
= V
.size
.x
+ (this.turn
== 'w' ? 0 : 1);
1094 for (let y
= 0; y
< 8; y
++)
1095 Array
.prototype.push
.apply(moves
, this.getReserveMoves([x
, y
]));
1099 moves
= super.getPotentialMovesFrom([fm
.appear
[0].x
, fm
.appear
[0].y
]);
1106 const L
= this.firstMove
.length
;
1107 const fm
= (L
> 0 ? this.firstMove
[L
-1] : null);
1110 this.subTurn
== 1 ||
1111 !([0, "daisy"].includes(fm
.end
.effect
))
1115 const [x
, y
] = [square
[0], square
[1]];
1116 const deltaX
= Math
.abs(fm
.appear
[0].x
- x
);
1117 const deltaY
= Math
.abs(fm
.appear
[0].y
- y
);
1119 fm
.end
.effect
== 0 &&
1120 (this.board
[x
][y
] == V
.EMPTY
|| this.getColor(x
, y
) == 'a') &&
1122 (fm
.vanish
[0].p
== V
.ROOK
&& deltaX
== 1 && deltaY
== 1) ||
1123 (fm
.vanish
[0].p
== V
.BISHOP
&& deltaX
+ deltaY
== 1)
1127 start: { x: -1, y: -1 },
1128 end: { x: x
, y: y
},
1134 p: (fm
.vanish
[0].p
== V
.ROOK
? V
.BANANA : V
.BOMB
)
1139 if (this.board
[x
][y
] != V
.EMPTY
) {
1141 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
1146 fm
.end
.effect
== "daisy" &&
1147 deltaX
== 0 && deltaY
== 0 &&
1148 !this.canMove([x
, y
])
1150 // No possible move: return empty move
1152 start: { x: -1, y: -1 },
1153 end: { x: -1, y: -1 },
1162 // if (!this.states) this.states = [];
1163 // const stateFen = this.getFen();
1164 // this.states.push(stateFen);
1166 move.flags
= JSON
.stringify(this.aggregateFlags());
1167 V
.PlayOnBoard(this.board
, move);
1168 move.turn
= [this.turn
, this.subTurn
];
1169 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
)) {
1170 this.firstMove
.push(move);
1174 this.turn
= V
.GetOppCol(this.turn
);
1178 this.postPlay(move);
1182 if (move.end
.effect
== "toadette") this.reserve
= this.captured
;
1183 else this.reserve
= undefined;
1184 const color
= move.turn
[0];
1185 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1186 // Capture: update this.captured
1187 let capturedPiece
= move.vanish
[1].p
;
1188 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1189 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1190 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1191 this.captured
[move.vanish
[1].c
][capturedPiece
]++;
1193 else if (move.vanish
.length
== 0) {
1194 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1195 // A piece is back on board
1196 this.captured
[move.appear
[0].c
][move.appear
[0].p
]--;
1198 if (move.appear
.length
== 0) {
1199 // Three cases: king "shell capture", Chomp or Koopa
1200 if (this.getPiece(move.start
.x
, move.start
.y
) == V
.KING
)
1201 // King remote capture:
1202 this.powerFlags
[color
][V
.KING
] = false;
1203 else if (move.end
.effect
== "chomp")
1204 this.captured
[color
][move.vanish
[0].p
]++;
1206 else if (move.appear
[0].p
== V
.INVISIBLE_QUEEN
)
1207 this.powerFlags
[move.appear
[0].c
][V
.QUEEN
] = false;
1208 if (this.subTurn
== 2) return;
1210 move.turn
[1] == 1 &&
1211 move.appear
.length
== 0 ||
1212 !(Object
.keys(V
.IMMOBILIZE_DECODE
).includes(move.appear
[0].p
))
1214 // Look for an immobilized piece of my color: it can now move
1215 for (let i
=0; i
<8; i
++) {
1216 for (let j
=0; j
<8; j
++) {
1217 if (this.board
[i
][j
] != V
.EMPTY
) {
1218 const piece
= this.getPiece(i
, j
);
1220 this.getColor(i
, j
) == color
&&
1221 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
1223 this.board
[i
][j
] = color
+ V
.IMMOBILIZE_DECODE
[piece
];
1224 move.wasImmobilized
= [i
, j
];
1230 // Also make opponent invisible queen visible again, if any
1231 const oppCol
= V
.GetOppCol(color
);
1232 for (let i
=0; i
<8; i
++) {
1233 for (let j
=0; j
<8; j
++) {
1235 this.board
[i
][j
] != V
.EMPTY
&&
1236 this.getColor(i
, j
) == oppCol
&&
1237 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
1239 this.board
[i
][j
] = oppCol
+ V
.QUEEN
;
1240 move.wasInvisible
= [i
, j
];
1247 this.disaggregateFlags(JSON
.parse(move.flags
));
1248 V
.UndoOnBoard(this.board
, move);
1249 if ([0, "kingboo", "toadette", "daisy"].includes(move.end
.effect
))
1250 this.firstMove
.pop();
1251 else this.movesCount
--;
1252 this.turn
= move.turn
[0];
1253 this.subTurn
= move.turn
[1];
1254 this.postUndo(move);
1256 // const stateFen = this.getFen();
1257 // if (stateFen != this.states[this.states.length-1]) debugger;
1258 // this.states.pop();
1262 if (!!move.wasImmobilized
) {
1263 const [i
, j
] = move.wasImmobilized
;
1265 this.getColor(i
, j
) + V
.IMMOBILIZE_CODE
[this.getPiece(i
, j
)];
1267 if (!!move.wasInvisible
) {
1268 const [i
, j
] = move.wasInvisible
;
1269 this.board
[i
][j
] = this.getColor(i
, j
) + V
.INVISIBLE_QUEEN
;
1271 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1272 let capturedPiece
= move.vanish
[1].p
;
1273 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1274 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1275 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1276 this.captured
[move.vanish
[1].c
][capturedPiece
]--;
1278 else if (move.vanish
.length
== 0) {
1279 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1280 // A piece was back on board
1281 this.captured
[move.appear
[0].c
][move.appear
[0].p
]++;
1283 else if (move.appear
.length
== 0 && move.end
.effect
== "chomp")
1284 this.captured
[move.vanish
[0].c
][move.vanish
[0].p
]--;
1285 if (move.vanish
.length
== 0) this.reserve
= this.captured
;
1286 else this.reserve
= undefined;
1294 // Find kings (not tracked in this variant)
1295 let kingThere
= { w: false, b: false };
1296 for (let i
=0; i
<8; i
++) {
1297 for (let j
=0; j
<8; j
++) {
1299 this.board
[i
][j
] != V
.EMPTY
&&
1300 ['k', 'l'].includes(this.getPiece(i
, j
))
1302 kingThere
[this.getColor(i
, j
)] = true;
1306 if (!kingThere
['w']) return "0-1";
1307 if (!kingThere
['b']) return "1-0";
1308 if (!this.atLeastOneMove()) return (this.turn
== 'w' ? "0-1" : "1-0");
1312 static GenRandInitFen(randomness
) {
1314 SuicideRules
.GenRandInitFen(randomness
).slice(0, -1) +
1315 // Add Peach + Mario flags + capture counts
1320 filterValid(moves
) {
1324 static get VALUES() {
1325 return Object
.assign(
1343 static get SEARCH_DEPTH() {
1348 const moves
= this.getAllValidMoves();
1349 // Split into "normal" and "random" moves:
1350 // (Next splitting condition is OK because cannot take self object
1351 // without a banana or bomb on the way).
1352 const deterministicMoves
= moves
.filter(m
=> {
1353 return m
.vanish
.every(a
=> a
.c
!= 'a' || a
.p
== V
.MUSHROOM
);
1355 const randomMoves
= moves
.filter(m
=> {
1356 return m
.vanish
.some(a
=> a
.c
== 'a' && a
.p
!= V
.MUSHROOM
);
1358 if (Math
.random() < deterministicMoves
.length
/ randomMoves
.length
)
1359 // Play a deterministic one: capture king or material if possible
1360 return super.getComputerMove(deterministicMoves
);
1361 // Play a random effect move, at random:
1362 let move1
= randomMoves
[randInt(randomMoves
.length
)];
1364 let move2
= undefined;
1365 if (this.subTurn
== 2) {
1366 const moves2
= this.getAllValidMoves();
1367 move2
= moves2
[randInt(moves2
.length
)];
1370 if (!move2
) return move1
;
1371 return [move1
, move2
];
1375 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return "-";
1378 move.appear
.length
> 0 &&
1379 move.appear
[0].p
== V
.INVISIBLE_QUEEN
1383 const finalSquare
= V
.CoordsToSquare(move.end
);
1384 // Next condition also includes Toadette placements:
1385 if (move.appear
.length
> 0 && move.vanish
.every(a
=> a
.c
== 'a')) {
1387 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
1388 return piece
+ "@" + finalSquare
;
1390 else if (move.appear
.length
== 0) {
1391 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
1392 if (piece
== V
.KING
&& !move.end
.effect
)
1393 // King remote capture
1394 return "Kx" + finalSquare
;
1395 // Koopa or Chomp, or loopback after bananas, bombs & mushrooms:
1397 piece
.toUpperCase() + "x" + finalSquare
+
1400 ? "*" + (move.end
.effect
== "koopa" ? "K" : "C")
1405 if (move.appear
.length
== 1 && move.vanish
.length
== 1) {
1406 const moveStart
= move.appear
[0].p
.toUpperCase() + "@";
1407 if (move.appear
[0].c
== 'a' && move.vanish
[0].c
== 'a')
1408 // Bonus replacement:
1409 return moveStart
+ finalSquare
;
1411 move.vanish
[0].p
== V
.INVISIBLE_QUEEN
&&
1412 move.appear
[0].x
== move.vanish
[0].x
&&
1413 move.appear
[0].y
== move.vanish
[0].y
1415 // Toadette takes invisible queen
1416 return moveStart
+ "Q" + finalSquare
;
1420 move.appear
.length
== 2 &&
1421 move.vanish
.length
== 2 &&
1422 move.appear
.every(a
=> a
.c
!= 'a') &&
1423 move.vanish
.every(v
=> v
.c
!= 'a')
1425 // King Boo exchange
1426 return move.vanish
[1].p
.toUpperCase() + finalSquare
;
1428 const piece
= move.vanish
[0].p
;
1429 let notation
= undefined;
1430 if (piece
== V
.PAWN
) {
1432 if (move.vanish
.length
>= 2) {
1434 const startColumn
= V
.CoordToColumn(move.start
.y
);
1435 notation
= startColumn
+ "x" + finalSquare
;
1437 else notation
= finalSquare
;
1438 if (move.appear
[0].p
!= V
.PAWN
)
1440 notation
+= "=" + move.appear
[0].p
.toUpperCase();
1444 piece
.toUpperCase() +
1445 (move.vanish
.length
>= 2 ? "x" : "") +
1448 if (!!move.end
.effect
) {
1449 switch (move.end
.effect
) {
1464 const lastAppear
= move.appear
[move.appear
.length
- 1];
1466 V
.CoordsToSquare({ x: lastAppear
.x
, y : lastAppear
.y
});
1467 notation
+= "*" + move.end
.effect
[0].toUpperCase() + effectOn
;