0

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!

NAP
  • 11
  • 2
  • 2
    `int *gameBoard[3][3] ;` declares `gameBoard` as an array 3 of array 3 of pointer to `int`, not that what you probably want – P0W Oct 19 '13 at 19:33

1 Answers1

2

In your Whole program You are supposed to use two dimensional array

The way you declared is wrong.

int *gameBoard[3][3] ;  

You need to declare

int gameBoard[3][3] ;  

Before assigning Check Values of playOneGuessR and playOneGuessC

if( (playOneGuessR >=0 && playOneGuessR < 3) && (playOneGuessC >= 0  && playOneGuessC < 3) ) 
gameBoard[playOneGuessR][playOneGuessC] = 1 ;  

How dangerous is it to access an array out of bounds?

see this Programming Paradigms

Community
  • 1
  • 1
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 1
    Hmm, that was an easy fix, I cant believe I didn't catch that, but I am having some difficulty understanding pointers. Great suggestion of checking the values, I will keep that in mind although I am at the very beginning stages of the program. Thanks again. – NAP Oct 19 '13 at 20:25