Fix Align4, fix mushrooms effect for Chakart
[xogo.git] / variants / Chakart / class.js
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";
7
8 export default class ChakartRules extends ChessRules {
9
10 static get Options() {
11 return {
12 select: [
13 {
14 label: "Randomness",
15 variable: "randomness",
16 defaut: 2,
17 options: [
18 {label: "Deterministic", value: 0},
19 {label: "Symmetric random", value: 1},
20 {label: "Asymmetric random", value: 2}
21 ]
22 }
23 ],
24 styles: ["cylinder"]
25 };
26 }
27
28 get pawnPromotions() {
29 return ['q', 'r', 'n', 'b', 'k'];
30 }
31
32 get hasCastle() {
33 return false;
34 }
35 get hasEnpassant() {
36 return false;
37 }
38 get hasReserve() {
39 return true;
40 }
41 get hasReserveFen() {
42 return false;
43 }
44
45 static get IMMOBILIZE_CODE() {
46 return {
47 'p': 's',
48 'r': 'u',
49 'n': 'o',
50 'b': 'c',
51 'q': 't',
52 'k': 'l'
53 };
54 }
55
56 static get IMMOBILIZE_DECODE() {
57 return {
58 's': 'p',
59 'u': 'r',
60 'o': 'n',
61 'c': 'b',
62 't': 'q',
63 'l': 'k'
64 };
65 }
66
67 // Fictive color 'a', bomb banana mushroom egg
68 static get BOMB() {
69 return 'w'; //"Wario"
70 }
71 static get BANANA() {
72 return 'd'; //"Donkey"
73 }
74 static get EGG() {
75 return 'e';
76 }
77 static get MUSHROOM() {
78 return 'm';
79 }
80
81 static get EGG_SURPRISE() {
82 return [
83 "kingboo", "bowser", "daisy", "koopa",
84 "luigi", "waluigi", "toadette", "chomp"];
85 }
86
87 canIplay(x, y) {
88 if (
89 this.playerColor != this.turn ||
90 Object.keys(V.IMMOBILIZE_DECODE).includes(this.getPiece(x, y))
91 ) {
92 return false;
93 }
94 return this.egg == "kingboo" || this.getColor(x, y) == this.turn;
95 }
96
97 pieces(color, x, y) {
98 const specials = {
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"}
106 };
107 const bowsered = {
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"]}
114 };
115 return Object.assign(
116 {
117 'y': {
118 // Virtual piece for "king remote shell captures"
119 moves: [],
120 attack: [
121 {
122 steps: [
123 [0, 1], [0, -1], [1, 0], [-1, 0],
124 [1, 1], [1, -1], [-1, 1], [-1, -1]
125 ]
126 }
127 ]
128 }
129 },
130 specials, bowsered, super.pieces(color, x, y));
131 }
132
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);
142 }
143
144 fen2board(f) {
145 return (
146 f.charCodeAt() <= 90
147 ? "w" + f.toLowerCase()
148 : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f
149 );
150 }
151
152 setFlags(fenflags) {
153 // King can send shell? Queen can be invisible?
154 this.powerFlags = {
155 w: {k: false, q: false},
156 b: {k: false, q: false}
157 };
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";
162 }
163 }
164 }
165
166 aggregateFlags() {
167 return this.powerFlags;
168 }
169
170 disaggregateFlags(flags) {
171 this.powerFlags = flags;
172 }
173
174 getFlagsFen() {
175 return ['w', 'b'].map(c => {
176 return ['k', 'q'].map(p => this.powerFlags[c][p] ? "1" : "0").join("");
177 }).join("");
178 }
179
180 setOtherVariables(fenParsed) {
181 this.setFlags(fenParsed.flags);
182 this.reserve = {}; //to be filled later
183 this.egg = null;
184 this.moveStack = [];
185 // Change seed (after FEN generation!!)
186 // so that further calls differ between players:
187 Random.setSeed(Math.floor(19840 * Math.random()));
188 }
189
190 // For Toadette bonus
191 getDropMovesFrom([c, p]) {
192 if (typeof c != "string" || this.reserve[c][p] == 0)
193 return [];
194 let moves = [];
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') {
202 let m = new Move({
203 start: {x: c, y: p},
204 appear: [new PiPo({x: i, y: j, c: c, p: p})],
205 vanish: []
206 });
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}));
210 moves.push(m);
211 }
212 }
213 }
214 return moves;
215 }
216
217 getPotentialMovesFrom([x, y]) {
218 let moves = [];
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);
230 if (
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
235 (
236 piece != 'p' ||
237 (
238 (color != 'w' || i != 0) &&
239 (color != 'b' || i != this.size.x - 1)
240 )
241 )
242 &&
243 (
244 pieceIJ != 'p' ||
245 (
246 (colIJ != 'w' || x != 0) &&
247 (colIJ != 'b' || x != this.size.x - 1)
248 )
249 )
250 ) {
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)
254 moves.push(m);
255 }
256 }
257 }
258 }
259 else {
260 // Normal case (including bonus daisy)
261 switch (piece) {
262 case 'p':
263 moves = this.getPawnMovesFrom([x, y]); //apply promotions
264 break;
265 case 'q':
266 moves = this.getQueenMovesFrom([x, y]);
267 break;
268 case 'k':
269 moves = this.getKingMovesFrom([x, y]);
270 break;
271 case 'n':
272 moves = this.getKnightMovesFrom([x, y]);
273 break;
274 case 'b':
275 case 'r':
276 // Explicitely listing types to avoid moving immobilized piece
277 moves = this.getPotentialMovesOf(piece, [x, y]);
278 break;
279 }
280 }
281 return moves;
282 }
283
284 canStepOver(i, j) {
285 return (
286 this.board[i][j] == "" ||
287 ['i', V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
288 );
289 }
290
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);
296 let moves = [];
297 const frontPiece = this.getPiece(x + shiftX, y);
298 if (
299 this.board[x + shiftX][y] == "" ||
300 this.getColor(x + shiftX, y) == 'a' ||
301 frontPiece == 'i'
302 ) {
303 moves.push(this.getBasicMove([x, y], [x + shiftX, y]));
304 if (
305 [firstRank, firstRank + shiftX].includes(x) &&
306 ![V.BANANA, V.BOMB].includes(frontPiece) &&
307 (
308 this.board[x + 2 * shiftX][y] == "" ||
309 this.getColor(x + 2 * shiftX, y) == 'a' ||
310 this.getPiece(x + 2 * shiftX, y) == 'i'
311 )
312 ) {
313 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
314 }
315 }
316 for (let shiftY of [-1, 1]) {
317 const nextY = this.getY(y + shiftY);
318 if (
319 nextY >= 0 &&
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))
325 ) {
326 moves.push(this.getBasicMove([x, y], [x + shiftX, nextY]));
327 }
328 }
329 this.pawnPostProcess(moves, color, oppCol);
330 // Add mushroom on before-last square (+ potential segments)
331 moves.forEach(m => {
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({
338 x: mx,
339 y: my,
340 c: this.getColor(mx, my),
341 p: this.getPiece(mx, my)
342 }));
343 }
344 if (Math.abs(m.end.y - m.start.y) > 1) {
345 m.segments = [
346 [[x, y], [x, y]],
347 [[m.end.x, m.end.y], [m.end.x, m.end.y]]
348 ];
349 }
350 });
351 return moves;
352 }
353
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}));
358 return m;
359 });
360 }
361
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 => {
368 if (
369 m.appear.length == 1 &&
370 m.vanish.length == 1 &&
371 // Only simple non-capturing moves:
372 m.vanish[0].c != 'a'
373 ) {
374 let im = JSON.parse(JSON.stringify(m));
375 im.appear[0].p = 'i';
376 im.noAnimate = true;
377 invisibleMoves.push(im);
378 }
379 });
380 }
381 return normalMoves.concat(invisibleMoves);
382 }
383
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):
393 sc.vanish.shift();
394 sc.appear.shift();
395 });
396 Array.prototype.push.apply(moves, shellCaptures);
397 }
398 return moves;
399 }
400
401 play(move) {
402 const color = this.turn;
403 const oppCol = C.GetOppCol(color);
404 if (
405 move.appear.length > 0 &&
406 move.appear[0].p == 'p' &&
407 (
408 (color == 'w' && move.end.x == 0) ||
409 (color == 'b' && move.end.x == this.size.x - 1)
410 )
411 ) {
412 // "Forgotten" promotion, which occurred after some effect
413 let moves = [move];
414 super.pawnPostProcess(moves, color, oppCol);
415 super.showChoices(moves);
416 return false;
417 }
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);
422 switch (endPiece) {
423 case V.EGG:
424 move.egg = Random.sample(V.EGG_SURPRISE);
425 move.next = this.getEggEffect(move);
426 break;
427 case V.MUSHROOM:
428 move.next = this.getMushroomEffect(move);
429 break;
430 case V.BANANA:
431 case V.BOMB:
432 move.next = this.getBombBananaEffect(move, endPiece);
433 break;
434 }
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:
439 const bs =
440 this.getRandomSquare([move.end.x, move.end.y],
441 movingPiece == 'r'
442 ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
443 : [[1, 0], [-1, 0], [0, 1], [0, -1]],
444 "freeSquare");
445 if (bs) {
446 move.appear.push(
447 new PiPo({
448 x: bs[0],
449 y: bs[1],
450 c: 'a',
451 p: movingPiece == 'r' ? 'd' : 'w'
452 })
453 );
454 if (this.board[bs[0]][bs[1]] != "") {
455 move.vanish.push(
456 new PiPo({
457 x: bs[0],
458 y: bs[1],
459 c: this.getColor(bs[0], bs[1]),
460 p: this.getPiece(bs[0], bs[1])
461 })
462 );
463 }
464 }
465 }
466 }
467 move.nextComputed = true;
468 }
469 this.egg = move.egg;
470 if (move.egg == "toadette") {
471 this.reserve = { w: {}, b: {} };
472 // Randomly select a piece in pawnPromotions
473 if (!move.toadette)
474 move.toadette = Random.sample(this.pawnPromotions);
475 this.reserve[color][move.toadette] = 1;
476 this.re_drawReserve([color]);
477 }
478 else if (Object.keys(this.reserve).length > 0) {
479 this.reserve = {};
480 this.re_drawReserve([color]);
481 }
482 if (move.shell)
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) {
487 move.appear.push(
488 new PiPo({x: move.start.x, y: move.start.y, c: color, p: '?'}));
489 }
490 }
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);
497 if (
498 this.getColor(i, j) == color &&
499 Object.keys(V.IMMOBILIZE_DECODE).includes(piece)
500 ) {
501 move.vanish.push(new PiPo({
502 x: i, y: j, c: color, p: piece
503 }));
504 move.appear.push(new PiPo({
505 x: i, y: j, c: color, p: V.IMMOBILIZE_DECODE[piece]
506 }));
507 }
508 }
509 }
510 }
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++) {
514 if (
515 this.board[i][j] != "" &&
516 this.getColor(i, j) == oppCol
517 ) {
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'}));
522 }
523 else if (pieceIJ == '?')
524 move.vanish.push(new PiPo({x: i, y: j, c: oppCol, p: '?'}));
525 }
526 }
527 }
528 }
529 if (!move.next && !["daisy", "toadette", "kingboo"].includes(move.egg)) {
530 this.turn = oppCol;
531 this.movesCount++;
532 }
533 if (move.egg)
534 this.displayBonus(move);
535 this.playOnBoard(move);
536 this.nextMove = move.next;
537 return true;
538 }
539
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]));
543 if (freeSquare) {
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]))
547 });
548 }
549 if (validSteps.length == 0)
550 return null;
551 const step = validSteps[Random.randInt(validSteps.length)];
552 return [x + step[0], y + step[1]];
553 }
554
555 getEggEffect(move) {
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]);
562 }
563 }
564 if (bagOfPieces.length >= 1)
565 return Random.sample(bagOfPieces);
566 return null;
567 };
568 const color = this.turn;
569 let em = null;
570 switch (move.egg) {
571 case "luigi":
572 case "waluigi":
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);
577 if (coords) {
578 const piece = this.getPiece(coords[0], coords[1]);
579 em = new Move({
580 appear: [
581 new PiPo({x: coords[0], y: coords[1], c: newColor, p: piece})
582 ],
583 vanish: [
584 new PiPo({x: coords[0], y: coords[1], c: oldColor, p: piece})
585 ]
586 });
587 }
588 break;
589 case "bowser":
590 em = new Move({
591 appear: [
592 new PiPo({
593 x: move.end.x,
594 y: move.end.y,
595 c: color,
596 p: V.IMMOBILIZE_CODE[move.appear[0].p]
597 })
598 ],
599 vanish: [
600 new PiPo({
601 x: move.end.x,
602 y: move.end.y,
603 c: color,
604 p: move.appear[0].p
605 })
606 ]
607 });
608 break;
609 case "koopa":
610 // Reverse move
611 em = new Move({
612 appear: [
613 new PiPo({
614 x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p
615 })
616 ],
617 vanish: [
618 new PiPo({
619 x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
620 })
621 ]
622 });
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({
626 x: move.start.x,
627 y: move.start.y,
628 c: 'a',
629 p: this.getPiece(move.start.x, move.start.y)
630 }));
631 }
632 break;
633 case "chomp":
634 // Eat piece
635 em = new Move({
636 appear: [],
637 vanish: [
638 new PiPo({
639 x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
640 })
641 ],
642 end: {x: move.end.x, y: move.end.y}
643 });
644 break;
645 }
646 if (em && move.egg != "koopa")
647 em.noAnimate = true; //static move
648 return em;
649 }
650
651 getMushroomEffect(move) {
652 if (
653 typeof move.start.x == "string" || //drop move (toadette)
654 ['b', 'r', 'q'].includes(move.vanish[0].p) //slider
655 ) {
656 return null;
657 }
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
661 step[0] /= 2;
662 const nextSquare = [move.end.x + step[0], move.end.y + step[1]];
663 let nextMove = null;
664 if (
665 this.onBoard(nextSquare[0], nextSquare[1]) &&
666 (
667 this.board[nextSquare[0]][nextSquare[1]] == "" ||
668 this.getColor(nextSquare[0], nextSquare[1]) == 'a'
669 )
670 ) {
671 this.playOnBoard(move); //HACK for getBasicMove()
672 nextMove = this.getBasicMove([move.end.x, move.end.y], nextSquare);
673 this.undoOnBoard(move);
674 }
675 return nextMove;
676 }
677
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);
686 return res;
687 }
688
689 displayBonus(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);
696 }
697
698 atLeastOneMove() {
699 return true;
700 }
701
702 filterValid(moves) {
703 return moves;
704 }
705
706 playPlusVisual(move, r) {
707 const nextLines = () => {
708 if (!this.play(move))
709 return;
710 this.moveStack.push(move);
711 this.playVisual(move, r);
712 if (this.nextMove)
713 this.playPlusVisual(this.nextMove, r);
714 else {
715 this.afterPlay(this.moveStack);
716 this.moveStack = [];
717 }
718 };
719 if (this.moveStack.length == 0)
720 nextLines();
721 else
722 this.animate(move, nextLines);
723 }
724
725 };