X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FLosers.js;h=ea2838096ff8b1d5196d58a2d6a53f312c231e37;hb=0fb43db7c2858201e8410670b90f95ad8b138964;hp=d815ee7a501668dc1136c85619ee94df403668be;hpb=dac395887d96e2d642b209c6db6aaacc3ffacb34;p=vchess.git diff --git a/client/src/variants/Losers.js b/client/src/variants/Losers.js index d815ee7a..ea283809 100644 --- a/client/src/variants/Losers.js +++ b/client/src/variants/Losers.js @@ -2,191 +2,76 @@ import { ChessRules } from "@/base_rules"; import { ArrayFun } from "@/utils/array"; import { randInt } from "@/utils/alea"; -export const VariantRules = class LosersRules extends ChessRules -{ - static get HasFlags() { return false; } +export class LosersRules extends ChessRules { - getPotentialPawnMoves([x,y]) - { - let moves = super.getPotentialPawnMoves([x,y]); - - // Complete with promotion(s) into king, if possible - const color = this.turn; - const shift = (color == "w" ? -1 : 1); - const lastRank = (color == "w" ? 0 : V.size.x-1); - if (x+shift == lastRank) - { - // Normal move - if (this.board[x+shift][y] == V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING})); - // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) - && this.board[x+shift][y-1] != V.EMPTY) - { - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:V.KING})); - } - if (y m.vanish.length == 2 && m.appear.length == 1); } // Stop at the first capture found (if any) - atLeastOneCapture() - { + atLeastOneCapture() { const color = this.turn; - const oppCol = V.GetOppCol(color); - for (let i=0; i 0) - { - for (let k=0; k 0) - return true; - } - } + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if ( + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color && + this.getPotentialMovesFrom([i, j]).some(m => { + return ( + // Warning: discard castle moves + m.vanish.length == 2 && m.appear.length == 1 && + this.filterValid([m]).length == 1 + ); + }) + ) { + return true; } } } return false; } - // Trim all non-capturing moves - static KeepCaptures(moves) - { - return moves.filter(m => { return m.vanish.length == 2; }); - } - - getPossibleMovesFrom(sq) - { - let moves = this.filterValid( this.getPotentialMovesFrom(sq) ); - // This is called from interface: we need to know if a capture is possible - if (this.atLeastOneCapture()) - moves = V.KeepCaptures(moves); + getPossibleMovesFrom(sq) { + let moves = this.filterValid(this.getPotentialMovesFrom(sq)); + const captureMoves = V.KeepCaptures(moves); + if (captureMoves.length > 0) return captureMoves; + if (this.atLeastOneCapture()) return []; return moves; } - getAllValidMoves() - { - let moves = super.getAllValidMoves(); - if (moves.some(m => { return m.vanish.length == 2; })) - moves = V.KeepCaptures(moves); + getAllValidMoves() { + const moves = super.getAllValidMoves(); + if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1)) + return V.KeepCaptures(moves); return moves; } - underCheck(color) - { - return false; //No notion of check - } - - getCheckSquares(move) - { - return []; - } - - // No variables update because no royal king + no castling - updateVariables(move) { } - unupdateVariables(move) { } - - getCurrentScore() - { - if (this.atLeastOneMove()) // game not over - return "*"; - - // No valid move: the side who cannot move wins - return (this.turn == "w" ? "1-0" : "0-1"); - } - - static get VALUES() - { - // Experimental... - return { - 'p': 1, - 'r': 7, - 'n': 3, - 'b': 3, - 'q': 5, - 'k': 5 - }; + getCurrentScore() { + // If only my king remains, I win + const color = this.turn; + let onlyKing = true; + outerLoop: for (let i=0; i