From e9437f4b2ee9a40013ee514562f39a36d551faab Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 24 Mar 2021 14:38:01 +0100
Subject: [PATCH] Fix Titan Chess

---
 client/src/variants/Hamilton.js |  16 ++---
 client/src/variants/Titan.js    | 111 +++++++++++++++++---------------
 2 files changed, 63 insertions(+), 64 deletions(-)

diff --git a/client/src/variants/Hamilton.js b/client/src/variants/Hamilton.js
index f8669b7e..856725c1 100644
--- a/client/src/variants/Hamilton.js
+++ b/client/src/variants/Hamilton.js
@@ -19,10 +19,6 @@ export class HamiltonRules extends ChessRules {
     return "xx";
   }
 
-  hoverHighlight() {
-    return this.movesCount == 0;
-  }
-
   static board2fen(b) {
     if (b[0] == 'x') return 'x';
     return ChessRules.board2fen(b);
@@ -69,6 +65,10 @@ export class HamiltonRules extends ChessRules {
     return side == this.turn;
   }
 
+  hoverHighlight() {
+    return this.movesCount == 0;
+  }
+
   // Initiate the game by choosing a square for the knight:
   doClick(square) {
     if (this.movesCount > 0) return null;
@@ -86,13 +86,7 @@ export class HamiltonRules extends ChessRules {
       return [...Array(64).keys()].map(k => {
         const i = k % 8;
         const j = (k - i) / 8;
-        return new Move({
-          appear: [
-            new PiPo({ x: i, y: j, c: 'w', p: V.KNIGHT })
-          ],
-          vanish: [],
-          start: { x: -1, y: -1 }
-        });
+        return this.doClick([i, j]);
       });
     }
     for (let i=0; i<8; i++) {
diff --git a/client/src/variants/Titan.js b/client/src/variants/Titan.js
index 53420e83..836acc1f 100644
--- a/client/src/variants/Titan.js
+++ b/client/src/variants/Titan.js
@@ -120,26 +120,68 @@ export class TitanRules extends ChessRules {
     }
   }
 
+  canIplay(side, [x, y]) {
+    if (this.movesCount >= 4) return super.canIplay(side, [x, y]);
+    return (
+      this.turn == side &&
+      (
+        (side == 'w' && x == 7) ||
+        (side == 'b' && x == 0)
+      )
+    );
+  }
+
+  hoverHighlight([x, y]) {
+    const c = this.turn;
+    return (
+      this.movesCount <= 3 &&
+      ((c == 'w' && x == 7) || (c == 'b' && x == 0))
+    );
+  }
+
+  onlyClick([x, y]) {
+    return (
+      this.movesCount <= 3 ||
+      // TODO: next line theoretically shouldn't be required...
+      (this.movesCount == 4 && this.getColor(x, y) != this.turn)
+    );
+  }
+
+  // Special case of move 1 = choose squares, knight first, then bishop
+  doClick(square) {
+    if (this.movesCount >= 4) return null;
+    const color = this.turn;
+    const [x, y] = [square[0], square[1]];
+    if ((color == 'w' && x != 7) || (color == 'b' && x != 0)) return null;
+    const selectedPiece = this.board[x][y][1];
+    return new Move({
+      appear: [
+        new PiPo({
+          x: x,
+          y: y,
+          c: color,
+          p: this.getAugmented(selectedPiece)
+        })
+      ],
+      vanish: [
+        new PiPo({
+          x: x,
+          y: y,
+          c: color,
+          p: selectedPiece
+        })
+      ],
+      start: { x: x, y: y },
+      end: { x: x, y: y }
+    });
+  }
+
   // If piece not in usual list, bishop or knight appears.
   getPotentialMovesFrom([x, y]) {
     if (this.movesCount <= 3) {
       // Setup stage
-      const color = this.getColor(x, y);
-      const firstRank = (color == 'w' ? 7 : 0);
-      if (x != firstRank || V.AUGMENTED_PIECES.includes(this.board[x][y][1]))
-        return [];
-      const piece = this.getPiece(x, y);
-      const move = new Move({
-        appear: [
-          new PiPo({ x: x, y: y, c: color, p: this.getAugmented(piece) })
-        ],
-        vanish: [
-          new PiPo({ x: x, y: y, c: color, p: piece })
-        ],
-        start: { x: x, y: y },
-        end: { x: x, y: y }
-      });
-      return [move];
+      const move = this.doClick([x, y]);
+      return (!move ? [] : [move]);
     }
     let moves = super.getPotentialMovesFrom([x, y]);
     const initialPiece = this.getPiece(x, y);
@@ -185,43 +227,6 @@ export class TitanRules extends ChessRules {
     return moves;
   }
 
-  hoverHighlight([x, y]) {
-    const c = this.turn;
-    return (
-      this.movesCount <= 3 &&
-      ((c == 'w' && x == 7) || (c == 'b' && x == 0))
-    );
-  }
-
-  // Special case of move 1 = choose squares, knight first, then bishop
-  doClick(square) {
-    if (this.movesCount >= 4) return null;
-    const color = this.turn;
-    const [x, y] = [square[0], square[1]];
-    if ((color == 'w' && x != 7) || (color == 'b' && x != 0)) return null;
-    const selectedPiece = this.board[x][y][1];
-    return new Move({
-      appear: [
-        new PiPo({
-          x: x,
-          y: y,
-          c: color,
-          p: this.getAugmented(selectedPiece)
-        })
-      ],
-      vanish: [
-        new PiPo({
-          x: x,
-          y: y,
-          c: color,
-          p: selectedPiece
-        })
-      ],
-      start: { x: x, y: y },
-      end: { x: x, y: y }
-    });
-  }
-
   postPlay(move) {
     if (this.movesCount > 4) super.postPlay(move);
   }
-- 
2.44.0