From 616a8d7ae5ee96fe23d393cf6e4554b2cf3b9245 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 25 Jan 2023 10:11:55 +0100
Subject: [PATCH] Advance on Checkered. TODO: fix checks detection by checkered
 pieces

---
 base_rules.js                      | 12 ++++++------
 variants/Alapo/class.js            |  2 +-
 variants/Alice/class.js            |  2 +-
 variants/Allmate/class.js          |  4 ++--
 variants/Ambiguous/class.js        |  6 +++---
 variants/Apocalypse/class.js       |  6 +++---
 variants/Avalanche/class.js        |  2 +-
 variants/Bario/class.js            |  2 +-
 variants/Baroque/class.js          |  2 +-
 variants/Benedict/class.js         |  2 +-
 variants/Chakart/class.js          | 12 ++++++------
 variants/Checkered/class.js        | 22 +++++++++++++++++-----
 variants/Checkered/style.css       | 10 +++++-----
 variants/Hex/class.js              |  6 +++---
 variants/Weiqi/class.js            |  4 ++--
 variants/_Flip/class.js            |  2 +-
 variants/_SpecialCaptures/class.js |  8 ++++----
 17 files changed, 58 insertions(+), 46 deletions(-)

diff --git a/base_rules.js b/base_rules.js
index aa499f8..ec2571a 100644
--- a/base_rules.js
+++ b/base_rules.js
@@ -640,7 +640,7 @@ export default class ChessRules {
           const color = this.getColor(i, j);
           const piece = this.getPiece(i, j);
           addPiece(i, j, "g_pieces", this.pieces(color, i, j)[piece]["class"]);
-          this.g_pieces[i][j].classList.add(C.GetColorClass(color));
+          this.g_pieces[i][j].classList.add(V.GetColorClass(color));
           if (this.enlightened && !this.enlightened[i][j])
             this.g_pieces[i][j].classList.add("hidden");
         }
