X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fbase_rules.js;h=e9ea7bc10e189f285100e5337b786f6b07805611;hb=9d15c433c207a2c3bb548d095939c3e08b4038fd;hp=d17ebe67a075bf77484e6923ce0726266268c3be;hpb=7e8a7ea1cb66adb4a987badfb0a3c2f99a21bd0a;p=vchess.git diff --git a/client/src/base_rules.js b/client/src/base_rules.js index d17ebe67..e9ea7bc1 100644 --- a/client/src/base_rules.js +++ b/client/src/base_rules.js @@ -112,6 +112,11 @@ export const ChessRules = class ChessRules { return false; } + // Some games are drawn unusually (bottom right corner is black) + static get DarkBottomRight() { + return false; + } + // Some variants require lines drawing static get Lines() { if (V.Monochrome) { @@ -126,6 +131,11 @@ export const ChessRules = class ChessRules { return null; } + // In some variants, the player who repeat a position loses + static get LoseOnRepetition() { + return false; + } + // Some variants use click infos: doClick() { return null; @@ -148,7 +158,7 @@ export const ChessRules = class ChessRules { // Turn "p" into "bp" (for board) static fen2board(f) { - return f.charCodeAt() <= 90 ? "w" + f.toLowerCase() : "b" + f; + return f.charCodeAt(0) <= 90 ? "w" + f.toLowerCase() : "b" + f; } // Check if FEN describes a board situation correctly @@ -187,7 +197,7 @@ export const ChessRules = class ChessRules { if (V.PIECES.includes(row[i].toLowerCase())) sumElts++; else { const num = parseInt(row[i], 10); - if (isNaN(num)) return false; + if (isNaN(num) || num <= 0) return false; sumElts += num; } } @@ -265,7 +275,7 @@ export const ChessRules = class ChessRules { // En-passant square, if any getEpSquare(moveOrSquare) { - if (!moveOrSquare) return undefined; + if (!moveOrSquare) return undefined; //TODO: necessary line?! if (typeof moveOrSquare === "string") { const square = moveOrSquare; if (square == "-") return undefined; @@ -518,6 +528,7 @@ export const ChessRules = class ChessRules { } // Scan board for kings positions + // TODO: should be done from board, no need for the complete FEN scanKings(fen) { // Squares of white and black king: this.kingPos = { w: [-1, -1], b: [-1, -1] }; @@ -643,20 +654,14 @@ export const ChessRules = class ChessRules { // MOVES GENERATION // All possible moves from selected square - getPotentialMovesFrom([x, y]) { - switch (this.getPiece(x, y)) { - case V.PAWN: - return this.getPotentialPawnMoves([x, y]); - case V.ROOK: - return this.getPotentialRookMoves([x, y]); - case V.KNIGHT: - return this.getPotentialKnightMoves([x, y]); - case V.BISHOP: - return this.getPotentialBishopMoves([x, y]); - case V.QUEEN: - return this.getPotentialQueenMoves([x, y]); - case V.KING: - return this.getPotentialKingMoves([x, y]); + getPotentialMovesFrom(sq) { + switch (this.getPiece(sq[0], sq[1])) { + case V.PAWN: return this.getPotentialPawnMoves(sq); + case V.ROOK: return this.getPotentialRookMoves(sq); + case V.KNIGHT: return this.getPotentialKnightMoves(sq); + case V.BISHOP: return this.getPotentialBishopMoves(sq); + case V.QUEEN: return this.getPotentialQueenMoves(sq); + case V.KING: return this.getPotentialKingMoves(sq); } return []; //never reached } @@ -671,8 +676,8 @@ export const ChessRules = class ChessRules { new PiPo({ x: ex, y: ey, - c: tr ? tr.c : initColor, - p: tr ? tr.p : initPiece + c: !!tr ? tr.c : initColor, + p: !!tr ? tr.p : initPiece }) ], vanish: [ @@ -888,7 +893,6 @@ export const ChessRules = class ChessRules { // Castling ? const oppCol = V.GetOppCol(c); let moves = []; - let i = 0; // King, then rook: finalSquares = finalSquares || [ [2, 3], [V.size.y - 2, V.size.y - 3] ]; const castlingKing = this.board[x][y].charAt(1); @@ -915,7 +919,7 @@ export const ChessRules = class ChessRules { // Nothing on the path of the king ? (and no checks) const finDist = finalSquares[castleSide][0] - y; let step = finDist / Math.max(1, Math.abs(finDist)); - i = y; + let i = y; do { if ( (!castleInCheck && this.isAttacked([x, i], oppCol)) || @@ -969,7 +973,7 @@ export const ChessRules = class ChessRules { ], vanish: [ // King might be initially disguised (Titan...) - new PiPo({ x: x, y: y, p: this.board[x][y][1], c: c }), + new PiPo({ x: x, y: y, p: castlingKing, c: c }), new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c }) ], end: @@ -1067,8 +1071,10 @@ export const ChessRules = class ChessRules { } if ( V.OnBoard(rx, ry) && + this.board[rx][ry] != V.EMPTY && this.getPiece(rx, ry) == piece && - this.getColor(rx, ry) == color + this.getColor(rx, ry) == color && + this.canTake([rx, ry], [x, y]) //for Paco-Sako (TODO: necessary?) ) { return true; } @@ -1168,6 +1174,7 @@ export const ChessRules = class ChessRules { } updateCastleFlags(move, piece, color) { + // TODO: check flags. If already off, no need to always re-evaluate const c = color || V.GetOppCol(this.turn); const firstRank = (c == "w" ? V.size.x - 1 : 0); // Update castling flags if rooks are moved