Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Janggi.js
index 98660a4..8ede111 100644 (file)
@@ -1,7 +1,12 @@
 import { ChessRules, Move, PiPo } from "@/base_rules";
+import { randInt } from "@/utils/alea";
 
 export class JanggiRules extends ChessRules {
 
+  static get Options() {
+    return null;
+  }
+
   static get Monochrome() {
     return true;
   }
@@ -34,10 +39,6 @@ export class JanggiRules extends ChessRules {
     return false;
   }
 
-  static get LoseOnRepetition() {
-    return true;
-  }
-
   static get ELEPHANT() {
     return "e";
   }
@@ -55,7 +56,7 @@ export class JanggiRules extends ChessRules {
   }
 
   getPpath(b) {
-    return "Jiangqi/" + b;
+    return "Janggi/" + b;
   }
 
   static get size() {
@@ -109,9 +110,14 @@ export class JanggiRules extends ChessRules {
     // TODO: next "if" is mutually exclusive with the block above
     if (this.movesCount <= 1) {
       const firstRank = (this.movesCount == 0 ? 9 : 0);
-      const [initFile, destFile] = (this.subTurn == 1 ? [1, 2] : [7, 6]);
-      // Only option is knight / elephant swap:
-      if (x == firstRank && y == initFile) {
+      const initDestFile = new Map([[1, 2], [7, 6]]);
+      // Only option is knight --> elephant swap:
+      if (
+        x == firstRank &&
+        !!initDestFile.get(y) &&
+        this.getPiece(x, y) == V.KNIGHT
+      ) {
+        const destFile = initDestFile.get(y);
         moves.push(
           new Move({
             appear: [
@@ -191,7 +197,7 @@ export class JanggiRules extends ChessRules {
       if (y == 3) steps.push([shiftX, 1]);
       else if (y == 5) steps.push([shiftX, -1]);
     }
-    return super.getSlideNJumpMoves([x, y], steps, "oneStep");
+    return super.getSlideNJumpMoves([x, y], steps, 1);
   }
 
   knightStepsFromRookStep(step) {
@@ -209,7 +215,7 @@ export class JanggiRules extends ChessRules {
           this.knightStepsFromRookStep(rookStep));
       }
     }
-    return super.getSlideNJumpMoves([x, y], steps, "oneStep");
+    return super.getSlideNJumpMoves([x, y], steps, 1);
   }
 
   elephantStepsFromRookStep(step) {
@@ -232,7 +238,7 @@ export class JanggiRules extends ChessRules {
         }
       }
     }
-    return super.getSlideNJumpMoves([x, y], steps, "oneStep");
+    return super.getSlideNJumpMoves([x, y], steps, 1);
   }
 
   palacePeopleMoves([x, y]) {
@@ -276,7 +282,7 @@ export class JanggiRules extends ChessRules {
       // At the middle: all directions available
       Array.prototype.push.apply(steps, ChessRules.steps[V.BISHOP]);
     }
-    return super.getSlideNJumpMoves([x, y], steps, "oneStep");
+    return super.getSlideNJumpMoves([x, y], steps, 1);
   }
 
   getPotentialAdvisorMoves(sq) {
@@ -308,9 +314,7 @@ export class JanggiRules extends ChessRules {
       // In the middle of a palace: 4 one-diagonal-step to check
       Array.prototype.push.apply(
         moves,
-        super.getSlideNJumpMoves([x, y],
-                                 ChessRules.steps[V.BISHOP],
-                                 "oneStep")
+        super.getSlideNJumpMoves([x, y], ChessRules.steps[V.BISHOP], 1)
       );
     }
     return moves;
@@ -389,7 +393,7 @@ export class JanggiRules extends ChessRules {
   isAttackedByPawn([x, y], color) {
     const shiftX = (color == 'w' ? 1 : -1); //shift from king
     if (super.isAttackedBySlideNJump(
-      [x, y], color, V.PAWN, [[shiftX, 0], [0, 1], [0, -1]], "oneStep")
+      [x, y], color, V.PAWN, [[shiftX, 0], [0, 1], [0, -1]], 1)
     ) {
       return true;
     }
@@ -426,7 +430,7 @@ export class JanggiRules extends ChessRules {
       }
     }
     return (
-      super.isAttackedBySlideNJump([x, y], color, V.KNIGHT, steps, "oneStep")
+      super.isAttackedBySlideNJump([x, y], color, V.KNIGHT, steps, 1)
     );
   }
 
@@ -448,7 +452,7 @@ export class JanggiRules extends ChessRules {
       }
     }
     return (
-      super.isAttackedBySlideNJump([x, y], color, V.ELEPHANT, steps, "oneStep")
+      super.isAttackedBySlideNJump([x, y], color, V.ELEPHANT, steps, 1)
     );
   }
 
@@ -590,6 +594,24 @@ export class JanggiRules extends ChessRules {
     if (move.vanish.length > 0) super.postUndo(move);
   }
 
+  getComputerMove() {
+    if (this.movesCount <= 1) {
+      // Special case: swap and pass at random
+      const moves1 = this.getAllValidMoves();
+      const m1 = moves1[randInt(moves1.length)];
+      this.play(m1);
+      if (m1.vanish.length == 0) {
+        this.undo(m1);
+        return m1;
+      }
+      const moves2 = this.getAllValidMoves();
+      const m2 = moves2[randInt(moves2.length)];
+      this.undo(m1);
+      return [m1, m2];
+    }
+    return super.getComputerMove();
+  }
+
   getNotation(move) {
     if (move.vanish.length == 0) return "pass";
     if (move.appear.length == 2) return "S"; //"swap"