Fix some variants and improve a few other things a little bit
[vchess.git] / client / src / variants / Knightrelay.js
index 8ccd0e2..de1e89a 100644 (file)
@@ -5,7 +5,8 @@ export const VariantRules = class KnightrelayRules extends ChessRules {
     let moves = super.getPotentialMovesFrom([x, y]);
 
     // Expand possible moves if guarded by a knight:
-    if (this.getPiece(x,y) != V.KNIGHT) {
+    const piece = this.getPiece(x,y);
+    if (piece != V.KNIGHT) {
       const color = this.turn;
       let guardedByKnight = false;
       for (const step of V.steps[V.KNIGHT]) {
@@ -19,12 +20,24 @@ export const VariantRules = class KnightrelayRules extends ChessRules {
         }
       }
       if (guardedByKnight) {
+        const lastRank = color == "w" ? 0 : V.size.x - 1;
         for (const step of V.steps[V.KNIGHT]) {
           if (
             V.OnBoard(x+step[0],y+step[1]) &&
             this.getColor(x+step[0],y+step[1]) != color
           ) {
-            moves.push(this.getBasicMove([x,y], [x+step[0],y+step[1]]));
+            // Potential promotions:
+            const finalPieces = piece == V.PAWN && x + step[0] == lastRank
+              ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
+              : [piece];
+            for (let p of finalPieces) {
+              moves.push(
+                this.getBasicMove([x,y], [x+step[0],y+step[1]], {
+                  c: color,
+                  p: p
+                })
+              );
+            }
           }
         }
       }
@@ -64,6 +77,17 @@ export const VariantRules = class KnightrelayRules extends ChessRules {
     return false;
   }
 
+  static get VALUES() {
+    return {
+      p: 1,
+      r: 5,
+      n: 7, //the knight is valuable
+      b: 3,
+      q: 9,
+      k: 1000
+    };
+  }
+
   getNotation(move) {
     if (move.appear.length == 2 && move.appear[0].p == V.KING)
       // Castle