X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FSynchrone.js;h=365dd9aabc1a6ff9b7b0bcf806ed025a0bbe12c0;hb=7e8a7ea1cb66adb4a987badfb0a3c2f99a21bd0a;hp=ad14e0517073917ea17697241eb221c3c6aba952;hpb=2c5d7b20742b802d9c47916915c1114bcfc9a9c3;p=vchess.git diff --git a/client/src/variants/Synchrone.js b/client/src/variants/Synchrone.js index ad14e051..365dd9aa 100644 --- a/client/src/variants/Synchrone.js +++ b/client/src/variants/Synchrone.js @@ -2,6 +2,7 @@ import { ChessRules } from "@/base_rules"; import { randInt } from "@/utils/alea"; export class SynchroneRules extends ChessRules { + static get CanAnalyze() { return false; } @@ -10,6 +11,10 @@ export class SynchroneRules extends ChessRules { return "byrow"; } + static get SomeHiddenMoves() { + return true; + } + static IsGoodFen(fen) { if (!ChessRules.IsGoodFen(fen)) return false; const fenParsed = V.ParseFen(fen); @@ -74,7 +79,6 @@ export class SynchroneRules extends ChessRules { : null; } - // After undo(): no need to re-set INIT_COL_KING scanKings() { this.kingPos = { w: [-1, -1], b: [-1, -1] }; for (let i = 0; i < V.size.x; i++) { @@ -107,19 +111,19 @@ export class SynchroneRules extends ChessRules { } getPossibleMovesFrom([x, y]) { - return ( - this.filterValid(super.getPotentialMovesFrom([x, y])) - // Augment with potential recaptures: - .concat(this.getRecaptures()) - ); + let moves = this.filterValid(super.getPotentialMovesFrom([x, y])); + if (!this.underCheck(this.getColor(x, y))) + // Augment with potential recaptures, except if we are under check + Array.prototype.push.apply(moves, this.getRecaptures([x, y])); + return moves; } // Aux function used to find opponent and self captures - getCaptures(x, y, color) { + getCaptures(from, to, color) { const sliderAttack = (xx, yy, allowedSteps) => { - const deltaX = xx - x, + const deltaX = xx - to[0], absDeltaX = Math.abs(deltaX); - const deltaY = yy - y, + const deltaY = yy - to[1], absDeltaY = Math.abs(deltaY); const step = [ deltaX / absDeltaX || 0, deltaY / absDeltaY || 0 ]; if ( @@ -129,61 +133,70 @@ export class SynchroneRules extends ChessRules { ) { return null; } - let sq = [ x + step[0], y + step[1] ]; + let sq = [ to[0] + step[0], to[1] + step[1] ]; while (sq[0] != xx || sq[1] != yy) { // NOTE: no need to check OnBoard in this special case if (this.board[sq[0]][sq[1]] != V.EMPTY) return null; sq[0] += step[0]; sq[1] += step[1]; } - return this.getBasicMove([xx, yy], [x, y]); + return this.getBasicMove([xx, yy], [to[0], to[1]]); }; - // Can I take on the square [x, y] ? + // Can I take on the square 'to' ? // If yes, return the (list of) capturing move(s) + const getTargetedCaptures = ([i, j]) => { + let move = null; + // From [i, j]: + switch (this.getPiece(i, j)) { + case V.PAWN: { + // Pushed pawns move as enemy pawns + const shift = (color == 'w' ? 1 : -1); + if (to[0] + shift == i && Math.abs(to[1] - j) == 1) + move = this.getBasicMove([i, j], to); + break; + } + case V.KNIGHT: { + const deltaX = Math.abs(i - to[0]); + const deltaY = Math.abs(j - to[1]); + if ( + deltaX + deltaY == 3 && + [1, 2].includes(deltaX) && + [1, 2].includes(deltaY) + ) { + move = this.getBasicMove([i, j], to); + } + break; + } + case V.KING: + if (Math.abs(i - to[0]) <= 1 && Math.abs(j - to[1]) <= 1) + move = this.getBasicMove([i, j], to); + break; + case V.ROOK: { + move = sliderAttack(i, j, V.steps[V.ROOK]); + break; + } + case V.BISHOP: { + move = sliderAttack(i, j, V.steps[V.BISHOP]); + break; + } + case V.QUEEN: { + move = sliderAttack(i, j, V.steps[V.ROOK].concat(V.steps[V.BISHOP])); + break; + } + } + return move; + }; let moves = []; - for (let i=0; i<8; i++) { - for (let j=0; j<8; j++) { - if (this.getColor(i, j) == color) { - switch (this.getPiece(i, j)) { - case V.PAWN: { - // Pushed pawns move as enemy pawns - const shift = (color == 'w' ? 1 : -1); - if (x + shift == i && Math.abs(y - j) == 1) - moves.push(this.getBasicMove([i, j], [x, y])); - break; - } - case V.KNIGHT: { - const deltaX = Math.abs(i - x); - const deltaY = Math.abs(j - y); - if ( - deltaX + deltaY == 3 && - [1, 2].includes(deltaX) && - [1, 2].includes(deltaY) - ) { - moves.push(this.getBasicMove([i, j], [x, y])); - } - break; - } - case V.KING: - if (Math.abs(i - x) <= 1 && Math.abs(j - y) <= 1) - moves.push(this.getBasicMove([i, j], [x, y])); - break; - case V.ROOK: { - const mv = sliderAttack(i, j, V.steps[V.ROOK]); - if (!!mv) moves.push(mv); - break; - } - case V.BISHOP: { - const mv = sliderAttack(i, j, V.steps[V.BISHOP]); - if (!!mv) moves.push(mv); - break; - } - case V.QUEEN: { - const mv = sliderAttack( - i, j, V.steps[V.ROOK].concat(V.steps[V.BISHOP])); - if (!!mv) moves.push(mv); - break; - } + if (!!from) { + const theMove = getTargetedCaptures(from); + if (!!theMove) moves.push(theMove); + } + else { + for (let i=0; i<8; i++) { + for (let j=0; j<8; j++) { + if (this.getColor(i, j) == color) { + const newMove = getTargetedCaptures([i, j]); + if (!!newMove) moves.push(newMove); } } } @@ -191,7 +204,7 @@ export class SynchroneRules extends ChessRules { return this.filterValid(moves); } - getRecaptures() { + getRecaptures(from) { // 1) Generate all opponent's capturing moves let oppCaptureMoves = []; const color = this.turn; @@ -205,7 +218,7 @@ export class SynchroneRules extends ChessRules { ) { Array.prototype.push.apply( oppCaptureMoves, - this.getCaptures(i, j, oppCol) + this.getCaptures(null, [i, j], oppCol) ); } } @@ -226,7 +239,7 @@ export class SynchroneRules extends ChessRules { }; V.PlayOnBoard(this.board, justDisappear); // Can I take on [m.end.x, m.end.y] ? If yes, add to list: - this.getCaptures(m.end.x, m.end.y, color) + this.getCaptures(from, [m.end.x, m.end.y], color) .forEach(cm => moves.push(cm)); V.UndoOnBoard(this.board, justDisappear); } @@ -424,7 +437,8 @@ export class SynchroneRules extends ChessRules { } else this.whiteMove = move.whiteMove; } - getCheckSquares(color) { + getCheckSquares() { + const color = this.turn; if (color == 'b') { // kingPos must be reset for appropriate highlighting: var lastMove = JSON.parse(JSON.stringify(this.whiteMove)); @@ -502,4 +516,5 @@ export class SynchroneRules extends ChessRules { V.CoordsToSquare(move.end) ); } + };