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
)
133 genRandInitBaseFen() {
134 const s
= FenUtil
.setupPieces(
135 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
137 randomness: this.options
["randomness"],
142 fen: s
.b
.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
143 s
.w
.join("").toUpperCase(),
144 o: {flags: "1111"} //Peach + Mario
151 ? "w" + f
.toLowerCase()
152 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
157 // King can send shell? Queen can be invisible?
159 w: {k: false, q: false},
160 b: {k: false, q: false}
162 for (let c
of ['w', 'b']) {
163 for (let p
of ['k', 'q']) {
164 this.powerFlags
[c
][p
] =
165 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
171 return this.powerFlags
;
174 disaggregateFlags(flags
) {
175 this.powerFlags
= flags
;
179 return ['w', 'b'].map(c
=> {
180 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
184 setOtherVariables(fenParsed
) {
185 super.setOtherVariables(fenParsed
);
187 // Change seed (after FEN generation!!)
188 // so that further calls differ between players:
189 Random
.setSeed(Math
.floor(19840 * Math
.random()));
193 this.reserve
= {}; //to be filled later
198 this.board
[i
][j
] == "" ||
199 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
203 // For Toadette bonus
204 canDrop([c
, p
], [i
, j
]) {
207 this.board
[i
][j
] == "" ||
208 this.getColor(i
, j
) == 'a' ||
209 this.getPiece(i
, j
) == 'i'
212 (p
!= "p" || (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0))
216 getPotentialMovesFrom([x
, y
]) {
218 const piece
= this.getPiece(x
, y
);
219 if (this.egg
== "toadette")
220 moves
= this.getDropMovesFrom([x
, y
]);
221 else if (this.egg
== "kingboo") {
222 const color
= this.turn
;
223 const oppCol
= C
.GetOppTurn(color
);
224 // Only allow to swap (non-immobilized!) pieces
225 for (let i
=0; i
<this.size
.x
; i
++) {
226 for (let j
=0; j
<this.size
.y
; j
++) {
227 const colIJ
= this.getColor(i
, j
);
228 const pieceIJ
= this.getPiece(i
, j
);
230 (i
!= x
|| j
!= y
) &&
231 ['w', 'b'].includes(colIJ
) &&
232 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
233 // Next conditions = no pawn on last rank
237 (color
!= 'w' || i
!= 0) &&
238 (color
!= 'b' || i
!= this.size
.x
- 1)
245 (colIJ
!= 'w' || x
!= 0) &&
246 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
250 let m
= this.getBasicMove([x
, y
], [i
, j
]);
251 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
252 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
259 // Normal case (including bonus daisy)
262 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
265 moves
= this.getQueenMovesFrom([x
, y
]);
268 moves
= this.getKingMovesFrom([x
, y
]);
271 moves
= this.getKnightMovesFrom([x
, y
]);
275 // Explicitely listing types to avoid moving immobilized piece
276 moves
= super.getPotentialMovesOf(piece
, [x
, y
]);
283 getPawnMovesFrom([x
, y
]) {
284 const color
= this.turn
;
285 const oppCol
= C
.GetOppTurn(color
);
286 const shiftX
= (color
== 'w' ? -1 : 1);
287 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
289 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
291 this.board
[x
+ shiftX
][y
] == "" ||
292 this.getColor(x
+ shiftX
, y
) == 'a' ||
295 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
297 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
298 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
300 this.board
[x
+ 2 * shiftX
][y
] == "" ||
301 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
302 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
305 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
308 for (let shiftY
of [-1, 1]) {
309 const nextY
= this.getY(y
+ shiftY
);
312 nextY
< this.size
.y
&&
313 this.board
[x
+ shiftX
][nextY
] != "" &&
314 // Pawns cannot capture invisible queen this way!
315 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
316 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
318 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
321 moves
= super.pawnPostProcess(moves
, color
, oppCol
);
322 // Add mushroom on before-last square (+ potential segments)
324 let [mx
, my
] = [x
, y
];
325 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
326 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
327 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
328 if (mx
!= x
&& this.board
[mx
][my
] != "") {
329 m
.vanish
.push(new PiPo({
332 c: this.getColor(mx
, my
),
333 p: this.getPiece(mx
, my
)
336 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
339 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
346 getKnightMovesFrom([x
, y
]) {
347 // Add egg on initial square:
348 return super.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
349 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
354 getQueenMovesFrom(sq
) {
355 const normalMoves
= super.getPotentialMovesOf('q', sq
);
356 // If flag allows it, add 'invisible movements'
357 let invisibleMoves
= [];
358 if (this.powerFlags
[this.turn
]['q']) {
359 normalMoves
.forEach(m
=> {
361 m
.appear
.length
== 1 &&
362 m
.vanish
.length
== 1 &&
363 // Only simple non-capturing moves:
366 let im
= JSON
.parse(JSON
.stringify(m
));
367 im
.appear
[0].p
= 'i';
369 invisibleMoves
.push(im
);
373 return normalMoves
.concat(invisibleMoves
);
376 getKingMovesFrom([x
, y
]) {
377 let moves
= super.getPotentialMovesOf('k', [x
, y
]);
378 // If flag allows it, add 'remote shell captures'
379 if (this.powerFlags
[this.turn
]['k']) {
380 let shellCaptures
= super.getPotentialMovesOf('y', [x
, y
]);
381 shellCaptures
.forEach(sc
=> {
382 sc
.shell
= true; //easier play()
383 sc
.choice
= 'z'; //to display in showChoices()
384 // Fix move (Rifle style):
388 Array
.prototype.push
.apply(moves
, shellCaptures
);
394 const color
= this.turn
;
395 const oppCol
= C
.GetOppTurn(color
);
397 if (move.egg
== "toadette") {
398 this.reserve
= { w: {}, b: {} };
399 // Randomly select a piece in pawnPromotions
401 move.toadette
= Random
.sample(this.pawnPromotions
);
402 this.reserve
[color
][move.toadette
] = 1;
403 this.re_drawReserve([color
]);
405 else if (Object
.keys(this.reserve
).length
> 0) {
407 this.re_drawReserve([color
]);
410 this.powerFlags
[color
]['k'] = false;
411 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
412 this.powerFlags
[move.appear
[0].c
]['q'] = false;
413 if (color
== this.playerColor
) {
415 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
418 if (color
== this.playerColor
) {
419 // Look for an immobilized piece of my color: it can now move
420 for (let i
=0; i
<8; i
++) {
421 for (let j
=0; j
<8; j
++) {
422 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
423 const piece
= this.getPiece(i
, j
);
425 this.getColor(i
, j
) == color
&&
426 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
428 move.vanish
.push(new PiPo({
429 x: i
, y: j
, c: color
, p: piece
431 move.appear
.push(new PiPo({
432 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
438 // Also make opponent invisible queen visible again, if any
439 for (let i
=0; i
<8; i
++) {
440 for (let j
=0; j
<8; j
++) {
442 this.board
[i
][j
] != "" &&
443 this.getColor(i
, j
) == oppCol
445 const pieceIJ
= this.getPiece(i
, j
);
448 // Ensure that current move doesn't erase invisible queen
449 move.appear
.every(a
=> a
.x
!= i
|| a
.y
!= j
)
451 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
452 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
454 else if (pieceIJ
== '?')
455 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
460 this.playOnBoard(move);
461 super.postPlay(move);
464 playVisual(move, r
) {
465 super.playVisual(move, r
);
467 this.displayBonus(move);
470 buildMoveStack(move, r
) {
471 const color
= this.turn
;
473 move.appear
.length
> 0 &&
474 move.appear
[0].p
== 'p' &&
476 (color
== 'w' && move.end
.x
== 0) ||
477 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
480 // "Forgotten" promotion, which occurred after some effect
481 let moves
= super.pawnPostProcess([move], color
, C
.GetOppTurn(color
));
482 super.showChoices(moves
, r
);
485 super.buildMoveStack(move, r
);
488 computeNextMove(move) {
491 // Set potential random effects, so that play() is deterministic
492 // from opponent viewpoint:
493 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
496 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
497 move.next
= this.getEggEffect(move);
500 move.next
= this.getMushroomEffect(move);
504 move.next
= this.getBombBananaEffect(move, endPiece
);
507 // NOTE: Chakart has also some side-effects:
509 !move.next
&& move.appear
.length
> 0 &&
510 !move.kingboo
&& !move.luigiEffect
512 const movingPiece
= move.appear
[0].p
;
513 if (['b', 'r'].includes(movingPiece
)) {
514 // Drop a banana or bomb:
516 this.getRandomSquare([move.end
.x
, move.end
.y
],
518 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
519 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
527 p: movingPiece
== 'r' ? 'd' : 'w'
530 if (this.board
[bs
[0]][bs
[1]] != "") {
535 c: this.getColor(bs
[0], bs
[1]),
536 p: this.getPiece(bs
[0], bs
[1])
546 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
549 // Helper to set and apply banana/bomb effect
550 getRandomSquare([x
, y
], steps
, freeSquare
) {
551 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
553 // Square to put banana/bomb cannot be occupied by a piece
554 validSteps
= validSteps
.filter(s
=> {
555 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
558 if (validSteps
.length
== 0)
560 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
561 return [x
+ step
[0], y
+ step
[1]];
565 const getRandomPiece
= (c
) => {
566 let bagOfPieces
= [];
567 for (let i
=0; i
<this.size
.x
; i
++) {
568 for (let j
=0; j
<this.size
.y
; j
++) {
569 const pieceIJ
= this.getPiece(i
, j
);
571 this.getColor(i
, j
) == c
&& pieceIJ
!= 'k' &&
573 // The color will change, so pawns on first rank are ineligible
575 (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0)
578 bagOfPieces
.push([i
, j
]);
582 if (bagOfPieces
.length
>= 1)
583 return Random
.sample(bagOfPieces
);
586 const color
= this.turn
;
591 // Change color of friendly or enemy piece, king excepted
592 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppTurn(color
));
593 const newColor
= C
.GetOppTurn(oldColor
);
594 const coords
= getRandomPiece(oldColor
);
596 const piece
= this.getPiece(coords
[0], coords
[1]);
597 if (coords
[0] == move.start
.x
&& coords
[1] == move.start
.y
) {
598 // Moving piece change color: fix coords
599 coords
= [move.end
.x
, move.end
.y
];
603 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
606 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
609 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
619 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
637 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
642 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
646 if (this.board
[move.start
.x
][move.start
.y
] != "") {
647 // Pawn or knight let something on init square
648 em
.vanish
.push(new PiPo({
652 p: this.getPiece(move.start
.x
, move.start
.y
)
655 em
.koopa
= true; //avoid applying effect
663 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
666 end: {x: move.end
.x
, y: move.end
.y
}
670 if (em
&& move.egg
!= "koopa")
671 em
.noAnimate
= true; //static move
675 getMushroomEffect(move) {
677 typeof move.start
.x
== "string" || //drop move (toadette)
678 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
682 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
683 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
684 // Pawn initial 2-squares move: normalize step
686 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
689 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
691 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
692 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
695 this.playOnBoard(move); //HACK for getBasicMove()
696 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
697 this.undoOnBoard(move);
702 getBombBananaEffect(move, item
) {
703 const steps
= item
== V
.BANANA
704 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
705 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
706 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
707 this.playOnBoard(move); //HACK for getBasicMove()
708 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
709 this.undoOnBoard(move);
714 super.displayMessage(null, move.egg
, "bonus-text", 2000);
725 // Kingboo bonus can be animated better:
726 customAnimate(move, segments
, cb
) {
729 super.animateMoving(move.end
, move.start
, null,
730 segments
.reverse().map(s
=> s
.reverse()), cb
);