C#C
C#2y ago
pikau

Methods that check strings issues

Hey, im currently trying to pass a few strings of a 2D array through methods in order to find a winner of this game. Basically, no matter what, it always returns as no winner, rather than X or O. The relevant code in classes is below:

Board.cs
    public string GetRow(int row)
    // We need to convert the int row values into string values. (0 - 2)
    // Will use the same loop for row and column, basically you define the row/column string as empty,
    // then we pass this through a loop that iterates between the three rows/columns between -1 and 3 (0 < 3)
    // and this append the value (column or row) via the += operator to each individual string.
    {
        string rowString = "";
        for (int i = 0; i < 3; i++)
        {
            rowString += board[row, i];
        }
        return rowString;
    }

    /// <summary>
    /// Get the string representation of a column of the board, with 0 being the left column and 2 being the right column
    /// </summary>
    /// <param name="column">The column number</param>
    /// <returns>The string representation of a column of the board</returns>
    public string GetColumn(int column)
    {
        string columnString = "";
        for (int i = 0; i < 3; i++)
        {
            columnString += board[column, i];
        }
        return columnString;
    }

        /// <summary>
        /// Get the string representation of a diagonal of the board
        /// </summary>
        /// <param name="topLeftToBottomRight">Whether the diagonal is from top left to bottom right</param>
        /// <returns>The string representation of a diagonal of the board</returns>
        /// Only difference here is that I have to use the topLeftToBottomRight in an if or else statement becuase
        /// theres only 2 possible ways diagonals can go.
    public string GetDiagonal(bool topLeftToBottomRight)
    {
    string diagonalString = "";

    if (topLeftToBottomRight)
    {
        for (int i = 0; i < board.GetLength(0); i++)
        {
            diagonalString += board[i, i];
        }
    }
    else
    {
        for (int i=0; i < board.GetLength(0); i++)
        {
            diagonalString += board[i, board.GetLength(1) -1 -i];
        }
    }
    return diagonalString;
    }

        /// <summary>
        /// Check if a string is homogeneous, i.e. all characters in the string are the same
        /// </summary>
        /// <param name="line">The string to check for homogeneity</param>
        public static bool IsHomogeneous(string line)
        {
        if (line == "XXX" || line == "OOO") return true;
        else return false;
        }
    }
}
Was this page helpful?