Fix Omega promotions
[vchess.git] / client / src / variants / Omega.js
index fa95f5e..355960b 100644 (file)
@@ -36,6 +36,29 @@ export class OmegaRules extends ChessRules {
     return ([V.CHAMPION, V.WIZARD].includes(b[1]) ? "Omega/" : "") + b;
   }
 
+  // TODO: the wall position should be checked too
+  static IsGoodPosition(position) {
+    if (position.length == 0) return false;
+    const rows = position.split("/");
+    if (rows.length != V.size.x) return false;
+    let kings = { "k": 0, "K": 0 };
+    for (let row of rows) {
+      let sumElts = 0;
+      for (let i = 0; i < row.length; i++) {
+        if (['K','k'].includes(row[i])) kings[row[i]]++;
+        if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++;
+        else {
+          const num = parseInt(row[i], 10);
+          if (isNaN(num)) return false;
+          sumElts += num;
+        }
+      }
+      if (sumElts != V.size.y) return false;
+    }
+    if (Object.values(kings).some(v => v != 1)) return false;
+    return true;
+  }
+
   // NOTE: keep this extensive check because the board has holes
   static IsGoodEnpassant(enpassant) {
     if (enpassant != "-") {
@@ -139,21 +162,19 @@ export class OmegaRules extends ChessRules {
       // The second bishop must be on a square of different color
       let randIndex_tmp = 2 * randInt(5) + 1;
       const bishop2Pos = positions[randIndex_tmp];
-      positions.splice(Math.max(randIndex, randIndex_tmp), 1);
-      positions.splice(Math.min(randIndex, randIndex_tmp), 1);
 
       // Get random squares for champions
-      randIndex = 2 * randInt(4);
-      let bishopSameColorPos = (bishop1Pos % 2 == 0 ? bishop1Pos : bishop2Pos);
-      if (randIndex >= bishopSameColorPos) randIndex += 2;
-      const champion1Pos = positions[randIndex];
+      let randIndexC = 2 * randInt(4);
+      if (randIndexC >= bishop1Pos) randIndexC += 2;
+      const champion1Pos = positions[randIndexC];
       // The second champion must be on a square of different color
-      randIndex_tmp = 2 * randInt(4) + 1;
-      bishopSameColorPos = (bishop1Pos % 2 == 0 ? bishop1Pos : bishop2Pos);
-      if (randIndex_tmp >= bishopSameColorPos) randIndex_tmp += 2;
-      const champion2Pos = positions[randIndex_tmp];
-      positions.splice(Math.max(randIndex, randIndex_tmp), 1);
-      positions.splice(Math.min(randIndex, randIndex_tmp), 1);
+      let randIndex_tmpC = 2 * randInt(4) + 1;
+      if (randIndex_tmpC >= bishop2Pos) randIndex_tmpC += 2;
+      const champion2Pos = positions[randIndex_tmpC];
+
+      let usedIndices = [randIndex, randIndex_tmp, randIndexC, randIndex_tmpC];
+      usedIndices.sort();
+      for (let i = 3; i >= 0; i--) positions.splice(usedIndices[i], 1);
 
       // Get random squares for other pieces
       randIndex = randInt(6);
@@ -182,7 +203,7 @@ export class OmegaRules extends ChessRules {
       pieces[c][knight2Pos] = "n";
       pieces[c][rook2Pos] = "r";
       pieces[c][champion2Pos] = "c";
-      flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
+      flags += V.CoordToColumn(rook1Pos+1) + V.CoordToColumn(rook2Pos+1);
     }
     // Add turn + flags + enpassant
     return (
@@ -206,6 +227,15 @@ export class OmegaRules extends ChessRules {
     return res.slice(0, -1); //remove last comma
   }
 
+  canTake([x1, y1], [x2, y2]) {
+    return (
+      // Cannot take wall :)
+      // NOTE: this check is useful only for pawns where OnBoard() isn't used
+      this.board[x2][y2] != V.NOTHING &&
+      this.getColor(x1, y1) !== this.getColor(x2, y2)
+    );
+  }
+
   // En-passant after 2-sq or 3-sq jumps
   getEpSquare(moveOrSquare) {
     if (!moveOrSquare) return undefined;
@@ -252,7 +282,7 @@ export class OmegaRules extends ChessRules {
     }
   }
 
-  getEnpassanCaptures([x, y], shiftX) {
+  getEnpassantCaptures([x, y], shiftX) {
     const Lep = this.epSquares.length;
     const epSquare = this.epSquares[Lep - 1];
     let moves = [];
@@ -277,6 +307,22 @@ export class OmegaRules extends ChessRules {
     return moves;
   }
 
+  addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
+    let finalPieces = [V.PAWN];
+    const color = this.turn;
+    const lastRank = (color == "w" ? 1 : V.size.x - 2);
+    if (x2 == lastRank) {
+      // promotions arg: special override for Hiddenqueen variant
+      if (!!promotions) finalPieces = promotions;
+      else if (!!V.PawnSpecs.promotions) finalPieces = V.PawnSpecs.promotions;
+    }
+    let tr = null;
+    for (let piece of finalPieces) {
+      tr = (piece != V.PAWN ? { c: color, p: piece } : null);
+      moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
+    }
+  }
+
   getPotentialChampionMoves(sq) {
     return this.getSlideNJumpMoves(sq, V.steps[V.CHAMPION], "oneStep");
   }
@@ -447,4 +493,17 @@ export class OmegaRules extends ChessRules {
       k: 1000
     };
   }
+
+  evalPosition() {
+    let evaluation = 0;
+    for (let i = 0; i < V.size.x; i++) {
+      for (let j = 0; j < V.size.y; j++) {
+        if (![V.EMPTY,V.NOTHING].includes(this.board[i][j])) {
+          const sign = this.getColor(i, j) == "w" ? 1 : -1;
+          evaluation += sign * V.VALUES[this.getPiece(i, j)];
+        }
+      }
+    }
+    return evaluation;
+  }
 };