-import ChessRules from "/base_rules";
-import { SuicideRules } from "/variants/Suicide"; ////////:TODO generalize genRandInitFen ?!
-// constraints satisfaction ? + Chakart display bonus messages
-// + animation + multi-moves for bananas/bombs/mushrooms
-
-
-
-import { ArrayFun } from "/utils/array";
-import { randInt } from "/utils/alea";
+import ChessRules from "/base_rules.js";
+import GiveawayRules from "/variants/Giveaway/class.js";
+import {ArrayFun} from "/utils/array.js";
+import {Random} from "/utils/alea.js";
import PiPo from "/utils/PiPo.js";
import Move from "/utils/Move.js";
-export class ChakartRules extends ChessRules {
+export default class ChakartRules extends ChessRules {
static get Options() {
return {
variable: "randomness",
defaut: 2,
options: [
- { label: "Deterministic", value: 0 },
- { label: "Symmetric random", value: 1 },
- { label: "Asymmetric random", value: 2 }
+ {label: "Deterministic", value: 0},
+ {label: "Symmetric random", value: 1},
+ {label: "Asymmetric random", value: 2}
]
}
- ]
+ ],
+ styles: ["cylinder"]
};
}
- static get PawnSpecs() {
- return SuicideRules.PawnSpecs;
+ get pawnPromotions() {
+ return ['q', 'r', 'n', 'b', 'k'];
}
+
get hasCastle() {
return false;
}
get hasEnpassant() {
return false;
}
+ get hasReserve() {
+ return true;
+ }
+ get hasReserveFen() {
+ return false;
+ }
static get IMMOBILIZE_CODE() {
return {
};
}
- static get INVISIBLE_QUEEN() {
- return 'i';
- }
-
// Fictive color 'a', bomb banana mushroom egg
static get BOMB() {
return 'w'; //"Wario"
return 'm';
}
- static fen2board(f) {
- return (
- f.charCodeAt() <= 90
- ? "w" + f.toLowerCase()
- : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f
- );
- }
-
- static get PIECES() {
- return (
- ChessRules.PIECES.concat(
- Object.keys(V.IMMOBILIZE_DECODE)).concat(
- [V.BANANA, V.BOMB, V.EGG, V.MUSHROOM, V.INVISIBLE_QUEEN])
- );
+ static get EGG_SURPRISE() {
+ return [
+ "kingboo", "bowser", "daisy", "koopa",
+ "luigi", "waluigi", "toadette", "chomp"];
}
- getPpath(b) {
- let prefix = "";
+ canIplay(x, y) {
if (
- b[0] == 'a' ||
- b[1] == V.INVISIBLE_QUEEN ||
- Object.keys(V.IMMOBILIZE_DECODE).includes(b[1])
+ this.playerColor != this.turn ||
+ Object.keys(V.IMMOBILIZE_DECODE).includes(this.getPiece(x, y))
) {
- prefix = "Chakart/";
+ return false;
}
- return prefix + b;
- }
-
- getPPpath(m) {
- if (!!m.promoteInto) return m.promoteInto;
- if (m.appear.length == 0 && m.vanish.length == 1)
- // King 'remote shell capture', on an adjacent square:
- return this.getPpath(m.vanish[0].c + m.vanish[0].p);
- let piece = m.appear[0].p;
- if (Object.keys(V.IMMOBILIZE_DECODE).includes(piece))
- // Promotion by capture into immobilized piece: do not reveal!
- piece = V.IMMOBILIZE_DECODE[piece];
- return this.getPpath(m.appear[0].c + piece);
+ return this.egg == "kingboo" || this.getColor(x, y) == this.turn;
}
- static ParseFen(fen) {
- const fenParts = fen.split(" ");
+ pieces(color, x, y) {
+ const specials = {
+ 'i': {"class": "invisible"}, //queen
+ '?': {"class": "mystery"}, //...initial square
+ 'e': {"class": "egg"},
+ 'm': {"class": "mushroom"},
+ 'd': {"class": "banana"},
+ 'w': {"class": "bomb"},
+ 'z': {"class": "remote-capture"}
+ };
+ const bowsered = {
+ 's': {"class": ["immobilized", "pawn"]},
+ 'u': {"class": ["immobilized", "rook"]},
+ 'o': {"class": ["immobilized", "knight"]},
+ 'c': {"class": ["immobilized", "bishop"]},
+ 't': {"class": ["immobilized", "queen"]},
+ 'l': {"class": ["immobilized", "king"]}
+ };
return Object.assign(
- ChessRules.ParseFen(fen),
- { captured: fenParts[4] }
+ {
+ 'y': {
+ // Virtual piece for "king remote shell captures"
+ moves: [],
+ attack: [
+ {
+ steps: [
+ [0, 1], [0, -1], [1, 0], [-1, 0],
+ [1, 1], [1, -1], [-1, 1], [-1, -1]
+ ]
+ }
+ ]
+ }
+ },
+ specials, bowsered, super.pieces(color, x, y)
);
}
- static IsGoodFen(fen) {
- if (!ChessRules.IsGoodFen(fen)) return false;
- const captured = V.ParseFen(fen).captured;
- if (!captured || !captured.match(/^[0-9]{12,12}$/)) return false;
- return true;
+ genRandInitFen(seed) {
+ const options = Object.assign({mode: "suicide"}, this.options);
+ const gr = new GiveawayRules({options: options, genFenOnly: true});
+ const baseFen = gr.genRandInitFen(seed);
+ const fenParts = baseFen.split(" ");
+ let others = JSON.parse(fenParts[3]);
+ delete others["enpassant"];
+ others["flags"] = "1111"; //Peach + Mario flags
+ return fenParts.slice(0, 3).join(" ") + " " + JSON.stringify(others);
}
- // King can be l or L (immobilized) --> similar to Alice variant
- static IsGoodPosition(position) {
- if (position.length == 0) return false;
- const rows = position.split("/");
- if (rows.length != V.size.x) return false;
- let kings = { "k": 0, "K": 0, 'l': 0, 'L': 0 };
- for (let row of rows) {
- let sumElts = 0;
- for (let i = 0; i < row.length; i++) {
- if (['K', 'k', 'L', 'l'].includes(row[i])) kings[row[i]]++;
- if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
- else {
- const num = parseInt(row[i], 10);
- if (isNaN(num)) return false;
- sumElts += num;
- }
- }
- if (sumElts != V.size.y) return false;
- }
- if (kings['k'] + kings['l'] == 0 || kings['K'] + kings['L'] == 0)
- return false;
- return true;
- }
-
- static IsGoodFlags(flags) {
- // 4 for Peach + Mario w, b
- return !!flags.match(/^[01]{4,4}$/);
+ fen2board(f) {
+ return (
+ f.charCodeAt() <= 90
+ ? "w" + f.toLowerCase()
+ : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f
+ );
}
setFlags(fenflags) {
// King can send shell? Queen can be invisible?
this.powerFlags = {
- w: { 'k': false, 'q': false },
- b: { 'k': false, 'q': false }
+ w: {k: false, q: false},
+ b: {k: false, q: false}
};
- for (let c of ["w", "b"]) {
+ for (let c of ['w', 'b']) {
for (let p of ['k', 'q']) {
this.powerFlags[c][p] =
fenflags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1";
this.powerFlags = flags;
}
- getFen() {
- return super.getFen() + " " + this.getCapturedFen();
- }
-
- getFenForRepeat() {
- return super.getFenForRepeat() + "_" + this.getCapturedFen();
- }
-
- getCapturedFen() {
- let counts = [...Array(12).fill(0)];
- let i = 0;
- for (let p of V.RESERVE_PIECES) {
- counts[i] = this.captured["w"][p];
- counts[6 + i] = this.captured["b"][p];
- i++;
- }
- return counts.join("");
- }
-
- scanKings() {}
-
- setOtherVariables(fen) {
- super.setOtherVariables(fen);
- // Initialize captured pieces' counts from FEN
- const captured =
- V.ParseFen(fen).captured.split("").map(x => parseInt(x, 10));
- this.captured = {
- w: {
- [V.PAWN]: captured[0],
- [V.ROOK]: captured[1],
- [V.KNIGHT]: captured[2],
- [V.BISHOP]: captured[3],
- [V.QUEEN]: captured[4],
- [V.KING]: captured[5]
- },
- b: {
- [V.PAWN]: captured[6],
- [V.ROOK]: captured[7],
- [V.KNIGHT]: captured[8],
- [V.BISHOP]: captured[9],
- [V.QUEEN]: captured[10],
- [V.KING]: captured[11]
- }
- };
- this.effects = [];
- this.subTurn = 1;
- }
-
getFlagsFen() {
- let fen = "";
- // Add power flags
- for (let c of ["w", "b"])
- for (let p of ['k', 'q']) fen += (this.powerFlags[c][p] ? "1" : "0");
- return fen;
- }
-
- getColor(i, j) {
- if (i >= V.size.x) return i == V.size.x ? "w" : "b";
- return this.board[i][j].charAt(0);
+ return ['w', 'b'].map(c => {
+ return ['k', 'q'].map(p => this.powerFlags[c][p] ? "1" : "0").join("");
+ }).join("");
}
- getPiece(i, j) {
- if (i >= V.size.x) return V.RESERVE_PIECES[j];
- return this.board[i][j].charAt(1);
+ setOtherVariables(fenParsed) {
+ super.setOtherVariables(fenParsed);
+ this.egg = null;
+ // Change seed (after FEN generation!!)
+ // so that further calls differ between players:
+ Random.setSeed(Math.floor(19840 * Math.random()));
}
- getReservePpath(index, color) {
- return color + V.RESERVE_PIECES[index];
+ initReserves() {
+ this.reserve = {}; //to be filled later
}
- static get RESERVE_PIECES() {
- return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING];
+ canStepOver(i, j) {
+ return (
+ this.board[i][j] == "" ||
+ ['i', V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
+ );
}
- getReserveMoves([x, y]) {
- const color = this.turn;
- const p = V.RESERVE_PIECES[y];
- if (this.reserve[color][p] == 0) return [];
- let moves = [];
- const start = (color == 'w' && p == V.PAWN ? 1 : 0);
- const end = (color == 'b' && p == V.PAWN ? 7 : 8);
- for (let i = start; i < end; i++) {
- for (let j = 0; j < V.size.y; j++) {
- if (
- this.board[i][j] == V.EMPTY ||
- this.getColor(i, j) == 'a' ||
- this.getPiece(i, j) == V.INVISIBLE_QUEEN
- ) {
- let m = this.getBasicMove({ p: p, x: i, y: j});
- m.start = { x: x, y: y };
- moves.push(m);
- }
- }
- }
- return moves;
+ // For Toadette bonus
+ canDrop([c, p], [i, j]) {
+ return (
+ (
+ this.board[i][j] == "" ||
+ this.getColor(i, j) == 'a' ||
+ this.getPiece(i, j) == 'i'
+ )
+ &&
+ (p != "p" || (c == 'w' && i < this.size.x - 1) || (c == 'b' && i > 0))
+ );
}
getPotentialMovesFrom([x, y]) {
let moves = [];
- if (this.subTurn == 1) {
- moves = super.getPotentialMovesFrom([x, y]);
- const finalPieces = V.PawnSpecs.promotions;
+ const piece = this.getPiece(x, y);
+ if (this.egg == "toadette")
+ moves = this.getDropMovesFrom([x, y]);
+ else if (this.egg == "kingboo") {
const color = this.turn;
- const lastRank = (color == "w" ? 0 : 7);
- let pMoves = [];
- moves.forEach(m => {
- if (
- m.appear.length > 0 &&
- ['p', 's'].includes(m.appear[0].p) &&
- m.appear[0].x == lastRank
- ) {
- for (let i = 1; i < finalPieces.length; i++) {
- const piece = finalPieces[i];
- let otherM = JSON.parse(JSON.stringify(m));
- otherM.appear[0].p =
- m.appear[0].p == V.PAWN
- ? finalPieces[i]
- : V.IMMOBILIZE_CODE[finalPieces[i]];
- pMoves.push(otherM);
- }
- // Finally alter m itself:
- m.appear[0].p =
- m.appear[0].p == V.PAWN
- ? finalPieces[0]
- : V.IMMOBILIZE_CODE[finalPieces[0]];
- }
- });
- Array.prototype.push.apply(moves, pMoves);
- }
- else {
- // Subturn == 2
- const L = this.effects.length;
- switch (this.effects[L-1]) {
- case "kingboo":
- // Exchange position with any visible piece,
- // except pawns if arriving on last rank.
- const lastRank = { 'w': 0, 'b': 7 };
- const color = this.turn;
- const allowLastRank = (this.getPiece(x, y) != V.PAWN);
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- const colIJ = this.getColor(i, j);
- const pieceIJ = this.getPiece(i, j);
- if (
- (i != x || j != y) &&
- this.board[i][j] != V.EMPTY &&
- pieceIJ != V.INVISIBLE_QUEEN &&
- colIJ != 'a'
- ) {
- if (
- (pieceIJ != V.PAWN || x != lastRank[colIJ]) &&
- (allowLastRank || i != lastRank[color])
- ) {
- const movedUnit = new PiPo({
- x: x,
- y: y,
- c: colIJ,
- p: this.getPiece(i, j)
- });
- let mMove = this.getBasicMove({ x: x, y: y }, [i, j]);
- mMove.appear.push(movedUnit);
- moves.push(mMove);
- }
- }
- }
- }
- break;
- case "toadette":
- // Resurrect a captured piece
- if (x >= V.size.x) moves = this.getReserveMoves([x, y]);
- break;
- case "daisy":
- // Play again with any piece
- moves = super.getPotentialMovesFrom([x, y]);
- break;
- }
- }
- return moves;
- }
-
- // Helper for getBasicMove(): banana/bomb effect
- getRandomSquare([x, y], steps) {
- const validSteps = steps.filter(s => V.OnBoard(x + s[0], y + s[1]));
- const step = validSteps[randInt(validSteps.length)];
- return [x + step[0], y + step[1]];
- }
-
- // Apply mushroom, bomb or banana effect (hidden to the player).
- // Determine egg effect, too, and apply its first part if possible.
- getBasicMove_aux(psq1, sq2, tr, initMove) {
- const [x1, y1] = [psq1.x, psq1.y];
- const color1 = this.turn;
- const piece1 = (!!tr ? tr.p : (psq1.p || this.getPiece(x1, y1)));
- const oppCol = V.GetOppCol(color1);
- if (!sq2) {
- let move = {
- appear: [],
- vanish: []
- };
- // banana or bomb defines next square, or the move ends there
- move.appear = [
- new PiPo({
- x: x1,
- y: y1,
- c: color1,
- p: piece1
- })
- ];
- if (this.board[x1][y1] != V.EMPTY) {
- const initP1 = this.getPiece(x1, y1);
- move.vanish = [
- new PiPo({
- x: x1,
- y: y1,
- c: this.getColor(x1, y1),
- p: initP1
- })
- ];
- if ([V.BANANA, V.BOMB].includes(initP1)) {
- const steps = V.steps[initP1 == V.BANANA ? V.ROOK : V.BISHOP];
- move.next = this.getRandomSquare([x1, y1], steps);
- }
- }
- move.end = { x: x1, y: y1 };
- return move;
- }
- const [x2, y2] = [sq2[0], sq2[1]];
- // The move starts normally, on board:
- let move = super.getBasicMove([x1, y1], [x2, y2], tr);
- if (!!tr) move.promoteInto = tr.c + tr.p; //in case of (chomped...)
- const L = this.effects.length;
- if (
- [V.PAWN, V.KNIGHT].includes(piece1) &&
- !!initMove &&
- (this.subTurn == 1 || this.effects[L-1] == "daisy")
- ) {
- switch (piece1) {
- case V.PAWN: {
- const twoSquaresMove = (Math.abs(x2 - x1) == 2);
- const mushroomX = x1 + (twoSquaresMove ? (x2 - x1) / 2 : 0);
- move.appear.push(
- new PiPo({
- x: mushroomX,
- y: y1,
- c: 'a',
- p: V.MUSHROOM
- })
- );
- if (this.getColor(mushroomX, y1) == 'a') {
- move.vanish.push(
- new PiPo({
- x: mushroomX,
- y: y1,
- c: 'a',
- p: this.getPiece(mushroomX, y1)
- })
- );
- }
- break;
- }
- case V.KNIGHT: {
- const deltaX = Math.abs(x2 - x1);
- const deltaY = Math.abs(y2 - y1);
- let eggSquare = [
- x1 + (deltaX == 2 ? (x2 - x1) / 2 : 0),
- y1 + (deltaY == 2 ? (y2 - y1) / 2 : 0)
- ];
+ const oppCol = C.GetOppCol(color);
+ // Only allow to swap (non-immobilized!) pieces
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ const colIJ = this.getColor(i, j);
+ const pieceIJ = this.getPiece(i, j);
if (
- this.board[eggSquare[0]][eggSquare[1]] != V.EMPTY &&
- this.getColor(eggSquare[0], eggSquare[1]) != 'a'
+ (i != x || j != y) &&
+ ['w', 'b'].includes(colIJ) &&
+ !Object.keys(V.IMMOBILIZE_DECODE).includes(pieceIJ) &&
+ // Next conditions = no pawn on last rank
+ (
+ piece != 'p' ||
+ (
+ (color != 'w' || i != 0) &&
+ (color != 'b' || i != this.size.x - 1)
+ )
+ )
+ &&
+ (
+ pieceIJ != 'p' ||
+ (
+ (colIJ != 'w' || x != 0) &&
+ (colIJ != 'b' || x != this.size.x - 1)
+ )
+ )
) {
- eggSquare[0] = x1;
- eggSquare[1] = y1;
+ let m = this.getBasicMove([x, y], [i, j]);
+ m.appear.push(new PiPo({x: x, y: y, p: pieceIJ, c: colIJ}));
+ m.kingboo = true; //avoid some side effects (bananas/bombs)
+ moves.push(m);
}
- move.appear.push(
- new PiPo({
- x: eggSquare[0],
- y: eggSquare[1],
- c: 'a',
- p: V.EGG
- })
- );
- if (this.getColor(eggSquare[0], eggSquare[1]) == 'a') {
- move.vanish.push(
- new PiPo({
- x: eggSquare[0],
- y: eggSquare[1],
- c: 'a',
- p: this.getPiece(eggSquare[0], eggSquare[1])
- })
- );
- }
- break;
}
}
}
- // For (wa)luigi effect:
- const changePieceColor = (color) => {
- let pieces = [];
- const oppLastRank = (color == 'w' ? 7 : 0);
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- const piece = this.getPiece(i, j);
- if (
- (i != move.vanish[0].x || j != move.vanish[0].y) &&
- this.board[i][j] != V.EMPTY &&
- piece != V.INVISIBLE_QUEEN &&
- this.getColor(i, j) == color
- ) {
- if (piece != V.KING && (piece != V.PAWN || i != oppLastRank))
- pieces.push({ x: i, y: j, p: piece });
- }
- }
- }
- // Special case of the current piece (still at its initial position)
- if (color == color1)
- pieces.push({ x: move.appear[0].x, y: move.appear[0].y, p: piece1 });
- const cp = pieces[randInt(pieces.length)];
- if (move.appear[0].x != cp.x || move.appear[0].y != cp.y) {
- move.vanish.push(
- new PiPo({
- x: cp.x,
- y: cp.y,
- c: color,
- p: cp.p
- })
- );
- }
- else move.appear.shift();
- move.appear.push(
- new PiPo({
- x: cp.x,
- y: cp.y,
- c: V.GetOppCol(color),
- p: cp.p
- })
- );
- };
- const applyEggEffect = () => {
- if (this.subTurn == 2)
- // No egg effects at subTurn 2
- return;
- // 1) Determine the effect (some may be impossible)
- let effects = ["kingboo", "koopa", "chomp", "bowser", "daisy"];
- if (Object.values(this.captured[color1]).some(c => c >= 1))
- effects.push("toadette");
- const lastRank = { 'w': 0, 'b': 7 };
- if (
- this.board.some((b,i) =>
- b.some(cell => {
- return (
- cell[0] == oppCol &&
- cell[1] != V.KING &&
- (cell[1] != V.PAWN || i != lastRank[color1])
- );
- })
- )
- ) {
- effects.push("luigi");
- }
- if (
- (
- piece1 != V.KING &&
- (piece1 != V.PAWN || move.appear[0].x != lastRank[oppCol])
- ) ||
- this.board.some((b,i) =>
- b.some(cell => {
- return (
- cell[0] == color1 &&
- cell[1] != V.KING &&
- (cell[1] != V.PAWN || i != lastRank[oppCol])
- );
- })
- )
- ) {
- effects.push("waluigi");
- }
- const effect = effects[randInt(effects.length)];
- move.end.effect = effect;
- // 2) Apply it if possible
- if (!(["kingboo", "toadette", "daisy"].includes(effect))) {
- switch (effect) {
- case "koopa":
- move.appear = [];
- // Maybe egg effect was applied after others,
- // so just shift vanish array:
- move.vanish.shift();
- break;
- case "chomp":
- move.appear = [];
- break;
- case "bowser":
- move.appear[0].p = V.IMMOBILIZE_CODE[piece1];
- break;
- case "luigi":
- changePieceColor(oppCol);
- break;
- case "waluigi":
- changePieceColor(color1);
- break;
- }
- }
- };
- const applyMushroomEffect = () => {
- if ([V.PAWN, V.KING, V.KNIGHT].includes(piece1)) {
- // Just make another similar step, if possible (non-capturing)
- const [i, j] = [
- move.appear[0].x + (x2 - x1),
- move.appear[0].y + (y2 - y1)
- ];
- if (
- V.OnBoard(i, j) &&
- (
- this.board[i][j] == V.EMPTY ||
- this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
- this.getColor(i, j) == 'a'
- )
- ) {
- move.appear[0].x = i;
- move.appear[0].y = j;
- if (this.board[i][j] != V.EMPTY) {
- const object = this.getPiece(i, j);
- const color = this.getColor(i, j);
- move.vanish.push(
- new PiPo({
- x: i,
- y: j,
- c: color,
- p: object
- })
- );
- switch (object) {
- case V.BANANA:
- case V.BOMB:
- const steps = V.steps[object == V.BANANA ? V.ROOK : V.BISHOP];
- move.next = this.getRandomSquare([i, j], steps);
- break;
- case V.EGG:
- applyEggEffect();
- break;
- case V.MUSHROOM:
- applyMushroomEffect();
- break;
- }
- }
- }
- }
- else {
- // Queen, bishop or rook:
- const step = [
- (x2 - x1) / Math.abs(x2 - x1) || 0,
- (y2 - y1) / Math.abs(y2 - y1) || 0
- ];
- const next = [move.appear[0].x + step[0], move.appear[0].y + step[1]];
- if (
- V.OnBoard(next[0], next[1]) &&
- this.board[next[0]][next[1]] != V.EMPTY &&
- this.getPiece(next[0], next[1]) != V.INVISIBLE_QUEEN &&
- this.getColor(next[0], next[1]) != 'a'
- ) {
- const afterNext = [next[0] + step[0], next[1] + step[1]];
- if (V.OnBoard(afterNext[0], afterNext[1])) {
- const afterColor = this.getColor(afterNext[0], afterNext[1]);
- if (
- this.board[afterNext[0]][afterNext[1]] == V.EMPTY ||
- afterColor == 'a'
- ) {
- move.appear[0].x = afterNext[0];
- move.appear[0].y = afterNext[1];
- if (this.board[afterNext[0]][afterNext[1]] != V.EMPTY) {
- // object = banana, bomb, mushroom or egg
- const object = this.getPiece(afterNext[0], afterNext[1]);
- move.vanish.push(
- new PiPo({
- x: afterNext[0],
- y: afterNext[1],
- c: afterColor,
- p: object
- })
- );
- switch (object) {
- case V.BANANA:
- case V.BOMB:
- const steps =
- V.steps[object == V.BANANA ? V.ROOK : V.BISHOP];
- move.next = this.getRandomSquare(
- [afterNext[0], afterNext[1]], steps);
- break;
- case V.EGG:
- applyEggEffect();
- break;
- case V.MUSHROOM:
- applyMushroomEffect();
- break;
- }
- }
- }
- }
- }
- }
- };
- const color2 = this.getColor(x2, y2);
- const piece2 = this.getPiece(x2, y2);
- if (color2 == 'a') {
- switch (piece2) {
- case V.BANANA:
- case V.BOMB:
- const steps = V.steps[piece2 == V.BANANA ? V.ROOK : V.BISHOP];
- move.next = this.getRandomSquare([x2, y2], steps);
+ else {
+ // Normal case (including bonus daisy)
+ switch (piece) {
+ case 'p':
+ moves = this.getPawnMovesFrom([x, y]); //apply promotions
break;
- case V.MUSHROOM:
- applyMushroomEffect();
+ case 'q':
+ moves = this.getQueenMovesFrom([x, y]);
break;
- case V.EGG:
- if (this.subTurn == 1)
- // No egg effect at subTurn 2
- applyEggEffect();
+ case 'k':
+ moves = this.getKingMovesFrom([x, y]);
+ break;
+ case 'n':
+ moves = this.getKnightMovesFrom([x, y]);
+ break;
+ case 'b':
+ case 'r':
+ // Explicitely listing types to avoid moving immobilized piece
+ moves = super.getPotentialMovesOf(piece, [x, y]);
break;
}
}
- if (
- this.subTurn == 1 &&
- !move.next &&
- move.appear.length > 0 &&
- [V.ROOK, V.BISHOP].includes(piece1)
- ) {
- const finalSquare = [move.appear[0].x, move.appear[0].y];
- if (
- color2 != 'a' ||
- this.getColor(finalSquare[0], finalSquare[1]) != 'a' ||
- this.getPiece(finalSquare[0], finalSquare[1]) != V.EGG
- ) {
- const validSteps =
- V.steps[piece1 == V.ROOK ? V.BISHOP : V.ROOK].filter(s => {
- const [i, j] = [finalSquare[0] + s[0], finalSquare[1] + s[1]];
- return (
- V.OnBoard(i, j) &&
- // NOTE: do not place a bomb or banana on the invisible queen!
- (this.board[i][j] == V.EMPTY || this.getColor(i, j) == 'a')
- );
- });
- if (validSteps.length >= 1) {
- const randIdx = randInt(validSteps.length);
- const [x, y] = [
- finalSquare[0] + validSteps[randIdx][0],
- finalSquare[1] + validSteps[randIdx][1]
- ];
- move.appear.push(
- new PiPo({
- x: x,
- y: y,
- c: 'a',
- p: (piece1 == V.ROOK ? V.BANANA : V.BOMB)
- })
- );
- if (this.board[x][y] != V.EMPTY) {
- move.vanish.push(
- new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) }));
- }
- }
- }
- }
- return move;
- }
-
- getBasicMove(psq1, sq2, tr) {
- let moves = [];
- if (Array.isArray(psq1)) psq1 = { x: psq1[0], y: psq1[1] };
- let m = this.getBasicMove_aux(psq1, sq2, tr, "initMove");
- while (!!m.next) {
- // Last move ended on bomb or banana, direction change
- V.PlayOnBoard(this.board, m);
- moves.push(m);
- m = this.getBasicMove_aux(
- { x: m.appear[0].x, y: m.appear[0].y }, m.next);
- }
- for (let i=moves.length-1; i>=0; i--) V.UndoOnBoard(this.board, moves[i]);
- moves.push(m);
- // Now merge moves into one
- let move = {};
- // start is wrong for Toadette moves --> it's fixed later
- move.start = { x: psq1.x, y: psq1.y };
- move.end = !!sq2 ? { x: sq2[0], y: sq2[1] } : { x: psq1.x, y: psq1.y };
- if (!!tr) move.promoteInto = moves[0].promoteInto;
- let lm = moves[moves.length-1];
- if (this.subTurn == 1 && !!lm.end.effect)
- move.end.effect = lm.end.effect;
- if (moves.length == 1) {
- move.appear = moves[0].appear;
- move.vanish = moves[0].vanish;
- }
- else {
- // Keep first vanish and last appear (if any)
- move.appear = lm.appear;
- move.vanish = moves[0].vanish;
- if (
- move.vanish.length >= 1 &&
- move.appear.length >= 1 &&
- move.vanish[0].x == move.appear[0].x &&
- move.vanish[0].y == move.appear[0].y
- ) {
- // Loopback on initial square:
- move.vanish.shift();
- move.appear.shift();
- }
- for (let i=1; i < moves.length - 1; i++) {
- for (let v of moves[i].vanish) {
- // Only vanishing objects, not appearing at init move
- if (
- v.c == 'a' &&
- (
- moves[0].appear.length == 1 ||
- moves[0].appear[1].x != v.x ||
- moves[0].appear[1].y != v.y
- )
- ) {
- move.vanish.push(v);
- }
- }
- }
- // Final vanish is our piece, but others might be relevant
- // (for some egg bonuses at least).
- for (let i=1; i < lm.vanish.length; i++) {
- if (
- lm.vanish[i].c != 'a' ||
- moves[0].appear.length == 1 ||
- moves[0].appear[1].x != lm.vanish[i].x ||
- moves[0].appear[1].y != lm.vanish[i].y
- ) {
- move.vanish.push(lm.vanish[i]);
- }
- }
- }
- return move;
+ return moves;
}
- getPotentialPawnMoves([x, y]) {
+ getPawnMovesFrom([x, y]) {
const color = this.turn;
- const oppCol = V.GetOppCol(color);
- const [sizeX, sizeY] = [V.size.x, V.size.y];
- const shiftX = V.PawnSpecs.directions[color];
- const firstRank = (color == "w" ? sizeX - 1 : 0);
+ const oppCol = C.GetOppCol(color);
+ const shiftX = (color == 'w' ? -1 : 1);
+ const firstRank = (color == "w" ? this.size.x - 1 : 0);
let moves = [];
+ const frontPiece = this.getPiece(x + shiftX, y);
if (
- this.board[x + shiftX][y] == V.EMPTY ||
+ this.board[x + shiftX][y] == "" ||
this.getColor(x + shiftX, y) == 'a' ||
- this.getPiece(x + shiftX, y) == V.INVISIBLE_QUEEN
+ frontPiece == 'i'
) {
- this.addPawnMoves([x, y], [x + shiftX, y], moves);
+ moves.push(this.getBasicMove([x, y], [x + shiftX, y]));
if (
[firstRank, firstRank + shiftX].includes(x) &&
+ ![V.BANANA, V.BOMB].includes(frontPiece) &&
(
- this.board[x + 2 * shiftX][y] == V.EMPTY ||
+ this.board[x + 2 * shiftX][y] == "" ||
this.getColor(x + 2 * shiftX, y) == 'a' ||
- this.getPiece(x + 2 * shiftX, y) == V.INVISIBLE_QUEEN
+ this.getPiece(x + 2 * shiftX, y) == 'i'
)
) {
- moves.push(this.getBasicMove({ x: x, y: y }, [x + 2 * shiftX, y]));
+ moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
}
}
for (let shiftY of [-1, 1]) {
+ const nextY = this.getY(y + shiftY);
if (
- y + shiftY >= 0 &&
- y + shiftY < sizeY &&
- this.board[x + shiftX][y + shiftY] != V.EMPTY &&
+ nextY >= 0 &&
+ nextY < this.size.y &&
+ this.board[x + shiftX][nextY] != "" &&
// Pawns cannot capture invisible queen this way!
- this.getPiece(x + shiftX, y + shiftY) != V.INVISIBLE_QUEEN &&
- ['a', oppCol].includes(this.getColor(x + shiftX, y + shiftY))
+ this.getPiece(x + shiftX, nextY) != 'i' &&
+ ['a', oppCol].includes(this.getColor(x + shiftX, nextY))
) {
- this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves);
+ moves.push(this.getBasicMove([x, y], [x + shiftX, nextY]));
}
}
+ this.pawnPostProcess(moves, color, oppCol);
+ // Add mushroom on before-last square (+ potential segments)
+ moves.forEach(m => {
+ let [mx, my] = [x, y];
+ if (Math.abs(m.end.x - m.start.x) == 2)
+ mx = (m.start.x + m.end.x) / 2;
+ m.appear.push(new PiPo({x: mx, y: my, c: 'a', p: 'm'}));
+ if (mx != x && this.board[mx][my] != "") {
+ m.vanish.push(new PiPo({
+ x: mx,
+ y: my,
+ c: this.getColor(mx, my),
+ p: this.getPiece(mx, my)
+ }));
+ }
+ if (Math.abs(m.end.y - m.start.y) > 1) {
+ m.segments = [
+ [[x, y], [x, y]],
+ [[m.end.x, m.end.y], [m.end.x, m.end.y]]
+ ];
+ }
+ });
return moves;
}
- getPotentialQueenMoves(sq) {
- const normalMoves = super.getPotentialQueenMoves(sq);
+ getKnightMovesFrom([x, y]) {
+ // Add egg on initial square:
+ return super.getPotentialMovesOf('n', [x, y]).map(m => {
+ m.appear.push(new PiPo({p: "e", c: "a", x: x, y: y}));
+ return m;
+ });
+ }
+
+ getQueenMovesFrom(sq) {
+ const normalMoves = super.getPotentialMovesOf('q', sq);
// If flag allows it, add 'invisible movements'
let invisibleMoves = [];
- if (this.powerFlags[this.turn][V.QUEEN]) {
+ if (this.powerFlags[this.turn]['q']) {
normalMoves.forEach(m => {
if (
m.appear.length == 1 &&
m.vanish[0].c != 'a'
) {
let im = JSON.parse(JSON.stringify(m));
- im.appear[0].p = V.INVISIBLE_QUEEN;
- im.end.noHighlight = true;
+ im.appear[0].p = 'i';
+ im.noAnimate = true;
invisibleMoves.push(im);
}
});
return normalMoves.concat(invisibleMoves);
}
- getPotentialKingMoves([x, y]) {
- let moves = super.getPotentialKingMoves([x, y]);
- const color = this.turn;
+ getKingMovesFrom([x, y]) {
+ let moves = super.getPotentialMovesOf('k', [x, y]);
// If flag allows it, add 'remote shell captures'
- if (this.powerFlags[this.turn][V.KING]) {
- V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(step => {
- let [i, j] = [x + step[0], y + step[1]];
- while (
- V.OnBoard(i, j) &&
- (
- this.board[i][j] == V.EMPTY ||
- this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
- (
- this.getColor(i, j) == 'a' &&
- [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
- )
- )
- ) {
- i += step[0];
- j += step[1];
- }
- if (V.OnBoard(i, j)) {
- const colIJ = this.getColor(i, j);
- if (colIJ != color) {
- // May just destroy a bomb or banana:
- moves.push(
- new Move({
- start: { x: x, y: y},
- end: { x: i, y: j },
- appear: [],
- vanish: [
- new PiPo({
- x: i, y: j, c: colIJ, p: this.getPiece(i, j)
- })
- ]
- })
- );
- }
- }
+ if (this.powerFlags[this.turn]['k']) {
+ let shellCaptures = super.getPotentialMovesOf('y', [x, y]);
+ shellCaptures.forEach(sc => {
+ sc.shell = true; //easier play()
+ sc.choice = 'z'; //to display in showChoices()
+ // Fix move (Rifle style):
+ sc.vanish.shift();
+ sc.appear.shift();
});
+ Array.prototype.push.apply(moves, shellCaptures);
}
return moves;
}
- getSlideNJumpMoves([x, y], steps, oneStep) {
- let moves = [];
- outerLoop: for (let step of steps) {
- let i = x + step[0];
- let j = y + step[1];
- while (
- V.OnBoard(i, j) &&
- (
- this.board[i][j] == V.EMPTY ||
- this.getPiece(i, j) == V.INVISIBLE_QUEEN ||
- (
- this.getColor(i, j) == 'a' &&
- [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
- )
- )
- ) {
- moves.push(this.getBasicMove({ x: x, y: y }, [i, j]));
- if (oneStep) continue outerLoop;
- i += step[0];
- j += step[1];
- }
- if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
- moves.push(this.getBasicMove({ x: x, y: y }, [i, j]));
+ play(move) {
+ const color = this.turn;
+ const oppCol = C.GetOppCol(color);
+ if (
+ move.appear.length > 0 &&
+ move.appear[0].p == 'p' &&
+ (
+ (color == 'w' && move.end.x == 0) ||
+ (color == 'b' && move.end.x == this.size.x - 1)
+ )
+ ) {
+ // "Forgotten" promotion, which occurred after some effect
+ let moves = [move];
+ super.pawnPostProcess(moves, color, oppCol);
+ super.showChoices(moves);
+ return false;
}
- return moves;
+ this.postPlay(move, color, oppCol);
+ return true;
}
- getAllPotentialMoves() {
- if (this.subTurn == 1) return super.getAllPotentialMoves();
- let moves = [];
- const color = this.turn;
- const L = this.effects.length;
- switch (this.effects[L-1]) {
- case "kingboo": {
- let allPieces = [];
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- const colIJ = this.getColor(i, j);
- const pieceIJ = this.getPiece(i, j);
+ postPlay(move, color, oppCol) {
+ this.egg = move.egg;
+ if (move.egg == "toadette") {
+ this.reserve = { w: {}, b: {} };
+ // Randomly select a piece in pawnPromotions
+ if (!move.toadette)
+ move.toadette = Random.sample(this.pawnPromotions);
+ this.reserve[color][move.toadette] = 1;
+ this.re_drawReserve([color]);
+ }
+ else if (Object.keys(this.reserve).length > 0) {
+ this.reserve = {};
+ this.re_drawReserve([color]);
+ }
+ if (move.shell)
+ this.powerFlags[color]['k'] = false;
+ else if (move.appear.length > 0 && move.appear[0].p == 'i') {
+ this.powerFlags[move.appear[0].c]['q'] = false;
+ if (color == this.playerColor) {
+ move.appear.push(
+ new PiPo({x: move.start.x, y: move.start.y, c: color, p: '?'}));
+ }
+ }
+ if (color == this.playerColor) {
+ // Look for an immobilized piece of my color: it can now move
+ for (let i=0; i<8; i++) {
+ for (let j=0; j<8; j++) {
+ if ((i != move.end.x || j != move.end.y) && this.board[i][j] != "") {
+ const piece = this.getPiece(i, j);
if (
- i != x && j != y &&
- this.board[i][j] != V.EMPTY &&
- colIJ != 'a' &&
- pieceIJ != V.INVISIBLE_QUEEN
+ this.getColor(i, j) == color &&
+ Object.keys(V.IMMOBILIZE_DECODE).includes(piece)
) {
- allPieces.push({ x: i, y: j, c: colIJ, p: pieceIJ });
+ move.vanish.push(new PiPo({
+ x: i, y: j, c: color, p: piece
+ }));
+ move.appear.push(new PiPo({
+ x: i, y: j, c: color, p: V.IMMOBILIZE_DECODE[piece]
+ }));
}
}
}
- for (let x=0; x<8; x++) {
- for (let y=0; y<8; y++) {
- if (this.getColor(i, j) == color) {
- // Add exchange with something
- allPieces.forEach(pp => {
- if (pp.x != i || pp.y != j) {
- const movedUnit = new PiPo({
- x: x,
- y: y,
- c: pp.c,
- p: pp.p
- });
- let mMove = this.getBasicMove({ x: x, y: y }, [pp.x, pp.y]);
- mMove.appear.push(movedUnit);
- moves.push(mMove);
- }
- });
+ }
+ // Also make opponent invisible queen visible again, if any
+ for (let i=0; i<8; i++) {
+ for (let j=0; j<8; j++) {
+ if (
+ this.board[i][j] != "" &&
+ this.getColor(i, j) == oppCol
+ ) {
+ const pieceIJ = this.getPiece(i, j);
+ if (
+ pieceIJ == 'i' &&
+ // Ensure that current move doesn't erase invisible queen
+ move.appear.every(a => a.x != i || a.y != j)
+ ) {
+ move.vanish.push(new PiPo({x: i, y: j, c: oppCol, p: 'i'}));
+ move.appear.push(new PiPo({x: i, y: j, c: oppCol, p: 'q'}));
}
+ else if (pieceIJ == '?')
+ move.vanish.push(new PiPo({x: i, y: j, c: oppCol, p: '?'}));
}
}
- break;
}
- case "toadette": {
- const x = V.size.x + (this.turn == 'w' ? 0 : 1);
- for (let y = 0; y < 8; y++)
- Array.prototype.push.apply(moves, this.getReserveMoves([x, y]));
- break;
- }
- case "daisy":
- moves = super.getAllPotentialMoves();
- break;
}
- return moves;
+ this.playOnBoard(move);
+ super.postPlay(move);
}
- play(move) {
-// if (!this.states) this.states = [];
-// const stateFen = this.getFen();
-// this.states.push(stateFen);
-
- move.flags = JSON.stringify(this.aggregateFlags());
- V.PlayOnBoard(this.board, move);
- move.turn = [this.turn, this.subTurn];
- if (["kingboo", "toadette", "daisy"].includes(move.end.effect)) {
- this.effects.push(move.end.effect);
- this.subTurn = 2;
- }
- else {
- this.turn = V.GetOppCol(this.turn);
- this.movesCount++;
- this.subTurn = 1;
- }
- this.postPlay(move);
+ playVisual(move, r) {
+ super.playVisual(move, r);
+ if (move.egg)
+ this.displayBonus(move);
}
- postPlay(move) {
- if (move.end.effect == "toadette") this.reserve = this.captured;
- else this.reserve = undefined;
- const color = move.turn[0];
- if (
- move.vanish.length == 2 &&
- move.vanish[1].c != 'a' &&
- move.appear.length == 1 //avoid king Boo!
- ) {
- // Capture: update this.captured
- let capturedPiece = move.vanish[1].p;
- if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN;
- else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece))
- capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece];
- this.captured[move.vanish[1].c][capturedPiece]++;
- }
- else if (move.vanish.length == 0) {
- if (move.appear.length == 0 || move.appear[0].c == 'a') return;
- // A piece is back on board
- this.captured[move.appear[0].c][move.appear[0].p]--;
- }
- if (move.appear.length == 0) {
- // Three cases: king "shell capture", Chomp or Koopa
- if (this.getPiece(move.start.x, move.start.y) == V.KING)
- // King remote capture:
- this.powerFlags[color][V.KING] = false;
- else if (move.end.effect == "chomp")
- this.captured[color][move.vanish[0].p]++;
+ computeNextMove(move) {
+ // Set potential random effects, so that play() is deterministic
+ // from opponent viewpoint:
+ const endPiece = this.getPiece(move.end.x, move.end.y);
+ switch (endPiece) {
+ case V.EGG:
+ move.egg = Random.sample(V.EGG_SURPRISE);
+ move.next = this.getEggEffect(move);
+ break;
+ case V.MUSHROOM:
+ move.next = this.getMushroomEffect(move);
+ break;
+ case V.BANANA:
+ case V.BOMB:
+ move.next = this.getBombBananaEffect(move, endPiece);
+ break;
}
- else if (move.appear[0].p == V.INVISIBLE_QUEEN)
- this.powerFlags[move.appear[0].c][V.QUEEN] = false;
- if (this.subTurn == 2) return;
+ // NOTE: Chakart has also some side-effects:
if (
- move.turn[1] == 1 &&
- move.appear.length == 0 ||
- !(Object.keys(V.IMMOBILIZE_DECODE).includes(move.appear[0].p))
+ !move.next && move.appear.length > 0 &&
+ !move.kingboo && !move.luigiEffect
) {
- // Look for an immobilized piece of my color: it can now move
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- if (this.board[i][j] != V.EMPTY) {
- const piece = this.getPiece(i, j);
- if (
- this.getColor(i, j) == color &&
- Object.keys(V.IMMOBILIZE_DECODE).includes(piece)
- ) {
- this.board[i][j] = color + V.IMMOBILIZE_DECODE[piece];
- move.wasImmobilized = [i, j];
- }
+ const movingPiece = move.appear[0].p;
+ if (['b', 'r'].includes(movingPiece)) {
+ // Drop a banana or bomb:
+ const bs =
+ this.getRandomSquare([move.end.x, move.end.y],
+ movingPiece == 'r'
+ ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
+ : [[1, 0], [-1, 0], [0, 1], [0, -1]],
+ "freeSquare");
+ if (bs) {
+ move.appear.push(
+ new PiPo({
+ x: bs[0],
+ y: bs[1],
+ c: 'a',
+ p: movingPiece == 'r' ? 'd' : 'w'
+ })
+ );
+ if (this.board[bs[0]][bs[1]] != "") {
+ move.vanish.push(
+ new PiPo({
+ x: bs[0],
+ y: bs[1],
+ c: this.getColor(bs[0], bs[1]),
+ p: this.getPiece(bs[0], bs[1])
+ })
+ );
}
}
}
}
- // Also make opponent invisible queen visible again, if any
- const oppCol = V.GetOppCol(color);
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- if (
- this.board[i][j] != V.EMPTY &&
- this.getColor(i, j) == oppCol &&
- this.getPiece(i, j) == V.INVISIBLE_QUEEN
- ) {
- this.board[i][j] = oppCol + V.QUEEN;
- move.wasInvisible = [i, j];
- }
- }
- }
}
- undo(move) {
- this.disaggregateFlags(JSON.parse(move.flags));
- V.UndoOnBoard(this.board, move);
- if (["kingboo", "toadette", "daisy"].includes(move.end.effect))
- this.effects.pop();
- else this.movesCount--;
- this.turn = move.turn[0];
- this.subTurn = move.turn[1];
- this.postUndo(move);
-
-// const stateFen = this.getFen();
-// if (stateFen != this.states[this.states.length-1]) debugger;
-// this.states.pop();
+ isLastMove(move) {
+ return !move.next && !["daisy", "toadette", "kingboo"].includes(move.egg);
}
- postUndo(move) {
- if (!!move.wasImmobilized) {
- const [i, j] = move.wasImmobilized;
- this.board[i][j] =
- this.getColor(i, j) + V.IMMOBILIZE_CODE[this.getPiece(i, j)];
- }
- if (!!move.wasInvisible) {
- const [i, j] = move.wasInvisible;
- this.board[i][j] = this.getColor(i, j) + V.INVISIBLE_QUEEN;
- }
- if (move.vanish.length == 2 && move.vanish[1].c != 'a') {
- let capturedPiece = move.vanish[1].p;
- if (capturedPiece == V.INVISIBLE_QUEEN) capturedPiece = V.QUEEN;
- else if (Object.keys(V.IMMOBILIZE_DECODE).includes(capturedPiece))
- capturedPiece = V.IMMOBILIZE_DECODE[capturedPiece];
- this.captured[move.vanish[1].c][capturedPiece]--;
- }
- else if (move.vanish.length == 0) {
- if (move.appear.length == 0 || move.appear[0].c == 'a') return;
- // A piece was back on board
- this.captured[move.appear[0].c][move.appear[0].p]++;
+ // Helper to set and apply banana/bomb effect
+ getRandomSquare([x, y], steps, freeSquare) {
+ let validSteps = steps.filter(s => this.onBoard(x + s[0], y + s[1]));
+ if (freeSquare) {
+ // Square to put banana/bomb cannot be occupied by a piece
+ validSteps = validSteps.filter(s => {
+ return ["", 'a'].includes(this.getColor(x + s[0], y + s[1]))
+ });
}
- else if (move.appear.length == 0 && move.end.effect == "chomp")
- this.captured[move.vanish[0].c][move.vanish[0].p]--;
- if (move.vanish.length == 0) this.reserve = this.captured;
- else this.reserve = undefined;
- }
-
- getCheckSquares() {
- return [];
+ if (validSteps.length == 0)
+ return null;
+ const step = validSteps[Random.randInt(validSteps.length)];
+ return [x + step[0], y + step[1]];
}
- getCurrentScore() {
- // Find kings (not tracked in this variant)
- let kingThere = { w: false, b: false };
- for (let i=0; i<8; i++) {
- for (let j=0; j<8; j++) {
- if (
- this.board[i][j] != V.EMPTY &&
- ['k', 'l'].includes(this.getPiece(i, j))
- ) {
- kingThere[this.getColor(i, j)] = true;
+ getEggEffect(move) {
+ const getRandomPiece = (c) => {
+ let bagOfPieces = [];
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ if (this.getColor(i, j) == c && this.getPiece(i, j) != 'k')
+ bagOfPieces.push([i, j]);
}
}
+ if (bagOfPieces.length >= 1)
+ return Random.sample(bagOfPieces);
+ return null;
+ };
+ const color = this.turn;
+ let em = null;
+ switch (move.egg) {
+ case "luigi":
+ case "waluigi":
+ // Change color of friendly or enemy piece, king excepted
+ const oldColor = (move.egg == "waluigi" ? color : C.GetOppCol(color));
+ const newColor = C.GetOppCol(oldColor);
+ const coords = getRandomPiece(oldColor);
+ if (coords) {
+ const piece = this.getPiece(coords[0], coords[1]);
+ em = new Move({
+ appear: [
+ new PiPo({x: coords[0], y: coords[1], c: newColor, p: piece})
+ ],
+ vanish: [
+ new PiPo({x: coords[0], y: coords[1], c: oldColor, p: piece})
+ ]
+ });
+ em.luigiEffect = true; //avoid dropping bomb/banana by mistake
+ }
+ break;
+ case "bowser":
+ em = new Move({
+ appear: [
+ new PiPo({
+ x: move.end.x,
+ y: move.end.y,
+ c: color,
+ p: V.IMMOBILIZE_CODE[move.appear[0].p]
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: move.end.x,
+ y: move.end.y,
+ c: color,
+ p: move.appear[0].p
+ })
+ ]
+ });
+ break;
+ case "koopa":
+ // Reverse move
+ em = new Move({
+ appear: [
+ new PiPo({
+ x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
+ })
+ ]
+ });
+ if (this.board[move.start.x][move.start.y] != "") {
+ // Pawn or knight let something on init square
+ em.vanish.push(new PiPo({
+ x: move.start.x,
+ y: move.start.y,
+ c: 'a',
+ p: this.getPiece(move.start.x, move.start.y)
+ }));
+ }
+ break;
+ case "chomp":
+ // Eat piece
+ em = new Move({
+ appear: [],
+ vanish: [
+ new PiPo({
+ x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
+ })
+ ],
+ end: {x: move.end.x, y: move.end.y}
+ });
+ break;
}
- if (!kingThere['w']) return "0-1";
- if (!kingThere['b']) return "1-0";
- if (!this.atLeastOneMove()) return (this.turn == 'w' ? "0-1" : "1-0");
- return "*";
+ if (em && move.egg != "koopa")
+ em.noAnimate = true; //static move
+ return em;
}
- static GenRandInitFen(options) {
- return (
- SuicideRules.GenRandInitFen(options).slice(0, -1) +
- // Add Peach + Mario flags + capture counts
- "1111 000000000000"
- );
+ getMushroomEffect(move) {
+ if (
+ typeof move.start.x == "string" || //drop move (toadette)
+ ['b', 'r', 'q'].includes(move.vanish[0].p) //slider
+ ) {
+ return null;
+ }
+ let step = [move.end.x - move.start.x, move.end.y - move.start.y];
+ if (Math.abs(step[0]) == 2 && Math.abs(step[1]) == 0)
+ // Pawn initial 2-squares move: normalize step
+ step[0] /= 2;
+ const nextSquare = [move.end.x + step[0], move.end.y + step[1]];
+ let nextMove = null;
+ if (
+ this.onBoard(nextSquare[0], nextSquare[1]) &&
+ (
+ this.board[nextSquare[0]][nextSquare[1]] == "" ||
+ this.getColor(nextSquare[0], nextSquare[1]) == 'a'
+ )
+ ) {
+ this.playOnBoard(move); //HACK for getBasicMove()
+ nextMove = this.getBasicMove([move.end.x, move.end.y], nextSquare);
+ this.undoOnBoard(move);
+ }
+ return nextMove;
}
- filterValid(moves) {
- return moves;
+ getBombBananaEffect(move, item) {
+ const steps = item == V.BANANA
+ ? [[1, 0], [-1, 0], [0, 1], [0, -1]]
+ : [[1, 1], [1, -1], [-1, 1], [-1, -1]];
+ const nextSquare = this.getRandomSquare([move.end.x, move.end.y], steps);
+ this.playOnBoard(move); //HACK for getBasicMove()
+ const res = this.getBasicMove([move.end.x, move.end.y], nextSquare);
+ this.undoOnBoard(move);
+ return res;
}
- static get VALUES() {
- return Object.assign(
- {},
- ChessRules.VALUES,
- {
- s: 1,
- u: 5,
- o: 3,
- c: 3,
- t: 9,
- l: 1000,
- e: 0,
- d: 0,
- w: 0,
- m: 0
- }
- );
+ displayBonus(move) {
+ let divBonus = document.createElement("div");
+ divBonus.classList.add("bonus-text");
+ divBonus.innerHTML = move.egg;
+ let container = document.getElementById(this.containerId);
+ container.appendChild(divBonus);
+ setTimeout(() => container.removeChild(divBonus), 2000);
}
- static get SEARCH_DEPTH() {
- return 1;
+ atLeastOneMove() {
+ return true;
}
- getComputerMove() {
- const moves = this.getAllValidMoves();
- // Split into "normal" and "random" moves:
- // (Next splitting condition is OK because cannot take self object
- // without a banana or bomb on the way).
- const deterministicMoves = moves.filter(m => {
- return m.vanish.every(a => a.c != 'a' || a.p == V.MUSHROOM);
- });
- const randomMoves = moves.filter(m => {
- return m.vanish.some(a => a.c == 'a' && a.p != V.MUSHROOM);
- });
- if (Math.random() < deterministicMoves.length / randomMoves.length)
- // Play a deterministic one: capture king or material if possible
- return super.getComputerMove(deterministicMoves);
- // Play a random effect move, at random:
- let move1 = randomMoves[randInt(randomMoves.length)];
- this.play(move1);
- let move2 = undefined;
- if (this.subTurn == 2) {
- const moves2 = this.getAllValidMoves();
- move2 = moves2[randInt(moves2.length)];
- }
- this.undo(move1);
- if (!move2) return move1;
- return [move1, move2];
+ filterValid(moves) {
+ return moves;
}
- getNotation(move) {
- if (move.vanish.length == 0 && move.appear.length == 0) return "-";
- if (
- !move.end.effect &&
- move.appear.length > 0 &&
- move.appear[0].p == V.INVISIBLE_QUEEN
- ) {
- return "Q??";
- }
- const finalSquare = V.CoordsToSquare(move.end);
- // Next condition also includes Toadette placements:
- if (move.appear.length > 0 && move.vanish.every(a => a.c == 'a')) {
- const piece =
- move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : "";
- return piece + "@" + finalSquare;
- }
- else if (move.appear.length == 0) {
- const piece = this.getPiece(move.start.x, move.start.y);
- if (piece == V.KING && !move.end.effect)
- // King remote capture
- return "Kx" + finalSquare;
- // Koopa or Chomp, or loopback after bananas, bombs & mushrooms:
- return (
- piece.toUpperCase() + "x" + finalSquare +
- (
- !!move.end.effect
- ? "*" + (move.end.effect == "koopa" ? "K" : "C")
- : ""
- )
- );
- }
- if (move.appear.length == 1 && move.vanish.length == 1) {
- const moveStart = move.appear[0].p.toUpperCase() + "@";
- if (move.appear[0].c == 'a' && move.vanish[0].c == 'a')
- // Bonus replacement:
- return moveStart + finalSquare;
- if (
- move.vanish[0].p == V.INVISIBLE_QUEEN &&
- move.appear[0].x == move.vanish[0].x &&
- move.appear[0].y == move.vanish[0].y
- ) {
- // Toadette takes invisible queen
- return moveStart + "Q" + finalSquare;
- }
- }
- if (
- move.appear.length == 2 &&
- move.vanish.length == 2 &&
- move.appear.every(a => a.c != 'a') &&
- move.vanish.every(v => v.c != 'a')
- ) {
- // King Boo exchange
- return V.CoordsToSquare(move.start) + finalSquare;
- }
- const piece = move.vanish[0].p;
- let notation = undefined;
- if (piece == V.PAWN) {
- // Pawn move
- if (this.board[move.end.x][move.end.y] != V.EMPTY) {
- // Capture
- const startColumn = V.CoordToColumn(move.start.y);
- notation = startColumn + "x" + finalSquare;
- }
- else notation = finalSquare;
- if (move.appear[0].p != V.PAWN)
- // Promotion
- notation += "=" + move.appear[0].p.toUpperCase();
- }
- else {
- notation =
- piece.toUpperCase() +
- (this.board[move.end.x][move.end.y] != V.EMPTY ? "x" : "") +
- finalSquare;
- }
- if (!!move.end.effect) {
- switch (move.end.effect) {
- case "kingboo":
- notation += "*B";
- break;
- case "toadette":
- notation += "*T";
- break;
- case "daisy":
- notation += "*D";
- break;
- case "bowser":
- notation += "*M";
- break;
- case "luigi":
- case "waluigi":
- const lastAppear = move.appear[move.appear.length - 1];
- const effectOn =
- V.CoordsToSquare({ x: lastAppear.x, y : lastAppear.y });
- notation += "*" + move.end.effect[0].toUpperCase() + effectOn;
- break;
- }
- }
- return notation;
+ // Kingboo bonus can be animated better:
+ customAnimate(move, segments, cb) {
+ if (!move.kingboo)
+ return 0;
+ super.animateMoving(move.end, move.start, null,
+ segments.reverse().map(s => s.reverse()), cb);
+ return 1;
}
};