X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FShogi.js;h=95be2d1b3a1fffa8c866fd047ebb527c8ca3df9e;hb=e30523f27d5989903de6743a8b4f194a390d576c;hp=63c0d726642f40f974bb4741ae7b31688ffc0681;hpb=6c00a6e55209930057bcf040cf444f40e7d47030;p=vchess.git diff --git a/client/src/variants/Shogi.js b/client/src/variants/Shogi.js index 63c0d726..95be2d1b 100644 --- a/client/src/variants/Shogi.js +++ b/client/src/variants/Shogi.js @@ -16,10 +16,19 @@ export class ShogiRules extends ChessRules { return true; } + get showFirstTurn() { + return true; + } + static get Notoodark() { return true; } + get loseOnRepetition() { + // If current side is under check: lost + return this.underCheck(this.turn); + } + static IsGoodFen(fen) { if (!ChessRules.IsGoodFen(fen)) return false; const fenParsed = V.ParseFen(fen); @@ -104,8 +113,8 @@ export class ShogiRules extends ChessRules { ); } - static GenRandInitFen(randomness) { - if (randomness == 0) { + static GenRandInitFen(options) { + if (options.randomness == 0) { return ( "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " + "w 0 00000000000000" @@ -116,7 +125,7 @@ export class ShogiRules extends ChessRules { let pieces1 = { w: new Array(4), b: new Array(4) }; let positions2 = { w: new Array(2), b: new Array(2) }; for (let c of ["w", "b"]) { - if (c == 'b' && randomness == 1) { + if (c == 'b' && options.randomness == 1) { pieces1['b'] = JSON.parse(JSON.stringify(pieces1['w'])).reverse(); positions2['b'] = JSON.parse(JSON.stringify(positions2['w'])).reverse() @@ -278,7 +287,9 @@ export class ShogiRules extends ChessRules { if (p == V.PAWN) { // Do not drop on checkmate: this.play(mv); - const res = (this.underCheck(oppCol) && !this.atLeastOneMove()); + const res = ( + this.underCheck(oppCol) && !this.atLeastOneMove("noReserve") + ); this.undo(mv); if (res) continue; } @@ -324,7 +335,7 @@ export class ShogiRules extends ChessRules { } // Modified to take promotions into account - getSlideNJumpMoves([x, y], steps, options) { + getSlideNJumpMoves_opt([x, y], steps, options) { options = options || {}; const color = this.turn; const oneStep = options.oneStep; @@ -364,7 +375,7 @@ export class ShogiRules extends ChessRules { getPotentialGoldMoves(sq) { const forward = (this.turn == 'w' ? -1 : 1); - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, V.steps[V.ROOK].concat([ [forward, 1], [forward, -1] ]), { oneStep: true } @@ -374,7 +385,7 @@ export class ShogiRules extends ChessRules { getPotentialPawnMoves(sq) { const forward = (this.turn == 'w' ? -1 : 1); return ( - this.getSlideNJumpMoves( + this.getSlideNJumpMoves_opt( sq, [[forward, 0]], { @@ -388,7 +399,7 @@ export class ShogiRules extends ChessRules { getPotentialSilverMoves(sq) { const forward = (this.turn == 'w' ? -1 : 1); - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, V.steps[V.BISHOP].concat([ [forward, 0] ]), { @@ -400,7 +411,7 @@ export class ShogiRules extends ChessRules { getPotentialKnightMoves(sq) { const forward = (this.turn == 'w' ? -2 : 2); - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, [ [forward, 1], [forward, -1] ], { @@ -413,9 +424,9 @@ export class ShogiRules extends ChessRules { getPotentialLanceMoves(sq) { const forward = (this.turn == 'w' ? -1 : 1); - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, - [[forward, 0]], + [ [forward, 0] ], { promote: V.P_LANCE, force: true @@ -424,26 +435,26 @@ export class ShogiRules extends ChessRules { } getPotentialRookMoves(sq) { - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, V.steps[V.ROOK], { promote: V.P_ROOK }); } getPotentialBishopMoves(sq) { - return this.getSlideNJumpMoves( + return this.getSlideNJumpMoves_opt( sq, V.steps[V.BISHOP], { promote: V.P_BISHOP }); } getPotentialDragonMoves(sq) { return ( - this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( - this.getSlideNJumpMoves(sq, V.steps[V.BISHOP], { oneStep: true })) + this.getSlideNJumpMoves_opt(sq, V.steps[V.ROOK]).concat( + this.getSlideNJumpMoves_opt(sq, V.steps[V.BISHOP], { oneStep: true })) ); } getPotentialHorseMoves(sq) { return ( - this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( - this.getSlideNJumpMoves(sq, V.steps[V.ROOK], { oneStep: true })) + this.getSlideNJumpMoves_opt(sq, V.steps[V.BISHOP]).concat( + this.getSlideNJumpMoves_opt(sq, V.steps[V.ROOK], { oneStep: true })) ); } @@ -479,37 +490,21 @@ export class ShogiRules extends ChessRules { return false; } - isAttackedBySilver([x, y], color) { + isAttackedBySilver(sq, color) { const shift = (color == 'w' ? 1 : -1); - for (let step of V.steps[V.BISHOP].concat([[shift, 0]])) { - const [i, j] = [x + step[0], y + step[1]]; - if ( - V.OnBoard(i, j) && - this.board[i][j] != V.EMPTY && - this.getColor(i, j) == color && - this.getPiece(i, j) == V.SILVER_G - ) { - return true; - } - } - return false; + return this.isAttackedBySlideNJump( + sq, color, V.SILVER, V.steps[V.BISHOP].concat([ [shift, 0] ]), 1); } - isAttackedByPawn([x, y], color) { + isAttackedByPawn(sq, color) { const shift = (color == 'w' ? 1 : -1); - const [i, j] = [x + shift, y]; - return ( - V.OnBoard(i, j) && - this.board[i][j] != V.EMPTY && - this.getColor(i, j) == color && - this.getPiece(i, j) == V.PAWN - ); + return this.isAttackedBySlideNJump(sq, color, V.PAWN, [ [shift, 0] ], 1); } isAttackedByKnight(sq, color) { const forward = (color == 'w' ? 2 : -2); return this.isAttackedBySlideNJump( - sq, color, V.KNIGHT, [[forward, 1], [forward, -1]], "oneStep"); + sq, color, V.KNIGHT, [ [forward, 1], [forward, -1] ], 1); } isAttackedByLance(sq, color) { @@ -520,19 +515,36 @@ export class ShogiRules extends ChessRules { isAttackedByDragon(sq, color) { return ( this.isAttackedBySlideNJump(sq, color, V.P_ROOK, V.steps[V.ROOK]) || - this.isAttackedBySlideNJump( - sq, color, V.P_ROOK, V.steps[V.BISHOP], "oneStep") + this.isAttackedBySlideNJump(sq, color, V.P_ROOK, V.steps[V.BISHOP], 1) ); } isAttackedByHorse(sq, color) { return ( this.isAttackedBySlideNJump(sq, color, V.P_BISHOP, V.steps[V.BISHOP]) || - this.isAttackedBySlideNJump( - sq, color, V.P_BISHOP, V.steps[V.ROOK], "oneStep") + this.isAttackedBySlideNJump(sq, color, V.P_BISHOP, V.steps[V.ROOK], 1) ); } + filterValid(moves) { + if (moves.length == 0) return []; + const color = this.turn; + const lastRanks = (color == 'w' ? [0, 1] : [8, 7]); + return moves.filter(m => { + if ( + (m.appear[0].p == V.KNIGHT && lastRanks.includes(m.end.x)) || + ([V.PAWN, V.LANCE].includes(m.appear[0].p) && lastRanks[0] == m.end.x) + ) { + // Forbid moves resulting in a blocked piece + return false; + } + this.play(m); + const res = !this.underCheck(color); + this.undo(m); + return res; + }); + } + getAllValidMoves() { let moves = super.getAllPotentialMoves(); const color = this.turn; @@ -544,14 +556,16 @@ export class ShogiRules extends ChessRules { return this.filterValid(moves); } - atLeastOneMove() { + atLeastOneMove(noReserve) { if (!super.atLeastOneMove()) { - // Search one reserve move - for (let i = 0; i < V.RESERVE_PIECES.length; i++) { - let moves = this.filterValid( - this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) - ); - if (moves.length > 0) return true; + if (!noReserve) { + // Search one reserve move + for (let i = 0; i < V.RESERVE_PIECES.length; i++) { + let moves = this.filterValid( + this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) + ); + if (moves.length > 0) return true; + } } return false; }