1 import ChessRules
from "/base_rules.js";
2 import GiveawayRules
from "/variants/Giveaway/class.js";
3 import { ArrayFun
} from "/utils/array.js";
4 import { Random
} from "/utils/alea.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"
123 [0, 1], [0, -1], [1, 0], [-1, 0],
124 [1, 1], [1, -1], [-1, 1], [-1, -1]
130 specials
, bowsered
, super.pieces(color
, x
, y
)
134 genRandInitFen(seed
) {
135 const options
= Object
.assign({mode: "suicide"}, this.options
);
136 const gr
= new GiveawayRules({options: options
, genFenOnly: true});
137 const baseFen
= gr
.genRandInitFen(seed
);
138 const fenParts
= baseFen
.split(" ");
139 let others
= JSON
.parse(fenParts
[3]);
140 delete others
["enpassant"];
141 others
["flags"] = "1111"; //Peach + Mario flags
142 return fenParts
.slice(0, 3).join(" ") + " " + JSON
.stringify(others
);
148 ? "w" + f
.toLowerCase()
149 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
154 // King can send shell? Queen can be invisible?
156 w: {k: false, q: false},
157 b: {k: false, q: false}
159 for (let c
of ['w', 'b']) {
160 for (let p
of ['k', 'q']) {
161 this.powerFlags
[c
][p
] =
162 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
168 return this.powerFlags
;
171 disaggregateFlags(flags
) {
172 this.powerFlags
= flags
;
176 return ['w', 'b'].map(c
=> {
177 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
181 setOtherVariables(fenParsed
) {
182 super.setOtherVariables(fenParsed
);
184 // Change seed (after FEN generation!!)
185 // so that further calls differ between players:
186 Random
.setSeed(Math
.floor(19840 * Math
.random()));
190 this.reserve
= {}; //to be filled later
195 this.board
[i
][j
] == "" ||
196 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
200 // For Toadette bonus
201 canDrop([c
, p
], [i
, j
]) {
204 this.board
[i
][j
] == "" ||
205 this.getColor(i
, j
) == 'a' ||
206 this.getPiece(i
, j
) == 'i'
209 (p
!= "p" || (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0))
213 getPotentialMovesFrom([x
, y
]) {
215 const piece
= this.getPiece(x
, y
);
216 if (this.egg
== "toadette")
217 moves
= this.getDropMovesFrom([x
, y
]);
218 else if (this.egg
== "kingboo") {
219 const color
= this.turn
;
220 const oppCol
= C
.GetOppCol(color
);
221 // Only allow to swap (non-immobilized!) pieces
222 for (let i
=0; i
<this.size
.x
; i
++) {
223 for (let j
=0; j
<this.size
.y
; j
++) {
224 const colIJ
= this.getColor(i
, j
);
225 const pieceIJ
= this.getPiece(i
, j
);
227 (i
!= x
|| j
!= y
) &&
228 ['w', 'b'].includes(colIJ
) &&
229 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
230 // Next conditions = no pawn on last rank
234 (color
!= 'w' || i
!= 0) &&
235 (color
!= 'b' || i
!= this.size
.x
- 1)
242 (colIJ
!= 'w' || x
!= 0) &&
243 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
247 let m
= this.getBasicMove([x
, y
], [i
, j
]);
248 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
249 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
256 // Normal case (including bonus daisy)
259 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
262 moves
= this.getQueenMovesFrom([x
, y
]);
265 moves
= this.getKingMovesFrom([x
, y
]);
268 moves
= this.getKnightMovesFrom([x
, y
]);
272 // Explicitely listing types to avoid moving immobilized piece
273 moves
= super.getPotentialMovesOf(piece
, [x
, y
]);
280 getPawnMovesFrom([x
, y
]) {
281 const color
= this.turn
;
282 const oppCol
= C
.GetOppCol(color
);
283 const shiftX
= (color
== 'w' ? -1 : 1);
284 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
286 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
288 this.board
[x
+ shiftX
][y
] == "" ||
289 this.getColor(x
+ shiftX
, y
) == 'a' ||
292 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
294 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
295 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
297 this.board
[x
+ 2 * shiftX
][y
] == "" ||
298 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
299 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
302 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
305 for (let shiftY
of [-1, 1]) {
306 const nextY
= this.getY(y
+ shiftY
);
309 nextY
< this.size
.y
&&
310 this.board
[x
+ shiftX
][nextY
] != "" &&
311 // Pawns cannot capture invisible queen this way!
312 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
313 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
315 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
318 this.pawnPostProcess(moves
, color
, oppCol
);
319 // Add mushroom on before-last square (+ potential segments)
321 let [mx
, my
] = [x
, y
];
322 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
323 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
324 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
325 if (mx
!= x
&& this.board
[mx
][my
] != "") {
326 m
.vanish
.push(new PiPo({
329 c: this.getColor(mx
, my
),
330 p: this.getPiece(mx
, my
)
333 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
336 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
343 getKnightMovesFrom([x
, y
]) {
344 // Add egg on initial square:
345 return super.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
346 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
351 getQueenMovesFrom(sq
) {
352 const normalMoves
= super.getPotentialMovesOf('q', sq
);
353 // If flag allows it, add 'invisible movements'
354 let invisibleMoves
= [];
355 if (this.powerFlags
[this.turn
]['q']) {
356 normalMoves
.forEach(m
=> {
358 m
.appear
.length
== 1 &&
359 m
.vanish
.length
== 1 &&
360 // Only simple non-capturing moves:
363 let im
= JSON
.parse(JSON
.stringify(m
));
364 im
.appear
[0].p
= 'i';
366 invisibleMoves
.push(im
);
370 return normalMoves
.concat(invisibleMoves
);
373 getKingMovesFrom([x
, y
]) {
374 let moves
= super.getPotentialMovesOf('k', [x
, y
]);
375 // If flag allows it, add 'remote shell captures'
376 if (this.powerFlags
[this.turn
]['k']) {
377 let shellCaptures
= super.getPotentialMovesOf('y', [x
, y
]);
378 shellCaptures
.forEach(sc
=> {
379 sc
.shell
= true; //easier play()
380 sc
.choice
= 'z'; //to display in showChoices()
381 // Fix move (Rifle style):
385 Array
.prototype.push
.apply(moves
, shellCaptures
);
391 const color
= this.turn
;
392 const oppCol
= C
.GetOppCol(color
);
394 move.appear
.length
> 0 &&
395 move.appear
[0].p
== 'p' &&
397 (color
== 'w' && move.end
.x
== 0) ||
398 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
401 // "Forgotten" promotion, which occurred after some effect
403 super.pawnPostProcess(moves
, color
, oppCol
);
404 super.showChoices(moves
);
407 this.postPlay(move, color
, oppCol
);
411 postPlay(move, color
, oppCol
) {
413 if (move.egg
== "toadette") {
414 this.reserve
= { w: {}, b: {} };
415 // Randomly select a piece in pawnPromotions
417 move.toadette
= Random
.sample(this.pawnPromotions
);
418 this.reserve
[color
][move.toadette
] = 1;
419 this.re_drawReserve([color
]);
421 else if (Object
.keys(this.reserve
).length
> 0) {
423 this.re_drawReserve([color
]);
426 this.powerFlags
[color
]['k'] = false;
427 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
428 this.powerFlags
[move.appear
[0].c
]['q'] = false;
429 if (color
== this.playerColor
) {
431 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
434 if (color
== this.playerColor
) {
435 // Look for an immobilized piece of my color: it can now move
436 for (let i
=0; i
<8; i
++) {
437 for (let j
=0; j
<8; j
++) {
438 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
439 const piece
= this.getPiece(i
, j
);
441 this.getColor(i
, j
) == color
&&
442 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
444 move.vanish
.push(new PiPo({
445 x: i
, y: j
, c: color
, p: piece
447 move.appear
.push(new PiPo({
448 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
454 // Also make opponent invisible queen visible again, if any
455 for (let i
=0; i
<8; i
++) {
456 for (let j
=0; j
<8; j
++) {
458 this.board
[i
][j
] != "" &&
459 this.getColor(i
, j
) == oppCol
461 const pieceIJ
= this.getPiece(i
, j
);
464 // Ensure that current move doesn't erase invisible queen
465 move.appear
.every(a
=> a
.x
!= i
|| a
.y
!= j
)
467 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
468 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
470 else if (pieceIJ
== '?')
471 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
476 this.playOnBoard(move);
477 super.postPlay(move);
480 playVisual(move, r
) {
481 super.playVisual(move, r
);
483 this.displayBonus(move);
486 computeNextMove(move) {
487 // Set potential random effects, so that play() is deterministic
488 // from opponent viewpoint:
489 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
492 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
493 move.next
= this.getEggEffect(move);
496 move.next
= this.getMushroomEffect(move);
500 move.next
= this.getBombBananaEffect(move, endPiece
);
503 // NOTE: Chakart has also some side-effects:
505 !move.next
&& move.appear
.length
> 0 &&
506 !move.kingboo
&& !move.luigiEffect
508 const movingPiece
= move.appear
[0].p
;
509 if (['b', 'r'].includes(movingPiece
)) {
510 // Drop a banana or bomb:
512 this.getRandomSquare([move.end
.x
, move.end
.y
],
514 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
515 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
523 p: movingPiece
== 'r' ? 'd' : 'w'
526 if (this.board
[bs
[0]][bs
[1]] != "") {
531 c: this.getColor(bs
[0], bs
[1]),
532 p: this.getPiece(bs
[0], bs
[1])
542 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
545 // Helper to set and apply banana/bomb effect
546 getRandomSquare([x
, y
], steps
, freeSquare
) {
547 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
549 // Square to put banana/bomb cannot be occupied by a piece
550 validSteps
= validSteps
.filter(s
=> {
551 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
554 if (validSteps
.length
== 0)
556 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
557 return [x
+ step
[0], y
+ step
[1]];
561 const getRandomPiece
= (c
) => {
562 let bagOfPieces
= [];
563 for (let i
=0; i
<this.size
.x
; i
++) {
564 for (let j
=0; j
<this.size
.y
; j
++) {
565 if (this.getColor(i
, j
) == c
&& this.getPiece(i
, j
) != 'k')
566 bagOfPieces
.push([i
, j
]);
569 if (bagOfPieces
.length
>= 1)
570 return Random
.sample(bagOfPieces
);
573 const color
= this.turn
;
578 // Change color of friendly or enemy piece, king excepted
579 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppCol(color
));
580 const newColor
= C
.GetOppCol(oldColor
);
581 const coords
= getRandomPiece(oldColor
);
583 const piece
= this.getPiece(coords
[0], coords
[1]);
586 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
589 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
592 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
602 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
620 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
625 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
629 if (this.board
[move.start
.x
][move.start
.y
] != "") {
630 // Pawn or knight let something on init square
631 em
.vanish
.push(new PiPo({
635 p: this.getPiece(move.start
.x
, move.start
.y
)
645 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
648 end: {x: move.end
.x
, y: move.end
.y
}
652 if (em
&& move.egg
!= "koopa")
653 em
.noAnimate
= true; //static move
657 getMushroomEffect(move) {
659 typeof move.start
.x
== "string" || //drop move (toadette)
660 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
664 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
665 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
666 // Pawn initial 2-squares move: normalize step
668 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
671 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
673 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
674 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
677 this.playOnBoard(move); //HACK for getBasicMove()
678 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
679 this.undoOnBoard(move);
684 getBombBananaEffect(move, item
) {
685 const steps
= item
== V
.BANANA
686 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
687 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
688 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
689 this.playOnBoard(move); //HACK for getBasicMove()
690 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
691 this.undoOnBoard(move);
696 let divBonus
= document
.createElement("div");
697 divBonus
.classList
.add("bonus-text");
698 divBonus
.innerHTML
= move.egg
;
699 let container
= document
.getElementById(this.containerId
);
700 container
.appendChild(divBonus
);
701 setTimeout(() => container
.removeChild(divBonus
), 2000);
712 // Kingboo bonus can be animated better:
713 customAnimate(move, segments
, cb
) {
716 super.animateMoving(move.end
, move.start
, null,
717 segments
.reverse().map(s
=> s
.reverse()), cb
);