Fix HalfChess, more random Loser initial position, continue draft of Ultima
[vchess.git] / public / javascripts / variants / Loser.js
index 0c484c1..6a322b9 100644 (file)
@@ -2,12 +2,15 @@ class LoserRules extends ChessRules
 {
        initVariables(fen)
        {
-               // No castling, hence no flags
                const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
                this.epSquares = [ epSq ];
        }
 
-       setFlags(fen) { }
+       setFlags(fen)
+       {
+               // No castling, hence no flags; but flags defined for compatibility
+               this.castleFlags = { "w":[false,false], "b":[false,false] };
+       }
 
        getPotentialPawnMoves([x,y])
        {
@@ -21,15 +24,20 @@ class LoserRules extends ChessRules
                const lastRank = (color == "w" ? 0 : sizeX-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<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:V.KING}));
+                       }
                }
 
                return moves;
@@ -102,20 +110,13 @@ class LoserRules extends ChessRules
                return [];
        }
 
-       play(move, ingame)
-       {
-               if (!!ingame)
-                       move.notation = this.getNotation(move);
-               this.moves.push(move);
-               this.epSquares.push( this.getEpSquare(move) );
-               VariantRules.PlayOnBoard(this.board, move);
-       }
+       // Unused:
+       updateVariables(move) { }
+       unupdateVariables(move) { }
 
-       undo(move)
+       getFlagsFen()
        {
-               VariantRules.UndoOnBoard(this.board, move);
-               this.epSquares.pop();
-               this.moves.pop();
+               return "-";
        }
 
        checkGameEnd()
@@ -141,4 +142,60 @@ 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() +
+                       " 0000"; //add flags (TODO?!)
+       }
 }