Пример #1
0
    void Start()
    {
        if (gameModeIndexSet)
        {
            gameMode = (GameMode)gameModeIndex;
        }
        if (gameMode == GameMode.Regular)
        {
            // GetComponent<Clock>().StartClock();
        }

        Board.SetPositionFromFen(Definitions.gameStartFen, true);

        ZobristKey.Init();
        Evaluation.Init();
        if (regenerateOpeningBook)
        {
            OpeningBookGenerator.GenerateBook();
        }
        if (useOpeningBook)
        {
            OpeningBookReader.Init();
        }

        playerManager = GetComponent <MoveManager> ();

        playerManager.CreatePlayers();

        Board.SetPositionFromFen(Definitions.gameStartFen, true);
    }
Пример #2
0
    /// Sets the board position from a given fen string
    /// Note that this will clear the board history
    public static void SetPositionFromFen(string fen, bool updateBoardUI = true)
    {
        Init();
        threefoldRepCheck.Clear();
        halfmoveCountSinceLastPawnMoveOrCap = 0;

        boardArray       = new int[128];
        boardColourArray = new int[128];
        for (int i = 0; i <= 127; i++)            // clear colour array (all values to -1)
        {
            boardColourArray[i] = -1;
        }

        ushort initialGameState = 0;

        string[] fenSections = fen.Split(' ');

        string pieceChars = "rnbqkpRNBQKP";
        string pieceFen   = fenSections [0];
        int    boardX     = 0;
        int    boardY     = 7;

        for (int i = 0; i < pieceFen.Length; i++)
        {
            char key = pieceFen [i];

            if (pieceChars.Contains(key.ToString()))
            {
                int  squareIndex64  = boardY * 8 + boardX;
                int  squareIndex128 = Convert64to128(squareIndex64);
                bool white          = char.IsUpper(key);
                int  pieceCode      = ColourCode(white);

                switch (key.ToString().ToUpper().ToCharArray()[0])
                {
                case 'R':
                    pieceCode |= rookCode;
                    break;

                case 'N':
                    pieceCode |= knightCode;
                    break;

                case 'B':
                    pieceCode |= bishopCode;
                    break;

                case 'Q':
                    pieceCode |= queenCode;
                    break;

                case 'K':
                    pieceCode |= kingCode;
                    if (white)
                    {
                        whiteKingIndex = squareIndex128;
                    }
                    else
                    {
                        blackKingIndex = squareIndex128;
                    }
                    break;

                case 'P':
                    pieceCode |= pawnCode;
                    break;
                }

                UpdatePieceCount(pieceCode, 1);

                boardArray[squareIndex128] = pieceCode;
                SetColourBoard(squareIndex128, squareIndex128, pieceCode & 1);

                boardX++;
            }
            else if (key == '/')
            {
                boardX = 0;
                boardY--;
            }
            else
            {
                int skipCount;
                if (int.TryParse(key + "", out skipCount))
                {
                    boardX += skipCount;
                }
            }
        }

        // Game state
        string sideToMove             = fenSections [1];
        string castlingRights         = fenSections [2];
        string enPassantCaptureSquare = fenSections [3];

        halfmoveCount = 0;
        if (fenSections.Length > 4)
        {
            int.TryParse(fenSections[4], out halfmoveCount);
        }
        //	string fullMoveNumber = fenSections [5];

        // Set side to move (bit 1)
        if (sideToMove == "w")
        {
            initialGameState += 1;
        }

        // Set castling rights (bits 2,3,4,5)
        for (int i = 0; i < castlingRights.Length; i++)
        {
            switch (castlingRights [i])
            {
            case 'K':
                initialGameState += 1 << 1;
                break;

            case 'Q':
                initialGameState += 1 << 2;
                break;

            case 'k':
                initialGameState += 1 << 3;
                break;

            case 'q':
                initialGameState += 1 << 4;
                break;
            }
        }

        // En passant capture file (bits 6,7,8,9)
        if (enPassantCaptureSquare [0] != '-')
        {
            initialGameState += (ushort)(Definitions.fileNames.IndexOf(enPassantCaptureSquare [0]) << 5);
        }

        gameStateHistory.Clear();
        gameStateHistory.Push(initialGameState);

        zobristKey = ZobristKey.GetZobristKey();

        if (updateBoardUI)
        {
            UpdatePhysicalBoard();
        }
    }