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 // Add Peach + mario flags
137 return gr
.genRandInitFen(seed
).slice(0, -17) + '{"flags":"1111"}';
143 ? "w" + f
.toLowerCase()
144 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
149 // King can send shell? Queen can be invisible?
151 w: {k: false, q: false},
152 b: {k: false, q: false}
154 for (let c
of ['w', 'b']) {
155 for (let p
of ['k', 'q']) {
156 this.powerFlags
[c
][p
] =
157 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
163 return this.powerFlags
;
166 disaggregateFlags(flags
) {
167 this.powerFlags
= flags
;
171 return ['w', 'b'].map(c
=> {
172 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
176 setOtherVariables(fenParsed
) {
177 this.setFlags(fenParsed
.flags
);
178 this.reserve
= {}; //to be filled later
181 // Change seed (after FEN generation!!)
182 // so that further calls differ between players:
183 Random
.setSeed(Math
.floor(19840 * Math
.random()));
186 // For Toadette bonus
187 getDropMovesFrom([c
, p
]) {
188 if (typeof c
!= "string" || this.reserve
[c
][p
] == 0)
191 const start
= (c
== 'w' && p
== 'p' ? 1 : 0);
192 const end
= (c
== 'b' && p
== 'p' ? 7 : 8);
193 for (let i
= start
; i
< end
; i
++) {
194 for (let j
= 0; j
< this.size
.y
; j
++) {
195 const pieceIJ
= this.getPiece(i
, j
);
196 const colIJ
= this.getColor(i
, j
);
197 if (this.board
[i
][j
] == "" || colIJ
== 'a' || pieceIJ
== 'i') {
200 appear: [new PiPo({x: i
, y: j
, c: c
, p: p
})],
203 // A drop move may remove a bonus (or hidden queen!)
204 if (this.board
[i
][j
] != "")
205 m
.vanish
.push(new PiPo({x: i
, y: j
, c: colIJ
, p: pieceIJ
}));
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
= this.getPotentialMovesOf(piece
, [x
, y
]);
282 this.board
[i
][j
] == "" ||
283 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
287 getPawnMovesFrom([x
, y
]) {
288 const color
= this.turn
;
289 const oppCol
= C
.GetOppCol(color
);
290 const shiftX
= (color
== 'w' ? -1 : 1);
291 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
293 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
295 this.board
[x
+ shiftX
][y
] == "" ||
296 this.getColor(x
+ shiftX
, y
) == 'a' ||
299 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
301 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
302 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
304 this.board
[x
+ 2 * shiftX
][y
] == "" ||
305 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
306 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
309 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
312 for (let shiftY
of [-1, 1]) {
313 const nextY
= this.getY(y
+ shiftY
);
316 nextY
< this.size
.y
&&
317 this.board
[x
+ shiftX
][nextY
] != "" &&
318 // Pawns cannot capture invisible queen this way!
319 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
320 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
322 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
325 this.pawnPostProcess(moves
, color
, oppCol
);
326 // Add mushroom on before-last square (+ potential segments)
328 let [mx
, my
] = [x
, y
];
329 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
330 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
331 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
332 if (mx
!= x
&& this.board
[mx
][my
] != "") {
333 m
.vanish
.push(new PiPo({
336 c: this.getColor(mx
, my
),
337 p: this.getPiece(mx
, my
)
340 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
343 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
350 getKnightMovesFrom([x
, y
]) {
351 // Add egg on initial square:
352 return this.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
353 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
358 getQueenMovesFrom(sq
) {
359 const normalMoves
= this.getPotentialMovesOf('q', sq
);
360 // If flag allows it, add 'invisible movements'
361 let invisibleMoves
= [];
362 if (this.powerFlags
[this.turn
]['q']) {
363 normalMoves
.forEach(m
=> {
365 m
.appear
.length
== 1 &&
366 m
.vanish
.length
== 1 &&
367 // Only simple non-capturing moves:
370 let im
= JSON
.parse(JSON
.stringify(m
));
371 im
.appear
[0].p
= 'i';
373 invisibleMoves
.push(im
);
377 return normalMoves
.concat(invisibleMoves
);
380 getKingMovesFrom([x
, y
]) {
381 let moves
= this.getPotentialMovesOf('k', [x
, y
]);
382 // If flag allows it, add 'remote shell captures'
383 if (this.powerFlags
[this.turn
]['k']) {
384 let shellCaptures
= this.getPotentialMovesOf('y', [x
, y
]);
385 shellCaptures
.forEach(sc
=> {
386 sc
.shell
= true; //easier play()
387 sc
.choice
= 'z'; //to display in showChoices()
388 // Fix move (Rifle style):
392 Array
.prototype.push
.apply(moves
, shellCaptures
);
398 const color
= this.turn
;
399 const oppCol
= C
.GetOppCol(color
);
401 move.appear
.length
> 0 &&
402 move.appear
[0].p
== 'p' &&
404 (color
== 'w' && move.end
.x
== 0) ||
405 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
408 // "Forgotten" promotion, which occurred after some effect
410 super.pawnPostProcess(moves
, color
, oppCol
);
411 super.showChoices(moves
);
414 if (!move.nextComputed
) {
415 // Set potential random effects, so that play() is deterministic
416 // from opponent viewpoint:
417 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
420 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
421 move.next
= this.getEggEffect(move);
424 move.next
= this.getMushroomEffect(move);
428 move.next
= this.getBombBananaEffect(move, endPiece
);
431 if (!move.next
&& move.appear
.length
> 0 && !move.kingboo
) {
432 const movingPiece
= move.appear
[0].p
;
433 if (['b', 'r'].includes(movingPiece
)) {
434 // Drop a banana or bomb:
436 this.getRandomSquare([move.end
.x
, move.end
.y
],
438 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
439 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
447 p: movingPiece
== 'r' ? 'd' : 'w'
450 if (this.board
[bs
[0]][bs
[1]] != "") {
455 c: this.getColor(bs
[0], bs
[1]),
456 p: this.getPiece(bs
[0], bs
[1])
463 move.nextComputed
= true;
466 if (move.egg
== "toadette") {
467 this.reserve
= { w: {}, b: {} };
468 // Randomly select a piece in pawnPromotions
470 move.toadette
= Random
.sample(this.pawnPromotions
);
471 this.reserve
[color
][move.toadette
] = 1;
472 this.re_drawReserve([color
]);
474 else if (Object
.keys(this.reserve
).length
> 0) {
476 this.re_drawReserve([color
]);
479 this.powerFlags
[color
]['k'] = false;
480 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
481 this.powerFlags
[move.appear
[0].c
]['q'] = false;
482 if (color
== this.playerColor
) {
484 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
487 if (color
== this.playerColor
) {
488 // Look for an immobilized piece of my color: it can now move
489 for (let i
=0; i
<8; i
++) {
490 for (let j
=0; j
<8; j
++) {
491 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
492 const piece
= this.getPiece(i
, j
);
494 this.getColor(i
, j
) == color
&&
495 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
497 move.vanish
.push(new PiPo({
498 x: i
, y: j
, c: color
, p: piece
500 move.appear
.push(new PiPo({
501 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
507 // Also make opponent invisible queen visible again, if any
508 for (let i
=0; i
<8; i
++) {
509 for (let j
=0; j
<8; j
++) {
511 this.board
[i
][j
] != "" &&
512 this.getColor(i
, j
) == oppCol
514 const pieceIJ
= this.getPiece(i
, j
);
515 if (pieceIJ
== 'i') {
516 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
517 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
519 else if (pieceIJ
== '?')
520 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
525 if (!move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
)) {
530 this.displayBonus(move);
531 this.playOnBoard(move);
532 this.nextMove
= move.next
;
536 // Helper to set and apply banana/bomb effect
537 getRandomSquare([x
, y
], steps
, freeSquare
) {
538 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
540 // Square to put banana/bomb cannot be occupied by a piece
541 validSteps
= validSteps
.filter(s
=> {
542 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
545 if (validSteps
.length
== 0)
547 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
548 return [x
+ step
[0], y
+ step
[1]];
552 const getRandomPiece
= (c
) => {
553 let bagOfPieces
= [];
554 for (let i
=0; i
<this.size
.x
; i
++) {
555 for (let j
=0; j
<this.size
.y
; j
++) {
556 if (this.getColor(i
, j
) == c
&& this.getPiece(i
, j
) != 'k')
557 bagOfPieces
.push([i
, j
]);
560 if (bagOfPieces
.length
>= 1)
561 return Random
.sample(bagOfPieces
);
564 const color
= this.turn
;
569 // Change color of friendly or enemy piece, king excepted
570 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppCol(color
));
571 const newColor
= C
.GetOppCol(oldColor
);
572 const coords
= getRandomPiece(oldColor
);
574 const piece
= this.getPiece(coords
[0], coords
[1]);
577 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
580 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
592 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
610 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
615 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
619 if (this.board
[move.start
.x
][move.start
.y
] != "") {
620 // Pawn or knight let something on init square
621 em
.vanish
.push(new PiPo({
625 p: this.getPiece(move.start
.x
, move.start
.y
)
635 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
638 end: {x: move.end
.x
, y: move.end
.y
}
642 if (em
&& move.egg
!= "koopa")
643 em
.noAnimate
= true; //static move
647 getMushroomEffect(move) {
648 if (typeof move.start
.x
== "string") //drop move (toadette)
650 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
651 if ([0, 1].some(i
=> Math
.abs(step
[i
]) >= 2 && Math
.abs(step
[1-i
]) != 1)) {
652 // Slider, multi-squares: normalize step
653 for (let j
of [0, 1])
654 step
[j
] = step
[j
] / Math
.abs(step
[j
]) || 0;
656 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
658 [nextSquare
[0] + step
[0], nextSquare
[1] + step
[1]];
660 if (this.onBoard(nextSquare
[0], nextSquare
[1])) {
661 this.playOnBoard(move); //HACK for getBasicMove()
662 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
663 this.undoOnBoard(move);
668 getBombBananaEffect(move, item
) {
669 const steps
= item
== V
.BANANA
670 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
671 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
672 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
673 this.playOnBoard(move); //HACK for getBasicMove()
674 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
675 this.undoOnBoard(move);
680 let divBonus
= document
.createElement("div");
681 divBonus
.classList
.add("bonus-text");
682 divBonus
.innerHTML
= move.egg
;
683 let container
= document
.getElementById(this.containerId
);
684 container
.appendChild(divBonus
);
685 setTimeout(() => container
.removeChild(divBonus
), 2000);
696 playPlusVisual(move, r
) {
697 const nextLines
= () => {
698 if (!this.play(move))
700 this.moveStack
.push(move);
701 this.playVisual(move, r
);
703 this.playPlusVisual(this.nextMove
, r
);
705 this.afterPlay(this.moveStack
);
709 if (this.moveStack
.length
== 0)
712 this.animate(move, nextLines
);