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
));
133 genRandInitFen(seed
) {
134 const options
= Object
.assign({mode: "suicide"}, this.options
);
135 const gr
= new GiveawayRules({options: options
, genFenOnly: true});
136 const baseFen
= gr
.genRandInitFen(seed
);
137 const fenParts
= baseFen
.split(" ");
138 let others
= JSON
.parse(fenParts
[3]);
139 delete others
["enpassant"];
140 others
["flags"] = "1111"; //Peach + Mario flags
141 return fenParts
.slice(0, 3).join(" ") + " " + JSON
.stringify(others
);
147 ? "w" + f
.toLowerCase()
148 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
153 // King can send shell? Queen can be invisible?
155 w: {k: false, q: false},
156 b: {k: false, q: false}
158 for (let c
of ['w', 'b']) {
159 for (let p
of ['k', 'q']) {
160 this.powerFlags
[c
][p
] =
161 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
167 return this.powerFlags
;
170 disaggregateFlags(flags
) {
171 this.powerFlags
= flags
;
175 return ['w', 'b'].map(c
=> {
176 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
180 setOtherVariables(fenParsed
) {
181 super.setOtherVariables(fenParsed
);
183 // Change seed (after FEN generation!!)
184 // so that further calls differ between players:
185 Random
.setSeed(Math
.floor(19840 * Math
.random()));
189 this.reserve
= {}; //to be filled later
194 this.board
[i
][j
] == "" ||
195 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
199 // For Toadette bonus
200 canDrop([c
, p
], [i
, j
]) {
203 this.board
[i
][j
] == "" ||
204 this.getColor(i
, j
) == 'a' ||
205 this.getPiece(i
, j
) == 'i'
208 (p
!= "p" || (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0))
212 getPotentialMovesFrom([x
, y
]) {
214 const piece
= this.getPiece(x
, y
);
215 if (this.egg
== "toadette")
216 moves
= this.getDropMovesFrom([x
, y
]);
217 else if (this.egg
== "kingboo") {
218 const color
= this.turn
;
219 const oppCol
= C
.GetOppCol(color
);
220 // Only allow to swap (non-immobilized!) pieces
221 for (let i
=0; i
<this.size
.x
; i
++) {
222 for (let j
=0; j
<this.size
.y
; j
++) {
223 const colIJ
= this.getColor(i
, j
);
224 const pieceIJ
= this.getPiece(i
, j
);
226 (i
!= x
|| j
!= y
) &&
227 ['w', 'b'].includes(colIJ
) &&
228 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
229 // Next conditions = no pawn on last rank
233 (color
!= 'w' || i
!= 0) &&
234 (color
!= 'b' || i
!= this.size
.x
- 1)
241 (colIJ
!= 'w' || x
!= 0) &&
242 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
246 let m
= this.getBasicMove([x
, y
], [i
, j
]);
247 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
248 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
255 // Normal case (including bonus daisy)
258 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
261 moves
= this.getQueenMovesFrom([x
, y
]);
264 moves
= this.getKingMovesFrom([x
, y
]);
267 moves
= this.getKnightMovesFrom([x
, y
]);
271 // Explicitely listing types to avoid moving immobilized piece
272 moves
= this.getPotentialMovesOf(piece
, [x
, y
]);
279 getPawnMovesFrom([x
, y
]) {
280 const color
= this.turn
;
281 const oppCol
= C
.GetOppCol(color
);
282 const shiftX
= (color
== 'w' ? -1 : 1);
283 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
285 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
287 this.board
[x
+ shiftX
][y
] == "" ||
288 this.getColor(x
+ shiftX
, y
) == 'a' ||
291 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
293 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
294 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
296 this.board
[x
+ 2 * shiftX
][y
] == "" ||
297 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
298 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
301 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
304 for (let shiftY
of [-1, 1]) {
305 const nextY
= this.getY(y
+ shiftY
);
308 nextY
< this.size
.y
&&
309 this.board
[x
+ shiftX
][nextY
] != "" &&
310 // Pawns cannot capture invisible queen this way!
311 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
312 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
314 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
317 this.pawnPostProcess(moves
, color
, oppCol
);
318 // Add mushroom on before-last square (+ potential segments)
320 let [mx
, my
] = [x
, y
];
321 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
322 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
323 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
324 if (mx
!= x
&& this.board
[mx
][my
] != "") {
325 m
.vanish
.push(new PiPo({
328 c: this.getColor(mx
, my
),
329 p: this.getPiece(mx
, my
)
332 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
335 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
342 getKnightMovesFrom([x
, y
]) {
343 // Add egg on initial square:
344 return this.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
345 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
350 getQueenMovesFrom(sq
) {
351 const normalMoves
= this.getPotentialMovesOf('q', sq
);
352 // If flag allows it, add 'invisible movements'
353 let invisibleMoves
= [];
354 if (this.powerFlags
[this.turn
]['q']) {
355 normalMoves
.forEach(m
=> {
357 m
.appear
.length
== 1 &&
358 m
.vanish
.length
== 1 &&
359 // Only simple non-capturing moves:
362 let im
= JSON
.parse(JSON
.stringify(m
));
363 im
.appear
[0].p
= 'i';
365 invisibleMoves
.push(im
);
369 return normalMoves
.concat(invisibleMoves
);
372 getKingMovesFrom([x
, y
]) {
373 let moves
= this.getPotentialMovesOf('k', [x
, y
]);
374 // If flag allows it, add 'remote shell captures'
375 if (this.powerFlags
[this.turn
]['k']) {
376 let shellCaptures
= this.getPotentialMovesOf('y', [x
, y
]);
377 shellCaptures
.forEach(sc
=> {
378 sc
.shell
= true; //easier play()
379 sc
.choice
= 'z'; //to display in showChoices()
380 // Fix move (Rifle style):
384 Array
.prototype.push
.apply(moves
, shellCaptures
);
390 const color
= this.turn
;
391 const oppCol
= C
.GetOppCol(color
);
393 move.appear
.length
> 0 &&
394 move.appear
[0].p
== 'p' &&
396 (color
== 'w' && move.end
.x
== 0) ||
397 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
400 // "Forgotten" promotion, which occurred after some effect
402 super.pawnPostProcess(moves
, color
, oppCol
);
403 super.showChoices(moves
);
406 this.postPlay(move, color
, oppCol
);
410 postPlay(move, color
, oppCol
) {
412 if (move.egg
== "toadette") {
413 this.reserve
= { w: {}, b: {} };
414 // Randomly select a piece in pawnPromotions
416 move.toadette
= Random
.sample(this.pawnPromotions
);
417 this.reserve
[color
][move.toadette
] = 1;
418 this.re_drawReserve([color
]);
420 else if (Object
.keys(this.reserve
).length
> 0) {
422 this.re_drawReserve([color
]);
425 this.powerFlags
[color
]['k'] = false;
426 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
427 this.powerFlags
[move.appear
[0].c
]['q'] = false;
428 if (color
== this.playerColor
) {
430 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
433 if (color
== this.playerColor
) {
434 // Look for an immobilized piece of my color: it can now move
435 for (let i
=0; i
<8; i
++) {
436 for (let j
=0; j
<8; j
++) {
437 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
438 const piece
= this.getPiece(i
, j
);
440 this.getColor(i
, j
) == color
&&
441 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
443 move.vanish
.push(new PiPo({
444 x: i
, y: j
, c: color
, p: piece
446 move.appear
.push(new PiPo({
447 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
453 // Also make opponent invisible queen visible again, if any
454 for (let i
=0; i
<8; i
++) {
455 for (let j
=0; j
<8; j
++) {
457 this.board
[i
][j
] != "" &&
458 this.getColor(i
, j
) == oppCol
460 const pieceIJ
= this.getPiece(i
, j
);
461 if (pieceIJ
== 'i') {
462 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
463 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
465 else if (pieceIJ
== '?')
466 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
471 this.playOnBoard(move);
472 super.postPlay(move);
475 playVisual(move, r
) {
476 super.playVisual(move, r
);
478 this.displayBonus(move);
481 computeNextMove(move) {
482 // Set potential random effects, so that play() is deterministic
483 // from opponent viewpoint:
484 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
487 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
488 move.next
= this.getEggEffect(move);
491 move.next
= this.getMushroomEffect(move);
495 move.next
= this.getBombBananaEffect(move, endPiece
);
498 // NOTE: Chakart has also some side-effects:
500 !move.next
&& move.appear
.length
> 0 &&
501 !move.kingboo
&& !move.luigiEffect
503 const movingPiece
= move.appear
[0].p
;
504 if (['b', 'r'].includes(movingPiece
)) {
505 // Drop a banana or bomb:
507 this.getRandomSquare([move.end
.x
, move.end
.y
],
509 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
510 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
518 p: movingPiece
== 'r' ? 'd' : 'w'
521 if (this.board
[bs
[0]][bs
[1]] != "") {
526 c: this.getColor(bs
[0], bs
[1]),
527 p: this.getPiece(bs
[0], bs
[1])
537 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
540 // Helper to set and apply banana/bomb effect
541 getRandomSquare([x
, y
], steps
, freeSquare
) {
542 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
544 // Square to put banana/bomb cannot be occupied by a piece
545 validSteps
= validSteps
.filter(s
=> {
546 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
549 if (validSteps
.length
== 0)
551 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
552 return [x
+ step
[0], y
+ step
[1]];
556 const getRandomPiece
= (c
) => {
557 let bagOfPieces
= [];
558 for (let i
=0; i
<this.size
.x
; i
++) {
559 for (let j
=0; j
<this.size
.y
; j
++) {
560 if (this.getColor(i
, j
) == c
&& this.getPiece(i
, j
) != 'k')
561 bagOfPieces
.push([i
, j
]);
564 if (bagOfPieces
.length
>= 1)
565 return Random
.sample(bagOfPieces
);
568 const color
= this.turn
;
573 // Change color of friendly or enemy piece, king excepted
574 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppCol(color
));
575 const newColor
= C
.GetOppCol(oldColor
);
576 const coords
= getRandomPiece(oldColor
);
578 const piece
= this.getPiece(coords
[0], coords
[1]);
581 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
584 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
587 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
597 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
615 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
620 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
624 if (this.board
[move.start
.x
][move.start
.y
] != "") {
625 // Pawn or knight let something on init square
626 em
.vanish
.push(new PiPo({
630 p: this.getPiece(move.start
.x
, move.start
.y
)
640 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
643 end: {x: move.end
.x
, y: move.end
.y
}
647 if (em
&& move.egg
!= "koopa")
648 em
.noAnimate
= true; //static move
652 getMushroomEffect(move) {
654 typeof move.start
.x
== "string" || //drop move (toadette)
655 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
659 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
660 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
661 // Pawn initial 2-squares move: normalize step
663 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
666 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
668 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
669 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
672 this.playOnBoard(move); //HACK for getBasicMove()
673 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
674 this.undoOnBoard(move);
679 getBombBananaEffect(move, item
) {
680 const steps
= item
== V
.BANANA
681 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
682 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
683 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
684 this.playOnBoard(move); //HACK for getBasicMove()
685 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
686 this.undoOnBoard(move);
691 let divBonus
= document
.createElement("div");
692 divBonus
.classList
.add("bonus-text");
693 divBonus
.innerHTML
= move.egg
;
694 let container
= document
.getElementById(this.containerId
);
695 container
.appendChild(divBonus
);
696 setTimeout(() => container
.removeChild(divBonus
), 2000);
707 // Kingboo bonus can be animated better:
708 customAnimate(move, segments
, cb
) {
711 super.animateMoving(move.end
, move.start
, null,
712 segments
.reverse().map(s
=> s
.reverse()), cb
);