Some fixes, draw lines on board, add 7 variants
[vchess.git] / client / src / base_rules.js
index 167be91..99de626 100644 (file)
@@ -80,6 +80,11 @@ export const ChessRules = class ChessRules {
     return V.ShowMoves;
   }
 
+  // Used for Monochrome variant (TODO: harmonize: !canFlip ==> showFirstTurn)
+  get showFirstTurn() {
+    return false;
+  }
+
   // Some variants always show the same orientation
   static get CanFlip() {
     return true;
@@ -88,6 +93,25 @@ export const ChessRules = class ChessRules {
     return V.CanFlip;
   }
 
+  // For (generally old) variants without checkered board
+  static get Monochrome() {
+    return false;
+  }
+
+  // Some variants require lines drawing
+  static get Lines() {
+    if (V.Monochrome) {
+      let lines = [];
+      // Draw all inter-squares lines
+      for (let i = 0; i <= V.size.x; i++)
+        lines.push([[i, 0], [i, V.size.y]]);
+      for (let j = 0; j <= V.size.y; j++)
+        lines.push([[0, j], [V.size.x, j]]);
+      return lines;
+    }
+    return null;
+  }
+
   // Some variants use click infos:
   doClick() {
     return null;
@@ -263,7 +287,8 @@ export const ChessRules = class ChessRules {
   }
 
   // On which squares is color under check ? (for interface)
-  getCheckSquares(color) {
+  getCheckSquares() {
+    const color = this.turn;
     return (
       this.underCheck(color)
         // kingPos must be duplicated, because it may change:
@@ -468,7 +493,7 @@ export const ChessRules = class ChessRules {
       return;
     const fenParsed = V.ParseFen(fen);
     this.board = V.GetBoard(fenParsed.position);
-    this.turn = fenParsed.turn[0]; //[0] to work with MarseilleRules
+    this.turn = fenParsed.turn;
     this.movesCount = parseInt(fenParsed.movesCount);
     this.setOtherVariables(fen);
   }
@@ -703,7 +728,7 @@ export const ChessRules = class ChessRules {
   // Consider all potential promotions:
   addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
     let finalPieces = [V.PAWN];
-    const color = this.turn;
+    const color = this.turn; //this.getColor(x1, y1);
     const lastRank = (color == "w" ? 0 : V.size.x - 1);
     if (x2 == lastRank) {
       // promotions arg: special override for Hiddenqueen variant
@@ -720,7 +745,7 @@ export const ChessRules = class ChessRules {
 
   // What are the pawn moves from square x,y ?
   getPotentialPawnMoves([x, y], promotions) {
-    const color = this.turn;
+    const color = this.turn; //this.getColor(x, y);
     const [sizeX, sizeY] = [V.size.x, V.size.y];
     const pawnShiftX = V.PawnSpecs.directions[color];
     const firstRank = (color == "w" ? sizeX - 1 : 0);
@@ -989,12 +1014,11 @@ export const ChessRules = class ChessRules {
     const color = this.turn;
     for (let i = 0; i < V.size.x; i++) {
       for (let j = 0; j < V.size.y; j++) {
-        if (this.getColor(i, j) == color) {
+        if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
           const moves = this.getPotentialMovesFrom([i, j]);
           if (moves.length > 0) {
-            for (let k = 0; k < moves.length; k++) {
+            for (let k = 0; k < moves.length; k++)
               if (this.filterValid([moves[k]]).length > 0) return true;
-            }
           }
         }
       }
@@ -1036,21 +1060,15 @@ export const ChessRules = class ChessRules {
   }
 
   // Is square x,y attacked by 'color' pawns ?
-  isAttackedByPawn([x, y], color) {
+  isAttackedByPawn(sq, color) {
     const pawnShift = (color == "w" ? 1 : -1);
-    if (x + pawnShift >= 0 && x + pawnShift < V.size.x) {
-      for (let i of [-1, 1]) {
-        if (
-          y + i >= 0 &&
-          y + i < V.size.y &&
-          this.getPiece(x + pawnShift, y + i) == V.PAWN &&
-          this.getColor(x + pawnShift, y + i) == color
-        ) {
-          return true;
-        }
-      }
-    }
-    return false;
+    return this.isAttackedBySlideNJump(
+      sq,
+      color,
+      V.PAWN,
+      [[pawnShift, 1], [pawnShift, -1]],
+      "oneStep"
+    );
   }
 
   // Is square x,y attacked by 'color' rooks ?
@@ -1246,10 +1264,11 @@ export const ChessRules = class ChessRules {
     return 3;
   }
 
-  getComputerMove() {
+  // 'movesList' arg for some variants to provide a custom list
+  getComputerMove(movesList) {
     const maxeval = V.INFINITY;
     const color = this.turn;
-    let moves1 = this.getAllValidMoves();
+    let moves1 = movesList || this.getAllValidMoves();
 
     if (moves1.length == 0)
       // TODO: this situation should not happen