Play computer move in webworker to not freeze interface
[vchess.git] / public / javascripts / variants / Loser.js
index 0ed3edc..6731d32 100644 (file)
@@ -1,36 +1,41 @@
 class LoserRules extends ChessRules
 {
-       initVariables(fen)
+       static get HasFlags() { return false; }
+
+       setOtherVariables(fen)
        {
-               // No castling, hence no flags; but flags defined for compatibility
-               this.flags = "-";
-               const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
+               const parsedFen = V.ParseFen(fen);
+               const epSq = parsedFen.enpassant != "-"
+                       ? V.SquareToCoords(parsedFen.enpassant)
+                       : undefined;
                this.epSquares = [ epSq ];
+               this.scanKingsRooks(fen);
        }
 
-       setFlags(fen) { }
-
        getPotentialPawnMoves([x,y])
        {
                let moves = super.getPotentialPawnMoves([x,y]);
 
                // Complete with promotion(s) into king, if possible
                const color = this.turn;
-               const V = VariantRules;
-               const [sizeX,sizeY] = VariantRules.size;
                const shift = (color == "w" ? -1 : 1);
-               const lastRank = (color == "w" ? 0 : sizeX-1);
+               const lastRank = (color == "w" ? 0 : V.size.x-1);
                if (x+shift == lastRank)
                {
-                       let p = V.KING;
                        // Normal move
                        if (this.board[x+shift][y] == V.EMPTY)
-                               moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
+                               moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING}));
                        // Captures
-                       if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
-                               moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
-                       if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
-                               moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
+                       if (y>0 && this.canTake([x,y], [x+shift,y-1])
+                               && this.board[x+shift][y-1] != V.EMPTY)
+                       {
+                               moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:V.KING}));
+                       }
+                       if (y<V.size.y-1 && this.canTake([x,y], [x+shift,y+1])
+                               && this.board[x+shift][y+1] != V.EMPTY)
+                       {
+                               moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:V.KING}));
+                       }
                }
 
                return moves;
@@ -38,7 +43,7 @@ class LoserRules extends ChessRules
 
        getPotentialKingMoves(sq)
        {
-               const V = VariantRules;
+               // No castle:
                return this.getSlideNJumpMoves(sq,
                        V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
        }
@@ -48,12 +53,11 @@ class LoserRules extends ChessRules
        {
                const color = this.turn;
                const oppCol = this.getOppCol(color);
-               const [sizeX,sizeY] = VariantRules.size;
-               for (let i=0; i<sizeX; i++)
+               for (let i=0; i<V.size.x; i++)
                {
-                       for (let j=0; j<sizeY; j++)
+                       for (let j=0; j<V.size.y; j++)
                        {
-                               if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
+                               if (this.board[i][j] != V.EMPTY && this.getColor(i,j) != oppCol)
                                {
                                        const moves = this.getPotentialMovesFrom([i,j]);
                                        if (moves.length > 0)
@@ -81,7 +85,7 @@ class LoserRules extends ChessRules
                let moves = this.filterValid( this.getPotentialMovesFrom(sq) );
                // This is called from interface: we need to know if a capture is possible
                if (this.atLeastOneCapture())
-                       moves = VariantRules.KeepCaptures(moves);
+                       moves = V.KeepCaptures(moves);
                return moves;
        }
 
@@ -89,7 +93,7 @@ class LoserRules extends ChessRules
        {
                let moves = super.getAllValidMoves();
                if (moves.some(m => { return m.vanish.length == 2; }))
-                       moves = VariantRules.KeepCaptures(moves);
+                       moves = V.KeepCaptures(moves);
                return moves;
        }
 
@@ -103,10 +107,9 @@ class LoserRules extends ChessRules
                return [];
        }
 
-       // Unused:
+       // No variables update because no castling
        updateVariables(move) { }
        unupdateVariables(move) { }
-       parseFlags(flags) { }
 
        checkGameEnd()
        {
@@ -114,7 +117,9 @@ class LoserRules extends ChessRules
                return this.turn == "w" ? "1-0" : "0-1";
        }
 
-       static get VALUES() { //experimental...
+       static get VALUES()
+       {
+               // Experimental...
                return {
                        'p': 1,
                        'r': 7,
@@ -131,4 +136,62 @@ class LoserRules extends ChessRules
        {
                return - super.evalPosition(); //better with less material
        }
+
+       static GenRandInitFen()
+       {
+               let pieces = { "w": new Array(8), "b": new Array(8) };
+               // Shuffle pieces on first and last rank
+               for (let c of ["w","b"])
+               {
+                       let positions = _.range(8);
+
+                       // Get random squares for bishops
+                       let randIndex = 2 * _.random(3);
+                       let bishop1Pos = positions[randIndex];
+                       // The second bishop must be on a square of different color
+                       let randIndex_tmp = 2 * _.random(3) + 1;
+                       let bishop2Pos = positions[randIndex_tmp];
+                       // Remove chosen squares
+                       positions.splice(Math.max(randIndex,randIndex_tmp), 1);
+                       positions.splice(Math.min(randIndex,randIndex_tmp), 1);
+
+                       // Get random squares for knights
+                       randIndex = _.random(5);
+                       let knight1Pos = positions[randIndex];
+                       positions.splice(randIndex, 1);
+                       randIndex = _.random(4);
+                       let knight2Pos = positions[randIndex];
+                       positions.splice(randIndex, 1);
+
+                       // Get random square for queen
+                       randIndex = _.random(3);
+                       let queenPos = positions[randIndex];
+                       positions.splice(randIndex, 1);
+
+                       // Random square for king (no castle)
+                       randIndex = _.random(2);
+                       let kingPos = positions[randIndex];
+                       positions.splice(randIndex, 1);
+
+                       // Rooks positions are now fixed
+                       let rook1Pos = positions[0];
+                       let rook2Pos = positions[1];
+
+                       // Finally put the shuffled pieces in the board array
+                       pieces[c][rook1Pos] = 'r';
+                       pieces[c][knight1Pos] = 'n';
+                       pieces[c][bishop1Pos] = 'b';
+                       pieces[c][queenPos] = 'q';
+                       pieces[c][kingPos] = 'k';
+                       pieces[c][bishop2Pos] = 'b';
+                       pieces[c][knight2Pos] = 'n';
+                       pieces[c][rook2Pos] = 'r';
+               }
+               return pieces["b"].join("") +
+                       "/pppppppp/8/8/8/8/PPPPPPPP/" +
+                       pieces["w"].join("").toUpperCase() +
+                       " w -"; //no en-passant
+       }
 }
+
+const VariantRules = LoserRules;