I'm a beginner at C and I am attempting to create a Tic Tac To game to practice some things that I have recently learned. However, the game began having trouble as soon as I attempted to pass my multidimensional array to a function. Here's the code:
//Declaring the function to print out the game board
int printGameBoard(int gameBoard[3][3]) ;
int main(int argc, const char * argv[])
{
//declare a multidimentional array to be used as the game board
int *gameBoard[3][3] ;
// set all array values to 0
for (int r = 0; r < 3; r++) {
for (int c = 0 ; c < 3; c++) {
gameBoard[r][c] = 0 ;
}
}
//bool variable to determine whether the game loop should run
bool gameOn = true ;
//variables used to hold the X(C = Column) and Y(R = Row) values of each players guess
int playOneGuessR, playOneGuessC ;
int playTwoGuessR, playTwoGuessC ;
//Call the function to print the game board with all values initialized to 0 i.e.:
// [ 0 0 0 ]
// [ 0 0 0 ]
// [ 0 0 0 ]
printGameBoard(gameBoard) ;
//Begin game loop
while (gameOn == true) {
//Player 1 enters the Y(Row) value of their guess
printf("\nPlayer 1: \nPlease Enter The Row Number:\n") ;
scanf("%d", &playOneGuessR) ;
// Player 1 enters the X(Column) value of their guess
printf("Please Enter The Column Number:\n") ;
scanf("%d", &playOneGuessC) ;
//Based on players 1's guess, the appropriate array value is assigned to 1 (to denote player 1)
gameBoard[playOneGuessR][playOneGuessC] = 1 ;
//The function to print the game board is called again to reflect the newly assigned value of 1
printGameBoard(gameBoard) ;
return 0;
}
}
//The function to print the game board
int printGameBoard(int gameBoard[][3]) { //THIS IS WHERE IT GOES WRONG.
for (int r = 0; r < 3; r++) {
printf("Row %d [ ", r+1) ;
for (int c = 0; c < 3; c++) {
printf("%d ", gameBoard[r][c]) ;
}
printf("] \n") ;
}
return 0 ;
}
Long story short: this worked fine until I decided to put the code to print the game board into a separate function. I assume I am just passing the array incorrectly.
For example here is the output of one attempt:
Welcome to tic tac to!
Here is the game board:
Row 1 [ 0 0 0 ]
Row 2 [ 0 0 0 ]
Row 3 [ 0 0 0 ]
Player 1:
Please Enter The Row Number:
1
Please Enter The Column Number:
1
Row 1 [ 0 0 0 ]
Row 2 [ 0 0 0 ]
Row 3 [ 0 0 1 ]
Program ended with exit code: 0
Clearly the 1 is in the wrong place. It should be at gameBoard[1,1], which would be the middle. Any ideas? Thanks!