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 this.setFlags(fenParsed
.flags
);
182 this.reserve
= {}; //to be filled later
185 // Change seed (after FEN generation!!)
186 // so that further calls differ between players:
187 Random
.setSeed(Math
.floor(19840 * Math
.random()));
190 // For Toadette bonus
191 getDropMovesFrom([c
, p
]) {
192 if (typeof c
!= "string" || this.reserve
[c
][p
] == 0)
195 const start
= (c
== 'w' && p
== 'p' ? 1 : 0);
196 const end
= (c
== 'b' && p
== 'p' ? 7 : 8);
197 for (let i
= start
; i
< end
; i
++) {
198 for (let j
= 0; j
< this.size
.y
; j
++) {
199 const pieceIJ
= this.getPiece(i
, j
);
200 const colIJ
= this.getColor(i
, j
);
201 if (this.board
[i
][j
] == "" || colIJ
== 'a' || pieceIJ
== 'i') {
204 appear: [new PiPo({x: i
, y: j
, c: c
, p: p
})],
207 // A drop move may remove a bonus (or hidden queen!)
208 if (this.board
[i
][j
] != "")
209 m
.vanish
.push(new PiPo({x: i
, y: j
, c: colIJ
, p: pieceIJ
}));
217 getPotentialMovesFrom([x
, y
]) {
219 const piece
= this.getPiece(x
, y
);
220 if (this.egg
== "toadette")
221 moves
= this.getDropMovesFrom([x
, y
]);
222 else if (this.egg
== "kingboo") {
223 const color
= this.turn
;
224 const oppCol
= C
.GetOppCol(color
);
225 // Only allow to swap (non-immobilized!) pieces
226 for (let i
=0; i
<this.size
.x
; i
++) {
227 for (let j
=0; j
<this.size
.y
; j
++) {
228 const colIJ
= this.getColor(i
, j
);
229 const pieceIJ
= this.getPiece(i
, j
);
231 (i
!= x
|| j
!= y
) &&
232 ['w', 'b'].includes(colIJ
) &&
233 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
234 // Next conditions = no pawn on last rank
238 (color
!= 'w' || i
!= 0) &&
239 (color
!= 'b' || i
!= this.size
.x
- 1)
246 (colIJ
!= 'w' || x
!= 0) &&
247 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
251 let m
= this.getBasicMove([x
, y
], [i
, j
]);
252 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
253 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
260 // Normal case (including bonus daisy)
263 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
266 moves
= this.getQueenMovesFrom([x
, y
]);
269 moves
= this.getKingMovesFrom([x
, y
]);
272 moves
= this.getKnightMovesFrom([x
, y
]);
276 // Explicitely listing types to avoid moving immobilized piece
277 moves
= this.getPotentialMovesOf(piece
, [x
, y
]);
286 this.board
[i
][j
] == "" ||
287 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
291 getPawnMovesFrom([x
, y
]) {
292 const color
= this.turn
;
293 const oppCol
= C
.GetOppCol(color
);
294 const shiftX
= (color
== 'w' ? -1 : 1);
295 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
297 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
299 this.board
[x
+ shiftX
][y
] == "" ||
300 this.getColor(x
+ shiftX
, y
) == 'a' ||
303 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
305 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
306 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
308 this.board
[x
+ 2 * shiftX
][y
] == "" ||
309 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
310 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
313 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
316 for (let shiftY
of [-1, 1]) {
317 const nextY
= this.getY(y
+ shiftY
);
320 nextY
< this.size
.y
&&
321 this.board
[x
+ shiftX
][nextY
] != "" &&
322 // Pawns cannot capture invisible queen this way!
323 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
324 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
326 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
329 this.pawnPostProcess(moves
, color
, oppCol
);
330 // Add mushroom on before-last square (+ potential segments)
332 let [mx
, my
] = [x
, y
];
333 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
334 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
335 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
336 if (mx
!= x
&& this.board
[mx
][my
] != "") {
337 m
.vanish
.push(new PiPo({
340 c: this.getColor(mx
, my
),
341 p: this.getPiece(mx
, my
)
344 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
347 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
354 getKnightMovesFrom([x
, y
]) {
355 // Add egg on initial square:
356 return this.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
357 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
362 getQueenMovesFrom(sq
) {
363 const normalMoves
= this.getPotentialMovesOf('q', sq
);
364 // If flag allows it, add 'invisible movements'
365 let invisibleMoves
= [];
366 if (this.powerFlags
[this.turn
]['q']) {
367 normalMoves
.forEach(m
=> {
369 m
.appear
.length
== 1 &&
370 m
.vanish
.length
== 1 &&
371 // Only simple non-capturing moves:
374 let im
= JSON
.parse(JSON
.stringify(m
));
375 im
.appear
[0].p
= 'i';
377 invisibleMoves
.push(im
);
381 return normalMoves
.concat(invisibleMoves
);
384 getKingMovesFrom([x
, y
]) {
385 let moves
= this.getPotentialMovesOf('k', [x
, y
]);
386 // If flag allows it, add 'remote shell captures'
387 if (this.powerFlags
[this.turn
]['k']) {
388 let shellCaptures
= this.getPotentialMovesOf('y', [x
, y
]);
389 shellCaptures
.forEach(sc
=> {
390 sc
.shell
= true; //easier play()
391 sc
.choice
= 'z'; //to display in showChoices()
392 // Fix move (Rifle style):
396 Array
.prototype.push
.apply(moves
, shellCaptures
);
402 const color
= this.turn
;
403 const oppCol
= C
.GetOppCol(color
);
405 move.appear
.length
> 0 &&
406 move.appear
[0].p
== 'p' &&
408 (color
== 'w' && move.end
.x
== 0) ||
409 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
412 // "Forgotten" promotion, which occurred after some effect
414 super.pawnPostProcess(moves
, color
, oppCol
);
415 super.showChoices(moves
);
418 if (!move.nextComputed
) {
419 // Set potential random effects, so that play() is deterministic
420 // from opponent viewpoint:
421 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
424 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
425 move.next
= this.getEggEffect(move);
428 move.next
= this.getMushroomEffect(move);
432 move.next
= this.getBombBananaEffect(move, endPiece
);
435 if (!move.next
&& move.appear
.length
> 0 && !move.kingboo
) {
436 const movingPiece
= move.appear
[0].p
;
437 if (['b', 'r'].includes(movingPiece
)) {
438 // Drop a banana or bomb:
440 this.getRandomSquare([move.end
.x
, move.end
.y
],
442 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
443 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
451 p: movingPiece
== 'r' ? 'd' : 'w'
454 if (this.board
[bs
[0]][bs
[1]] != "") {
459 c: this.getColor(bs
[0], bs
[1]),
460 p: this.getPiece(bs
[0], bs
[1])
467 move.nextComputed
= true;
470 if (move.egg
== "toadette") {
471 this.reserve
= { w: {}, b: {} };
472 // Randomly select a piece in pawnPromotions
474 move.toadette
= Random
.sample(this.pawnPromotions
);
475 this.reserve
[color
][move.toadette
] = 1;
476 this.re_drawReserve([color
]);
478 else if (Object
.keys(this.reserve
).length
> 0) {
480 this.re_drawReserve([color
]);
483 this.powerFlags
[color
]['k'] = false;
484 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
485 this.powerFlags
[move.appear
[0].c
]['q'] = false;
486 if (color
== this.playerColor
) {
488 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
491 if (color
== this.playerColor
) {
492 // Look for an immobilized piece of my color: it can now move
493 for (let i
=0; i
<8; i
++) {
494 for (let j
=0; j
<8; j
++) {
495 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
496 const piece
= this.getPiece(i
, j
);
498 this.getColor(i
, j
) == color
&&
499 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
501 move.vanish
.push(new PiPo({
502 x: i
, y: j
, c: color
, p: piece
504 move.appear
.push(new PiPo({
505 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
511 // Also make opponent invisible queen visible again, if any
512 for (let i
=0; i
<8; i
++) {
513 for (let j
=0; j
<8; j
++) {
515 this.board
[i
][j
] != "" &&
516 this.getColor(i
, j
) == oppCol
518 const pieceIJ
= this.getPiece(i
, j
);
519 if (pieceIJ
== 'i') {
520 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
521 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
523 else if (pieceIJ
== '?')
524 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
529 if (!move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
)) {
534 this.displayBonus(move);
535 this.playOnBoard(move);
536 this.nextMove
= move.next
;
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
})
596 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
614 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
619 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
623 if (this.board
[move.start
.x
][move.start
.y
] != "") {
624 // Pawn or knight let something on init square
625 em
.vanish
.push(new PiPo({
629 p: this.getPiece(move.start
.x
, move.start
.y
)
639 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
642 end: {x: move.end
.x
, y: move.end
.y
}
646 if (em
&& move.egg
!= "koopa")
647 em
.noAnimate
= true; //static move
651 getMushroomEffect(move) {
653 typeof move.start
.x
== "string" || //drop move (toadette)
654 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
658 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
659 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
660 // Pawn initial 2-squares move: normalize step
662 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
665 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
667 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
668 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
671 this.playOnBoard(move); //HACK for getBasicMove()
672 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
673 this.undoOnBoard(move);
678 getBombBananaEffect(move, item
) {
679 const steps
= item
== V
.BANANA
680 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
681 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
682 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
683 this.playOnBoard(move); //HACK for getBasicMove()
684 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
685 this.undoOnBoard(move);
690 let divBonus
= document
.createElement("div");
691 divBonus
.classList
.add("bonus-text");
692 divBonus
.innerHTML
= move.egg
;
693 let container
= document
.getElementById(this.containerId
);
694 container
.appendChild(divBonus
);
695 setTimeout(() => container
.removeChild(divBonus
), 2000);
706 playPlusVisual(move, r
) {
707 const nextLines
= () => {
708 if (!this.play(move))
710 this.moveStack
.push(move);
711 this.playVisual(move, r
);
713 this.playPlusVisual(this.nextMove
, r
);
715 this.afterPlay(this.moveStack
);
719 if (this.moveStack
.length
== 0)
722 this.animate(move, nextLines
);