X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FXiangqi.js;h=4fef56536307d8d170b34d42c8f82d16114b6df7;hb=4313762da3237b04f204e121a20cab3ba7bb5dd2;hp=1b49cedba389a6356f69fdd1fe21a00ac5f37f02;hpb=9a1e3abe33fff07218b17c7c799eb622a730b7c7;p=vchess.git diff --git a/client/src/variants/Xiangqi.js b/client/src/variants/Xiangqi.js index 1b49cedb..4fef5653 100644 --- a/client/src/variants/Xiangqi.js +++ b/client/src/variants/Xiangqi.js @@ -2,6 +2,12 @@ import { ChessRules } from "@/base_rules"; export class XiangqiRules extends ChessRules { + static get Options() { + return null; + } + + // NOTE (TODO?) scanKings() could be more efficient (in Jangqi too) + static get Monochrome() { return true; } @@ -152,7 +158,7 @@ export class XiangqiRules extends ChessRules { if (y > 0) steps.push([0, -1]); if (y < 9) steps.push([0, 1]); } - return super.getSlideNJumpMoves([x, y], steps, "oneStep"); + return super.getSlideNJumpMoves([x, y], steps, 1); } knightStepsFromRookStep(step) { @@ -170,7 +176,7 @@ export class XiangqiRules extends ChessRules { this.knightStepsFromRookStep(rookStep)); } } - return super.getSlideNJumpMoves([x, y], steps, "oneStep"); + return super.getSlideNJumpMoves([x, y], steps, 1); } getPotentialElephantMoves([x, y]) { @@ -186,37 +192,58 @@ export class XiangqiRules extends ChessRules { // "out of board" checks delayed to next method } } - return super.getSlideNJumpMoves([x, y], steps, "oneStep"); - } - - insidePalace(x, y, c) { - return ( - (y >= 3 && y <= 5) && - ( - (c == 'w' && x >= 7) || - (c == 'b' && x <= 2) - ) - ); + return super.getSlideNJumpMoves([x, y], steps, 1); } getPotentialAdvisorMoves([x, y]) { // Diagonal steps inside palace - let steps = []; const c = this.getColor(x, y); - for (let s of ChessRules.steps[V.BISHOP]) { - if (this.insidePalace(x + s[0], y + s[1], c)) steps.push(s); + if ( + y != 4 || + (c == 'w' && x != V.size.x - 2) || + (c == 'b' && x != 1) + ) { + // In a corner: only one step available + let step = null; + const direction = (c == 'w' ? -1 : 1); + if ((c == 'w' && x == V.size.x - 1) || (c == 'b' && x == 0)) { + // On first line + if (y == 3) step = [direction, 1]; + else step = [direction, -1]; + } + else { + // On third line + if (y == 3) step = [-direction, 1]; + else step = [-direction, -1]; + } + return super.getSlideNJumpMoves([x, y], [step], 1); } - return super.getSlideNJumpMoves([x, y], steps, "oneStep"); + // In the middle of the palace: + return ( + super.getSlideNJumpMoves([x, y], ChessRules.steps[V.BISHOP], 1) + ); } getPotentialKingMoves([x, y]) { // Orthogonal steps inside palace - let steps = []; const c = this.getColor(x, y); - for (let s of ChessRules.steps[V.ROOK]) { - if (this.insidePalace(x + s[0], y + s[1], c)) steps.push(s); + if ( + y != 4 || + (c == 'w' && x != V.size.x - 2) || + (c == 'b' && x != 1) + ) { + // On the edge: only two steps available + let steps = []; + if (x < (c == 'w' ? V.size.x - 1 : 2)) steps.push([1, 0]); + if (x > (c == 'w' ? V.size.x - 3 : 0)) steps.push([-1, 0]); + if (y > 3) steps.push([0, -1]); + if (y < 5) steps.push([0, 1]); + return super.getSlideNJumpMoves([x, y], steps, 1); } - return super.getSlideNJumpMoves([x, y], steps, "oneStep"); + // In the middle of the palace: + return ( + super.getSlideNJumpMoves([x, y], ChessRules.steps[V.ROOK], 1) + ); } // NOTE: duplicated from Shako (TODO?) @@ -259,17 +286,8 @@ export class XiangqiRules extends ChessRules { isAttackedByPawn([x, y], color) { // The pawn necessarily crossed the river (attack on king) const shiftX = (color == 'w' ? 1 : -1); //shift from king - for (let s of [[shiftX, 0], [0, 1], [0, -1]]) { - const [i, j] = [x + s[0], y + s[1]]; - if ( - this.board[i][j] != V.EMPTY && - this.getColor(i, j) == color && - this.getPiece(i, j) == V.PAWN - ) { - return true; - } - } - return false; + return super.isAttackedBySlideNJump( + [x, y], color, V.PAWN, [[shiftX, 0], [0, 1], [0, -1]], 1); } knightStepsFromBishopStep(step) { @@ -289,7 +307,7 @@ export class XiangqiRules extends ChessRules { } } return ( - super.isAttackedBySlideNJump([x, y], color, V.KNIGHT, steps, "oneStep") + super.isAttackedBySlideNJump([x, y], color, V.KNIGHT, steps, 1) ); } @@ -369,9 +387,20 @@ export class XiangqiRules extends ChessRules { return evaluation; } + static get SEARCH_DEPTH() { + return 2; + } + static GenRandInitFen() { // No randomization here (TODO?) return "rneakaenr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNEAKAENR w 0"; } + getNotation(move) { + let notation = super.getNotation(move); + if (move.vanish.length == 2 && move.vanish[0].p == V.PAWN) + notation = "P" + notation.substr(1); + return notation; + } + };