1 import ChessRules
from "/base_rules.js";
2 import {ArrayFun
} from "/utils/array.js";
3 import {Random
} from "/utils/alea.js";
4 import {FenUtil
} from "/utils/setupPieces.js";
5 import PiPo
from "/utils/PiPo.js";
6 import Move
from "/utils/Move.js";
8 export default class ChakartRules
extends ChessRules
{
10 static get Options() {
15 variable: "randomness",
18 {label: "Deterministic", value: 0},
19 {label: "Symmetric random", value: 1},
20 {label: "Asymmetric random", value: 2}
28 get pawnPromotions() {
29 return ['q', 'r', 'n', 'b', 'k'];
45 static get IMMOBILIZE_CODE() {
56 static get IMMOBILIZE_DECODE() {
67 // Fictive color 'a', bomb banana mushroom egg
72 return 'd'; //"Donkey"
77 static get MUSHROOM() {
81 static get EGG_SURPRISE() {
83 "kingboo", "bowser", "daisy", "koopa",
84 "luigi", "waluigi", "toadette", "chomp"];
89 this.playerColor
!= this.turn
||
90 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(this.getPiece(x
, y
))
94 return this.egg
== "kingboo" || this.getColor(x
, y
) == this.turn
;
99 'i': {"class": "invisible"}, //queen
100 '?': {"class": "mystery"}, //...initial square
101 'e': {"class": "egg"},
102 'm': {"class": "mushroom"},
103 'd': {"class": "banana"},
104 'w': {"class": "bomb"},
105 'z': {"class": "remote-capture"}
108 's': {"class": ["immobilized", "pawn"]},
109 'u': {"class": ["immobilized", "rook"]},
110 'o': {"class": ["immobilized", "knight"]},
111 'c': {"class": ["immobilized", "bishop"]},
112 't': {"class": ["immobilized", "queen"]},
113 'l': {"class": ["immobilized", "king"]}
115 return Object
.assign(
118 // Virtual piece for "king remote shell captures"
122 [0, 1], [0, -1], [1, 0], [-1, 0],
123 [1, 1], [1, -1], [-1, 1], [-1, -1]
129 specials
, bowsered
, super.pieces(color
, x
, y
)
135 p
= this.getPiece(x
, y
);
136 return ['k', 'l'].includes(p
);
139 genRandInitBaseFen() {
140 const s
= FenUtil
.setupPieces(
141 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
143 randomness: this.options
["randomness"],
148 fen: s
.b
.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
149 s
.w
.join("").toUpperCase(),
150 o: {flags: "1111"} //Peach + Mario
157 ? "w" + f
.toLowerCase()
158 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
163 // King can send shell? Queen can be invisible?
165 w: {k: false, q: false},
166 b: {k: false, q: false}
168 for (let c
of ['w', 'b']) {
169 for (let p
of ['k', 'q']) {
170 this.powerFlags
[c
][p
] =
171 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
177 return this.powerFlags
;
180 disaggregateFlags(flags
) {
181 this.powerFlags
= flags
;
185 return ['w', 'b'].map(c
=> {
186 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
190 setOtherVariables(fenParsed
) {
191 super.setOtherVariables(fenParsed
);
193 // Change seed (after FEN generation!!)
194 // so that further calls differ between players:
195 Random
.setSeed(Math
.floor(19840 * Math
.random()));
199 this.reserve
= {}; //to be filled later
204 this.board
[i
][j
] == "" ||
205 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
209 // For Toadette bonus
210 canDrop([c
, p
], [i
, j
]) {
213 this.board
[i
][j
] == "" ||
214 this.getColor(i
, j
) == 'a' ||
215 this.getPiece(i
, j
) == 'i'
218 (p
!= "p" || (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0))
222 getPotentialMovesFrom([x
, y
]) {
224 const piece
= this.getPiece(x
, y
);
225 if (this.egg
== "toadette")
226 moves
= this.getDropMovesFrom([x
, y
]);
227 else if (this.egg
== "kingboo") {
228 const color
= this.turn
;
229 const oppCol
= C
.GetOppTurn(color
);
230 // Only allow to swap (non-immobilized!) pieces
231 for (let i
=0; i
<this.size
.x
; i
++) {
232 for (let j
=0; j
<this.size
.y
; j
++) {
233 const colIJ
= this.getColor(i
, j
);
234 const pieceIJ
= this.getPiece(i
, j
);
236 (i
!= x
|| j
!= y
) &&
237 ['w', 'b'].includes(colIJ
) &&
238 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
239 // Next conditions = no pawn on last rank
243 (color
!= 'w' || i
!= 0) &&
244 (color
!= 'b' || i
!= this.size
.x
- 1)
251 (colIJ
!= 'w' || x
!= 0) &&
252 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
256 let m
= this.getBasicMove([x
, y
], [i
, j
]);
257 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
258 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
265 // Normal case (including bonus daisy)
268 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
271 moves
= this.getQueenMovesFrom([x
, y
]);
274 moves
= this.getKingMovesFrom([x
, y
]);
277 moves
= this.getKnightMovesFrom([x
, y
]);
281 // Explicitely listing types to avoid moving immobilized piece
282 moves
= super.getPotentialMovesOf(piece
, [x
, y
]);
289 getPawnMovesFrom([x
, y
]) {
290 const color
= this.turn
;
291 const oppCol
= C
.GetOppTurn(color
);
292 const shiftX
= (color
== 'w' ? -1 : 1);
293 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
295 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
297 this.board
[x
+ shiftX
][y
] == "" ||
298 this.getColor(x
+ shiftX
, y
) == 'a' ||
301 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
303 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
304 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
306 this.board
[x
+ 2 * shiftX
][y
] == "" ||
307 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
308 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
311 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
314 for (let shiftY
of [-1, 1]) {
315 const nextY
= this.getY(y
+ shiftY
);
318 nextY
< this.size
.y
&&
319 this.board
[x
+ shiftX
][nextY
] != "" &&
320 // Pawns cannot capture invisible queen this way!
321 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
322 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
324 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
327 moves
= super.pawnPostProcess(moves
, color
, oppCol
);
328 // Add mushroom on before-last square (+ potential segments)
330 let [mx
, my
] = [x
, y
];
331 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
332 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
333 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
334 if (mx
!= x
&& this.board
[mx
][my
] != "") {
335 m
.vanish
.push(new PiPo({
338 c: this.getColor(mx
, my
),
339 p: this.getPiece(mx
, my
)
342 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
345 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
352 getKnightMovesFrom([x
, y
]) {
353 // Add egg on initial square:
354 return super.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
355 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
360 getQueenMovesFrom(sq
) {
361 const normalMoves
= super.getPotentialMovesOf('q', sq
);
362 // If flag allows it, add 'invisible movements'
363 let invisibleMoves
= [];
364 if (this.powerFlags
[this.turn
]['q']) {
365 normalMoves
.forEach(m
=> {
367 m
.appear
.length
== 1 &&
368 m
.vanish
.length
== 1 &&
369 // Only simple non-capturing moves:
372 let im
= JSON
.parse(JSON
.stringify(m
));
373 im
.appear
[0].p
= 'i';
375 invisibleMoves
.push(im
);
379 return normalMoves
.concat(invisibleMoves
);
382 getKingMovesFrom([x
, y
]) {
383 let moves
= super.getPotentialMovesOf('k', [x
, y
]);
384 // If flag allows it, add 'remote shell captures'
385 if (this.powerFlags
[this.turn
]['k']) {
386 let shellCaptures
= super.getPotentialMovesOf('y', [x
, y
]);
387 shellCaptures
.forEach(sc
=> {
388 sc
.shell
= true; //easier play()
389 sc
.choice
= 'z'; //to display in showChoices()
390 // Fix move (Rifle style):
394 Array
.prototype.push
.apply(moves
, shellCaptures
);
400 const color
= this.turn
;
401 const oppCol
= C
.GetOppTurn(color
);
403 if (move.egg
== "toadette") {
404 this.reserve
= { w: {}, b: {} };
405 // Randomly select a piece in pawnPromotions
407 move.toadette
= Random
.sample(this.pawnPromotions
);
408 this.reserve
[color
][move.toadette
] = 1;
409 this.re_drawReserve([color
]);
411 else if (Object
.keys(this.reserve
).length
> 0) {
413 this.re_drawReserve([color
]);
416 this.powerFlags
[color
]['k'] = false;
417 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
418 this.powerFlags
[move.appear
[0].c
]['q'] = false;
419 if (color
== this.playerColor
) {
421 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
424 if (color
== this.playerColor
) {
425 // Look for an immobilized piece of my color: it can now move
426 for (let i
=0; i
<8; i
++) {
427 for (let j
=0; j
<8; j
++) {
428 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
429 const piece
= this.getPiece(i
, j
);
431 this.getColor(i
, j
) == color
&&
432 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
434 move.vanish
.push(new PiPo({
435 x: i
, y: j
, c: color
, p: piece
437 move.appear
.push(new PiPo({
438 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
444 // Also make opponent invisible queen visible again, if any
445 for (let i
=0; i
<8; i
++) {
446 for (let j
=0; j
<8; j
++) {
448 this.board
[i
][j
] != "" &&
449 this.getColor(i
, j
) == oppCol
451 const pieceIJ
= this.getPiece(i
, j
);
454 // Ensure that current move doesn't erase invisible queen
455 move.appear
.every(a
=> a
.x
!= i
|| a
.y
!= j
)
457 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
458 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
460 else if (pieceIJ
== '?')
461 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
466 this.playOnBoard(move);
467 super.postPlay(move);
470 playVisual(move, r
) {
471 super.playVisual(move, r
);
473 this.displayBonus(move);
476 buildMoveStack(move, r
) {
477 const color
= this.turn
;
479 move.appear
.length
> 0 &&
480 move.appear
[0].p
== 'p' &&
482 (color
== 'w' && move.end
.x
== 0) ||
483 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
486 // "Forgotten" promotion, which occurred after some effect
487 let moves
= super.pawnPostProcess([move], color
, C
.GetOppTurn(color
));
488 super.showChoices(moves
, r
);
491 super.buildMoveStack(move, r
);
494 computeNextMove(move) {
497 // Set potential random effects, so that play() is deterministic
498 // from opponent viewpoint:
499 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
502 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
503 move.next
= this.getEggEffect(move);
506 move.next
= this.getMushroomEffect(move);
510 move.next
= this.getBombBananaEffect(move, endPiece
);
513 // NOTE: Chakart has also some side-effects:
515 !move.next
&& move.appear
.length
> 0 &&
516 !move.kingboo
&& !move.luigiEffect
518 const movingPiece
= move.appear
[0].p
;
519 if (['b', 'r'].includes(movingPiece
)) {
520 // Drop a banana or bomb:
522 this.getRandomSquare([move.end
.x
, move.end
.y
],
524 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
525 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
533 p: movingPiece
== 'r' ? 'd' : 'w'
536 if (this.board
[bs
[0]][bs
[1]] != "") {
541 c: this.getColor(bs
[0], bs
[1]),
542 p: this.getPiece(bs
[0], bs
[1])
552 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
555 // Helper to set and apply banana/bomb effect
556 getRandomSquare([x
, y
], steps
, freeSquare
) {
557 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
559 // Square to put banana/bomb cannot be occupied by a piece
560 validSteps
= validSteps
.filter(s
=> {
561 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
564 if (validSteps
.length
== 0)
566 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
567 return [x
+ step
[0], y
+ step
[1]];
571 const getRandomPiece
= (c
) => {
572 let bagOfPieces
= [];
573 for (let i
=0; i
<this.size
.x
; i
++) {
574 for (let j
=0; j
<this.size
.y
; j
++) {
575 const pieceIJ
= this.getPiece(i
, j
);
577 this.getColor(i
, j
) == c
&& pieceIJ
!= 'k' &&
579 // The color will change, so pawns on first rank are ineligible
581 (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0)
584 bagOfPieces
.push([i
, j
]);
588 if (bagOfPieces
.length
>= 1)
589 return Random
.sample(bagOfPieces
);
592 const color
= this.turn
;
597 // Change color of friendly or enemy piece, king excepted
598 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppTurn(color
));
599 const newColor
= C
.GetOppTurn(oldColor
);
600 const coords
= getRandomPiece(oldColor
);
602 const piece
= this.getPiece(coords
[0], coords
[1]);
603 if (coords
[0] == move.start
.x
&& coords
[1] == move.start
.y
) {
604 // Moving piece change color: fix coords
605 coords
= [move.end
.x
, move.end
.y
];
609 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
612 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
615 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
625 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
639 // Reverse move, if possible
644 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
647 end: {x: move.start
.x
, y: move.start
.y
} //may be irrelevant
649 em
.koopa
= true; //avoid applying effect
650 if (move.vanish
.length
== 0)
651 // After toadette+drop, just erase piece
655 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
658 if (this.board
[move.start
.x
][move.start
.y
] != "") {
659 // Pawn or knight let something on init square
660 em
.vanish
.push(new PiPo({
664 p: this.getPiece(move.start
.x
, move.start
.y
)
674 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
677 end: {x: move.end
.x
, y: move.end
.y
}
681 if (em
&& move.egg
!= "koopa")
682 em
.noAnimate
= true; //static move
686 getMushroomEffect(move) {
688 typeof move.start
.x
== "string" || //drop move (toadette)
689 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
693 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
694 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
695 // Pawn initial 2-squares move: normalize step
697 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
700 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
702 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
703 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
706 this.playOnBoard(move); //HACK for getBasicMove()
707 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
708 this.undoOnBoard(move);
713 getBombBananaEffect(move, item
) {
714 const steps
= item
== V
.BANANA
715 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
716 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
717 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
718 this.playOnBoard(move); //HACK for getBasicMove()
719 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
720 this.undoOnBoard(move);
725 super.displayMessage(null, move.egg
, "bonus-text", 2000);
736 // Kingboo bonus can be animated better:
737 customAnimate(move, segments
, cb
) {
740 super.animateMoving(move.end
, move.start
, null,
741 segments
.reverse().map(s
=> s
.reverse()), cb
);