- // 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);
- break;
- case V.MUSHROOM:
- applyMushroomEffect();
- break;
- case V.EGG:
- if (this.subTurn == 1)
- // No egg effect at subTurn 2
- applyEggEffect();
- 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]);
- }
- }