X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=variants%2FChakart%2Fclass.js;h=cee02423af311fa7cfafea8383e7363984f45bab;hb=24872b22ca84fc01cc1d47247580e4e8280fff1e;hp=04036246d4fe4e0054c0ec8026fce6c5e9f17a49;hpb=c7c2f41c6f3e1aaf02a8539afd85336c9d923213;p=xogo.git diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js index 0403624..cee0242 100644 --- a/variants/Chakart/class.js +++ b/variants/Chakart/class.js @@ -1,11 +1,11 @@ -import ChessRules from "/base_rules"; -import GiveawayRules from "/variants/Giveaway"; +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 { @@ -15,9 +15,9 @@ export class ChakartRules extends ChessRules { 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} ] } ] @@ -34,6 +34,12 @@ export class ChakartRules extends ChessRules { get hasEnpassant() { return false; } + get hasReserve() { + return true; + } + get hasReserveFen() { + return false; + } static get IMMOBILIZE_CODE() { return { @@ -75,13 +81,28 @@ export class ChakartRules extends ChessRules { return 'm'; } + static get EGG_SURPRISE() { + return [ + "kingboo", "bowser", "daisy", "koopa", + "luigi", "waluigi", "toadette", "chomp"]; + } + + pieces(color, x, y) { + let specials = { + 'i': {"class": "invisible"}, //queen + 'e': {"class": "egg"}, + 'm': {"class": "mushroom"}, + 'd': {"class": "banana"}, + 'w': {"class": "bomb"} + }; + return Object.assign(specials, super.pieces(color, x, y)); + } + genRandInitFen(seed) { - const gr = new GiveawayRules({mode: "suicide"}, true); - return ( - gr.genRandInitFen(seed).slice(0, -1) + - // Add Peach + Mario flags + capture counts - '{"flags": "1111", "ccount": "000000000000"}' - ); + const gr = new GiveawayRules( + {mode: "suicide", options: {}, genFenOnly: true}); + // Add Peach + mario flags + return gr.genRandInitFen(seed).slice(0, -17) + '{"flags":"1111"}'; } fen2board(f) { @@ -114,39 +135,22 @@ export class ChakartRules extends ChessRules { this.powerFlags = flags; } - getFen() { - return super.getFen() + " " + this.getCapturedFen(); - } - getFlagsFen() { return ['w', 'b'].map(c => { return ['k', 'q'].map(p => this.powerFlags[c][p] ? "1" : "0").join(""); }).join(""); } - getCapturedFen() { - const res = ['w', 'b'].map(c => { - Object.values(this.captured[c]) - }); - return res[0].concat(res[1]).join(""); - } - setOtherVariables(fenParsed) { - super.setOtherVariables(fenParsed); - // Initialize captured pieces' counts from FEN - const allCapts = fenParsed.captured.split("").map(x => parseInt(x, 10)); - const pieces = ['p', 'r', 'n', 'b', 'q', 'k']; - this.captured = { - w: Array.toObject(pieces, allCapts.slice(0, 6)), - b: Array.toObject(pieces, allCapts.slice(6, 12)) - }; - this.reserve = { w: {}, b: {} }; //to be replaced by this.captured - this.effect = ""; + this.setFlags(fenParsed.flags); + this.reserve = {}; //to be filled later + this.egg = null; + this.moveStack = []; } // For Toadette bonus getDropMovesFrom([c, p]) { - if (this.reserve[c][p] == 0) + if (typeof c != "string" || this.reserve[c][p] == 0) return []; let moves = []; const start = (c == 'w' && p == 'p' ? 1 : 0); @@ -175,600 +179,316 @@ export class ChakartRules extends ChessRules { return moves; } - - - - - - - - - -// TODO: rethink from here: - getPotentialMovesFrom([x, y]) { let moves = []; - if (this.subTurn == 1) { - moves = super.getPotentialMovesFrom([x, y]); - const finalPieces = V.PawnSpecs.promotions; - 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); + if (this.egg == "toadette") + moves = this.getDropMovesFrom([x, y]); + else if (this.egg == "kingboo") { + const initPiece = this.getPiece(x, y); + const color = this.getColor(x, y); + const oppCol = C.GetOppCol(color); + // Only allow to swap pieces + for (let i=0; i= V.size.x) moves = this.getReserveMoves([x, y]); + case 'k': + moves = this.getKingMovesFrom([x, y]); break; - case "daisy": - // Play again with any piece - moves = super.getPotentialMovesFrom([x, y]); + case 'n': + moves = this.getKnightMovesFrom([x, y]); + break; + case 'b': + case 'r': + // explicitely listing types to avoid moving immobilized piece + moves = this.getRookOrBishopMovesFrom([x, y], piece); break; } } + // Set potential random effects, so that play() is deterministic + moves.forEach(m => { + switch (this.getPiece(m.end.x, m.end.y)) { + case V.EGG: + m.egg = Random.sample(V.EGG_SURPRISE); + m.next = this.getEggEffect(m); + break; + case V.BANANA: + m.next = this.getRandomSquare( + [m.end.x, m.end.y], [[1, 1], [1, -1], [-1, 1], [-1, -1]]); + break; + case V.BOMB: + m.next = this.getRandomSquare( + [m.end.x, m.end.y], [[1, 0], [-1, 0], [0, 1], [0, -1]]); + 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); + getEggEffect(move) { + const getRandomPiece = (c) => { + let bagOfPieces = []; + for (let i=0; i= 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}) + ] + }); + } + break; + case "bowser": + em = new Move({ + appear: [ new PiPo({ - x: mushroomX, - y: y1, - c: 'a', - p: V.MUSHROOM + x: move.end.x, + y: move.end.y, + c: color, + p: V.IMMOBILIZED_CODE[move.appear[0].p] }) - ); - 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) - ]; - if ( - this.board[eggSquare[0]][eggSquare[1]] != V.EMPTY && - this.getColor(eggSquare[0], eggSquare[1]) != 'a' - ) { - eggSquare[0] = x1; - eggSquare[1] = y1; - } - move.appear.push( + ], + vanish: [ new PiPo({ - x: eggSquare[0], - y: eggSquare[1], - c: 'a', - p: V.EGG + x: move.end.x, + y: move.end.y, + c: color, + p: move.appear[0].p }) - ); - 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); - 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( + ] + }); + break; + case "koopa": + // Reverse move + em = new Move({ + appear: [ new PiPo({ - x: x, - y: y, - c: 'a', - p: (piece1 == V.ROOK ? V.BANANA : V.BOMB) + x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p }) - ); - if (this.board[x][y] != V.EMPTY) { - move.vanish.push( - new PiPo({ x: x, y: y, c: 'a', p: this.getPiece(x, y) })); - } - } - } + ], + vanish: [ + new PiPo({ + x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p + }) + ] + }); + 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; } - return move; + return em; } - 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; + // 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 { - // 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' && + if (validSteps.length == 0) + return null; + const step = validSteps[Random.randInt(validSteps.length)]; + return [x + step[0], y + step[1]]; + } + + getPotentialMovesOf(piece, [x, y]) { + const color = this.getColor(x, y); + const stepSpec = this.pieces(color, x, y)[piece]; + let moves = []; + const findAddMoves = (type, stepArray) => { + for (let s of stepArray) { + outerLoop: for (let step of s.steps) { + let [i, j] = [x + step[0], y + step[1]]; + let stepCounter = 1; + while ( + this.onBoard(i, j) && ( - moves[0].appear.length == 1 || - moves[0].appear[1].x != v.x || - moves[0].appear[1].y != v.y + this.board[i][j] == "" || + [V.MUSHROOM, V.EGG].includes(this.getPiece(i, j)) ) ) { - move.vanish.push(v); + if (type != "attack") + moves.push(this.getBasicMove([x, y], [i, j])); + if (s.range <= stepCounter++) + continue outerLoop; + [i, j] = [i + step[0], j + step[1]]; } + if (!this.onBoard(i, j)) + continue; + const pieceIJ = this.getPiece(i, j); + if (type != "moveonly" && this.getColor(i, j) != color) + moves.push(this.getBasicMove([x, y], [i, j])); } } - // 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; + }; + const specialAttack = !!stepSpec.attack; + if (specialAttack) + findAddMoves("attack", stepSpec.attack); + findAddMoves(specialAttack ? "moveonly" : "all", stepSpec.moves); + 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 = []; 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 ) { - this.addPawnMoves([x, y], [x + shiftX, y], moves); + moves.push(this.getBasicMove([x, y], [x + shiftX, y])); if ( [firstRank, firstRank + shiftX].includes(x) && ( - 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 ) ) { - 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]) { if ( y + shiftY >= 0 && - y + shiftY < sizeY && - this.board[x + shiftX][y + shiftY] != V.EMPTY && + y + shiftY < this.size.y && + this.board[x + shiftX][y + shiftY] != "" && // 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.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves); + moves.push(this.getBasicMove([x, y], [x + shiftX, y + shiftY])); } } + this.pawnPostProcess(moves, color, oppCol); + // Add mushroom on initial square + moves.forEach(m => { + m.appear.push(new PiPo({x: m.start.x, y: m.start.y, c: 'a', p: 'm'})); + }); return moves; } - getPotentialQueenMoves(sq) { - const normalMoves = super.getPotentialQueenMoves(sq); + getRookOrBishopMovesFrom([x, y], type) { + // Add banana if possible, diagonaly + return this.getPotentialMovesOf(type, [x, y]).map(m => { + const bs = + this.getRandomSquare([m.end.x, m.end.y], + type == 'r' + ? [[1, 1], [1, -1], [-1, 1], [-1, -1]] + : [[1, 0], [-1, 0], [0, 1], [0, -1]], + "freeSquare"); + if (bs) { + m.appear.push( + new PiPo({ + x: bs[0], + y: bs[1], + c: 'a', + p: type == 'r' ? 'd' : 'w' + }) + ); + if (this.board[bs[0]][bs[1]] != "") { + m.vanish.push( + new PiPo({ + x: bs[0], + y: bs[1], + c: this.getColor(bs[0], bs[1]), + p: this.getPiece(bs[0], bs[1]) + }) + ); + } + } + return m; + }); + } + + getKnightMovesFrom([x, y]) { + // Add egg on initial square: + return this.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 = this.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 && @@ -778,7 +498,7 @@ export class ChakartRules extends ChessRules { ) { let im = JSON.parse(JSON.stringify(m)); im.appear[0].p = V.INVISIBLE_QUEEN; - im.end.noHighlight = true; + im.noAnimate = true; invisibleMoves.push(im); } }); @@ -786,17 +506,16 @@ export class ChakartRules extends ChessRules { return normalMoves.concat(invisibleMoves); } - getPotentialKingMoves([x, y]) { - let moves = super.getPotentialKingMoves([x, y]); - const color = this.turn; + getKingMovesFrom([x, y]) { + let moves = this.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 => { + if (this.powerFlags[this.turn]['k']) { + super.pieces()['k'].moves[0].steps.forEach(step => { let [i, j] = [x + step[0], y + step[1]]; while ( - V.OnBoard(i, j) && + this.onBoard(i, j) && ( - this.board[i][j] == V.EMPTY || + this.board[i][j] == "" || this.getPiece(i, j) == V.INVISIBLE_QUEEN || ( this.getColor(i, j) == 'a' && @@ -807,22 +526,20 @@ export class ChakartRules extends ChessRules { i += step[0]; j += step[1]; } - if (V.OnBoard(i, j)) { + if (this.onBoard(i, j)) { const colIJ = this.getColor(i, j); - if (colIJ != color) { + if (colIJ != this.turn) { // 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) - }) - ] - }) - ); + let shellCapture = 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)}) + ] + }); + shellCapture.shell = true; //easier play() + moves.push(shellCapture); } } }); @@ -830,191 +547,114 @@ export class ChakartRules extends ChessRules { 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])); - } - return moves; - } - - 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); - if ( - i != x && j != y && - this.board[i][j] != V.EMPTY && - colIJ != 'a' && - pieceIJ != V.INVISIBLE_QUEEN - ) { - allPieces.push({ x: i, y: j, c: colIJ, p: pieceIJ }); - } - } - } - 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); - } - }); - } - } - } - 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; - } - - - - - - - - - - - - prePlay(move) { - if (move.effect == "toadette") - this.reserve = this.captured; - else - this.reserve = { w: {}, b: {} };; + play(move) { + this.egg = move.egg; const color = this.turn; - 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]++; + if (move.egg == "toadette") { + this.reserve = { w: {}, b: {} }; + // Randomly select a piece in pawnPromotions + this.reserve[color][Random.sample(this.pawnPromotions)] = 1; + this.re_drawReserve([color]); } - 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]--; + else if (Object.keys(this.reserve).length > 0) { + this.reserve = {}; + this.re_drawReserve([color]); } - 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]++; - } - else if (move.appear[0].p == V.INVISIBLE_QUEEN) - this.powerFlags[move.appear[0].c][V.QUEEN] = false; - if (this.subTurn == 2) return; - if ( - move.turn[1] == 1 && - move.appear.length == 0 || - !(Object.keys(V.IMMOBILIZE_DECODE).includes(move.appear[0].p)) - ) { - // 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]; - } + if (this.getPiece(move.end.x, move.end.y) == V.MUSHROOM) + move.next = this.getMushroomEffect(move); + if (move.shell) + this.powerFlags[color]['k'] = false; + else if (move.appear.length > 0 && move.appear[0].p == V.INVISIBLE_QUEEN) + this.powerFlags[move.appear[0].c]['q'] = false; + this.playOnBoard(move); + // 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 ( + this.getColor(i, j) == color && + Object.keys(V.IMMOBILIZE_DECODE).includes(piece) + ) { + this.board[i][j] = color + V.IMMOBILIZE_DECODE[piece]; } } } } // Also make opponent invisible queen visible again, if any - const oppCol = V.GetOppCol(color); + const oppCol = C.GetOppCol(color); for (let i=0; i<8; i++) { for (let j=0; j<8; j++) { if ( - this.board[i][j] != V.EMPTY && + this.board[i][j] != "" && this.getColor(i, j) == oppCol && this.getPiece(i, j) == V.INVISIBLE_QUEEN ) { this.board[i][j] = oppCol + V.QUEEN; - move.wasInvisible = [i, j]; } } } - } - - play(move) { - this.prePlay(move); - this.playOnBoard(move); - if (["kingboo", "toadette", "daisy"].includes(move.effect)) { - this.effect = move.effect; - this.subTurn = 2; - } - else { - this.turn = C.GetOppCol(this.turn); + if (!move.next && !["daisy", "toadette", "kingboo"].includes(move.egg)) { + this.turn = oppCol; this.movesCount++; - this.subTurn = 1; } + if (move.egg) + this.displayBonus(move.egg); + this.nextMove = move.next; + } + + displayBonus(egg) { + alert(egg); //TODO: nicer display } filterValid(moves) { return moves; } - // TODO + display bonus messages - // + animation + multi-moves for bananas/bombs/mushrooms + getMushroomEffect(move) { + let step = [move.end.x - move.start.x, move.end.y - move.start.y]; + if ([0, 1].some(i => step[i] >= 2 && step[1-i] != 1)) { + // Slider, multi-squares: normalize step + for (let j of [0, 1]) + step[j] = step[j] / Math.abs(step[j]) || 0; + } + const nextSquare = [move.end.x + step[0], move.end.y + step[1]]; + const afterSquare = + [nextSquare[0] + step[0], nextSquare[1] + step[1]]; + let nextMove = null; + if ( + this.onBoard(nextSquare[0], nextSquare[1]) && + this.board[nextSquare[0]][nextSquare[1]] == "" && + ['k', 'p', 'n'].includes(move.vanish[0].p) + ) { + // Speed up non-sliders + nextMove = this.getBasicMove([move.end.x, move.end.y], nextSquare); + } + else if ( + this.onBoard(afterSquare[0], afterSquare[1]) && + this.board[nextSquare[0]][nextSquare[1]] != "" && + this.getColor(nextSquare[0], nextSquare[1]) != 'a' && + this.getColor(afterSquare[0], afterSquare[1]) != this.turn + ) { + nextMove = this.getBasicMove([move.end.x, move.end.y], afterSquare); + } + return nextMove; + } + + playPlusVisual(move, r) { + this.moveStack.push(move); + this.play(move); + this.playVisual(move, r); + if (this.nextMove) + this.playPlusVisual(this.nextMove, r); + else + this.afterPlay(this.moveStack); + } + + // TODO: set some special moves (effects) as noAnimate + // TODO: put bomb/banana only at final location of a move ? Seems more logical + // + fix bishop takes mushroom and jump + // Also improve showChoices for invisible queen + king shell + // + fix turn issues after multimove (like bishop put bomb) };