X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FShogi.js;h=521e46806310c6a71d183271f9250ab20058e700;hb=b4f2488a58c5c92e1673cd64c6bba13afcafbf66;hp=bbd3af5b43387d69921a65afc145d1c86afd80fa;hpb=ad2494bd14f9a2fd068d713df0f8ac1fb3c3743f;p=vchess.git diff --git a/client/src/variants/Shogi.js b/client/src/variants/Shogi.js index bbd3af5b..521e4680 100644 --- a/client/src/variants/Shogi.js +++ b/client/src/variants/Shogi.js @@ -1,6 +1,6 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; import { ArrayFun } from "@/utils/array"; -import { shuffle } from "@/utils/alea"; +import { sample, shuffle } from "@/utils/alea"; export class ShogiRules extends ChessRules { static get HasFlags() { @@ -11,6 +11,14 @@ export class ShogiRules extends ChessRules { return false; } + static get Monochrome() { + return true; + } + + static get Notoodark() { + return true; + } + static IsGoodFen(fen) { if (!ChessRules.IsGoodFen(fen)) return false; const fenParsed = V.ParseFen(fen); @@ -35,7 +43,7 @@ export class ShogiRules extends ChessRules { static get SILVER_G() { return "s"; } - static get LANCER() { + static get LANCE() { return "l"; } @@ -49,7 +57,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() { @@ -68,11 +76,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 ]; @@ -102,20 +110,49 @@ export class ShogiRules extends ChessRules { "w 0 00000000000000" ); } - let pieces = { w: new Array(9), b: new Array(9) }; + // Randomization following these indications: + // http://www.shogi.net/shogi-l/Archive/2007/Nmar16-02.txt + 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) { - pieces['b'] = pieces['w']; + pieces1['b'] = JSON.parse(JSON.stringify(pieces1['w'])).reverse(); + positions2['b'] = + JSON.parse(JSON.stringify(positions2['w'])).reverse() + .map(p => 8 - p); 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]; + let positions = shuffle(ArrayFun.range(4)); + const composition = ['s', 's', 'g', 'g']; + for (let i = 0; i < 4; i++) pieces1[c][positions[i]] = composition[i]; + positions2[c] = sample(ArrayFun.range(9), 2).sort(); } return ( - pieces["b"].join("") + - "/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/" + - pieces["w"].join("").toUpperCase() + + ( + "ln" + + pieces1["b"].slice(0, 2).join("") + + "k" + + pieces1["b"].slice(2, 4).join("") + + "nl/" + ) + + ( + (positions2['b'][0] || "") + 'r' + + (positions2['b'][1] - positions2['b'][0] - 1 || "") + 'b' + + (8 - positions2['b'][1] || "") + ) + + "/ppppppppp/9/9/9/PPPPPPPPP/" + + ( + (positions2['w'][0] || "") + 'B' + + (positions2['w'][1] - positions2['w'][0] - 1 || "") + 'R' + + (8 - positions2['w'][1] || "") + ) + + ( + "/LN" + + pieces1["w"].slice(0, 2).join("").toUpperCase() + + "K" + + pieces1["w"].slice(2, 4).join("").toUpperCase() + + "NL" + ) + " w 0 00000000000000" ); } @@ -149,7 +186,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]), @@ -158,7 +195,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]) } }; } @@ -187,7 +224,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] ); } @@ -213,7 +250,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; @@ -266,8 +303,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: @@ -278,7 +315,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 @@ -372,6 +409,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 }); @@ -382,12 +431,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( @@ -418,7 +461,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) @@ -433,7 +476,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; @@ -475,9 +518,9 @@ 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) {