Some fixes + draft newmove pingback logic (unfinished, not working)
[vchess.git] / client / src / base_rules.js
index 149bb4d..b39fe9c 100644 (file)
@@ -197,12 +197,13 @@ export const ChessRules = class ChessRules {
     const move = moveOrSquare;
     const s = move.start,
           e = move.end;
-    // NOTE: next conditions are first for Atomic, and last for Checkered
+    // NOTE: next conditions are first for Crazyhouse, and last for Checkered
+    // TODO: Checkered exceptions are too weird and should move in its own file.
     if (
-      move.appear.length > 0 &&
+      move.vanish.length > 0 &&
       Math.abs(s.x - e.x) == 2 &&
       s.y == e.y &&
-      move.appear[0].p == V.PAWN &&
+      move.vanish[0].p == V.PAWN &&
       ["w", "b"].includes(move.appear[0].c)
     ) {
       return {
@@ -238,11 +239,20 @@ export const ChessRules = class ChessRules {
   /////////////
   // FEN UTILS
 
-  // Setup the initial random (assymetric) position
-  static GenRandInitFen() {
+  // Setup the initial random (asymmetric) position
+  static GenRandInitFen(randomness) {
+    if (randomness == 0)
+      // Deterministic:
+      return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 1111 -";
+
     let pieces = { w: new Array(8), b: new Array(8) };
-    // Shuffle pieces on first and last rank
+    // Shuffle pieces on first (and last rank if randomness == 2)
     for (let c of ["w", "b"]) {
+      if (c == 'b' && randomness == 1) {
+        pieces['b'] = pieces['w'];
+        break;
+      }
+
       let positions = ArrayFun.range(8);
 
       // Get random squares for bishops
@@ -310,16 +320,24 @@ export const ChessRules = class ChessRules {
   // Return current fen (game state)
   getFen() {
     return (
-      this.getBaseFen() +
-      " " +
-      this.getTurnFen() +
-      " " +
+      this.getBaseFen() + " " +
+      this.getTurnFen() + " " +
       this.movesCount +
       (V.HasFlags ? " " + this.getFlagsFen() : "") +
       (V.HasEnpassant ? " " + this.getEnpassantFen() : "")
     );
   }
 
+  getFenForRepeat() {
+    // Omit movesCount, only variable allowed to differ
+    return (
+      this.getBaseFen() + "_" +
+      this.getTurnFen() +
+      (V.HasFlags ? "_" + this.getFlagsFen() : "") +
+      (V.HasEnpassant ? "_" + this.getEnpassantFen() : "")
+    );
+  }
+
   // Position part of the FEN string
   getBaseFen() {
     let position = "";
@@ -395,14 +413,12 @@ export const ChessRules = class ChessRules {
   //////////////////
   // INITIALIZATION
 
-  constructor(fen) {
-    // In printDiagram() fen isn't supply because only getPpath() is used
-    if (fen)
-      this.re_init(fen);
-  }
-
   // Fen string fully describes the game state
-  re_init(fen) {
+  constructor(fen) {
+    if (!fen)
+      // In printDiagram() fen isn't supply because only getPpath() is used
+      // TODO: find a better solution!
+      return;
     const fenParsed = V.ParseFen(fen);
     this.board = V.GetBoard(fenParsed.position);
     this.turn = fenParsed.turn[0]; //[0] to work with MarseilleRules