X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FLosers.js;h=81d2730a97976ba3af711a3826d3975d4358b9b4;hp=d815ee7a501668dc1136c85619ee94df403668be;hb=6808d7a16ec1e761c6a2dffec2281c96953e4d89;hpb=ae2c49bb0bbaac3953f63be5b720e9c6835f00b6 diff --git a/client/src/variants/Losers.js b/client/src/variants/Losers.js index d815ee7a..81d2730a 100644 --- a/client/src/variants/Losers.js +++ b/client/src/variants/Losers.js @@ -2,63 +2,71 @@ 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 const VariantRules = class LosersRules extends ChessRules { + static get HasFlags() { + return false; + } - getPotentialPawnMoves([x,y]) - { - let moves = super.getPotentialPawnMoves([x,y]); + 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) - { + 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})); + 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 > 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 0) - { - for (let k=0; k 0) + 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) != oppCol) { + const moves = this.getPotentialMovesFrom([i, j]); + if (moves.length > 0) { + for (let k = 0; k < moves.length; k++) { + if ( + moves[k].vanish.length == 2 && + this.filterValid([moves[k]]).length > 0 + ) return true; } } @@ -69,77 +77,75 @@ export const VariantRules = class LosersRules extends ChessRules } // Trim all non-capturing moves - static KeepCaptures(moves) - { - return moves.filter(m => { return m.vanish.length == 2; }); + static KeepCaptures(moves) { + return moves.filter(m => { + return m.vanish.length == 2; + }); } - getPossibleMovesFrom(sq) - { - let moves = this.filterValid( this.getPotentialMovesFrom(sq) ); + 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); + if (this.atLeastOneCapture()) moves = V.KeepCaptures(moves); return moves; } - getAllValidMoves() - { + getAllValidMoves() { let moves = super.getAllValidMoves(); - if (moves.some(m => { return m.vanish.length == 2; })) + if ( + moves.some(m => { + return m.vanish.length == 2; + }) + ) moves = V.KeepCaptures(moves); return moves; } - underCheck(color) - { + underCheck() { return false; //No notion of check } - getCheckSquares(move) - { + getCheckSquares() { return []; } // No variables update because no royal king + no castling - updateVariables(move) { } - unupdateVariables(move) { } + updateVariables() {} + unupdateVariables() {} - getCurrentScore() - { - if (this.atLeastOneMove()) // game not over + 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"); + return this.turn == "w" ? "1-0" : "0-1"; } - static get VALUES() - { + static get VALUES() { // Experimental... return { - 'p': 1, - 'r': 7, - 'n': 3, - 'b': 3, - 'q': 5, - 'k': 5 + p: 1, + r: 7, + n: 3, + b: 3, + q: 5, + k: 5 }; } - static get SEARCH_DEPTH() { return 4; } + static get SEARCH_DEPTH() { + return 4; + } - evalPosition() - { - return - super.evalPosition(); //better with less material + evalPosition() { + return -super.evalPosition(); //better with less material } - static GenRandInitFen() - { - let pieces = { "w": new Array(8), "b": new Array(8) }; + static GenRandInitFen() { + let pieces = { w: new Array(8), b: new Array(8) }; // Shuffle pieces on first and last rank - for (let c of ["w","b"]) - { + for (let c of ["w", "b"]) { let positions = ArrayFun.range(8); // Get random squares for bishops @@ -149,8 +155,8 @@ export const VariantRules = class LosersRules extends ChessRules let randIndex_tmp = 2 * randInt(4) + 1; let bishop2Pos = positions[randIndex_tmp]; // Remove chosen squares - positions.splice(Math.max(randIndex,randIndex_tmp), 1); - positions.splice(Math.min(randIndex,randIndex_tmp), 1); + positions.splice(Math.max(randIndex, randIndex_tmp), 1); + positions.splice(Math.min(randIndex, randIndex_tmp), 1); // Get random squares for knights randIndex = randInt(6); @@ -175,18 +181,20 @@ export const VariantRules = class LosersRules extends ChessRules let rook2Pos = positions[1]; // Finally put the shuffled pieces in the board array - pieces[c][rook1Pos] = 'r'; - pieces[c][knight1Pos] = 'n'; - pieces[c][bishop1Pos] = 'b'; - pieces[c][queenPos] = 'q'; - pieces[c][kingPos] = 'k'; - pieces[c][bishop2Pos] = 'b'; - pieces[c][knight2Pos] = 'n'; - pieces[c][rook2Pos] = 'r'; + pieces[c][rook1Pos] = "r"; + pieces[c][knight1Pos] = "n"; + pieces[c][bishop1Pos] = "b"; + pieces[c][queenPos] = "q"; + pieces[c][kingPos] = "k"; + pieces[c][bishop2Pos] = "b"; + pieces[c][knight2Pos] = "n"; + pieces[c][rook2Pos] = "r"; } - return pieces["b"].join("") + + return ( + pieces["b"].join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + pieces["w"].join("").toUpperCase() + - " w 0 -"; //en-passant allowed, but no flags + " w 0 -" + ); //en-passant allowed, but no flags } -} +};