Draft code reorganisation (+ fix Alice rules + stateless VariantRules object)
[vchess.git] / public / javascripts / variants / Crazyhouse.js
index afebcc3..3d9c743 100644 (file)
@@ -11,10 +11,9 @@ class CrazyhouseRules extends ChessRules
                // 6) Check promoted array
                if (!fenParsed.promoted)
                        return false;
-               fenpromoted = fenParsed.promoted;
-               if (fenpromoted == "-")
+               if (fenParsed.promoted == "-")
                        return true; //no promoted piece on board
-               const squares = fenpromoted.split(",");
+               const squares = fenParsed.promoted.split(",");
                for (let square of squares)
                {
                        const c = V.SquareToCoords(square);
@@ -24,10 +23,21 @@ class CrazyhouseRules extends ChessRules
                return true;
        }
 
+       static ParseFen(fen)
+       {
+               const fenParts = fen.split(" ");
+               return Object.assign(
+                       ChessRules.ParseFen(fen),
+                       {
+                               reserve: fenParts[5],
+                               promoted: fenParts[6],
+                       }
+               );
+       }
+
        static GenRandInitFen()
        {
-               const fen = ChessRules.GenRandInitFen();
-               return fen.replace(" w 1111", " w 1111 0000000000 -");
+               return ChessRules.GenRandInitFen() + " 0000000000 -";
        }
 
        getFen()
@@ -37,8 +47,8 @@ class CrazyhouseRules extends ChessRules
 
        getReserveFen()
        {
-               let counts = _.map(_.range(10), 0);
-               for (let i=0; i<V.PIECES.length; i++)
+               let counts = new Array(10);
+               for (let i=0; i<V.PIECES.length-1; i++) //-1: no king reserve
                {
                        counts[i] = this.reserve["w"][V.PIECES[i]];
                        counts[5+i] = this.reserve["b"][V.PIECES[i]];
@@ -59,6 +69,8 @@ class CrazyhouseRules extends ChessRules
                }
                if (res.length > 0)
                        res = res.slice(0,-1); //remove last comma
+               else
+                       res = "-";
                return res;
        }
 
@@ -87,10 +99,13 @@ class CrazyhouseRules extends ChessRules
                        }
                };
                this.promoted = doubleArray(V.size.x, V.size.y, false);
-               for (let square of fenParsd.promoted.split(","))
+               if (fenParsed.promoted != "-")
                {
-                       const [x,y] = V.SquareToCoords(square);
-                       promoted[x][y] = true;
+                       for (let square of fenParsed.promoted.split(","))
+                       {
+                               const [x,y] = V.SquareToCoords(square);
+                               promoted[x][y] = true;
+                       }
                }
        }
 
@@ -197,7 +212,7 @@ class CrazyhouseRules extends ChessRules
                super.updateVariables(move);
                if (move.vanish.length == 2 && move.appear.length == 2)
                        return; //skip castle
-               const color = this.turn;
+               const color = move.appear[0].c;
                if (move.vanish.length == 0)
                {
                        this.reserve[color][move.appear[0].p]--;