update
[xogo.git] / variants / Coregal / class.js
index b563fec..15a99e2 100644 (file)
@@ -1,10 +1,7 @@
-import { ChessRules, Move, PiPo } from "@/base_rules";
-import { ArrayFun } from "@/utils/array";
-import { randInt, sample } from "@/utils/alea";
+import ChessRules from "/base_rules.js";
+import {FenUtil} from "/utils/setupPieces.js"
 
-export class CoregalRules extends ChessRules {
-
-//TODO: CSS royal queen symbol
+export default class CoregalRules extends ChessRules {
 
   genRandInitBaseFen() {
     const s = FenUtil.setupPieces(
@@ -13,55 +10,122 @@ export class CoregalRules extends ChessRules {
         randomness: this.options["randomness"],
         between: [{p1: 'k', p2: 'r'}, {p1: 'l', p2: 'r'}],
         diffCol: ['b'],
-        flags: ['r', 'k', 'l'] //TODO: add 'k' to all 'flags' calls ??!
+        // 'k' and 'l' useful only to get relative position
+        flags: ['r', 'k', 'l']
       }
     );
+    // Re-arrange flags: king + royal queen positions are only
+    // useful to know ordering, and thus allowed castles.
+    let flags = "";
+    let relPos = { 'w': {}, 'b': {} };
+    for (let c of [0, 1]) {
+      const col = (c == 0 ? 'w' : 'b');
+      let first = "";
+      for (let i=4*c; i<4*(c+1); i++) {
+        const pos = parseInt(flags.charAt(i), 10);
+        const symb = s[col][pos];
+        if (['k', 'l'].includes(symb)) {
+          if (!first) {
+            relPos[col][symb] = '0'; //left
+            first = symb;
+          }
+          else
+            relPos[col][symb] = '1'; //right
+        }
+        else
+          flags += flags.charAt(i);
+      }
+    }
     return {
       fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
            s.w.join("").toUpperCase(),
-      o: {flags: s.flags}
+      o: {
+        flags: flags + flags, //duplicate: one for each royal piece
+        relPos: (
+          relPos['w']['k'] + relPos['w']['l'] +
+          relPos['b']['k'] + relPos['b']['l']
+        )
+      }
+    };
+  }
+
+  getPartFen(o) {
+    return (Object.assign(
+      {"relpos": o.relPos},
+      super.getPartFen(o)
+    ));
+  }
+
+  setOtherVariables(fenParsed, pieceArray) {
+    
+//TODO: issue, relPos is set at init but doesn't persist --> see base_rules.js line 263
+console.log(fenParsed);
+    super.setOtherVariables(fenParsed, pieceArray);
+    this.relPos = {
+      'w': {
+        'k': fenParsed.relpos[0],
+        'l': fenParsed.relpos[1]
+      },
+      'b': {
+        'k': fenParsed.relpos[2],
+        'l': fenParsed.relpos[3]
+      }
     };
   }
 
   pieces() {
     let res = super.pieces();
     res['l'] = JSON.parse(JSON.stringify(res['q']));
+    // TODO: CSS royal queen symbol (with cross?)
     res['l']["class"] = "royal_queen";
     return res;
   }
 
   setFlags(fenflags) {
-    // white pieces positions, then black pieces positions
-    this.castleFlags = { w: [...Array(4)], b: [...Array(4)] };
-    for (let i = 0; i < 8; i++) {
-      this.castleFlags[i < 4 ? "w" : "b"][i % 4] =
-        parseInt(fenflags.charAt(i), 10);
-    }
+    this.castleFlags = {
+      k: {
+        w: [0, 1].map(i => parseInt(fenflags.charAt(i), 10)),
+        b: [2, 3].map(i => parseInt(fenflags.charAt(i), 10))
+      },
+      l: {
+        w: [4, 5].map(i => parseInt(fenflags.charAt(i), 10)),
+        b: [6, 7].map(i => parseInt(fenflags.charAt(i), 10))
+      }
+    };
+  }
+
+  getFlagsFen() {
+    return ['k', 'l'].map(p => {
+      return ['w', 'b'].map(c => {
+        return this.castleFlags[p][c].map(x => x.toString(10)).join("");
+      }).join("")
+    }).join("");
   }
 
   isKing(x, y, p) {
     if (!p)
       p = this.getPiece(x, y);
-    ['k', 'l'].includes(p); //no cannibal mode
+    return ['k', 'l'].includes(p); //no cannibal mode
   }
 
   getCastleMoves([x, y]) {
+    const c = this.getColor(x, y),
+          p = this.getPiece(x, y);
     // Relative position of the selected piece: left or right ?
     // If left: small castle left, large castle right.
     // If right: usual situation.
-    const c = this.getColor(x, y);
-    const relPos = (this.castleFlags[c][1] == y ? "left" : "right");
-
     const finalSquares = [
-      relPos == "left" ? [1, 2] : [2, 3],
-      relPos == "right" ? [6, 5] : [5, 4]
+      this.relPos[c][p] == '0' ? [1, 2] : [2, 3], //0 == left
+      this.relPos[c][p] == '1' ? [6, 5] : [5, 4] //1 == right
     ];
-    const saveFlags = JSON.stringify(this.castleFlags[c]);
-    // Alter flags to follow base_rules semantic
-    this.castleFlags[c] = [0, 3].map(i => this.castleFlags[c][i]);
-    const moves = super.getCastleMoves([x, y], finalSquares);
-    this.castleFlags[c] = JSON.parse(saveFlags);
+    const moves =
+      super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p][c]);
     return moves;
   }
 
+  updateCastleFlags(move) {
+    super.updateCastleFlags(move, this.castleFlags['k'], 'k');
+    super.updateCastleFlags(move, this.castleFlags['l'], 'l');
+  }
+
 };