Improve Fanorona. Not sure that the getPotentialMove_s() approach is good.. main
authorBenjamin Auder <benjamin.auder@somewhere>
Thu, 26 Nov 2026 13:13:22 +0000 (14:13 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Thu, 26 Nov 2026 13:13:22 +0000 (14:13 +0100)
variants/Fanorona/class.js

index f1f4ba8..05b351b 100644 (file)
@@ -101,21 +101,6 @@ export default class FanoronaRules extends AbstractOnGridRules {
     domPiece.style.setProperty('--rotate-by', V.ArrowToAngle(piece));
   }
 
-  // After moving, add stones captured in "step" direction from new
-  // location [x, y] to mv.vanish (if any captured stone!)
-  addCapture([x, y], step, move) {
-    let [i, j] = [x + step[0], y + step[1]];
-    while (
-      this.onBoard(i, j) &&
-      this.board[i][j] != "" &&
-      this.getColor(i, j) == oppCol
-    ) {
-      move.vanish.push(new PiPo({ x: i, y: j, c: oppCol, p: 's' }));
-      [i, j] = [i + step[0], j + step[1]];
-    }
-    return (move.vanish.length >= 2);
-  }
-
   stepToArrow(s, forward) {
     const baseShift = (forward ? 0 : 8),
           colShift = (this.playerColor=='w' ? 0 : 4);
@@ -136,6 +121,21 @@ export default class FanoronaRules extends AbstractOnGridRules {
     return ''; //never reached
   };
 
+  // After moving, add stones captured in "step" direction from new
+  // location [x, y] to mv.vanish (if any captured stone!)
+  addCapture([x, y], step, move, oppCol) {
+    let [i, j] = [x + step[0], y + step[1]];
+    while (
+      this.onBoard(i, j) &&
+      this.board[i][j] != "" &&
+      this.getColor(i, j) == oppCol
+    ) {
+      move.vanish.push(new PiPo({ x: i, y: j, c: oppCol, p: 's' }));
+      [i, j] = [i + step[0], j + step[1]];
+    }
+    return (move.vanish.length >= 2);
+  }
+
   getPotentialMovesFrom([x, y], justCapt) {
     const oppCol = C.GetOppTurn(this.turn);
     const L = this.captures.length;
@@ -168,7 +168,7 @@ export default class FanoronaRules extends AbstractOnGridRules {
       if (this.onBoard(i, j) && this.board[i][j] == "") {
         // The move is possible. Might lead to 2 different captures
         let mv = super.getBasicMove([x, y], [i, j]);
-        const capt = this.addCapture([i, j], s, mv);
+        const capt = this.addCapture([i, j], s, mv, oppCol);
         if (capt) {
           if (!!justCapt)
             return true;
@@ -176,7 +176,7 @@ export default class FanoronaRules extends AbstractOnGridRules {
           moves.push(mv);
           mv = super.getBasicMove([x, y], [i, j]); //cheap enough
         }
-        const capt_bw = this.addCapture([x, y], [-s[0], -s[1]], mv);
+        const capt_bw = this.addCapture([x, y], [-s[0], -s[1]], mv, oppCol);
         if (capt_bw) {
           if (!!justCapt)
             return true;
@@ -199,8 +199,29 @@ export default class FanoronaRules extends AbstractOnGridRules {
     ) {
       return [];
     }
-    // If canCapture but nothing behind or in front, invalid
+    const L = this.captures.length;
+    let c = undefined;
+    if (L > 0) { //implies "canCapture == true"
+      c = this.captures[L-1];
+      if (x1 != c.square.x + c.step[0] || y1 != c.square.y + c.step[1])
+        return [];
+    }
     const step = [x2 - x1, y2 - y1];
+    if (
+      L > 0 && this.canCapture &&
+      step[0] == - c.step[0] && step[1] == - c.step[1]
+    ) {
+      // Return a move to say "I'm done capturing"
+      let mv = new Move({
+        appear: [],
+        vanish: [],
+        start: { x: x1, y: y1 },
+        end: { x: x1 - c.step[0], y: y1 - c.step[1] }
+      });
+      mv.noAnimate = true;
+      return [mv];
+    }
+    // If canCapture but nothing behind or in front, invalid
     if (
       this.canCapture &&
       (
@@ -209,15 +230,42 @@ export default class FanoronaRules extends AbstractOnGridRules {
       )
       &&
       (
-        !this.onBoard(x1 + step[0], y1 + step[1]) ||
-        this.board[x1 + step[0]][y1 + step[1]] == ""
+        !this.onBoard(x2 + step[0], y2 + step[1]) ||
+        this.board[x2 + step[0]][y2 + step[1]] == ""
       )
     ) {
       return [];
     }
-    // Ok, the move seems valid
+    // Ok, the move seems valid: capturing or moving
     let moves = [];
-////////// TODO ?????????
+    const oppCol = C.GetOppTurn(this.turn);
+    if (this.canCapture) {
+      // Something in front ?
+      if (
+        this.onBoard(x2 + step[0], y2 + step[1]) &&
+        this.getColor(x2 + step[0], y2 + step[1]) == oppCol
+      ) {
+        let mv = super.getBasicMove([x1, y1], [x2, y2]);
+        this.addCapture([x2, y2], step, mv, oppCol);
+        mv.choice = this.stepToArrow(step[0] + "_" + step[1], true);
+        moves.push(mv);
+      }
+      // Something behind ?
+      if (
+        this.onBoard(x1 - step[0], y1 - step[1]) &&
+        this.getColor(x1 - step[0], y1 - step[1]) == oppCol
+      ) {
+        let mv = super.getBasicMove([x1, y1], [x2, y2]);
+        this.addCapture([x1, y1], [-step[0], -step[1]], mv, oppCol);
+        mv.choice = this.stepToArrow(-step[0] + "_" + (-step[1]), false);
+        moves.push(mv);
+      }
+    }
+    else {
+      // Non-capturing move: simple situation
+      moves.push(super.getBasicMove([x1, y1], [x2, y2]));
+    }
+    return moves;
   }
 
   atLeastOneCapture() {
@@ -234,6 +282,9 @@ export default class FanoronaRules extends AbstractOnGridRules {
           this.getColor(i, j) == this.turn &&
           this.getPotentialMovesFrom([i, j], true)
         ) {
+
+console.log(i, j); //TODO: seems that canCapture is wrong with few stones...
+
           return true;
         }
       }