1 import ChessRules
from "/base_rules";
2 import GiveawayRules
from "/variants/Giveaway";
4 // TODO + display bonus messages
5 // + animation + multi-moves for bananas/bombs/mushrooms
9 import { ArrayFun
} from "/utils/array";
10 import { randInt
} from "/utils/alea";
11 import PiPo
from "/utils/PiPo.js";
12 import Move
from "/utils/Move.js";
14 export class ChakartRules
extends ChessRules
{
16 static get Options() {
21 variable: "randomness",
24 { label: "Deterministic", value: 0 },
25 { label: "Symmetric random", value: 1 },
26 { label: "Asymmetric random", value: 2 }
33 static get PawnSpecs() {
34 return SuicideRules
.PawnSpecs
;
43 static get IMMOBILIZE_CODE() {
54 static get IMMOBILIZE_DECODE() {
65 static get INVISIBLE_QUEEN() {
69 // Fictive color 'a', bomb banana mushroom egg
74 return 'd'; //"Donkey"
79 static get MUSHROOM() {
86 ? "w" + f
.toLowerCase()
87 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
93 ChessRules
.PIECES
.concat(
94 Object
.keys(V
.IMMOBILIZE_DECODE
)).concat(
95 [V
.BANANA
, V
.BOMB
, V
.EGG
, V
.MUSHROOM
, V
.INVISIBLE_QUEEN
])
103 b
[1] == V
.INVISIBLE_QUEEN
||
104 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(b
[1])
112 if (!!m
.promoteInto
) return m
.promoteInto
;
113 if (m
.appear
.length
== 0 && m
.vanish
.length
== 1)
114 // King 'remote shell capture', on an adjacent square:
115 return this.getPpath(m
.vanish
[0].c
+ m
.vanish
[0].p
);
116 let piece
= m
.appear
[0].p
;
117 if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
))
118 // Promotion by capture into immobilized piece: do not reveal!
119 piece
= V
.IMMOBILIZE_DECODE
[piece
];
120 return this.getPpath(m
.appear
[0].c
+ piece
);
123 static ParseFen(fen
) {
124 const fenParts
= fen
.split(" ");
125 return Object
.assign(
126 ChessRules
.ParseFen(fen
),
127 { captured: fenParts
[4] }
131 static IsGoodFen(fen
) {
132 if (!ChessRules
.IsGoodFen(fen
)) return false;
133 const captured
= V
.ParseFen(fen
).captured
;
134 if (!captured
|| !captured
.match(/^[0-9]{12,12}$/)) return false;
138 // King can be l or L (immobilized) --> similar to Alice variant
139 static IsGoodPosition(position
) {
140 if (position
.length
== 0) return false;
141 const rows
= position
.split("/");
142 if (rows
.length
!= V
.size
.x
) return false;
143 let kings
= { "k": 0, "K": 0, 'l': 0, 'L': 0 };
144 for (let row
of rows
) {
146 for (let i
= 0; i
< row
.length
; i
++) {
147 if (['K', 'k', 'L', 'l'].includes(row
[i
])) kings
[row
[i
]]++;
148 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
150 const num
= parseInt(row
[i
], 10);
151 if (isNaN(num
)) return false;
155 if (sumElts
!= V
.size
.y
) return false;
157 if (kings
['k'] + kings
['l'] == 0 || kings
['K'] + kings
['L'] == 0)
162 static IsGoodFlags(flags
) {
163 // 4 for Peach + Mario w, b
164 return !!flags
.match(/^[01]{4,4}$/);
168 // King can send shell? Queen can be invisible?
170 w: { 'k': false, 'q': false },
171 b: { 'k': false, 'q': false }
173 for (let c
of ["w", "b"]) {
174 for (let p
of ['k', 'q']) {
175 this.powerFlags
[c
][p
] =
176 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
182 return this.powerFlags
;
185 disaggregateFlags(flags
) {
186 this.powerFlags
= flags
;
190 return super.getFen() + " " + this.getCapturedFen();
194 return super.getFenForRepeat() + "_" + this.getCapturedFen();
198 let counts
= [...Array(12).fill(0)];
200 for (let p
of V
.RESERVE_PIECES
) {
201 counts
[i
] = this.captured
["w"][p
];
202 counts
[6 + i
] = this.captured
["b"][p
];
205 return counts
.join("");
210 setOtherVariables(fen
) {
211 super.setOtherVariables(fen
);
212 // Initialize captured pieces' counts from FEN
214 V
.ParseFen(fen
).captured
.split("").map(x
=> parseInt(x
, 10));
217 [V
.PAWN
]: captured
[0],
218 [V
.ROOK
]: captured
[1],
219 [V
.KNIGHT
]: captured
[2],
220 [V
.BISHOP
]: captured
[3],
221 [V
.QUEEN
]: captured
[4],
222 [V
.KING
]: captured
[5]
225 [V
.PAWN
]: captured
[6],
226 [V
.ROOK
]: captured
[7],
227 [V
.KNIGHT
]: captured
[8],
228 [V
.BISHOP
]: captured
[9],
229 [V
.QUEEN
]: captured
[10],
230 [V
.KING
]: 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.effects
.length
;
321 switch (this.effects
[L
-1]) {
323 // Exchange position with any visible piece,
324 // except pawns if arriving on last rank.
325 const lastRank
= { 'w': 0, 'b': 7 };
326 const color
= this.turn
;
327 const allowLastRank
= (this.getPiece(x
, y
) != V
.PAWN
);
328 for (let i
=0; i
<8; i
++) {
329 for (let j
=0; j
<8; j
++) {
330 const colIJ
= this.getColor(i
, j
);
331 const pieceIJ
= this.getPiece(i
, j
);
333 (i
!= x
|| j
!= y
) &&
334 this.board
[i
][j
] != V
.EMPTY
&&
335 pieceIJ
!= V
.INVISIBLE_QUEEN
&&
339 (pieceIJ
!= V
.PAWN
|| x
!= lastRank
[colIJ
]) &&
340 (allowLastRank
|| i
!= lastRank
[color
])
342 const movedUnit
= new PiPo({
346 p: this.getPiece(i
, j
)
348 let mMove
= this.getBasicMove({ x: x
, y: y
}, [i
, j
]);
349 mMove
.appear
.push(movedUnit
);
357 // Resurrect a captured piece
358 if (x
>= V
.size
.x
) moves
= this.getReserveMoves([x
, y
]);
361 // Play again with any piece
362 moves
= super.getPotentialMovesFrom([x
, y
]);
369 // Helper for getBasicMove(): banana/bomb effect
370 getRandomSquare([x
, y
], steps
) {
371 const validSteps
= steps
.filter(s
=> V
.OnBoard(x
+ s
[0], y
+ s
[1]));
372 const step
= validSteps
[randInt(validSteps
.length
)];
373 return [x
+ step
[0], y
+ step
[1]];
376 // Apply mushroom, bomb or banana effect (hidden to the player).
377 // Determine egg effect, too, and apply its first part if possible.
378 getBasicMove_aux(psq1
, sq2
, tr
, initMove
) {
379 const [x1
, y1
] = [psq1
.x
, psq1
.y
];
380 const color1
= this.turn
;
381 const piece1
= (!!tr
? tr
.p : (psq1
.p
|| this.getPiece(x1
, y1
)));
382 const oppCol
= V
.GetOppCol(color1
);
388 // banana or bomb defines next square, or the move ends there
397 if (this.board
[x1
][y1
] != V
.EMPTY
) {
398 const initP1
= this.getPiece(x1
, y1
);
403 c: this.getColor(x1
, y1
),
407 if ([V
.BANANA
, V
.BOMB
].includes(initP1
)) {
408 const steps
= V
.steps
[initP1
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
409 move.next
= this.getRandomSquare([x1
, y1
], steps
);
412 move.end
= { x: x1
, y: y1
};
415 const [x2
, y2
] = [sq2
[0], sq2
[1]];
416 // The move starts normally, on board:
417 let move = super.getBasicMove([x1
, y1
], [x2
, y2
], tr
);
418 if (!!tr
) move.promoteInto
= tr
.c
+ tr
.p
; //in case of (chomped...)
419 const L
= this.effects
.length
;
421 [V
.PAWN
, V
.KNIGHT
].includes(piece1
) &&
423 (this.subTurn
== 1 || this.effects
[L
-1] == "daisy")
427 const twoSquaresMove
= (Math
.abs(x2
- x1
) == 2);
428 const mushroomX
= x1
+ (twoSquaresMove
? (x2
- x1
) / 2 : 0);
437 if (this.getColor(mushroomX
, y1
) == 'a') {
443 p: this.getPiece(mushroomX
, y1
)
450 const deltaX
= Math
.abs(x2
- x1
);
451 const deltaY
= Math
.abs(y2
- y1
);
453 x1
+ (deltaX
== 2 ? (x2
- x1
) / 2 : 0),
454 y1
+ (deltaY
== 2 ? (y2
- y1
) / 2 : 0)
457 this.board
[eggSquare
[0]][eggSquare
[1]] != V
.EMPTY
&&
458 this.getColor(eggSquare
[0], eggSquare
[1]) != 'a'
471 if (this.getColor(eggSquare
[0], eggSquare
[1]) == 'a') {
477 p: this.getPiece(eggSquare
[0], eggSquare
[1])
485 // For (wa)luigi effect:
486 const changePieceColor
= (color
) => {
488 const oppLastRank
= (color
== 'w' ? 7 : 0);
489 for (let i
=0; i
<8; i
++) {
490 for (let j
=0; j
<8; j
++) {
491 const piece
= this.getPiece(i
, j
);
493 (i
!= move.vanish
[0].x
|| j
!= move.vanish
[0].y
) &&
494 this.board
[i
][j
] != V
.EMPTY
&&
495 piece
!= V
.INVISIBLE_QUEEN
&&
496 this.getColor(i
, j
) == color
498 if (piece
!= V
.KING
&& (piece
!= V
.PAWN
|| i
!= oppLastRank
))
499 pieces
.push({ x: i
, y: j
, p: piece
});
503 // Special case of the current piece (still at its initial position)
505 pieces
.push({ x: move.appear
[0].x
, y: move.appear
[0].y
, p: piece1
});
506 const cp
= pieces
[randInt(pieces
.length
)];
507 if (move.appear
[0].x
!= cp
.x
|| move.appear
[0].y
!= cp
.y
) {
517 else move.appear
.shift();
522 c: V
.GetOppCol(color
),
527 const applyEggEffect
= () => {
528 if (this.subTurn
== 2)
529 // No egg effects at subTurn 2
531 // 1) Determine the effect (some may be impossible)
532 let effects
= ["kingboo", "koopa", "chomp", "bowser", "daisy"];
533 if (Object
.values(this.captured
[color1
]).some(c
=> c
>= 1))
534 effects
.push("toadette");
535 const lastRank
= { 'w': 0, 'b': 7 };
537 this.board
.some((b
,i
) =>
542 (cell
[1] != V
.PAWN
|| i
!= lastRank
[color1
])
547 effects
.push("luigi");
552 (piece1
!= V
.PAWN
|| move.appear
[0].x
!= lastRank
[oppCol
])
554 this.board
.some((b
,i
) =>
559 (cell
[1] != V
.PAWN
|| i
!= lastRank
[oppCol
])
564 effects
.push("waluigi");
566 const effect
= effects
[randInt(effects
.length
)];
567 move.end
.effect
= effect
;
568 // 2) Apply it if possible
569 if (!(["kingboo", "toadette", "daisy"].includes(effect
))) {
573 // Maybe egg effect was applied after others,
574 // so just shift vanish array:
581 move.appear
[0].p
= V
.IMMOBILIZE_CODE
[piece1
];
584 changePieceColor(oppCol
);
587 changePieceColor(color1
);
592 const applyMushroomEffect
= () => {
593 if ([V
.PAWN
, V
.KING
, V
.KNIGHT
].includes(piece1
)) {
594 // Just make another similar step, if possible (non-capturing)
596 move.appear
[0].x
+ (x2
- x1
),
597 move.appear
[0].y
+ (y2
- y1
)
602 this.board
[i
][j
] == V
.EMPTY
||
603 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
604 this.getColor(i
, j
) == 'a'
607 move.appear
[0].x
= i
;
608 move.appear
[0].y
= j
;
609 if (this.board
[i
][j
] != V
.EMPTY
) {
610 const object
= this.getPiece(i
, j
);
611 const color
= this.getColor(i
, j
);
623 const steps
= V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
624 move.next
= this.getRandomSquare([i
, j
], steps
);
630 applyMushroomEffect();
637 // Queen, bishop or rook:
639 (x2
- x1
) / Math
.abs(x2
- x1
) || 0,
640 (y2
- y1
) / Math
.abs(y2
- y1
) || 0
642 const next
= [move.appear
[0].x
+ step
[0], move.appear
[0].y
+ step
[1]];
644 V
.OnBoard(next
[0], next
[1]) &&
645 this.board
[next
[0]][next
[1]] != V
.EMPTY
&&
646 this.getPiece(next
[0], next
[1]) != V
.INVISIBLE_QUEEN
&&
647 this.getColor(next
[0], next
[1]) != 'a'
649 const afterNext
= [next
[0] + step
[0], next
[1] + step
[1]];
650 if (V
.OnBoard(afterNext
[0], afterNext
[1])) {
651 const afterColor
= this.getColor(afterNext
[0], afterNext
[1]);
653 this.board
[afterNext
[0]][afterNext
[1]] == V
.EMPTY
||
656 move.appear
[0].x
= afterNext
[0];
657 move.appear
[0].y
= afterNext
[1];
658 if (this.board
[afterNext
[0]][afterNext
[1]] != V
.EMPTY
) {
659 // object = banana, bomb, mushroom or egg
660 const object
= this.getPiece(afterNext
[0], afterNext
[1]);
673 V
.steps
[object
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
674 move.next
= this.getRandomSquare(
675 [afterNext
[0], afterNext
[1]], steps
);
681 applyMushroomEffect();
690 const color2
= this.getColor(x2
, y2
);
691 const piece2
= this.getPiece(x2
, y2
);
696 const steps
= V
.steps
[piece2
== V
.BANANA
? V
.ROOK : V
.BISHOP
];
697 move.next
= this.getRandomSquare([x2
, y2
], steps
);
700 applyMushroomEffect();
703 if (this.subTurn
== 1)
704 // No egg effect at subTurn 2
712 move.appear
.length
> 0 &&
713 [V
.ROOK
, V
.BISHOP
].includes(piece1
)
715 const finalSquare
= [move.appear
[0].x
, move.appear
[0].y
];
718 this.getColor(finalSquare
[0], finalSquare
[1]) != 'a' ||
719 this.getPiece(finalSquare
[0], finalSquare
[1]) != V
.EGG
722 V
.steps
[piece1
== V
.ROOK
? V
.BISHOP : V
.ROOK
].filter(s
=> {
723 const [i
, j
] = [finalSquare
[0] + s
[0], finalSquare
[1] + s
[1]];
726 // NOTE: do not place a bomb or banana on the invisible queen!
727 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == 'a')
730 if (validSteps
.length
>= 1) {
731 const randIdx
= randInt(validSteps
.length
);
733 finalSquare
[0] + validSteps
[randIdx
][0],
734 finalSquare
[1] + validSteps
[randIdx
][1]
741 p: (piece1
== V
.ROOK
? V
.BANANA : V
.BOMB
)
744 if (this.board
[x
][y
] != V
.EMPTY
) {
746 new PiPo({ x: x
, y: y
, c: 'a', p: this.getPiece(x
, y
) }));
754 getBasicMove(psq1
, sq2
, tr
) {
756 if (Array
.isArray(psq1
)) psq1
= { x: psq1
[0], y: psq1
[1] };
757 let m
= this.getBasicMove_aux(psq1
, sq2
, tr
, "initMove");
759 // Last move ended on bomb or banana, direction change
760 V
.PlayOnBoard(this.board
, m
);
762 m
= this.getBasicMove_aux(
763 { x: m
.appear
[0].x
, y: m
.appear
[0].y
}, m
.next
);
765 for (let i
=moves
.length
-1; i
>=0; i
--) V
.UndoOnBoard(this.board
, moves
[i
]);
767 // Now merge moves into one
769 // start is wrong for Toadette moves --> it's fixed later
770 move.start
= { x: psq1
.x
, y: psq1
.y
};
771 move.end
= !!sq2
? { x: sq2
[0], y: sq2
[1] } : { x: psq1
.x
, y: psq1
.y
};
772 if (!!tr
) move.promoteInto
= moves
[0].promoteInto
;
773 let lm
= moves
[moves
.length
-1];
774 if (this.subTurn
== 1 && !!lm
.end
.effect
)
775 move.end
.effect
= lm
.end
.effect
;
776 if (moves
.length
== 1) {
777 move.appear
= moves
[0].appear
;
778 move.vanish
= moves
[0].vanish
;
781 // Keep first vanish and last appear (if any)
782 move.appear
= lm
.appear
;
783 move.vanish
= moves
[0].vanish
;
785 move.vanish
.length
>= 1 &&
786 move.appear
.length
>= 1 &&
787 move.vanish
[0].x
== move.appear
[0].x
&&
788 move.vanish
[0].y
== move.appear
[0].y
790 // Loopback on initial square:
794 for (let i
=1; i
< moves
.length
- 1; i
++) {
795 for (let v
of moves
[i
].vanish
) {
796 // Only vanishing objects, not appearing at init move
800 moves
[0].appear
.length
== 1 ||
801 moves
[0].appear
[1].x
!= v
.x
||
802 moves
[0].appear
[1].y
!= v
.y
809 // Final vanish is our piece, but others might be relevant
810 // (for some egg bonuses at least).
811 for (let i
=1; i
< lm
.vanish
.length
; i
++) {
813 lm
.vanish
[i
].c
!= 'a' ||
814 moves
[0].appear
.length
== 1 ||
815 moves
[0].appear
[1].x
!= lm
.vanish
[i
].x
||
816 moves
[0].appear
[1].y
!= lm
.vanish
[i
].y
818 move.vanish
.push(lm
.vanish
[i
]);
825 getPotentialPawnMoves([x
, y
]) {
826 const color
= this.turn
;
827 const oppCol
= V
.GetOppCol(color
);
828 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
829 const shiftX
= V
.PawnSpecs
.directions
[color
];
830 const firstRank
= (color
== "w" ? sizeX
- 1 : 0);
833 this.board
[x
+ shiftX
][y
] == V
.EMPTY
||
834 this.getColor(x
+ shiftX
, y
) == 'a' ||
835 this.getPiece(x
+ shiftX
, y
) == V
.INVISIBLE_QUEEN
837 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
], moves
);
839 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
841 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
||
842 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
843 this.getPiece(x
+ 2 * shiftX
, y
) == V
.INVISIBLE_QUEEN
846 moves
.push(this.getBasicMove({ x: x
, y: y
}, [x
+ 2 * shiftX
, y
]));
849 for (let shiftY
of [-1, 1]) {
852 y
+ shiftY
< sizeY
&&
853 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
854 // Pawns cannot capture invisible queen this way!
855 this.getPiece(x
+ shiftX
, y
+ shiftY
) != V
.INVISIBLE_QUEEN
&&
856 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, y
+ shiftY
))
858 this.addPawnMoves([x
, y
], [x
+ shiftX
, y
+ shiftY
], moves
);
864 getPotentialQueenMoves(sq
) {
865 const normalMoves
= super.getPotentialQueenMoves(sq
);
866 // If flag allows it, add 'invisible movements'
867 let invisibleMoves
= [];
868 if (this.powerFlags
[this.turn
][V
.QUEEN
]) {
869 normalMoves
.forEach(m
=> {
871 m
.appear
.length
== 1 &&
872 m
.vanish
.length
== 1 &&
873 // Only simple non-capturing moves:
876 let im
= JSON
.parse(JSON
.stringify(m
));
877 im
.appear
[0].p
= V
.INVISIBLE_QUEEN
;
878 im
.end
.noHighlight
= true;
879 invisibleMoves
.push(im
);
883 return normalMoves
.concat(invisibleMoves
);
886 getPotentialKingMoves([x
, y
]) {
887 let moves
= super.getPotentialKingMoves([x
, y
]);
888 const color
= this.turn
;
889 // If flag allows it, add 'remote shell captures'
890 if (this.powerFlags
[this.turn
][V
.KING
]) {
891 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]).forEach(step
=> {
892 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
896 this.board
[i
][j
] == V
.EMPTY
||
897 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
899 this.getColor(i
, j
) == 'a' &&
900 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
907 if (V
.OnBoard(i
, j
)) {
908 const colIJ
= this.getColor(i
, j
);
909 if (colIJ
!= color
) {
910 // May just destroy a bomb or banana:
913 start: { x: x
, y: y
},
918 x: i
, y: j
, c: colIJ
, p: this.getPiece(i
, j
)
930 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
932 outerLoop: for (let step
of steps
) {
938 this.board
[i
][j
] == V
.EMPTY
||
939 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
||
941 this.getColor(i
, j
) == 'a' &&
942 [V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
946 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
947 if (oneStep
) continue outerLoop
;
951 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
952 moves
.push(this.getBasicMove({ x: x
, y: y
}, [i
, j
]));
957 getAllPotentialMoves() {
958 if (this.subTurn
== 1) return super.getAllPotentialMoves();
960 const color
= this.turn
;
961 const L
= this.effects
.length
;
962 switch (this.effects
[L
-1]) {
965 for (let i
=0; i
<8; i
++) {
966 for (let j
=0; j
<8; j
++) {
967 const colIJ
= this.getColor(i
, j
);
968 const pieceIJ
= this.getPiece(i
, j
);
971 this.board
[i
][j
] != V
.EMPTY
&&
973 pieceIJ
!= V
.INVISIBLE_QUEEN
975 allPieces
.push({ x: i
, y: j
, c: colIJ
, p: pieceIJ
});
979 for (let x
=0; x
<8; x
++) {
980 for (let y
=0; y
<8; y
++) {
981 if (this.getColor(i
, j
) == color
) {
982 // Add exchange with something
983 allPieces
.forEach(pp
=> {
984 if (pp
.x
!= i
|| pp
.y
!= j
) {
985 const movedUnit
= new PiPo({
991 let mMove
= this.getBasicMove({ x: x
, y: y
}, [pp
.x
, pp
.y
]);
992 mMove
.appear
.push(movedUnit
);
1002 const x
= V
.size
.x
+ (this.turn
== 'w' ? 0 : 1);
1003 for (let y
= 0; y
< 8; y
++)
1004 Array
.prototype.push
.apply(moves
, this.getReserveMoves([x
, y
]));
1008 moves
= super.getAllPotentialMoves();
1015 // if (!this.states) this.states = [];
1016 // const stateFen = this.getFen();
1017 // this.states.push(stateFen);
1019 move.flags
= JSON
.stringify(this.aggregateFlags());
1020 V
.PlayOnBoard(this.board
, move);
1021 move.turn
= [this.turn
, this.subTurn
];
1022 if (["kingboo", "toadette", "daisy"].includes(move.end
.effect
)) {
1023 this.effects
.push(move.end
.effect
);
1027 this.turn
= V
.GetOppCol(this.turn
);
1031 this.postPlay(move);
1035 if (move.end
.effect
== "toadette") this.reserve
= this.captured
;
1036 else this.reserve
= undefined;
1037 const color
= move.turn
[0];
1039 move.vanish
.length
== 2 &&
1040 move.vanish
[1].c
!= 'a' &&
1041 move.appear
.length
== 1 //avoid king Boo!
1043 // Capture: update this.captured
1044 let capturedPiece
= move.vanish
[1].p
;
1045 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1046 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1047 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1048 this.captured
[move.vanish
[1].c
][capturedPiece
]++;
1050 else if (move.vanish
.length
== 0) {
1051 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1052 // A piece is back on board
1053 this.captured
[move.appear
[0].c
][move.appear
[0].p
]--;
1055 if (move.appear
.length
== 0) {
1056 // Three cases: king "shell capture", Chomp or Koopa
1057 if (this.getPiece(move.start
.x
, move.start
.y
) == V
.KING
)
1058 // King remote capture:
1059 this.powerFlags
[color
][V
.KING
] = false;
1060 else if (move.end
.effect
== "chomp")
1061 this.captured
[color
][move.vanish
[0].p
]++;
1063 else if (move.appear
[0].p
== V
.INVISIBLE_QUEEN
)
1064 this.powerFlags
[move.appear
[0].c
][V
.QUEEN
] = false;
1065 if (this.subTurn
== 2) return;
1067 move.turn
[1] == 1 &&
1068 move.appear
.length
== 0 ||
1069 !(Object
.keys(V
.IMMOBILIZE_DECODE
).includes(move.appear
[0].p
))
1071 // Look for an immobilized piece of my color: it can now move
1072 for (let i
=0; i
<8; i
++) {
1073 for (let j
=0; j
<8; j
++) {
1074 if (this.board
[i
][j
] != V
.EMPTY
) {
1075 const piece
= this.getPiece(i
, j
);
1077 this.getColor(i
, j
) == color
&&
1078 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
1080 this.board
[i
][j
] = color
+ V
.IMMOBILIZE_DECODE
[piece
];
1081 move.wasImmobilized
= [i
, j
];
1087 // Also make opponent invisible queen visible again, if any
1088 const oppCol
= V
.GetOppCol(color
);
1089 for (let i
=0; i
<8; i
++) {
1090 for (let j
=0; j
<8; j
++) {
1092 this.board
[i
][j
] != V
.EMPTY
&&
1093 this.getColor(i
, j
) == oppCol
&&
1094 this.getPiece(i
, j
) == V
.INVISIBLE_QUEEN
1096 this.board
[i
][j
] = oppCol
+ V
.QUEEN
;
1097 move.wasInvisible
= [i
, j
];
1104 this.disaggregateFlags(JSON
.parse(move.flags
));
1105 V
.UndoOnBoard(this.board
, move);
1106 if (["kingboo", "toadette", "daisy"].includes(move.end
.effect
))
1108 else this.movesCount
--;
1109 this.turn
= move.turn
[0];
1110 this.subTurn
= move.turn
[1];
1111 this.postUndo(move);
1113 // const stateFen = this.getFen();
1114 // if (stateFen != this.states[this.states.length-1]) debugger;
1115 // this.states.pop();
1119 if (!!move.wasImmobilized
) {
1120 const [i
, j
] = move.wasImmobilized
;
1122 this.getColor(i
, j
) + V
.IMMOBILIZE_CODE
[this.getPiece(i
, j
)];
1124 if (!!move.wasInvisible
) {
1125 const [i
, j
] = move.wasInvisible
;
1126 this.board
[i
][j
] = this.getColor(i
, j
) + V
.INVISIBLE_QUEEN
;
1128 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= 'a') {
1129 let capturedPiece
= move.vanish
[1].p
;
1130 if (capturedPiece
== V
.INVISIBLE_QUEEN
) capturedPiece
= V
.QUEEN
;
1131 else if (Object
.keys(V
.IMMOBILIZE_DECODE
).includes(capturedPiece
))
1132 capturedPiece
= V
.IMMOBILIZE_DECODE
[capturedPiece
];
1133 this.captured
[move.vanish
[1].c
][capturedPiece
]--;
1135 else if (move.vanish
.length
== 0) {
1136 if (move.appear
.length
== 0 || move.appear
[0].c
== 'a') return;
1137 // A piece was back on board
1138 this.captured
[move.appear
[0].c
][move.appear
[0].p
]++;
1140 else if (move.appear
.length
== 0 && move.end
.effect
== "chomp")
1141 this.captured
[move.vanish
[0].c
][move.vanish
[0].p
]--;
1142 if (move.vanish
.length
== 0) this.reserve
= this.captured
;
1143 else this.reserve
= undefined;
1151 // Find kings (not tracked in this variant)
1152 let kingThere
= { w: false, b: false };
1153 for (let i
=0; i
<8; i
++) {
1154 for (let j
=0; j
<8; j
++) {
1156 this.board
[i
][j
] != V
.EMPTY
&&
1157 ['k', 'l'].includes(this.getPiece(i
, j
))
1159 kingThere
[this.getColor(i
, j
)] = true;
1163 if (!kingThere
['w']) return "0-1";
1164 if (!kingThere
['b']) return "1-0";
1165 if (!this.atLeastOneMove()) return (this.turn
== 'w' ? "0-1" : "1-0");
1169 genRandInitFen(seed
) {
1170 const gr
= new GiveawayRules({}, true);
1172 gr
.genRandInitFen(seed
).slice(0, -1) +
1173 // Add Peach + Mario flags + capture counts
1174 '{"flags": "1111", "ccount": "000000000000"}'
1178 filterValid(moves
) {
1182 static get VALUES() {
1183 return Object
.assign(
1201 static get SEARCH_DEPTH() {
1206 const moves
= this.getAllValidMoves();
1207 // Split into "normal" and "random" moves:
1208 // (Next splitting condition is OK because cannot take self object
1209 // without a banana or bomb on the way).
1210 const deterministicMoves
= moves
.filter(m
=> {
1211 return m
.vanish
.every(a
=> a
.c
!= 'a' || a
.p
== V
.MUSHROOM
);
1213 const randomMoves
= moves
.filter(m
=> {
1214 return m
.vanish
.some(a
=> a
.c
== 'a' && a
.p
!= V
.MUSHROOM
);
1216 if (Math
.random() < deterministicMoves
.length
/ randomMoves
.length
)
1217 // Play a deterministic one: capture king or material if possible
1218 return super.getComputerMove(deterministicMoves
);
1219 // Play a random effect move, at random:
1220 let move1
= randomMoves
[randInt(randomMoves
.length
)];
1222 let move2
= undefined;
1223 if (this.subTurn
== 2) {
1224 const moves2
= this.getAllValidMoves();
1225 move2
= moves2
[randInt(moves2
.length
)];
1228 if (!move2
) return move1
;
1229 return [move1
, move2
];
1233 if (move.vanish
.length
== 0 && move.appear
.length
== 0) return "-";
1236 move.appear
.length
> 0 &&
1237 move.appear
[0].p
== V
.INVISIBLE_QUEEN
1241 const finalSquare
= V
.CoordsToSquare(move.end
);
1242 // Next condition also includes Toadette placements:
1243 if (move.appear
.length
> 0 && move.vanish
.every(a
=> a
.c
== 'a')) {
1245 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
1246 return piece
+ "@" + finalSquare
;
1248 else if (move.appear
.length
== 0) {
1249 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
1250 if (piece
== V
.KING
&& !move.end
.effect
)
1251 // King remote capture
1252 return "Kx" + finalSquare
;
1253 // Koopa or Chomp, or loopback after bananas, bombs & mushrooms:
1255 piece
.toUpperCase() + "x" + finalSquare
+
1258 ? "*" + (move.end
.effect
== "koopa" ? "K" : "C")
1263 if (move.appear
.length
== 1 && move.vanish
.length
== 1) {
1264 const moveStart
= move.appear
[0].p
.toUpperCase() + "@";
1265 if (move.appear
[0].c
== 'a' && move.vanish
[0].c
== 'a')
1266 // Bonus replacement:
1267 return moveStart
+ finalSquare
;
1269 move.vanish
[0].p
== V
.INVISIBLE_QUEEN
&&
1270 move.appear
[0].x
== move.vanish
[0].x
&&
1271 move.appear
[0].y
== move.vanish
[0].y
1273 // Toadette takes invisible queen
1274 return moveStart
+ "Q" + finalSquare
;
1278 move.appear
.length
== 2 &&
1279 move.vanish
.length
== 2 &&
1280 move.appear
.every(a
=> a
.c
!= 'a') &&
1281 move.vanish
.every(v
=> v
.c
!= 'a')
1283 // King Boo exchange
1284 return V
.CoordsToSquare(move.start
) + finalSquare
;
1286 const piece
= move.vanish
[0].p
;
1287 let notation
= undefined;
1288 if (piece
== V
.PAWN
) {
1290 if (this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
) {
1292 const startColumn
= V
.CoordToColumn(move.start
.y
);
1293 notation
= startColumn
+ "x" + finalSquare
;
1295 else notation
= finalSquare
;
1296 if (move.appear
[0].p
!= V
.PAWN
)
1298 notation
+= "=" + move.appear
[0].p
.toUpperCase();
1302 piece
.toUpperCase() +
1303 (this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
? "x" : "") +
1306 if (!!move.end
.effect
) {
1307 switch (move.end
.effect
) {
1322 const lastAppear
= move.appear
[move.appear
.length
- 1];
1324 V
.CoordsToSquare({ x: lastAppear
.x
, y : lastAppear
.y
});
1325 notation
+= "*" + move.end
.effect
[0].toUpperCase() + effectOn
;