C
C#3mo 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;
}
}
}
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;
}
}
}
14 Replies
pikau
pikau3mo ago
Game.cs:
public bool HasWinner()
{
// Create a for loop that checks if there is a row, column, or diagonal
// that contains 3 of the same input (e.g. someone has won). Usin inbuilt functions
for(int i = 0; i < 3 ; i++)
{
if (Board.GetRow(i) == "XXX" || Board.GetRow(i) == "OOO")
return true;
if (Board.GetColumn(i) == "XXX" || Board.GetColumn(i) == "OOO")
return true;
}

string leftDiagonal = Board.GetDiagonal(true);
string rightDiagonal = Board.GetDiagonal(false);
if (leftDiagonal == "XXX" || leftDiagonal == "OOO")
return true;
if (rightDiagonal == "XXX" || leftDiagonal == "OOO")
return true;

else
return false;
}
public string GetWinner()
{
// Same as HasWinner, except now check all e (Rows, columns, diagonals) again
// and print either "X", "O" or "." depending on the winner
if (!HasWinner())
{
for (int i = 0; i < 3; i++)
{
if (Board.GetRow(i) == "XXX")
return "X";
if (Board.GetRow(i) == "OOO")
return "O";
if (Board.GetColumn(i) == "XXX")
return "X";
if (Board.GetColumn(i) == "OOO")
return "O";
}

string leftDiagonal = Board.GetDiagonal(true);
string rightDiagonal = Board.GetDiagonal(false);
if (leftDiagonal == "XXX" || rightDiagonal == "XXX")
return "X";
if (leftDiagonal == "OOO" || rightDiagonal == "OOO")
return "O";

}
return ".";
public bool HasWinner()
{
// Create a for loop that checks if there is a row, column, or diagonal
// that contains 3 of the same input (e.g. someone has won). Usin inbuilt functions
for(int i = 0; i < 3 ; i++)
{
if (Board.GetRow(i) == "XXX" || Board.GetRow(i) == "OOO")
return true;
if (Board.GetColumn(i) == "XXX" || Board.GetColumn(i) == "OOO")
return true;
}

string leftDiagonal = Board.GetDiagonal(true);
string rightDiagonal = Board.GetDiagonal(false);
if (leftDiagonal == "XXX" || leftDiagonal == "OOO")
return true;
if (rightDiagonal == "XXX" || leftDiagonal == "OOO")
return true;

else
return false;
}
public string GetWinner()
{
// Same as HasWinner, except now check all e (Rows, columns, diagonals) again
// and print either "X", "O" or "." depending on the winner
if (!HasWinner())
{
for (int i = 0; i < 3; i++)
{
if (Board.GetRow(i) == "XXX")
return "X";
if (Board.GetRow(i) == "OOO")
return "O";
if (Board.GetColumn(i) == "XXX")
return "X";
if (Board.GetColumn(i) == "OOO")
return "O";
}

string leftDiagonal = Board.GetDiagonal(true);
string rightDiagonal = Board.GetDiagonal(false);
if (leftDiagonal == "XXX" || rightDiagonal == "XXX")
return "X";
if (leftDiagonal == "OOO" || rightDiagonal == "OOO")
return "O";

}
return ".";
Angius
Angius3mo ago
Debug?
pikau
pikau3mo ago
is that like what you wanted im kinda new to C#
Angius
Angius3mo ago
$debug
MODiX
MODiX3mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
pikau
pikau3mo ago
is that not inherently done by just running the code theres no error messages currently or anything popping up in the error list
Angius
Angius3mo ago
Debugging is not the same as showing errors A debugger allows you to, for example, place a breakpoint to inspect what each variable's value is at a given point You can track how those variables change over time Step through the code execution line by line
pikau
pikau3mo ago
oh right ok well i will read up on that more thoroughly
pikau
pikau3mo ago
No description
pikau
pikau3mo ago
i tested it with the getRow method, in which it should retirve the value of one row of th 3x3 grid it says these are the values for presumable 0 in and 1 down in the grid which is incorrect the 2D that this analysed looked like this: OXX OOX . . X it said that same value for every space on the board up to [2, 2]
Angius
Angius3mo ago
Which means board has no actual elements in it At this point, it's been created, but not filled with data So, now that tells you something's wrong with whatever code it is that fills the board with data
pikau
pikau3mo ago
right so the methods themselves are potentially sound but maybe a board constructor to do with filling the data is not
Angius
Angius3mo ago
Yep Now you can debug that
pikau
pikau3mo ago
alright i will give it a go- thank you