@@ -707,7 +707,7 @@ export default class ChessRules {
         rcontainer.appendChild(r_cell);
         let piece = document.createElement("piece");
         C.AddClass_es(piece, this.pieces(c, c, p)[p]["class"]);
-        piece.classList.add(C.GetColorClass(c));
+        piece.classList.add(V.GetColorClass(c));
         piece.style.width = "100%";
         piece.style.height = "100%";
         this.r_pieces[c][p] = piece;
@@ -1016,7 +1016,7 @@ export default class ChessRules {
       const cdisp = moves[i].choice || moves[i].appear[0].p;
       C.AddClass_es(piece,
         this.pieces(color, moves[i].end.x, moves[i].end.y)[cdisp]["class"]);
-      piece.classList.add(C.GetColorClass(color));
+      piece.classList.add(V.GetColorClass(color));
       piece.style.width = "100%";
       piece.style.height = "100%";
       choice.appendChild(piece);
@@ -2426,7 +2426,7 @@ export default class ChessRules {
       this.g_pieces[a.x][a.y] = document.createElement("piece");
       C.AddClass_es(this.g_pieces[a.x][a.y],
                     this.pieces(a.c, a.x, a.y)[a.p]["class"]);
-      this.g_pieces[a.x][a.y].classList.add(C.GetColorClass(a.c));
+      this.g_pieces[a.x][a.y].classList.add(V.GetColorClass(a.c));
       this.g_pieces[a.x][a.y].style.width = pieceWidth + "px";
       this.g_pieces[a.x][a.y].style.height = pieceWidth + "px";
       const [ip, jp] = this.getPixelPosition(a.x, a.y, r);
@@ -2503,8 +2503,8 @@ export default class ChessRules {
       C.RemoveClass_es(movingPiece, pieces[startCode]["class"]);
       C.AddClass_es(movingPiece, pieces[drag.p]["class"]);
       if (apparentColor != drag.c) {
-        movingPiece.classList.remove(C.GetColorClass(apparentColor));
-        movingPiece.classList.add(C.GetColorClass(drag.c));
+        movingPiece.classList.remove(V.GetColorClass(apparentColor));
+        movingPiece.classList.add(V.GetColorClass(drag.c));
       }
     }
     container.appendChild(movingPiece);
diff --git a/variants/Alapo/class.js b/variants/Alapo/class.js
index 969c80f..9fcfc98 100644
--- a/variants/Alapo/class.js
+++ b/variants/Alapo/class.js
@@ -108,7 +108,7 @@ export default class AlapoRules extends ChessRules {
     // Try both colors (to detect potential suicides)
     let won = {};
     for (let c of ['w', 'b']) {
-      const oppCol = C.GetOppCol(c);
+      const oppCol = C.GetOppTurn(c);
       const goal = (c == 'w' ? 0 : 5);
       won[c] = this.board[goal].some((b,j) => {
         return (
diff --git a/variants/Alice/class.js b/variants/Alice/class.js
index d1e389d..b6dd9e5 100644
--- a/variants/Alice/class.js
+++ b/variants/Alice/class.js
@@ -104,7 +104,7 @@ export default class AliceRules extends ChessRules {
 
   filterValid(moves) {
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     const kingPos = this.searchKingPos(color)[0];
     const kingPiece = this.getPiece(kingPos[0], kingPos[1]);
     return super.filterValid(moves).filter(m => {
diff --git a/variants/Allmate/class.js b/variants/Allmate/class.js
index 5b237c0..e491e6d 100644
--- a/variants/Allmate/class.js
+++ b/variants/Allmate/class.js
@@ -35,7 +35,7 @@ export default class AllmateRules extends ChessRules {
     if (move.appear.length > 0)
       this.curMove = move;
     const color = this.turn;
-    const oppCol = C.GetOppCol(this.turn);
+    const oppCol = C.GetOppTurn(this.turn);
     let mv = new Move({
       start: this.curMove.end,
       end: this.curMove.end,
@@ -58,7 +58,7 @@ export default class AllmateRules extends ChessRules {
 
   // is piece on square x,y mated by color?
   isMated(x, y, color) {
-    const myColor = C.GetOppCol(color);
+    const myColor = C.GetOppTurn(color);
     if (!super.underAttack([x, y], color))
       return false;
     for (let i=0; i<this.size.x; i++) {
diff --git a/variants/Ambiguous/class.js b/variants/Ambiguous/class.js
index 6a83374..a0cb7f2 100644
--- a/variants/Ambiguous/class.js
+++ b/variants/Ambiguous/class.js
@@ -45,7 +45,7 @@ export default class AmbiguousRules extends ChessRules {
   // Subturn 2: play a move for me (which just indicate a square).
   getPotentialMovesFrom([x, y]) {
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     if (this.subTurn == 2) {
       // Just play a normal move (which in fact only indicate a square)
       let movesHash = {};
@@ -161,7 +161,7 @@ export default class AmbiguousRules extends ChessRules {
 
   getCurrentScore() {
     // This function is only called at subTurn 1
-    const color = C.GetOppCol(this.turn);
+    const color = C.GetOppTurn(this.turn);
     if (this.searchKingPos(color).length == 0)
       return (color == 'w' ? "0-1" : "1-0");
     return "*";
@@ -170,7 +170,7 @@ export default class AmbiguousRules extends ChessRules {
   postPlay(move) {
     const color = this.turn;
     if (this.subTurn == 2 || this.searchKingPos(color).length == 0) {
-      this.turn = C.GetOppCol(color);
+      this.turn = C.GetOppTurn(color);
       this.movesCount++;
     }
     this.subTurn = 3 - this.subTurn;
diff --git a/variants/Apocalypse/class.js b/variants/Apocalypse/class.js
index ace9c4d..3b735b1 100644
--- a/variants/Apocalypse/class.js
+++ b/variants/Apocalypse/class.js
@@ -102,7 +102,7 @@ export default class ApocalypseRules extends ChessRules {
       }
     }
     else {
-      const oppCol = C.GetOppCol(this.getColor(x, y));
+      const oppCol = C.GetOppTurn(this.getColor(x, y));
       moves = super.getPotentialMovesFrom([x, y]).filter(m => {
         // Remove pawn push toward own color (absurd)
         return (
@@ -222,7 +222,7 @@ export default class ApocalypseRules extends ChessRules {
       if (!res.wm || !res.bm)
         return;
       for (let c of ['w', 'b']) {
-        const myMove = res[c + 'm'], oppMove = res[C.GetOppCol(c) + 'm'];
+        const myMove = res[c + 'm'], oppMove = res[C.GetOppTurn(c) + 'm'];
         if (
           // More general test than checking moves ends,
           // because of potential pawn relocation
@@ -306,7 +306,7 @@ export default class ApocalypseRules extends ChessRules {
       };
       this.playVisual(revFirstMove);
     }
-    this.turn = C.GetOppCol(color);
+    this.turn = C.GetOppTurn(color);
     this.movesCount++;
     this.subTurn = 1;
     this.firstMove = null;
diff --git a/variants/Avalanche/class.js b/variants/Avalanche/class.js
index a1d4d54..dbbe9d5 100644
--- a/variants/Avalanche/class.js
+++ b/variants/Avalanche/class.js
@@ -118,7 +118,7 @@ export default class AvalancheRules extends ChessRules {
 
   postPlay(move) {
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     this.promotion = (
       this.subTurn == 2 &&
       move.end.x == (oppCol == 'w' ? 0 : this.size.x - 1) &&
diff --git a/variants/Bario/class.js b/variants/Bario/class.js
index 1874383..2dd64d6 100644
--- a/variants/Bario/class.js
+++ b/variants/Bario/class.js
@@ -233,7 +233,7 @@ export default class BarioRules extends ChessRules {
   tryChangeTurn(move, captureUndef) {
     this.definition = null;
     this.subTurn = captureUndef ? 0 : 1;
-    this.turn = C.GetOppCol(this.turn);
+    this.turn = C.GetOppTurn(this.turn);
     this.movesCount++;
   }
 
diff --git a/variants/Baroque/class.js b/variants/Baroque/class.js
index f2039d5..6434e05 100644
--- a/variants/Baroque/class.js
+++ b/variants/Baroque/class.js
@@ -99,7 +99,7 @@ export default class BaroqueRules extends AbstractSpecialCaptureRules {
   isImmobilized([x, y]) {
     const piece = this.getPiece(x, y);
     const color = this.getColor(x, y);
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     const adjacentSteps = this.pieces()['k'].moves[0].steps;
     for (let step of adjacentSteps) {
       const [i, j] = [x + step[0], this.getY(y + step[1])];
diff --git a/variants/Benedict/class.js b/variants/Benedict/class.js
index 5503f2f..0fda796 100644
--- a/variants/Benedict/class.js
+++ b/variants/Benedict/class.js
@@ -35,7 +35,7 @@ export default class BenedictRules extends AbstractFlipRules {
   }
 
   postProcessPotentialMoves(moves) {
-    const oppCol = C.GetOppCol(this.turn);
+    const oppCol = C.GetOppTurn(this.turn);
     let bMoves = super.postProcessPotentialMoves(moves);
     bMoves.forEach(m => {
       m.flips = [];
diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js
index 4c55220..6ddc37c 100644
--- a/variants/Chakart/class.js
+++ b/variants/Chakart/class.js
@@ -220,7 +220,7 @@ export default class ChakartRules extends ChessRules {
       moves = this.getDropMovesFrom([x, y]);
     else if (this.egg == "kingboo") {
       const color = this.turn;
-      const oppCol = C.GetOppCol(color);
+      const oppCol = C.GetOppTurn(color);
       // Only allow to swap (non-immobilized!) pieces
       for (let i=0; i<this.size.x; i++) {
         for (let j=0; j<this.size.y; j++) {
@@ -282,7 +282,7 @@ export default class ChakartRules extends ChessRules {
 
   getPawnMovesFrom([x, y]) {
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     const shiftX = (color == 'w' ? -1 : 1);
     const firstRank = (color == "w" ? this.size.x - 1 : 0);
     let moves = [];
@@ -392,7 +392,7 @@ export default class ChakartRules extends ChessRules {
 
   play(move) {
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     this.egg = move.egg;
     if (move.egg == "toadette") {
       this.reserve = { w: {}, b: {} };
@@ -478,7 +478,7 @@ export default class ChakartRules extends ChessRules {
       )
     ) {
       // "Forgotten" promotion, which occurred after some effect
-      let moves = super.pawnPostProcess([move], color, C.GetOppCol(color));
+      let moves = super.pawnPostProcess([move], color, C.GetOppTurn(color));
       super.showChoices(moves, r);
     }
     else
@@ -589,8 +589,8 @@ export default class ChakartRules extends ChessRules {
       case "luigi":
       case "waluigi":
         // Change color of friendly or enemy piece, king excepted
-        const oldColor = (move.egg == "waluigi" ? color : C.GetOppCol(color));
-        const newColor = C.GetOppCol(oldColor);
+        const oldColor = (move.egg == "waluigi" ? color : C.GetOppTurn(color));
+        const newColor = C.GetOppTurn(oldColor);
         const coords = getRandomPiece(oldColor);
         if (coords) {
           const piece = this.getPiece(coords[0], coords[1]);
diff --git a/variants/Checkered/class.js b/variants/Checkered/class.js
index 9c78243..35bfc79 100644
--- a/variants/Checkered/class.js
+++ b/variants/Checkered/class.js
@@ -28,6 +28,12 @@ export default class CheckeredRules extends ChessRules {
     };
   }
 
+  static GetColorClass(c) {
+    if (c == 'c')
+      return "checkered";
+    return C.GetColorClass(c);
+  }
+
   static board2fen(b) {
     const checkered_codes = {
       p: "s",
@@ -60,6 +66,12 @@ export default class CheckeredRules extends ChessRules {
     return super.fen2board(f);
   }
 
+  genRandInitBaseFen() {
+    let res = super.genRandInitBaseFen();
+    res.o.flags += "1".repeat(16); //pawns flags
+    return res;
+  }
+
   getPartFen(o) {
     return Object.assign(
       {
@@ -243,7 +255,7 @@ export default class CheckeredRules extends ChessRules {
             return false; //forbidden 2-squares jumps
           }
           if (
-            this.board[m.end.x][m.end.y] == V.EMPTY &&
+            this.board[m.end.x][m.end.y] == "" &&
             m.vanish.length == 2 &&
             this.getColor(m.start.x, m.start.y) == "c"
           ) {
@@ -304,7 +316,7 @@ export default class CheckeredRules extends ChessRules {
 
   filterValid(moves) {
     const color = this.turn;
-    if (stage == 2 && this.sideCheckered == color)
+    if (this.stage == 2 && this.sideCheckered == color)
       // Checkered cannot be under check (no king)
       return moves;
     let kingPos = super.searchKingPos(color);
@@ -315,7 +327,7 @@ export default class CheckeredRules extends ChessRules {
     return moves.filter(m => {
       this.playOnBoard(m);
       let res = true;
-      if (stage == 1)
+      if (this.stage == 1)
         res = !this.oppositeMoves(this.cmove, m);
       if (res && m.appear.length > 0)
         res = !this.underCheck(kingPos, oppCols);
@@ -353,7 +365,7 @@ export default class CheckeredRules extends ChessRules {
   underCheck(square_s, oppCols) {
     if (this.stage == 2 && oppCol != this.sideCheckered)
       return false; //checkered pieces is me, I'm not under check
-    return super.underAttack(square_s, oppCols);
+    return square_s.some(sq => super.underAttack(sq, oppCols));
   }
 
   prePlay(move) {
@@ -361,7 +373,7 @@ export default class CheckeredRules extends ChessRules {
       super.prePlay(move);
       if (
         [1, 6].includes(move.start.x) &&
-        move.vanish[0].p == V.PAWN &&
+        move.vanish[0].p == 'p' &&
         Math.abs(move.end.x - move.start.x) == 2
       ) {
         // This move turns off a 2-squares pawn flag
diff --git a/variants/Checkered/style.css b/variants/Checkered/style.css
index a6582e7..91b60ac 100644
--- a/variants/Checkered/style.css
+++ b/variants/Checkered/style.css
@@ -1,17 +1,17 @@
 @import url("/base_pieces.css");
 
-piece.checkered-pawn {
+piece.checkered.pawn {
   background-image: url('/variants/Checkered/pieces/cp.svg');
 }
-piece.checkered-rook {
+piece.checkered.rook {
   background-image: url('/variants/Checkered/pieces/cr.svg');
 }
-piece.checkered-knight {
+piece.checkered.knight {
   background-image: url('/variants/Checkered/pieces/cn.svg');
 }
-piece.checkered-bishop {
+piece.checkered.bishop {
   background-image: url('/variants/Checkered/pieces/cb.svg');
 }
-piece.checkered-queen {
+piece.checkered.queen {
   background-image: url('/variants/Checkered/pieces/cq.svg');
 }
diff --git a/variants/Hex/class.js b/variants/Hex/class.js
index f58800f..05200be 100644
--- a/variants/Hex/class.js
+++ b/variants/Hex/class.js
@@ -66,7 +66,7 @@ export default class HexRules extends AbstractClickFillRules {
         new PiPo({
           x: coords.x,
           y: coords.y,
-          c: C.GetOppCol(this.turn),
+          c: C.GetOppTurn(this.turn),
           p: 'p'
         })
       );
@@ -168,11 +168,11 @@ export default class HexRules extends AbstractClickFillRules {
   play(move) {
     this.playOnBoard(move);
     this.movesCount++;
-    this.turn = C.GetOppCol(this.turn);
+    this.turn = C.GetOppTurn(this.turn);
   }
 
   getCurrentScore() {
-    const oppCol = C.GetOppCol(this.turn);
+    const oppCol = C.GetOppTurn(this.turn);
     // Search for connecting path of opp color:
     let explored = {}, component;
     let min, max;
diff --git a/variants/Weiqi/class.js b/variants/Weiqi/class.js
index ca13fe1..04ad40a 100644
--- a/variants/Weiqi/class.js
+++ b/variants/Weiqi/class.js
@@ -137,7 +137,7 @@ export default class WeiqiRules extends ChessRules {
     if (this.board[x][y] != "" || this.turn != this.playerColor)
       return null;
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     let move = new Move({
       appear: [new PiPo({x: x, y: y, c: color, p: 's'})],
       vanish: [],
@@ -201,7 +201,7 @@ export default class WeiqiRules extends ChessRules {
     if (move.pass) {
       if (this.turn != this.playerColor)
         super.displayMessage(null, "pass", "pass-text", 2000);
-      this.turn = C.GetOppCol(this.turn);
+      this.turn = C.GetOppTurn(this.turn);
     }
     else
       super.play(move);
diff --git a/variants/_Flip/class.js b/variants/_Flip/class.js
index e2e2bf5..5af268f 100644
--- a/variants/_Flip/class.js
+++ b/variants/_Flip/class.js
@@ -27,7 +27,7 @@ export default class AbstractFlipRules extends ChessRules {
 
   flipColorOf(flips) {
     for (let xy of flips) {
-      const newColor = C.GetOppCol(this.getColor(xy.x, xy.y));
+      const newColor = C.GetOppTurn(this.getColor(xy.x, xy.y));
       this.board[xy.x][xy.y] = newColor + this.board[xy.x][xy.y][1];
     }
   }
diff --git a/variants/_SpecialCaptures/class.js b/variants/_SpecialCaptures/class.js
index 9b48266..e6a0db1 100644
--- a/variants/_SpecialCaptures/class.js
+++ b/variants/_SpecialCaptures/class.js
@@ -23,7 +23,7 @@ export default class AbstractSpecialCaptureRules extends ChessRules {
   addPincerCaptures(moves, byChameleon) {
     const steps = this.pieces()['p'].moves[0].steps;
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     moves.forEach(m => {
       if (byChameleon && m.start.x != m.end.x && m.start.y != m.end.y)
         // Chameleon not moving as pawn
@@ -61,7 +61,7 @@ export default class AbstractSpecialCaptureRules extends ChessRules {
 
   addCoordinatorCaptures(moves, byChameleon) {
     const color = this.turn;
-    const oppCol = V.GetOppCol(color);
+    const oppCol = V.GetOppTurn(color);
     const kp = this.searchKingPos(color)[0];
     moves.forEach(m => {
       // Check piece-king rectangle (if any) corners for enemy pieces
@@ -91,7 +91,7 @@ export default class AbstractSpecialCaptureRules extends ChessRules {
     // Look in every direction for captures
     const steps = this.pieces()['r'].moves[0].steps;
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     let moves = [];
     outerLoop: for (let step of steps) {
       let [i, j] = [x + step[0], this.getY(y + step[1])];
@@ -160,7 +160,7 @@ export default class AbstractSpecialCaptureRules extends ChessRules {
     const adjacentSteps = this.pieces()['r'].moves[0].steps;
     let capturingPullDir = {};
     const color = this.turn;
-    const oppCol = C.GetOppCol(color);
+    const oppCol = C.GetOppTurn(color);
     if (type != "push") {
       adjacentSteps.forEach(step => {
         const [bi, bj] = [sx - step[0], this.getY(sy - step[1])];
-- 
2.44.0