update
[xogo.git] / variants / Chakart / class.js
CommitLineData
2b9b90da 1import ChessRules from "/base_rules";
f382c57b 2import GiveawayRules from "/variants/Giveaway";
8f57fbf2
BA
3import { ArrayFun } from "/utils/array.js";
4import { Random } from "/utils/alea.js";
5import PiPo from "/utils/PiPo.js";
6import Move from "/utils/Move.js";
f382c57b 7
f8b43ef7
BA
8export 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 };
25 }
26
8f57fbf2
BA
27 get pawnPromotions() {
28 return ['q', 'r', 'n', 'b', 'k'];
f8b43ef7 29 }
8f57fbf2 30
2b9b90da 31 get hasCastle() {
f8b43ef7
BA
32 return false;
33 }
2b9b90da 34 get hasEnpassant() {
f8b43ef7
BA
35 return false;
36 }
37
f8b43ef7
BA
38 static get IMMOBILIZE_CODE() {
39 return {
40 'p': 's',
41 'r': 'u',
42 'n': 'o',
43 'b': 'c',
44 'q': 't',
45 'k': 'l'
46 };
47 }
48
49 static get IMMOBILIZE_DECODE() {
50 return {
51 's': 'p',
52 'u': 'r',
53 'o': 'n',
54 'c': 'b',
55 't': 'q',
56 'l': 'k'
57 };
58 }
59
60 static get INVISIBLE_QUEEN() {
61 return 'i';
62 }
63
64 // Fictive color 'a', bomb banana mushroom egg
65 static get BOMB() {
66 return 'w'; //"Wario"
67 }
68 static get BANANA() {
69 return 'd'; //"Donkey"
70 }
71 static get EGG() {
72 return 'e';
73 }
74 static get MUSHROOM() {
75 return 'm';
76 }
77
91339921
BA
78 genRandInitFen(seed) {
79 const gr = new GiveawayRules({mode: "suicide"}, true);
80 return (
81 gr.genRandInitFen(seed).slice(0, -1) +
82 // Add Peach + Mario flags + capture counts
83 '{"flags": "1111", "ccount": "000000000000"}'
84 );
85 }
86
8f57fbf2 87 fen2board(f) {
f8b43ef7
BA
88 return (
89 f.charCodeAt() <= 90
90 ? "w" + f.toLowerCase()
91 : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f
92 );
93 }
94
f8b43ef7
BA
95 setFlags(fenflags) {
96 // King can send shell? Queen can be invisible?
97 this.powerFlags = {
8f57fbf2
BA
98 w: {k: false, q: false},
99 b: {k: false, q: false}
f8b43ef7 100 };
8f57fbf2 101 for (let c of ['w', 'b']) {
f8b43ef7
BA
102 for (let p of ['k', 'q']) {
103 this.powerFlags[c][p] =
104 fenflags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1";
105 }
106 }
107 }
108
109 aggregateFlags() {
110 return this.powerFlags;
111 }
112
113 disaggregateFlags(flags) {
114 this.powerFlags = flags;
115 }
116
117 getFen() {
118 return super.getFen() + " " + this.getCapturedFen();
119 }
120
91339921
BA
121 getFlagsFen() {
122 return ['w', 'b'].map(c => {
123 return ['k', 'q'].map(p => this.powerFlags[c][p] ? "1" : "0").join("");
124 }).join("");
125 }
126
f8b43ef7 127 getCapturedFen() {
8f57fbf2
BA
128 const res = ['w', 'b'].map(c => {
129 Object.values(this.captured[c])
130 });
131 return res[0].concat(res[1]).join("");
f8b43ef7
BA
132 }
133
8f57fbf2
BA
134 setOtherVariables(fenParsed) {
135 super.setOtherVariables(fenParsed);
f8b43ef7 136 // Initialize captured pieces' counts from FEN
8f57fbf2
BA
137 const allCapts = fenParsed.captured.split("").map(x => parseInt(x, 10));
138 const pieces = ['p', 'r', 'n', 'b', 'q', 'k'];
f8b43ef7 139 this.captured = {
8f57fbf2
BA
140 w: Array.toObject(pieces, allCapts.slice(0, 6)),
141 b: Array.toObject(pieces, allCapts.slice(6, 12))
f8b43ef7 142 };
91339921
BA
143 this.reserve = { w: {}, b: {} }; //to be replaced by this.captured
144 this.effect = "";
f8b43ef7
BA
145 }
146
c7c2f41c 147 // For Toadette bonus
91339921 148 getDropMovesFrom([c, p]) {
c7c2f41c
BA
149 if (this.reserve[c][p] == 0)
150 return [];
f8b43ef7 151 let moves = [];
91339921
BA
152 const start = (c == 'w' && p == 'p' ? 1 : 0);
153 const end = (color == 'b' && p == 'p' ? 7 : 8);
f8b43ef7 154 for (let i = start; i < end; i++) {
91339921
BA
155 for (let j = 0; j < this.size.y; j++) {
156 const pieceIJ = this.getPiece(i, j);
f8b43ef7 157 if (
91339921 158 this.board[i][j] == "" ||
f8b43ef7 159 this.getColor(i, j) == 'a' ||
91339921 160 pieceIJ == V.INVISIBLE_QUEEN
f8b43ef7 161 ) {
91339921
BA
162 let m = new Move({
163 start: {x: c, y: p},
164 end: {x: i, y: j},
165 appear: [new PiPo({x: i, y: j, c: c, p: p})],
166 vanish: []
167 });
168 // A drop move may remove a bonus (or hidden queen!)
169 if (this.board[i][j] != "")
170 m.vanish.push(new PiPo({x: i, y: j, c: 'a', p: pieceIJ}));
f8b43ef7
BA
171 moves.push(m);
172 }
173 }
174 }
175 return moves;
176 }
177
91339921
BA
178
179
180
181
182
183
184
185
186
187// TODO: rethink from here:
188
f8b43ef7
BA
189 getPotentialMovesFrom([x, y]) {
190 let moves = [];
191 if (this.subTurn == 1) {
192 moves = super.getPotentialMovesFrom([x, y]);
193 const finalPieces = V.PawnSpecs.promotions;
194 const color = this.turn;
195 const lastRank = (color == "w" ? 0 : 7);
196 let pMoves = [];
197 moves.forEach(m => {
198 if (
199 m.appear.length > 0 &&
200 ['p', 's'].includes(m.appear[0].p) &&
201 m.appear[0].x == lastRank
202 ) {
203 for (let i = 1; i < finalPieces.length; i++) {
204 const piece = finalPieces[i];
205 let otherM = JSON.parse(JSON.stringify(m));
206 otherM.appear[0].p =
207 m.appear[0].p == V.PAWN
208 ? finalPieces[i]
209 : V.IMMOBILIZE_CODE[finalPieces[i]];
210 pMoves.push(otherM);
211 }
212 // Finally alter m itself:
213 m.appear[0].p =
214 m.appear[0].p == V.PAWN
215 ? finalPieces[0]
216 : V.IMMOBILIZE_CODE[finalPieces[0]];
217 }
218 });
219 Array.prototype.push.apply(moves, pMoves);
220 }
221 else {
222 // Subturn == 2
223 const L = this.effects.length;
224 switch (this.effects[L-1]) {
225 case "kingboo":
226 // Exchange position with any visible piece,
227 // except pawns if arriving on last rank.
228 const lastRank = { 'w': 0, 'b': 7 };
229 const color = this.turn;
230 const allowLastRank = (this.getPiece(x, y) != V.PAWN);
231 for (let i=0; i<8; i++) {
232 for (let j=0; j<8; j++) {
233 const colIJ = this.getColor(i, j);
234 const pieceIJ = this.getPiece(i, j);
235 if (
236 (i != x || j != y) &&
237 this.board[i][j] != V.EMPTY &&
238 pieceIJ != V.INVISIBLE_QUEEN &&
239 colIJ != 'a'
240 ) {
241 if (
242 (pieceIJ != V.PAWN || x != lastRank[colIJ]) &&
243 (allowLastRank || i != lastRank[color])
244 ) {
245 const movedUnit = new PiPo({
246 x: x,
247 y: y,
248 c: colIJ,
249 p: this.getPiece(i, j)
250 });
251 let mMove = this.getBasicMove({ x: x, y: y }, [i, j]);
252 mMove.appear.push(movedUnit);
253 moves.push(mMove);
254 }
255 }
256 }
257 }
258 break;
259 case "toadette":
260 // Resurrect a captured piece
261 if (x >= V.size.x) moves = this.getReserveMoves([x, y]);
262 break;
263 case "daisy":
264 // Play again with any piece
265 moves = super.getPotentialMovesFrom([x, y]);
266 break;
267 }
268 }
269 return moves;
270 }
271
272 // Helper for getBasicMove(): banana/bomb effect
273 getRandomSquare([x, y], steps) {
274 const validSteps = steps.filter(s => V.OnBoard(x + s[0], y + s[1]));
275 const step = validSteps[randInt(validSteps.length)];
276 return [x + step[0], y + step[1]];
277 }
278
279 // Apply mushroom, bomb or banana effect (hidden to the player).
280 // Determine egg effect, too, and apply its first part if possible.
281 getBasicMove_aux(psq1, sq2, tr, initMove) {
282 const [x1, y1] = [psq1.x, psq1.y];
283 const color1 = this.turn;
284 const piece1 = (!!tr ? tr.p : (psq1.p || this.getPiece(x1, y1)));
285 const oppCol = V.GetOppCol(color1);
286 if (!sq2) {
287 let move = {
288 appear: [],
289 vanish: []
290 };
291 // banana or bomb defines next square, or the move ends there
292 move.appear = [
293 new PiPo({
294 x: x1,
295 y: y1,
296 c: color1,
297 p: piece1
298 })
299 ];
300 if (this.board[x1][y1] != V.EMPTY) {
301 const initP1 = this.getPiece(x1, y1);
302 move.vanish = [
303 new PiPo({
304 x: x1,
305 y: y1,
306 c: this.getColor(x1, y1),
307 p: initP1
308 })
309 ];
310 if ([V.BANANA, V.BOMB].includes(initP1)) {
311 const steps = V.steps[initP1 == V.BANANA ? V.ROOK : V.BISHOP];
312 move.next = this.getRandomSquare([x1, y1], steps);
313 }
314 }
315 move.end = { x: x1, y: y1 };
316 return move;
317 }
318 const [x2, y2] = [sq2[0], sq2[1]];
319 // The move starts normally, on board:
320 let move = super.getBasicMove([x1, y1], [x2, y2], tr);
321 if (!!tr) move.promoteInto = tr.c + tr.p; //in case of (chomped...)
322 const L = this.effects.length;
323 if (
324 [V.PAWN, V.KNIGHT].includes(piece1) &&
325 !!initMove &&
326 (this.subTurn == 1 || this.effects[L-1] == "daisy")
327 ) {
328 switch (piece1) {
329 case V.PAWN: {
330 const twoSquaresMove = (Math.abs(x2 - x1) == 2);
331 const mushroomX = x1 + (twoSquaresMove ? (x2 - x1) / 2 : 0);
332 move.appear.push(
333 new PiPo({
334 x: mushroomX,
335 y: y1,
336 c: 'a',
337 p: V.MUSHROOM
338 })
339 );
340 if (this.getColor(mushroomX, y1) == 'a') {
341 move.vanish.push(
342 new PiPo({
343 x: mushroomX,
344 y: y1,
345 c: 'a',
346 p: this.getPiece(mushroomX, y1)
347 })
348 );
349 }
350 break;
351 }
352 case V.KNIGHT: {
353 const deltaX = Math.abs(x2 - x1);
354 const deltaY = Math.abs(y2 - y1);
355 let eggSquare = [
356 x1 + (deltaX == 2 ? (x2 - x1) / 2 : 0),
357 y1 + (deltaY == 2 ? (y2 - y1) / 2 : 0)
358 ];
359 if (
360 this.board[eggSquare[0]][eggSquare[1]] != V.EMPTY &&
361 this.getColor(eggSquare[0], eggSquare[1]) != 'a'
362 ) {
363 eggSquare[0] = x1;
364 eggSquare[1] = y1;
365 }
366 move.appear.push(
367 new PiPo({
368 x: eggSquare[0],
369 y: eggSquare[1],
370 c: 'a',
371 p: V.EGG
372 })
373 );
374 if (this.getColor(eggSquare[0], eggSquare[1]) == 'a') {
375 move.vanish.push(
376 new PiPo({
377 x: eggSquare[0],
378 y: eggSquare[1],
379 c: 'a',
380 p: this.getPiece(eggSquare[0], eggSquare[1])
381 })
382 );
383 }
384 break;
385 }
386 }
387 }
388 // For (wa)luigi effect:
389 const changePieceColor = (color) => {
390 let pieces = [];
391 const oppLastRank = (color == 'w' ? 7 : 0);
392 for (let i=0; i<8; i++) {
393 for (let j=0; j<8; j++) {
394 const piece = this.getPiece(i, j);
395 if (
396 (i != move.vanish[0].x || j != move.vanish[0].y) &&
397 this.board[i][j] != V.EMPTY &&
398 piece != V.INVISIBLE_QUEEN &&
399 this.getColor(i, j) == color
400 ) {
401 if (piece != V.KING && (piece != V.PAWN || i != oppLastRank))
402 pieces.push({ x: i, y: j, p: piece });
403 }
404 }
405 }
406 // Special case of the current piece (still at its initial position)
407 if (color == color1)
408 pieces.push({ x: move.appear[0].x, y: move.appear[0].y, p: piece1 });
409 const cp = pieces[randInt(pieces.length)];
410 if (move.appear[0].x != cp.x || move.appear[0].y != cp.y) {
411 move.vanish.push(
412 new PiPo({
413 x: cp.x,
414 y: cp.y,
415 c: color,
416 p: cp.p
417 })
418 );
419 }
420 else move.appear.shift();
421 move.appear.push(
422 new PiPo({
423 x: cp.x,
424 y: cp.y,
425 c: V.GetOppCol(color),
426 p: cp.p
427 })
428 );
429 };
430 const applyEggEffect = () => {
431 if (this.subTurn == 2)
432 // No egg effects at subTurn 2
433 return;
434 // 1) Determine the effect (some may be impossible)
435 let effects = ["kingboo", "koopa", "chomp", "bowser", "daisy"];
436 if (Object.values(this.captured[color1]).some(c => c >= 1))
437 effects.push("toadette");
438 const lastRank = { 'w': 0, 'b': 7 };
439 if (
440 this.board.some((b,i) =>
441 b.some(cell => {
442 return (
443 cell[0] == oppCol &&
444 cell[1] != V.KING &&
445 (cell[1] != V.PAWN || i != lastRank[color1])
446 );
447 })
448 )
449 ) {
450 effects.push("luigi");
451 }
452 if (
453 (
454 piece1 != V.KING &&
455 (piece1 != V.PAWN || move.appear[0].x != lastRank[oppCol])
456 ) ||
457 this.board.some((b,i) =>
458 b.some(cell => {
459 return (
460 cell[0] == color1 &&
461 cell[1] != V.KING &&
462 (cell[1] != V.PAWN || i != lastRank[oppCol])
463 );
464 })
465 )
466 ) {
467 effects.push("waluigi");
468 }
469 const effect = effects[randInt(effects.length)];
470 move.end.effect = effect;
471 // 2) Apply it if possible
472 if (!(["kingboo", "toadette", "daisy"].includes(effect))) {
473 switch (effect) {
474 case "koopa":
475 move.appear = [];
476 // Maybe egg effect was applied after others,
477 // so just shift vanish array:
478 move.vanish.shift();
479 break;
480 case "chomp":
481 move.appear = [];
482 break;
483 case "bowser":
484 move.appear[0].p = V.IMMOBILIZE_CODE[piece1];
485 break;
486 case "luigi":
487 changePieceColor(oppCol);
488 break;
489 case "waluigi":
490 changePieceColor(color1);
491 break;
492 }
493 }
494 };
495 const applyMushroomEffect = () => {
496 if ([V.PAWN, V.KING, V.KNIGHT].includes(piece1)) {
497 // Just make another similar step, if possible (non-capturing)
498 const [i, j] = [
499 move.appear[0].x + (x2 - x1),
500 move.appear[0].y + (y2 - y1)
501 ];
502 if (
503 V.OnBoard(i, j) &&
504 (
505 this.board[i][j] == V.EMPTY ||
506 this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
507 this.getColor(i, j) == 'a'
508 )
509 ) {
510 move.appear[0].x = i;
511 move.appear[0].y = j;
512 if (this.board[i][j] != V.EMPTY) {
513 const object = this.getPiece(i, j);
514 const color = this.getColor(i, j);
515 move.vanish.push(
516 new PiPo({
517 x: i,
518 y: j,
519 c: color,
520 p: object
521 })
522 );
523 switch (object) {
524 case V.BANANA:
525 case V.BOMB:
526 const steps = V.steps[object == V.BANANA ? V.ROOK : V.BISHOP];
527 move.next = this.getRandomSquare([i, j], steps);
528 break;
529 case V.EGG:
530 applyEggEffect();
531 break;
532 case V.MUSHROOM:
533 applyMushroomEffect();
534 break;
535 }
536 }
537 }
538 }
539 else {
540 // Queen, bishop or rook:
541 const step = [
542 (x2 - x1) / Math.abs(x2 - x1) || 0,
543 (y2 - y1) / Math.abs(y2 - y1) || 0
544 ];
545 const next = [move.appear[0].x + step[0], move.appear[0].y + step[1]];
546 if (
547 V.OnBoard(next[0], next[1]) &&
548 this.board[next[0]][next[1]] != V.EMPTY &&
549 this.getPiece(next[0], next[1]) != V.INVISIBLE_QUEEN &&
550 this.getColor(next[0], next[1]) != 'a'
551 ) {
552 const afterNext = [next[0] + step[0], next[1] + step[1]];
553 if (V.OnBoard(afterNext[0], afterNext[1])) {
554 const afterColor = this.getColor(afterNext[0], afterNext[1]);
555 if (
556 this.board[afterNext[0]][afterNext[1]] == V.EMPTY ||
557 afterColor == 'a'
558 ) {
559 move.appear[0].x = afterNext[0];
560 move.appear[0].y = afterNext[1];
561 if (this.board[afterNext[0]][afterNext[1]] != V.EMPTY) {
562 // object = banana, bomb, mushroom or egg
563 const object = this.getPiece(afterNext[0], afterNext[1]);
564 move.vanish.push(
565 new PiPo({
566 x: afterNext[0],
567 y: afterNext[1],
568 c: afterColor,
569 p: object
570 })
571 );
572 switch (object) {
573 case V.BANANA:
574 case V.BOMB:
575 const steps =
576 V.steps[object == V.BANANA ? V.ROOK : V.BISHOP];
577 move.next = this.getRandomSquare(
578 [afterNext[0], afterNext[1]], steps);
579 break;
580 case V.EGG:
581 applyEggEffect();
582 break;
583 case V.MUSHROOM:
584 applyMushroomEffect();
585 break;
586 }
587 }
588 }
589 }
590 }
591 }
592 };
593 const color2 = this.getColor(x2, y2);
594 const piece2 = this.getPiece(x2, y2);
595 if (color2 == 'a') {
596 switch (piece2) {
597 case V.BANANA:
598 case V.BOMB:
599 const steps = V.steps[piece2 == V.BANANA ? V.ROOK : V.BISHOP];
600 move.next = this.getRandomSquare([x2, y2], steps);
601 break;
602 case V.MUSHROOM:
603 applyMushroomEffect();
604 break;
605 case V.EGG:
606 if (this.subTurn == 1)
607 // No egg effect at subTurn 2
608 applyEggEffect();
609 break;
610 }
611 }
612 if (
613 this.subTurn == 1 &&
614 !move.next &&
615 move.appear.length > 0 &&
616 [V.ROOK, V.BISHOP].includes(piece1)
617 ) {
618 const finalSquare = [move.appear[0].x, move.appear[0].y];
619 if (
620 color2 != 'a' ||
621 this.getColor(finalSquare[0], finalSquare[1]) != 'a' ||
622 this.getPiece(finalSquare[0], finalSquare[1]) != V.EGG
623 ) {
624 const validSteps =
625 V.steps[piece1 == V.ROOK ? V.BISHOP : V.ROOK].filter(s => {
626 const [i, j] = [finalSquare[0] + s[0], finalSquare[1] + s[1]];
627 return (
628 V.OnBoard(i, j) &&
629 // NOTE: do not place a bomb or banana on the invisible queen!
630 (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a')
631 );
632 });
633 if (validSteps.length >= 1) {
634 const randIdx = randInt(validSteps.length);
635 const [x, y] = [
636 finalSquare[0] + validSteps[randIdx][0],
637 finalSquare[1] + validSteps[randIdx][1]
638 ];
639 move.appear.push(
640 new PiPo({
641 x: x,
642 y: y,
643 c: 'a',
644 p: (piece1 == V.ROOK ? V.BANANA : V.BOMB)
645 })
646 );
647 if (this.board[x][y] != V.EMPTY) {
648 move.vanish.push(
649 new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) }));
650 }
651 }
652 }
653 }
654 return move;
655 }
656
657 getBasicMove(psq1, sq2, tr) {
658 let moves = [];
659 if (Array.isArray(psq1)) psq1 = { x: psq1[0], y: psq1[1] };
660 let m = this.getBasicMove_aux(psq1, sq2, tr, "initMove");
661 while (!!m.next) {
662 // Last move ended on bomb or banana, direction change
663 V.PlayOnBoard(this.board, m);
664 moves.push(m);
665 m = this.getBasicMove_aux(
666 { x: m.appear[0].x, y: m.appear[0].y }, m.next);
667 }
668 for (let i=moves.length-1; i>=0; i--) V.UndoOnBoard(this.board, moves[i]);
669 moves.push(m);
670 // Now merge moves into one
671 let move = {};
672 // start is wrong for Toadette moves --> it's fixed later
673 move.start = { x: psq1.x, y: psq1.y };
674 move.end = !!sq2 ? { x: sq2[0], y: sq2[1] } : { x: psq1.x, y: psq1.y };
675 if (!!tr) move.promoteInto = moves[0].promoteInto;
676 let lm = moves[moves.length-1];
677 if (this.subTurn == 1 && !!lm.end.effect)
678 move.end.effect = lm.end.effect;
679 if (moves.length == 1) {
680 move.appear = moves[0].appear;
681 move.vanish = moves[0].vanish;
682 }
683 else {
684 // Keep first vanish and last appear (if any)
685 move.appear = lm.appear;
686 move.vanish = moves[0].vanish;
687 if (
688 move.vanish.length >= 1 &&
689 move.appear.length >= 1 &&
690 move.vanish[0].x == move.appear[0].x &&
691 move.vanish[0].y == move.appear[0].y
692 ) {
693 // Loopback on initial square:
694 move.vanish.shift();
695 move.appear.shift();
696 }
697 for (let i=1; i < moves.length - 1; i++) {
698 for (let v of moves[i].vanish) {
699 // Only vanishing objects, not appearing at init move
700 if (
701 v.c == 'a' &&
702 (
703 moves[0].appear.length == 1 ||
704 moves[0].appear[1].x != v.x ||
705 moves[0].appear[1].y != v.y
706 )
707 ) {
708 move.vanish.push(v);
709 }
710 }
711 }
712 // Final vanish is our piece, but others might be relevant
713 // (for some egg bonuses at least).
714 for (let i=1; i < lm.vanish.length; i++) {
715 if (
716 lm.vanish[i].c != 'a' ||
717 moves[0].appear.length == 1 ||
718 moves[0].appear[1].x != lm.vanish[i].x ||
719 moves[0].appear[1].y != lm.vanish[i].y
720 ) {
721 move.vanish.push(lm.vanish[i]);
722 }
723 }
724 }
725 return move;
726 }
727
728 getPotentialPawnMoves([x, y]) {
729 const color = this.turn;
730 const oppCol = V.GetOppCol(color);
731 const [sizeX, sizeY] = [V.size.x, V.size.y];
732 const shiftX = V.PawnSpecs.directions[color];
733 const firstRank = (color == "w" ? sizeX - 1 : 0);
734 let moves = [];
735 if (
736 this.board[x + shiftX][y] == V.EMPTY ||
737 this.getColor(x + shiftX, y) == 'a' ||
738 this.getPiece(x + shiftX, y) == V.INVISIBLE_QUEEN
739 ) {
740 this.addPawnMoves([x, y], [x + shiftX, y], moves);
741 if (
742 [firstRank, firstRank + shiftX].includes(x) &&
743 (
744 this.board[x + 2 * shiftX][y] == V.EMPTY ||
745 this.getColor(x + 2 * shiftX, y) == 'a' ||
746 this.getPiece(x + 2 * shiftX, y) == V.INVISIBLE_QUEEN
747 )
748 ) {
749 moves.push(this.getBasicMove({ x: x, y: y }, [x + 2 * shiftX, y]));
750 }
751 }
752 for (let shiftY of [-1, 1]) {
753 if (
754 y + shiftY >= 0 &&
755 y + shiftY < sizeY &&
756 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
757 // Pawns cannot capture invisible queen this way!
758 this.getPiece(x + shiftX, y + shiftY) != V.INVISIBLE_QUEEN &&
759 ['a', oppCol].includes(this.getColor(x + shiftX, y + shiftY))
760 ) {
761 this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves);
762 }
763 }
764 return moves;
765 }
766
767 getPotentialQueenMoves(sq) {
768 const normalMoves = super.getPotentialQueenMoves(sq);
769 // If flag allows it, add 'invisible movements'
770 let invisibleMoves = [];
771 if (this.powerFlags[this.turn][V.QUEEN]) {
772 normalMoves.forEach(m => {
773 if (
774 m.appear.length == 1 &&
775 m.vanish.length == 1 &&
776 // Only simple non-capturing moves:
777 m.vanish[0].c != 'a'
778 ) {
779 let im = JSON.parse(JSON.stringify(m));
780 im.appear[0].p = V.INVISIBLE_QUEEN;
781 im.end.noHighlight = true;
782 invisibleMoves.push(im);
783 }
784 });
785 }
786 return normalMoves.concat(invisibleMoves);
787 }
788
789 getPotentialKingMoves([x, y]) {
790 let moves = super.getPotentialKingMoves([x, y]);
791 const color = this.turn;
792 // If flag allows it, add 'remote shell captures'
793 if (this.powerFlags[this.turn][V.KING]) {
794 V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(step => {
795 let [i, j] = [x + step[0], y + step[1]];
796 while (
797 V.OnBoard(i, j) &&
798 (
799 this.board[i][j] == V.EMPTY ||
800 this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
801 (
802 this.getColor(i, j) == 'a' &&
803 [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
804 )
805 )
806 ) {
807 i += step[0];
808 j += step[1];
809 }
810 if (V.OnBoard(i, j)) {
811 const colIJ = this.getColor(i, j);
812 if (colIJ != color) {
813 // May just destroy a bomb or banana:
814 moves.push(
815 new Move({
816 start: { x: x, y: y},
817 end: { x: i, y: j },
818 appear: [],
819 vanish: [
820 new PiPo({
821 x: i, y: j, c: colIJ, p: this.getPiece(i, j)
822 })
823 ]
824 })
825 );
826 }
827 }
828 });
829 }
830 return moves;
831 }
832
833 getSlideNJumpMoves([x, y], steps, oneStep) {
834 let moves = [];
835 outerLoop: for (let step of steps) {
836 let i = x + step[0];
837 let j = y + step[1];
838 while (
839 V.OnBoard(i, j) &&
840 (
841 this.board[i][j] == V.EMPTY ||
842 this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
843 (
844 this.getColor(i, j) == 'a' &&
845 [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
846 )
847 )
848 ) {
849 moves.push(this.getBasicMove({ x: x, y: y }, [i, j]));
850 if (oneStep) continue outerLoop;
851 i += step[0];
852 j += step[1];
853 }
854 if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
855 moves.push(this.getBasicMove({ x: x, y: y }, [i, j]));
856 }
857 return moves;
858 }
859
860 getAllPotentialMoves() {
861 if (this.subTurn == 1) return super.getAllPotentialMoves();
862 let moves = [];
863 const color = this.turn;
864 const L = this.effects.length;
865 switch (this.effects[L-1]) {
866 case "kingboo": {
867 let allPieces = [];
868 for (let i=0; i<8; i++) {
869 for (let j=0; j<8; j++) {
870 const colIJ = this.getColor(i, j);
871 const pieceIJ = this.getPiece(i, j);
872 if (
873 i != x && j != y &&
874 this.board[i][j] != V.EMPTY &&
875 colIJ != 'a' &&
876 pieceIJ != V.INVISIBLE_QUEEN
877 ) {
878 allPieces.push({ x: i, y: j, c: colIJ, p: pieceIJ });
879 }
880 }
881 }
882 for (let x=0; x<8; x++) {
883 for (let y=0; y<8; y++) {
884 if (this.getColor(i, j) == color) {
885 // Add exchange with something
886 allPieces.forEach(pp => {
887 if (pp.x != i || pp.y != j) {
888 const movedUnit = new PiPo({
889 x: x,
890 y: y,
891 c: pp.c,
892 p: pp.p
893 });
894 let mMove = this.getBasicMove({ x: x, y: y }, [pp.x, pp.y]);
895 mMove.appear.push(movedUnit);
896 moves.push(mMove);
897 }
898 });
899 }
900 }
901 }
902 break;
903 }
904 case "toadette": {
905 const x = V.size.x + (this.turn == 'w' ? 0 : 1);
906 for (let y = 0; y < 8; y++)
907 Array.prototype.push.apply(moves, this.getReserveMoves([x, y]));
908 break;
909 }
910 case "daisy":
911 moves = super.getAllPotentialMoves();
912 break;
913 }
914 return moves;
915 }
916
f8b43ef7 917
f8b43ef7 918
91339921
BA
919
920
921
922
923
924
925
926
927 prePlay(move) {
928 if (move.effect == "toadette")
929 this.reserve = this.captured;
930 else
931 this.reserve = { w: {}, b: {} };;
932 const color = this.turn;
f8b43ef7
BA
933 if (
934 move.vanish.length == 2 &&
935 move.vanish[1].c != 'a' &&
936 move.appear.length == 1 //avoid king Boo!
937 ) {
938 // Capture: update this.captured
939 let capturedPiece = move.vanish[1].p;
91339921
BA
940 if (capturedPiece == V.INVISIBLE_QUEEN)
941 capturedPiece = V.QUEEN;
f8b43ef7
BA
942 else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece))
943 capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece];
944 this.captured[move.vanish[1].c][capturedPiece]++;
945 }
946 else if (move.vanish.length == 0) {
947 if (move.appear.length == 0 || move.appear[0].c == 'a') return;
948 // A piece is back on board
949 this.captured[move.appear[0].c][move.appear[0].p]--;
950 }
951 if (move.appear.length == 0) {
952 // Three cases: king "shell capture", Chomp or Koopa
953 if (this.getPiece(move.start.x, move.start.y) == V.KING)
954 // King remote capture:
955 this.powerFlags[color][V.KING] = false;
956 else if (move.end.effect == "chomp")
957 this.captured[color][move.vanish[0].p]++;
958 }
959 else if (move.appear[0].p == V.INVISIBLE_QUEEN)
960 this.powerFlags[move.appear[0].c][V.QUEEN] = false;
961 if (this.subTurn == 2) return;
962 if (
963 move.turn[1] == 1 &&
964 move.appear.length == 0 ||
965 !(Object.keys(V.IMMOBILIZE_DECODE).includes(move.appear[0].p))
966 ) {
967 // Look for an immobilized piece of my color: it can now move
968 for (let i=0; i<8; i++) {
969 for (let j=0; j<8; j++) {
970 if (this.board[i][j] != V.EMPTY) {
971 const piece = this.getPiece(i, j);
972 if (
973 this.getColor(i, j) == color &&
974 Object.keys(V.IMMOBILIZE_DECODE).includes(piece)
975 ) {
976 this.board[i][j] = color + V.IMMOBILIZE_DECODE[piece];
977 move.wasImmobilized = [i, j];
978 }
979 }
980 }
981 }
982 }
983 // Also make opponent invisible queen visible again, if any
984 const oppCol = V.GetOppCol(color);
985 for (let i=0; i<8; i++) {
986 for (let j=0; j<8; j++) {
987 if (
988 this.board[i][j] != V.EMPTY &&
989 this.getColor(i, j) == oppCol &&
990 this.getPiece(i, j) == V.INVISIBLE_QUEEN
991 ) {
992 this.board[i][j] = oppCol + V.QUEEN;
993 move.wasInvisible = [i, j];
994 }
995 }
996 }
997 }
998
91339921
BA
999 play(move) {
1000 this.prePlay(move);
1001 this.playOnBoard(move);
1002 if (["kingboo", "toadette", "daisy"].includes(move.effect)) {
1003 this.effect = move.effect;
1004 this.subTurn = 2;
f8b43ef7 1005 }
91339921
BA
1006 else {
1007 this.turn = C.GetOppCol(this.turn);
1008 this.movesCount++;
1009 this.subTurn = 1;
f8b43ef7 1010 }
f8b43ef7
BA
1011 }
1012
1013 filterValid(moves) {
1014 return moves;
1015 }
1016
91339921
BA
1017 // TODO + display bonus messages
1018 // + animation + multi-moves for bananas/bombs/mushrooms
f8b43ef7
BA
1019
1020};