Add Makpong, Hoppelpoppel, and Jangqi (rules unwritten yet)
[vchess.git] / client / src / variants / Hoppelpoppel.js
diff --git a/client/src/variants/Hoppelpoppel.js b/client/src/variants/Hoppelpoppel.js
new file mode 100644 (file)
index 0000000..c350466
--- /dev/null
@@ -0,0 +1,64 @@
+import { ChessRules } from "@/base_rules";
+
+export class HoppelpoppelRules extends ChessRules {
+
+  getSlideNJumpMoves_([x, y], steps, oneStep, options) {
+    options = options || {};
+    let moves = [];
+    outerLoop: for (let step of steps) {
+      let i = x + step[0];
+      let j = y + step[1];
+      while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
+        if (!options.onlyTake) moves.push(this.getBasicMove([x, y], [i, j]));
+        if (!!oneStep) continue outerLoop;
+        i += step[0];
+        j += step[1];
+      }
+      if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]) && !options.onlyMove)
+        moves.push(this.getBasicMove([x, y], [i, j]));
+    }
+    return moves;
+  }
+
+  getPotentialKnightMoves(sq) {
+    // The knight captures like a bishop
+    return (
+      this.getSlideNJumpMoves_(
+        sq, ChessRules.steps[V.KNIGHT], "oneStep", { onlyMove: true })
+      .concat(
+        this.getSlideNJumpMoves_(
+          sq, ChessRules.steps[V.BISHOP], null, { onlyTake: true }))
+    );
+  }
+
+  getPotentialBishopMoves(sq) {
+    // The bishop captures like a knight
+    return (
+      this.getSlideNJumpMoves_(
+        sq, ChessRules.steps[V.BISHOP], null, { onlyMove: true })
+      .concat(
+        this.getSlideNJumpMoves_(
+          sq, ChessRules.steps[V.KNIGHT], "oneStep", { onlyTake: true }))
+    );
+  }
+
+  isAttackedByKnight([x, y], color) {
+    return super.isAttackedBySlideNJump(
+      [x, y],
+      color,
+      V.KNIGHT,
+      V.steps[V.BISHOP]
+    );
+  }
+
+  isAttackedByAntiking([x, y], color) {
+    return super.isAttackedBySlideNJump(
+      [x, y],
+      color,
+      V.BISHOP,
+      V.steps[V.KNIGHT],
+      "oneStep"
+    );
+  }
+
+};