5202b489dfeaad00f5a74cac6f4a678a87a7cc94
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 genRandInitBaseFen() {
135 const options
= Object
.assign({mode: "suicide"}, this.options
);
136 const gr
= new GiveawayRules({options: options
, genFenOnly: true});
137 let res
= gr
.genRandInitBaseFen();
138 res
.o
["flags"] = "1111"; //Peach + Mario flags
145 ? "w" + f
.toLowerCase()
146 : (['w', 'd', 'e', 'm'].includes(f
) ? "a" : "b") + f
151 // King can send shell? Queen can be invisible?
153 w: {k: false, q: false},
154 b: {k: false, q: false}
156 for (let c
of ['w', 'b']) {
157 for (let p
of ['k', 'q']) {
158 this.powerFlags
[c
][p
] =
159 fenflags
.charAt((c
== "w" ? 0 : 2) + (p
== 'k' ? 0 : 1)) == "1";
165 return this.powerFlags
;
168 disaggregateFlags(flags
) {
169 this.powerFlags
= flags
;
173 return ['w', 'b'].map(c
=> {
174 return ['k', 'q'].map(p
=> this.powerFlags
[c
][p
] ? "1" : "0").join("");
178 setOtherVariables(fenParsed
) {
179 super.setOtherVariables(fenParsed
);
181 // Change seed (after FEN generation!!)
182 // so that further calls differ between players:
183 Random
.setSeed(Math
.floor(19840 * Math
.random()));
187 this.reserve
= {}; //to be filled later
192 this.board
[i
][j
] == "" ||
193 ['i', V
.EGG
, V
.MUSHROOM
].includes(this.getPiece(i
, j
))
197 // For Toadette bonus
198 canDrop([c
, p
], [i
, j
]) {
201 this.board
[i
][j
] == "" ||
202 this.getColor(i
, j
) == 'a' ||
203 this.getPiece(i
, j
) == 'i'
206 (p
!= "p" || (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0))
210 getPotentialMovesFrom([x
, y
]) {
212 const piece
= this.getPiece(x
, y
);
213 if (this.egg
== "toadette")
214 moves
= this.getDropMovesFrom([x
, y
]);
215 else if (this.egg
== "kingboo") {
216 const color
= this.turn
;
217 const oppCol
= C
.GetOppCol(color
);
218 // Only allow to swap (non-immobilized!) pieces
219 for (let i
=0; i
<this.size
.x
; i
++) {
220 for (let j
=0; j
<this.size
.y
; j
++) {
221 const colIJ
= this.getColor(i
, j
);
222 const pieceIJ
= this.getPiece(i
, j
);
224 (i
!= x
|| j
!= y
) &&
225 ['w', 'b'].includes(colIJ
) &&
226 !Object
.keys(V
.IMMOBILIZE_DECODE
).includes(pieceIJ
) &&
227 // Next conditions = no pawn on last rank
231 (color
!= 'w' || i
!= 0) &&
232 (color
!= 'b' || i
!= this.size
.x
- 1)
239 (colIJ
!= 'w' || x
!= 0) &&
240 (colIJ
!= 'b' || x
!= this.size
.x
- 1)
244 let m
= this.getBasicMove([x
, y
], [i
, j
]);
245 m
.appear
.push(new PiPo({x: x
, y: y
, p: pieceIJ
, c: colIJ
}));
246 m
.kingboo
= true; //avoid some side effects (bananas/bombs)
253 // Normal case (including bonus daisy)
256 moves
= this.getPawnMovesFrom([x
, y
]); //apply promotions
259 moves
= this.getQueenMovesFrom([x
, y
]);
262 moves
= this.getKingMovesFrom([x
, y
]);
265 moves
= this.getKnightMovesFrom([x
, y
]);
269 // Explicitely listing types to avoid moving immobilized piece
270 moves
= super.getPotentialMovesOf(piece
, [x
, y
]);
277 getPawnMovesFrom([x
, y
]) {
278 const color
= this.turn
;
279 const oppCol
= C
.GetOppCol(color
);
280 const shiftX
= (color
== 'w' ? -1 : 1);
281 const firstRank
= (color
== "w" ? this.size
.x
- 1 : 0);
283 const frontPiece
= this.getPiece(x
+ shiftX
, y
);
285 this.board
[x
+ shiftX
][y
] == "" ||
286 this.getColor(x
+ shiftX
, y
) == 'a' ||
289 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
]));
291 [firstRank
, firstRank
+ shiftX
].includes(x
) &&
292 ![V
.BANANA
, V
.BOMB
].includes(frontPiece
) &&
294 this.board
[x
+ 2 * shiftX
][y
] == "" ||
295 this.getColor(x
+ 2 * shiftX
, y
) == 'a' ||
296 this.getPiece(x
+ 2 * shiftX
, y
) == 'i'
299 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
302 for (let shiftY
of [-1, 1]) {
303 const nextY
= this.getY(y
+ shiftY
);
306 nextY
< this.size
.y
&&
307 this.board
[x
+ shiftX
][nextY
] != "" &&
308 // Pawns cannot capture invisible queen this way!
309 this.getPiece(x
+ shiftX
, nextY
) != 'i' &&
310 ['a', oppCol
].includes(this.getColor(x
+ shiftX
, nextY
))
312 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, nextY
]));
315 moves
= super.pawnPostProcess(moves
, color
, oppCol
);
316 // Add mushroom on before-last square (+ potential segments)
318 let [mx
, my
] = [x
, y
];
319 if (Math
.abs(m
.end
.x
- m
.start
.x
) == 2)
320 mx
= (m
.start
.x
+ m
.end
.x
) / 2;
321 m
.appear
.push(new PiPo({x: mx
, y: my
, c: 'a', p: 'm'}));
322 if (mx
!= x
&& this.board
[mx
][my
] != "") {
323 m
.vanish
.push(new PiPo({
326 c: this.getColor(mx
, my
),
327 p: this.getPiece(mx
, my
)
330 if (Math
.abs(m
.end
.y
- m
.start
.y
) > 1) {
333 [[m
.end
.x
, m
.end
.y
], [m
.end
.x
, m
.end
.y
]]
340 getKnightMovesFrom([x
, y
]) {
341 // Add egg on initial square:
342 return super.getPotentialMovesOf('n', [x
, y
]).map(m
=> {
343 m
.appear
.push(new PiPo({p: "e", c: "a", x: x
, y: y
}));
348 getQueenMovesFrom(sq
) {
349 const normalMoves
= super.getPotentialMovesOf('q', sq
);
350 // If flag allows it, add 'invisible movements'
351 let invisibleMoves
= [];
352 if (this.powerFlags
[this.turn
]['q']) {
353 normalMoves
.forEach(m
=> {
355 m
.appear
.length
== 1 &&
356 m
.vanish
.length
== 1 &&
357 // Only simple non-capturing moves:
360 let im
= JSON
.parse(JSON
.stringify(m
));
361 im
.appear
[0].p
= 'i';
363 invisibleMoves
.push(im
);
367 return normalMoves
.concat(invisibleMoves
);
370 getKingMovesFrom([x
, y
]) {
371 let moves
= super.getPotentialMovesOf('k', [x
, y
]);
372 // If flag allows it, add 'remote shell captures'
373 if (this.powerFlags
[this.turn
]['k']) {
374 let shellCaptures
= super.getPotentialMovesOf('y', [x
, y
]);
375 shellCaptures
.forEach(sc
=> {
376 sc
.shell
= true; //easier play()
377 sc
.choice
= 'z'; //to display in showChoices()
378 // Fix move (Rifle style):
382 Array
.prototype.push
.apply(moves
, shellCaptures
);
388 const color
= this.turn
;
389 const oppCol
= C
.GetOppCol(color
);
391 if (move.egg
== "toadette") {
392 this.reserve
= { w: {}, b: {} };
393 // Randomly select a piece in pawnPromotions
395 move.toadette
= Random
.sample(this.pawnPromotions
);
396 this.reserve
[color
][move.toadette
] = 1;
397 this.re_drawReserve([color
]);
399 else if (Object
.keys(this.reserve
).length
> 0) {
401 this.re_drawReserve([color
]);
404 this.powerFlags
[color
]['k'] = false;
405 else if (move.appear
.length
> 0 && move.appear
[0].p
== 'i') {
406 this.powerFlags
[move.appear
[0].c
]['q'] = false;
407 if (color
== this.playerColor
) {
409 new PiPo({x: move.start
.x
, y: move.start
.y
, c: color
, p: '?'}));
412 if (color
== this.playerColor
) {
413 // Look for an immobilized piece of my color: it can now move
414 for (let i
=0; i
<8; i
++) {
415 for (let j
=0; j
<8; j
++) {
416 if ((i
!= move.end
.x
|| j
!= move.end
.y
) && this.board
[i
][j
] != "") {
417 const piece
= this.getPiece(i
, j
);
419 this.getColor(i
, j
) == color
&&
420 Object
.keys(V
.IMMOBILIZE_DECODE
).includes(piece
)
422 move.vanish
.push(new PiPo({
423 x: i
, y: j
, c: color
, p: piece
425 move.appear
.push(new PiPo({
426 x: i
, y: j
, c: color
, p: V
.IMMOBILIZE_DECODE
[piece
]
432 // Also make opponent invisible queen visible again, if any
433 for (let i
=0; i
<8; i
++) {
434 for (let j
=0; j
<8; j
++) {
436 this.board
[i
][j
] != "" &&
437 this.getColor(i
, j
) == oppCol
439 const pieceIJ
= this.getPiece(i
, j
);
442 // Ensure that current move doesn't erase invisible queen
443 move.appear
.every(a
=> a
.x
!= i
|| a
.y
!= j
)
445 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'i'}));
446 move.appear
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: 'q'}));
448 else if (pieceIJ
== '?')
449 move.vanish
.push(new PiPo({x: i
, y: j
, c: oppCol
, p: '?'}));
454 this.playOnBoard(move);
455 super.postPlay(move);
458 playVisual(move, r
) {
459 super.playVisual(move, r
);
461 this.displayBonus(move);
464 buildMoveStack(move, r
) {
465 const color
= this.turn
;
467 move.appear
.length
> 0 &&
468 move.appear
[0].p
== 'p' &&
470 (color
== 'w' && move.end
.x
== 0) ||
471 (color
== 'b' && move.end
.x
== this.size
.x
- 1)
474 // "Forgotten" promotion, which occurred after some effect
475 let moves
= super.pawnPostProcess([move], color
, C
.GetOppCol(color
));
476 super.showChoices(moves
, r
);
479 super.buildMoveStack(move, r
);
482 computeNextMove(move) {
485 // Set potential random effects, so that play() is deterministic
486 // from opponent viewpoint:
487 const endPiece
= this.getPiece(move.end
.x
, move.end
.y
);
490 move.egg
= Random
.sample(V
.EGG_SURPRISE
);
491 move.next
= this.getEggEffect(move);
494 move.next
= this.getMushroomEffect(move);
498 move.next
= this.getBombBananaEffect(move, endPiece
);
501 // NOTE: Chakart has also some side-effects:
503 !move.next
&& move.appear
.length
> 0 &&
504 !move.kingboo
&& !move.luigiEffect
506 const movingPiece
= move.appear
[0].p
;
507 if (['b', 'r'].includes(movingPiece
)) {
508 // Drop a banana or bomb:
510 this.getRandomSquare([move.end
.x
, move.end
.y
],
512 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
513 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
521 p: movingPiece
== 'r' ? 'd' : 'w'
524 if (this.board
[bs
[0]][bs
[1]] != "") {
529 c: this.getColor(bs
[0], bs
[1]),
530 p: this.getPiece(bs
[0], bs
[1])
540 return !move.next
&& !["daisy", "toadette", "kingboo"].includes(move.egg
);
543 // Helper to set and apply banana/bomb effect
544 getRandomSquare([x
, y
], steps
, freeSquare
) {
545 let validSteps
= steps
.filter(s
=> this.onBoard(x
+ s
[0], y
+ s
[1]));
547 // Square to put banana/bomb cannot be occupied by a piece
548 validSteps
= validSteps
.filter(s
=> {
549 return ["", 'a'].includes(this.getColor(x
+ s
[0], y
+ s
[1]))
552 if (validSteps
.length
== 0)
554 const step
= validSteps
[Random
.randInt(validSteps
.length
)];
555 return [x
+ step
[0], y
+ step
[1]];
559 const getRandomPiece
= (c
) => {
560 let bagOfPieces
= [];
561 for (let i
=0; i
<this.size
.x
; i
++) {
562 for (let j
=0; j
<this.size
.y
; j
++) {
563 const pieceIJ
= this.getPiece(i
, j
);
565 this.getColor(i
, j
) == c
&& pieceIJ
!= 'k' &&
567 // The color will change, so pawns on first rank are ineligible
569 (c
== 'w' && i
< this.size
.x
- 1) || (c
== 'b' && i
> 0)
572 bagOfPieces
.push([i
, j
]);
576 if (bagOfPieces
.length
>= 1)
577 return Random
.sample(bagOfPieces
);
580 const color
= this.turn
;
585 // Change color of friendly or enemy piece, king excepted
586 const oldColor
= (move.egg
== "waluigi" ? color : C
.GetOppCol(color
));
587 const newColor
= C
.GetOppCol(oldColor
);
588 const coords
= getRandomPiece(oldColor
);
590 const piece
= this.getPiece(coords
[0], coords
[1]);
593 new PiPo({x: coords
[0], y: coords
[1], c: newColor
, p: piece
})
596 new PiPo({x: coords
[0], y: coords
[1], c: oldColor
, p: piece
})
599 em
.luigiEffect
= true; //avoid dropping bomb/banana by mistake
609 p: V
.IMMOBILIZE_CODE
[move.appear
[0].p
]
627 x: move.start
.x
, y: move.start
.y
, c: color
, p: move.appear
[0].p
632 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
636 if (this.board
[move.start
.x
][move.start
.y
] != "") {
637 // Pawn or knight let something on init square
638 em
.vanish
.push(new PiPo({
642 p: this.getPiece(move.start
.x
, move.start
.y
)
645 em
.koopa
= true; //avoid applying effect
653 x: move.end
.x
, y: move.end
.y
, c: color
, p: move.appear
[0].p
656 end: {x: move.end
.x
, y: move.end
.y
}
660 if (em
&& move.egg
!= "koopa")
661 em
.noAnimate
= true; //static move
665 getMushroomEffect(move) {
667 typeof move.start
.x
== "string" || //drop move (toadette)
668 ['b', 'r', 'q'].includes(move.vanish
[0].p
) //slider
672 let step
= [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
];
673 if (Math
.abs(step
[0]) == 2 && Math
.abs(step
[1]) == 0)
674 // Pawn initial 2-squares move: normalize step
676 const nextSquare
= [move.end
.x
+ step
[0], move.end
.y
+ step
[1]];
679 this.onBoard(nextSquare
[0], nextSquare
[1]) &&
681 this.board
[nextSquare
[0]][nextSquare
[1]] == "" ||
682 this.getColor(nextSquare
[0], nextSquare
[1]) == 'a'
685 this.playOnBoard(move); //HACK for getBasicMove()
686 nextMove
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
687 this.undoOnBoard(move);
692 getBombBananaEffect(move, item
) {
693 const steps
= item
== V
.BANANA
694 ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
695 : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
696 const nextSquare
= this.getRandomSquare([move.end
.x
, move.end
.y
], steps
);
697 this.playOnBoard(move); //HACK for getBasicMove()
698 const res
= this.getBasicMove([move.end
.x
, move.end
.y
], nextSquare
);
699 this.undoOnBoard(move);
704 super.displayMessage(null, move.egg
, "bonus-text", 2000);
715 // Kingboo bonus can be animated better:
716 customAnimate(move, segments
, cb
) {
719 super.animateMoving(move.end
, move.start
, null,
720 segments
.reverse().map(s
=> s
.reverse()), cb
);