YO. I’m a beginner in game development and I’m making a console tictactoe game, I’m trying to pass the board grid from the Board class for my game to main because I want to put the gameboard in it’s own class. I made a 2D array to hold the characters of the board but I’m having trouble returning it, the error is “cannot implicitly convert type ‘string[,]’ to ‘string’.
private int maxRow = 3;
private int maxColumn = 3;
private string[,] boardGrid = new string[3, 3]; //create the game board grid
//Initialise board method
public string InitBoard () //make an object of the board class, returns a 2d array because the board is essentially a grid
{
//initialise board
for (int row = 0; row < maxRow; row++)
{
for (int column = 0; column < maxColumn; column++)
{
boardGrid [row, column] = ".";
}
}
return boardGrid; //<---[The problem happens here!]
}
This is the code for the main class.
public static void Main (string[] args)
{
Board ticTacToeBoard = new Board ();
ticTacToeBoard = ticTacToeBoard.InitBoard ();
ticTacToeBoard.DisplayBoard (ticTacToeBoard);
}
I couldn’t find any other question on this but if there is one then feel free to point me to it. Cheers!
You have a few problems here:
string[,]from a method declared to returnstring. That’s not going to workstringto a variable of typeBoard. That’s not going to workYour main method should be as simple as:
Put all the initialization logic into your constructor, and make your
DisplayBoardmethod use the variable within the object itself.I’d also be tempted to make
maxRowandmaxColumnconstants, or make them parameters in the constructor.