Fix Shogi.js
[vchess.git] / client / src / variants / Shogi.js
index 7e827bc..5f85e75 100644 (file)
@@ -1,7 +1,9 @@
 import { ChessRules, PiPo, Move } from "@/base_rules";
 import { ArrayFun } from "@/utils/array";
+import { sample, shuffle } from "@/utils/alea";
 
 export class ShogiRules extends ChessRules {
+
   static get HasFlags() {
     return false;
   }
@@ -10,6 +12,23 @@ export class ShogiRules extends ChessRules {
     return false;
   }
 
+  static get Monochrome() {
+    return true;
+  }
+
+  get showFirstTurn() {
+    return true;
+  }
+
+  static get Notoodark() {
+    return true;
+  }
+
+  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);
@@ -34,7 +53,7 @@ export class ShogiRules extends ChessRules {
   static get SILVER_G() {
     return "s";
   }
-  static get LANCER() {
+  static get LANCE() {
     return "l";
   }
 
@@ -48,7 +67,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 +86,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 +113,57 @@ export class ShogiRules extends ChessRules {
     );
   }
 
-  static GenRandInitFen() {
-    // No randomization for now:
+  static GenRandInitFen(options) {
+    if (options.randomness == 0) {
+      return (
+        "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " +
+        "w 0 00000000000000"
+      );
+    }
+    // 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' && options.randomness == 1) {
+        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(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 (
-      "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " +
-      "w 0 00000000000000"
+      (
+        "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"
     );
   }
 
@@ -114,33 +179,34 @@ 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("");
   }
 
   setOtherVariables(fen) {
     super.setOtherVariables(fen);
-    const fenParsed = V.ParseFen(fen);
     // Also init reserves (used by the interface to show landable pieces)
+    const reserve =
+      V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10));
     this.reserve = {
       w: {
-        [V.PAWN]: parseInt(fenParsed.reserve[0]),
-        [V.ROOK]: parseInt(fenParsed.reserve[1]),
-        [V.BISHOP]: parseInt(fenParsed.reserve[2]),
-        [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.PAWN]: reserve[0],
+        [V.ROOK]: reserve[1],
+        [V.BISHOP]: reserve[2],
+        [V.GOLD_G]: reserve[3],
+        [V.SILVER_G]: reserve[4],
+        [V.KNIGHT]: reserve[5],
+        [V.LANCE]: reserve[6]
       },
       b: {
-        [V.PAWN]: parseInt(fenParsed.reserve[7]),
-        [V.ROOK]: parseInt(fenParsed.reserve[8]),
-        [V.BISHOP]: parseInt(fenParsed.reserve[9]),
-        [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.PAWN]: reserve[7],
+        [V.ROOK]: reserve[8],
+        [V.BISHOP]: reserve[9],
+        [V.GOLD_G]: reserve[10],
+        [V.SILVER_G]: reserve[11],
+        [V.KNIGHT]: reserve[12],
+        [V.LANCE]: reserve[13]
       }
     };
   }
@@ -169,7 +235,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 +261,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;
@@ -221,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;
           }
@@ -248,10 +316,10 @@ 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]);
+        return super.getPotentialKingMoves([x, y]);
       case V.P_ROOK:
         return this.getPotentialDragonMoves([x, y]);
       case V.P_BISHOP:
@@ -260,7 +328,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 +336,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 +422,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 +444,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(
@@ -384,14 +458,6 @@ export class ShogiRules extends ChessRules {
     );
   }
 
-  getPotentialKingMoves(sq) {
-    return this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      { oneStep: true }
-    );
-  }
-
   isAttacked(sq, color) {
     return (
       this.isAttackedByPawn(sq, color) ||
@@ -400,7 +466,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 +481,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;
@@ -424,57 +490,39 @@ 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);
   }
 
-  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")
+      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.DRAGON, V.steps[V.ROOK], "oneStep")
+      this.isAttackedBySlideNJump(sq, color, V.P_BISHOP, V.steps[V.ROOK], 1)
     );
   }
 
@@ -489,14 +537,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;
     }
@@ -594,4 +644,5 @@ export class ShogiRules extends ChessRules {
       )
     );
   }
+
 };