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
192 // For Toadette bonus
193 getDropMovesFrom([c
, p
]) {
194 if (typeof c
!= "string" || this.reserve
[c
][p
] == 0)
197 const start
= (c
== 'w' && p
== 'p' ? 1 : 0);
198 const end
= (c
== 'b' && p
== 'p' ? 7 : 8);
199 for (let i
= start
; i
< end
; i
++) {
200 for (let j
= 0; j
< this.size
.y
; j
++) {
201 const pieceIJ
= this.getPiece(i
, j
);
202 const colIJ
= this.getColor(i
, j
);
203 if (this.board
[i
][j
] == "" || colIJ
== 'a' || pieceIJ
== 'i') {
206 appear: [new PiPo({x: i
, y: j
, c: c
, p: p
})],
209 // A drop move may remove a bonus (or hidden queen!)
210 if (this.board
[i
][j
] != "")
211 m
.vanish
.push(new PiPo({x: i
, y: j
, c: colIJ
, p: pieceIJ
}));
219 getPotentialMovesFrom([x
, y
]) {
221 const piece
= this.getPiece(x
, y
);
222 if (this.egg
== "toadette")
223 moves
= this.getDropMovesFrom([x
, y
]);
224 else if (this.egg
== "kingboo") {
225 const color
= this.turn
;
226 const oppCol
= C
.GetOppCol(color
);
227 // Only allow to swap (non-immobilized!) pieces
228 for (let i
=0; i
<this.size
.x
; i
++) {
229 for (let j
=0; j
<this.size
.y
; j
++) {
230 const colIJ
= this.getColor(i
, j
);
231 const pieceIJ
= this.getPiece(i
, j
);
233 (i
!= x
|| j
!= y
) &&
234 ['w', 'b'].includes(colIJ
) &&
235 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
236 // Next conditions = no pawn on last rank
240 (color
!= 'w' || i
!= 0) &&
241 (color
!= 'b' || i
!= this.size
.x
- 1)
248 (colIJ
!= 'w' || x
!= 0) &&
249 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
253 let m
= this.getBasicMove([x
, y
], [i
, j
]);
254 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
255 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
262 // Normal case (including bonus daisy)
265 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
268 moves
= this.getQueenMovesFrom([x
, y
]);
271 moves
= this.getKingMovesFrom([x
, y
]);
274 moves
= this.getKnightMovesFrom([x
, y
]);
278 // Explicitely listing types to avoid moving immobilized piece
279 moves
= this.getPotentialMovesOf(piece
, [x
, y
]);
288 this.board
[i
][j
] == "" ||
289 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
293 getPawnMovesFrom([x
, y
]) {
294 const color
= this.turn
;
295 const oppCol
= C
.GetOppCol(color
);
296 const shiftX
= (color
== 'w' ? -1 : 1);
297 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
299 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
301 this.board
[x
+ shiftX
][y
] == "" ||
302 this.getColor(x
+ shiftX
, y
) == 'a' ||
305 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
307 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
308 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
310 this.board
[x
+ 2 * shiftX
][y
] == "" ||
311 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
312 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
315 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
318 for (let shiftY
of [-1, 1]) {
319 const nextY
= this.getY(y
+ shiftY
);
322 nextY
< this.size
.y
&&
323 this.board
[x
+ shiftX
][nextY
] != "" &&
324 // Pawns cannot capture invisible queen this way!
325 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
326 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
328 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
331 this.pawnPostProcess(moves
, color
, oppCol
);
332 // Add mushroom on before-last square (+ potential segments)
334 let [mx
, my
] = [x
, y
];
335 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
336 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
337 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
338 if (mx
!= x
&& this.board
[mx
][my
] != "") {
339 m
.vanish
.push(new PiPo({
342 c: this.getColor(mx
, my
),
343 p: this.getPiece(mx
, my
)
346 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
349 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
356 getKnightMovesFrom([x
, y
]) {
357 // Add egg on initial square:
358 return this.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
359 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
364 getQueenMovesFrom(sq
) {
365 const normalMoves
= this.getPotentialMovesOf('q', sq
);
366 // If flag allows it, add 'invisible movements'
367 let invisibleMoves
= [];
368 if (this.powerFlags
[this.turn
]['q']) {
369 normalMoves
.forEach(m
=> {
371 m
.appear
.length
== 1 &&
372 m
.vanish
.length
== 1 &&
373 // Only simple non-capturing moves:
376 let im
= JSON
.parse(JSON
.stringify(m
));
377 im
.appear
[0].p
= 'i';
379 invisibleMoves
.push(im
);
383 return normalMoves
.concat(invisibleMoves
);
386 getKingMovesFrom([x
, y
]) {
387 let moves
= this.getPotentialMovesOf('k', [x
, y
]);
388 // If flag allows it, add 'remote shell captures'
389 if (this.powerFlags
[this.turn
]['k']) {
390 let shellCaptures
= this.getPotentialMovesOf('y', [x
, y
]);
391 shellCaptures
.forEach(sc
=> {
392 sc
.shell
= true; //easier play()
393 sc
.choice
= 'z'; //to display in showChoices()
394 // Fix move (Rifle style):
398 Array
.prototype.push
.apply(moves
, shellCaptures
);
404 const color
= this.turn
;
405 const oppCol
= C
.GetOppCol(color
);
407 move.appear
.length
> 0 &&
408 move.appear
[0].p
== 'p' &&
410 (color
== 'w' && move.end
.x
== 0) ||
411 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
414 // "Forgotten" promotion, which occurred after some effect
416 super.pawnPostProcess(moves
, color
, oppCol
);
417 super.showChoices(moves
);
420 this.postPlay(move, color
, oppCol
);
424 postPlay(move, color
, oppCol
) {
426 if (move.egg
== "toadette") {
427 this.reserve
= { w: {}, b: {} };
428 // Randomly select a piece in pawnPromotions
430 move.toadette
= Random
.sample(this.pawnPromotions
);
431 this.reserve
[color
][move.toadette
] = 1;
432 this.re_drawReserve([color
]);
434 else if (Object
.keys(this.reserve
).length
> 0) {
436 this.re_drawReserve([color
]);
439 this.powerFlags
[color
]['k'] = false;
440 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
441 this.powerFlags
[move.appear
[0].c
]['q'] = false;
442 if (color
== this.playerColor
) {
444 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
447 if (color
== this.playerColor
) {
448 // Look for an immobilized piece of my color: it can now move
449 for (let i
=0; i
<8; i
++) {
450 for (let j
=0; j
<8; j
++) {
451 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
452 const piece
= this.getPiece(i
, j
);
454 this.getColor(i
, j
) == color
&&
455 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
457 move.vanish
.push(new PiPo({
458 x: i
, y: j
, c: color
, p: piece
460 move.appear
.push(new PiPo({
461 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
467 // Also make opponent invisible queen visible again, if any
468 for (let i
=0; i
<8; i
++) {
469 for (let j
=0; j
<8; j
++) {
471 this.board
[i
][j
] != "" &&
472 this.getColor(i
, j
) == oppCol
474 const pieceIJ
= this.getPiece(i
, j
);
475 if (pieceIJ
== 'i') {
476 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
477 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
479 else if (pieceIJ
== '?')
480 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
485 this.playOnBoard(move);
486 super.postPlay(move);
489 playVisual(move, r
) {
490 super.playVisual(move, r
);
492 this.displayBonus(move);
495 computeNextMove(move) {
496 // Set potential random effects, so that play() is deterministic
497 // from opponent viewpoint:
498 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
501 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
502 move.next
= this.getEggEffect(move);
505 move.next
= this.getMushroomEffect(move);
509 move.next
= this.getBombBananaEffect(move, endPiece
);
512 // NOTE: Chakart has also some side-effects:
514 !move.next
&& move.appear
.length
> 0 &&
515 !move.kingboo
&& !move.luigiEffect
517 const movingPiece
= move.appear
[0].p
;
518 if (['b', 'r'].includes(movingPiece
)) {
519 // Drop a banana or bomb:
521 this.getRandomSquare([move.end
.x
, move.end
.y
],
523 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
524 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
532 p: movingPiece
== 'r' ? 'd' : 'w'
535 if (this.board
[bs
[0]][bs
[1]] != "") {
540 c: this.getColor(bs
[0], bs
[1]),
541 p: this.getPiece(bs
[0], bs
[1])
551 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
554 // Helper to set and apply banana/bomb effect
555 getRandomSquare([x
, y
], steps
, freeSquare
) {
556 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
558 // Square to put banana/bomb cannot be occupied by a piece
559 validSteps
= validSteps
.filter(s
=> {
560 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
563 if (validSteps
.length
== 0)
565 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
566 return [x
+ step
[0], y
+ step
[1]];
570 const getRandomPiece
= (c
) => {
571 let bagOfPieces
= [];
572 for (let i
=0; i
<this.size
.x
; i
++) {
573 for (let j
=0; j
<this.size
.y
; j
++) {
574 if (this.getColor(i
, j
) == c
&& this.getPiece(i
, j
) != 'k')
575 bagOfPieces
.push([i
, j
]);
578 if (bagOfPieces
.length
>= 1)
579 return Random
.sample(bagOfPieces
);
582 const color
= this.turn
;
587 // Change color of friendly or enemy piece, king excepted
588 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppCol(color
));
589 const newColor
= C
.GetOppCol(oldColor
);
590 const coords
= getRandomPiece(oldColor
);
592 const piece
= this.getPiece(coords
[0], coords
[1]);
595 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
598 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
601 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
611 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
629 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
634 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
638 if (this.board
[move.start
.x
][move.start
.y
] != "") {
639 // Pawn or knight let something on init square
640 em
.vanish
.push(new PiPo({
644 p: this.getPiece(move.start
.x
, move.start
.y
)
654 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
657 end: {x: move.end
.x
, y: move.end
.y
}
661 if (em
&& move.egg
!= "koopa")
662 em
.noAnimate
= true; //static move
666 getMushroomEffect(move) {
668 typeof move.start
.x
== "string" || //drop move (toadette)
669 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
673 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
674 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
675 // Pawn initial 2-squares move: normalize step
677 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
680 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
682 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
683 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
686 this.playOnBoard(move); //HACK for getBasicMove()
687 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
688 this.undoOnBoard(move);
693 getBombBananaEffect(move, item
) {
694 const steps
= item
== V
.BANANA
695 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
696 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
697 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
698 this.playOnBoard(move); //HACK for getBasicMove()
699 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
700 this.undoOnBoard(move);
705 let divBonus
= document
.createElement("div");
706 divBonus
.classList
.add("bonus-text");
707 divBonus
.innerHTML
= move.egg
;
708 let container
= document
.getElementById(this.containerId
);
709 container
.appendChild(divBonus
);
710 setTimeout(() => container
.removeChild(divBonus
), 2000);
721 // Kingboo bonus can be animated better:
722 customAnimate(move, segments
, cb
) {
725 super.animateMoving(move.end
, move.start
, null,
726 segments
.reverse().map(s
=> s
.reverse()), cb
);