Add Knightmate2: two kings, as in Spartan Chess
[vchess.git] / client / src / variants / Knightmate1.js
diff --git a/client/src/variants/Knightmate1.js b/client/src/variants/Knightmate1.js
new file mode 100644 (file)
index 0000000..80e7005
--- /dev/null
@@ -0,0 +1,85 @@
+import { ChessRules } from "@/base_rules";
+
+export class Knightmate1Rules extends ChessRules {
+
+  static get COMMONER() {
+    return "c";
+  }
+
+  static get PIECES() {
+    return ChessRules.PIECES.concat([V.COMMONER]);
+  }
+
+  getPpath(b) {
+    return ([V.KING, V.COMMONER].includes(b[1]) ? "Knightmate/" : "") + b;
+  }
+
+  static GenRandInitFen(randomness) {
+    return ChessRules.GenRandInitFen(randomness)
+      .replace(/n/g, 'c').replace(/N/g, 'C');
+  }
+
+  getPotentialMovesFrom([x, y]) {
+    switch (this.getPiece(x, y)) {
+      case V.COMMONER:
+        return this.getPotentialCommonerMoves([x, y]);
+      default:
+        return super.getPotentialMovesFrom([x, y]);
+    }
+  }
+
+  getPotentialCommonerMoves(sq) {
+    return this.getSlideNJumpMoves(
+      sq,
+      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
+      "oneStep"
+    );
+  }
+
+  getPotentialKingMoves(sq) {
+    return super.getPotentialKnightMoves(sq).concat(super.getCastleMoves(sq));
+  }
+
+  isAttacked(sq, color) {
+    return (
+      this.isAttackedByCommoner(sq, color) ||
+      this.isAttackedByPawn(sq, color) ||
+      this.isAttackedByRook(sq, color) ||
+      this.isAttackedByBishop(sq, color) ||
+      this.isAttackedByQueen(sq, color) ||
+      this.isAttackedByKing(sq, color)
+    );
+  }
+
+  isAttackedByKing(sq, color) {
+    return this.isAttackedBySlideNJump(
+      sq,
+      color,
+      V.KING,
+      V.steps[V.KNIGHT],
+      "oneStep"
+    );
+  }
+
+  isAttackedByCommoner(sq, color) {
+    return this.isAttackedBySlideNJump(
+      sq,
+      color,
+      V.COMMONER,
+      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
+      "oneStep"
+    );
+  }
+
+  static get VALUES() {
+    return {
+      p: 1,
+      r: 5,
+      c: 5, //the commoner is valuable
+      b: 3,
+      q: 9,
+      k: 1000
+    };
+  }
+
+};