Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / variants / Checkered.js
index 882a32a..79ea63c 100644 (file)
@@ -71,8 +71,8 @@ export class CheckeredRules extends ChessRules {
   setFlags(fenflags) {
     super.setFlags(fenflags); //castleFlags
     this.pawnFlags = {
-      w: [...Array(8).fill(true)], //pawns can move 2 squares?
-      b: [...Array(8).fill(true)]
+      w: [...Array(8)], //pawns can move 2 squares?
+      b: [...Array(8)]
     };
     const flags = fenflags.substr(4); //skip first 4 letters, for castle
     for (let c of ["w", "b"]) {
@@ -303,7 +303,8 @@ export class CheckeredRules extends ChessRules {
   }
 
   isAttackedByBishop(sq, colors) {
-    return this.isAttackedBySlideNJump(sq, colors, V.BISHOP, V.steps[V.BISHOP]);
+    return this.isAttackedBySlideNJump(
+      sq, colors, V.BISHOP, V.steps[V.BISHOP]);
   }
 
   isAttackedByQueen(sq, colors) {
@@ -337,7 +338,7 @@ export class CheckeredRules extends ChessRules {
       "c"
     ]);
     let res = kingAttacked
-      ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
+      ? [JSON.parse(JSON.stringify(this.kingPos[color]))]
       : [];
     this.turn = color;
     return res;
@@ -402,19 +403,19 @@ export class CheckeredRules extends ChessRules {
 
   getFen() {
     const L = this.cmoves.length;
-    const cmoveFen = !this.cmoves[L - 1]
-      ? "-"
-      : ChessRules.CoordsToSquare(this.cmoves[L - 1].start) +
-        ChessRules.CoordsToSquare(this.cmoves[L - 1].end);
+    const cmoveFen =
+      !this.cmoves[L - 1]
+        ? "-"
+        : ChessRules.CoordsToSquare(this.cmoves[L - 1].start) +
+          ChessRules.CoordsToSquare(this.cmoves[L - 1].end);
     return super.getFen() + " " + cmoveFen;
   }
 
   getFlagsFen() {
     let fen = super.getFlagsFen();
     // Add pawns flags
-    for (let c of ["w", "b"]) {
-      for (let i = 0; i < 8; i++) fen += this.pawnFlags[c][i] ? "1" : "0";
-    }
+    for (let c of ["w", "b"])
+      for (let i = 0; i < 8; i++) fen += (this.pawnFlags[c][i] ? "1" : "0");
     return fen;
   }
 
@@ -429,37 +430,25 @@ export class CheckeredRules extends ChessRules {
       return "0-0";
     }
 
-    // Translate final square
     const finalSquare = V.CoordsToSquare(move.end);
-
     const piece = this.getPiece(move.start.x, move.start.y);
+    let notation = "";
     if (piece == V.PAWN) {
       // Pawn move
-      let notation = "";
       if (move.vanish.length > 1) {
         // Capture
         const startColumn = V.CoordToColumn(move.start.y);
-        notation =
-          startColumn +
-          "x" +
-          finalSquare +
-          "=" +
-          move.appear[0].p.toUpperCase();
-      } //no capture
-      else {
-        notation = finalSquare;
-        if (move.appear.length > 0 && piece != move.appear[0].p)
-          //promotion
-          notation += "=" + move.appear[0].p.toUpperCase();
-      }
-      return notation;
+        notation = startColumn + "x" + finalSquare;
+      } else notation = finalSquare;
+    } else {
+      // Piece movement
+      notation =
+        piece.toUpperCase() +
+        (move.vanish.length > 1 ? "x" : "") +
+        finalSquare;
     }
-    // Piece movement
-    return (
-      piece.toUpperCase() +
-      (move.vanish.length > 1 ? "x" : "") +
-      finalSquare +
-      (move.vanish.length > 1 ? "=" + move.appear[0].p.toUpperCase() : "")
-    );
+    if (move.appear[0].p != move.vanish[0].p)
+      notation += "=" + move.appear[0].p.toUpperCase();
+    return notation;
   }
 };