Add Gomoku + Atarigo
[vchess.git] / client / src / variants / Konane.js
index 1ff77ee..1daad7a 100644 (file)
@@ -1,4 +1,5 @@
 import { ChessRules, Move, PiPo } from "@/base_rules";
+import { randInt } from "@/utils/alea";
 
 export class KonaneRules extends ChessRules {
 
@@ -14,10 +15,6 @@ export class KonaneRules extends ChessRules {
     return true;
   }
 
-  static get PIECES() {
-    return V.PAWN;
-  }
-
   getPiece() {
     return V.PAWN;
   }
@@ -33,7 +30,7 @@ export class KonaneRules extends ChessRules {
     for (let row of rows) {
       let sumElts = 0;
       for (let i = 0; i < row.length; i++) {
-        if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
+        if (row[i].toLowerCase() == V.PAWN) sumElts++;
         else {
           const num = parseInt(row[i], 10);
           if (isNaN(num) || num <= 0) return false;
@@ -56,9 +53,9 @@ export class KonaneRules extends ChessRules {
     this.captures = []; //reinit for each move
   }
 
-  hoverHighlight(x, y) {
-    if (this.movesCount >= 2) return false;
+  hoverHighlight([x, y], side) {
     const c = this.turn;
+    if (this.movesCount >= 2 || (!!side && side != c)) return false;
     if (c == 'w') return (x == y && [0, 3, 4, 7].includes(x));
     // "Black": search for empty square and allow nearby
     for (let i of [0, 3, 4, 7]) {
@@ -199,8 +196,27 @@ export class KonaneRules extends ChessRules {
     else this.captures.pop();
   }
 
-  static get SEARCH_DEPTH() {
-    return 4;
+  getComputerMove() {
+    const color = this.turn;
+    let mvArray = [];
+    let mv = null;
+    const undoAll = () => {
+      for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]);
+    };
+    // Just play random moves (for now at least. TODO?)
+    while (this.turn == color) {
+      let moves = super.getAllValidMoves();
+      if (moves.length == 0) {
+        // Shouldn't happen, but...
+        undoAll();
+        return null;
+      }
+      mv = moves[randInt(moves.length)];
+      mvArray.push(mv);
+      this.play(mv);
+    }
+    undoAll();
+    return (mvArray.length > 1 ? mvArray : mvArray[0]);
   }
 
   getNotation(move) {