예제 #1
0
    public int calculateWinner(GameBoardData gameBoard)
    {
        GridElementData[ , ] board = gameBoard.getGameBoard();
        int rows    = gameBoard.boardRows;
        int columns = gameBoard.boardColumns;

        int  column, row, prev, count, playerAtField, rowCounter;
        bool firstPlayerWon  = false;
        bool secondPlayerWon = false;

        //check rows
        for (column = 0; column < columns; column++)
        {
            count = 1;
            prev  = 0;
            for (row = 0; row < rows; row++)
            {
                playerAtField = board[row, column].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }
            }
        }

        //check columns
        for (row = 0; row < rows; row++)
        {
            count = 1;
            prev  = 0;
            for (column = 0; column < columns; column++)
            {
                playerAtField = board[row, column].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }
            }
        }

        //check diagonal
        for (column = 0; column < columns; column++)
        {
            count      = 1;
            prev       = GameManager.NONE;
            rowCounter = 0;
            while (column + rowCounter < columns && rowCounter < rows)
            {
                playerAtField = board[rowCounter, column + rowCounter].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }

                rowCounter++;
            }
        }
        for (row = 0; row < rows; row++)
        {
            count      = 1;
            prev       = 0;
            rowCounter = 0;
            while (row + rowCounter < rows && rowCounter < columns)
            {
                playerAtField = board[row + rowCounter, rowCounter].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }

                rowCounter++;
            }
        }

        for (column = 0; column < columns; column++)
        {
            count      = 1;
            prev       = 0;
            rowCounter = 0;
            while (column - rowCounter >= 0 && rowCounter < rows)
            {
                playerAtField = board[rowCounter, column - rowCounter].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }

                rowCounter++;
            }
        }
        for (row = 0; row < rows; row++)
        {
            count      = 1;
            prev       = 0;
            rowCounter = 0;
            while (row - rowCounter >= 0 && rowCounter < columns)
            {
                playerAtField = board[row - rowCounter, rowCounter].player;
                if (playerAtField != prev)
                {
                    prev  = playerAtField;
                    count = 1;
                }
                else if (playerAtField == GameManager.FIRSTPLAYER)                    //first player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        firstPlayerWon = true;
                    }
                }
                else if (playerAtField == GameManager.SECONDPLAYER)                    //second player
                {
                    count++;
                    if (count >= GameManager.tokensToWin)
                    {
                        secondPlayerWon = true;
                    }
                }

                rowCounter++;
            }
        }

        if (!movePossible() || (firstPlayerWon && secondPlayerWon))
        {
            return(GameManager.DRAW);
        }
        else if (firstPlayerWon)
        {
            return(GameManager.FIRSTPLAYER);
        }
        else if (secondPlayerWon)
        {
            return(GameManager.SECONDPLAYER);
        }
        else
        {
            return(GameManager.NONE);
        }
    }