X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FShogi.js;h=1b2ba03a4c4f13a475065889936e7c74c583a9d4;hb=107dc1bd5361e2538b1551bdcc37c1e90a444b83;hp=7e827bc2ebbd9fac256768cd193553dd0ddd0743;hpb=0b9a6ff95fb80a9f40077e6554c420626a1e0454;p=vchess.git diff --git a/client/src/variants/Shogi.js b/client/src/variants/Shogi.js index 7e827bc2..1b2ba03a 100644 --- a/client/src/variants/Shogi.js +++ b/client/src/variants/Shogi.js @@ -1,5 +1,6 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; import { ArrayFun } from "@/utils/array"; +import { shuffle } from "@/utils/alea"; export class ShogiRules extends ChessRules { static get HasFlags() { @@ -10,6 +11,10 @@ export class ShogiRules extends ChessRules { return false; } + static get Monochrome() { + return true; + } + static IsGoodFen(fen) { if (!ChessRules.IsGoodFen(fen)) return false; const fenParsed = V.ParseFen(fen); @@ -34,7 +39,7 @@ export class ShogiRules extends ChessRules { static get SILVER_G() { return "s"; } - static get LANCER() { + static get LANCE() { return "l"; } @@ -48,7 +53,7 @@ export class ShogiRules extends ChessRules { static get P_SILVER() { return 't'; } - static get P_LANCER() { + static get P_LANCE() { return 'm'; } static get P_ROOK() { @@ -67,11 +72,11 @@ export class ShogiRules extends ChessRules { ChessRules.KING, V.GOLD_G, V.SILVER_G, - V.LANCER, + V.LANCE, V.P_PAWN, V.P_KNIGHT, V.P_SILVER, - V.P_LANCER, + V.P_LANCE, V.P_ROOK, V.P_BISHOP ]; @@ -94,11 +99,28 @@ export class ShogiRules extends ChessRules { ); } - static GenRandInitFen() { - // No randomization for now: + static GenRandInitFen(randomness) { + if (randomness == 0) { + return ( + "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " + + "w 0 00000000000000" + ); + } + let pieces = { w: new Array(9), b: new Array(9) }; + for (let c of ["w", "b"]) { + if (c == 'b' && randomness == 1) { + pieces['b'] = pieces['w']; + break; + } + let positions = shuffle(ArrayFun.range(9)); + const composition = ['l', 'l', 'n', 'n', 's', 's', 'g', 'g', 'k']; + for (let i = 0; i < 9; i++) pieces[c][positions[i]] = composition[i]; + } return ( - "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " + - "w 0 00000000000000" + pieces["b"].join("") + + "/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/" + + pieces["w"].join("").toUpperCase() + + " w 0 00000000000000" ); } @@ -114,7 +136,7 @@ export class ShogiRules extends ChessRules { let counts = new Array(14); for (let i = 0; i < V.RESERVE_PIECES.length; i++) { counts[i] = this.reserve["w"][V.RESERVE_PIECES[i]]; - counts[6 + i] = this.reserve["b"][V.RESERVE_PIECES[i]]; + counts[7 + i] = this.reserve["b"][V.RESERVE_PIECES[i]]; } return counts.join(""); } @@ -131,7 +153,7 @@ export class ShogiRules extends ChessRules { [V.GOLD_G]: parseInt(fenParsed.reserve[3]), [V.SILVER_G]: parseInt(fenParsed.reserve[4]), [V.KNIGHT]: parseInt(fenParsed.reserve[5]), - [V.LANCER]: parseInt(fenParsed.reserve[6]) + [V.LANCE]: parseInt(fenParsed.reserve[6]) }, b: { [V.PAWN]: parseInt(fenParsed.reserve[7]), @@ -140,7 +162,7 @@ export class ShogiRules extends ChessRules { [V.GOLD_G]: parseInt(fenParsed.reserve[10]), [V.SILVER_G]: parseInt(fenParsed.reserve[11]), [V.KNIGHT]: parseInt(fenParsed.reserve[12]), - [V.LANCER]: parseInt(fenParsed.reserve[13]) + [V.LANCE]: parseInt(fenParsed.reserve[13]) } }; } @@ -169,7 +191,7 @@ export class ShogiRules extends ChessRules { // Ordering on reserve pieces static get RESERVE_PIECES() { return ( - [V.PAWN, V.ROOK, V.BISHOP, V.GOLD_G, V.SILVER_G, V.KNIGHT, V.LANCER] + [V.PAWN, V.ROOK, V.BISHOP, V.GOLD_G, V.SILVER_G, V.KNIGHT, V.LANCE] ); } @@ -195,7 +217,7 @@ export class ShogiRules extends ChessRules { const lastRanks = color == 'w' ? [0, 1] : [8, 7]; for (let i = 0; i < V.size.x; i++) { if ( - (i == lastRanks[0] && [V.PAWN, V.KNIGHT, V.LANCER].includes(p)) || + (i == lastRanks[0] && [V.PAWN, V.KNIGHT, V.LANCE].includes(p)) || (i == lastRanks[1] && p == V.KNIGHT) ) { continue; @@ -248,8 +270,8 @@ export class ShogiRules extends ChessRules { return this.getPotentialBishopMoves([x, y]); case V.SILVER_G: return this.getPotentialSilverMoves([x, y]); - case V.LANCER: - return this.getPotentialLancerMoves([x, y]); + case V.LANCE: + return this.getPotentialLanceMoves([x, y]); case V.KING: return this.getPotentialKingMoves([x, y]); case V.P_ROOK: @@ -260,7 +282,7 @@ export class ShogiRules extends ChessRules { case V.P_PAWN: case V.P_SILVER: case V.P_KNIGHT: - case V.P_LANCER: + case V.P_LANCE: return this.getPotentialGoldMoves([x, y]); } return []; //never reached @@ -268,7 +290,7 @@ export class ShogiRules extends ChessRules { // Modified to take promotions into account getSlideNJumpMoves([x, y], steps, options) { - const options = options || {}; + options = options || {}; const color = this.turn; const oneStep = options.oneStep; const forcePromoteOnLastRank = options.force; @@ -354,6 +376,18 @@ export class ShogiRules extends ChessRules { ); } + getPotentialLanceMoves(sq) { + const forward = (this.turn == 'w' ? -1 : 1); + return this.getSlideNJumpMoves( + sq, + [[forward, 0]], + { + promote: V.P_LANCE, + force: true + } + ); + } + getPotentialRookMoves(sq) { return this.getSlideNJumpMoves( sq, V.steps[V.ROOK], { promote: V.P_ROOK }); @@ -364,12 +398,6 @@ export class ShogiRules extends ChessRules { sq, V.steps[V.BISHOP], { promote: V.P_BISHOP }); } - getPotentialLancerMoves(sq) { - const forward = (this.turn == 'w' ? -1 : 1); - return this.getSlideNJumpMoves( - sq, [[forward, 0]], { promote: V.P_LANCER }); - } - getPotentialDragonMoves(sq) { return ( this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( @@ -400,7 +428,7 @@ export class ShogiRules extends ChessRules { this.isAttackedByKnight(sq, color) || this.isAttackedByBishop(sq, color) || this.isAttackedByHorse(sq, color) || - this.isAttackedByLancer(sq, color) || + this.isAttackedByLance(sq, color) || this.isAttackedBySilver(sq, color) || this.isAttackedByGold(sq, color) || this.isAttackedByKing(sq, color) @@ -415,7 +443,7 @@ export class ShogiRules extends ChessRules { V.OnBoard(i, j) && this.board[i][j] != V.EMPTY && this.getColor(i, j) == color && - [V.GOLD_G, V.P_PAWN, V.P_SILVER, V.P_KNIGHT, V.P_LANCER] + [V.GOLD_G, V.P_PAWN, V.P_SILVER, V.P_KNIGHT, V.P_LANCE] .includes(this.getPiece(i, j)) ) { return true; @@ -457,16 +485,16 @@ export class ShogiRules extends ChessRules { sq, color, V.KNIGHT, [[forward, 1], [forward, -1]], "oneStep"); } - isAttackedByLancer(sq, color) { + isAttackedByLance(sq, color) { const forward = (color == 'w' ? 1 : -1); - return this.isAttackedBySlideNJump(sq, color, V.LANCER, [[forward, 0]]); + return this.isAttackedBySlideNJump(sq, color, V.LANCE, [[forward, 0]]); } isAttackedByDragon(sq, color) { return ( this.isAttackedBySlideNJump(sq, color, V.P_ROOK, V.steps[V.ROOK]) || this.isAttackedBySlideNJump( - sq, color, V.DRAGON, V.steps[V.BISHOP], "oneStep") + sq, color, V.P_ROOK, V.steps[V.BISHOP], "oneStep") ); } @@ -474,7 +502,7 @@ export class ShogiRules extends ChessRules { return ( this.isAttackedBySlideNJump(sq, color, V.P_BISHOP, V.steps[V.BISHOP]) || this.isAttackedBySlideNJump( - sq, color, V.DRAGON, V.steps[V.ROOK], "oneStep") + sq, color, V.P_BISHOP, V.steps[V.ROOK], "oneStep") ); }