X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FFootball.js;h=98e2372ea9a1708c036a564e8a332711fe583f44;hb=5c6c52ac1c7bbb2a58c60bf5f389e1be4b76b0c1;hp=b1630eac24bbedd3e734a0489df19794b3615476;hpb=4f3a08234f754abcb74f369067f960a8269557a3;p=vchess.git diff --git a/client/src/variants/Football.js b/client/src/variants/Football.js index b1630eac..98e2372e 100644 --- a/client/src/variants/Football.js +++ b/client/src/variants/Football.js @@ -1,5 +1,6 @@ import { ChessRules } from "@/base_rules"; -import { randInt } from "@/utils/alea"; +import { randInt, shuffle } from "@/utils/alea"; +import { ArrayFun } from "@/utils/array"; export class FootballRules extends ChessRules { @@ -107,7 +108,6 @@ export class FootballRules extends ChessRules { if (randomness == 0) return "rnbq1knbr/9/9/9/4a4/9/9/9/RNBQ1KNBR w 0"; - // TODO: following is mostly copy-paste from Suicide variant let pieces = { w: new Array(8), b: new Array(8) }; for (let c of ["w", "b"]) { if (c == 'b' && randomness == 1) { @@ -118,12 +118,16 @@ export class FootballRules extends ChessRules { // Get random squares for every piece, totally freely let positions = shuffle(ArrayFun.range(8)); const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; - const rem2 = positions[0] % 2; - if (rem2 == positions[1] % 2) { - // Fix bishops (on different colors) + // Fix bishops (on different colors) + const realOddity = + (pos) => { return (pos <= 3 ? pos % 2 : (pos + 1) % 2); }; + const rem2 = realOddity(positions[0]); + if (rem2 == realOddity(positions[1])) { for (let i=2; i<8; i++) { - if (positions[i] % 2 != rem2) + if (realOddity(positions[i]) != rem2) { [positions[1], positions[i]] = [positions[i], positions[1]]; + break; + } } } for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; @@ -187,15 +191,45 @@ export class FootballRules extends ChessRules { let [i, j] = [bp[0] + step[0], bp[1] + step[1]]; const horizontalStepOnGoalRow = ([0, 8].includes(bp[0]) && step.some(s => s == 0)); - if (emptySquare(i, j) && (!horizontalStepOnGoalRow || j != 4)) { + if ( + emptySquare(i, j) && + (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && + (!horizontalStepOnGoalRow || j != 4) + ) { moves.push(super.getBasicMove(bp, [i, j])); if (!oneStep) { do { i += step[0]; j += step[1]; if (!emptySquare(i, j)) break; - if (!horizontalStepOnGoalRow || j != 4) + if ( + (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && + (!horizontalStepOnGoalRow || j != 4) + ) { moves.push(super.getBasicMove(bp, [i, j])); + } + } while (true); + } + } + // Try the other direction (TODO: experimental) + [i, j] = [bp[0] - 2*step[0], bp[1] - 2*step[1]]; + if ( + emptySquare(i, j) && + (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && + (!horizontalStepOnGoalRow || j != 4) + ) { + moves.push(super.getBasicMove(bp, [i, j])); + if (!oneStep) { + do { + i -= step[0]; + j -= step[1]; + if (!emptySquare(i, j)) break; + if ( + (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && + (!horizontalStepOnGoalRow || j != 4) + ) { + moves.push(super.getBasicMove(bp, [i, j])); + } } while (true); } } @@ -206,12 +240,43 @@ export class FootballRules extends ChessRules { } getPotentialMovesFrom([x, y], computer) { - if (V.PIECES.includes(this.getPiece(x, y))) { + const piece = this.getPiece(x, y); + if (V.PIECES.includes(piece)) { if (this.subTurn > 1) return []; - return ( - super.getPotentialMovesFrom([x, y]) - .filter(m => m.end.y != 4 || ![0, 8].includes(m.end.x)) - ); + const moves = super.getPotentialMovesFrom([x, y]) + .filter(m => m.end.y != 4 || ![0, 8].includes(m.end.x)); + // If bishop stuck in a corner: allow to jump over the next obstacle + if (moves.length == 0 && piece == V.BISHOP) { + if ( + x == 0 && y == 0 && + this.board[1][1] != V.EMPTY && + this.board[2][2] == V.EMPTY + ) { + return [super.getBasicMove([x, y], [2, 2])]; + } + if ( + x == 0 && y == 8 && + this.board[1][7] != V.EMPTY && + this.board[2][6] == V.EMPTY + ) { + return [super.getBasicMove([x, y], [2, 6])]; + } + if ( + x == 8 && y == 0 && + this.board[7][1] != V.EMPTY && + this.board[6][2] == V.EMPTY + ) { + return [super.getBasicMove([x, y], [6, 2])]; + } + if ( + x == 8 && y == 8 && + this.board[7][7] != V.EMPTY && + this.board[6][6] == V.EMPTY + ) { + return [super.getBasicMove([x, y], [6, 6])]; + } + } + return moves; } // Kicking the ball: look for adjacent pieces. const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);