2d arrays- tic tac toe question

Hi everyone!

I'll try to be as clear as possible!
Basically I'm working on a tic tac toe program for my computer science class. We need to use 2d arrays to run it. I have most of it implemented but I'm having trouble getting the board to display correctly. My professor told me to use for loops, which I'm using. Only problem is I can't figure out how to get each space of the board to increment correctly.

Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void displayBoard(int space[][C]){

for(int r=0; r<=3; r++){
     for(int c=0; c<=3; c++){

     cout << "    |       |      " << endl;
     cout << "  " << space[r][c] << "  |  " << space[r][c] << "  |  " << space[r][c] << endl;
     cout << "____|_______|______" << endl;
     cout <<      |       |      " << endl;
     cout << "  " << space[r][c] << "  |  " << space[r][c] << "  |  " << space[r][c] << endl;
     cout << "____|_______|______" << endl;
     cout << "    |       |      " << endl;
     cout << "  " << space[r][c] << "  |  " << space[r][c] << "  |  " << space[r][c] << endl;
     cout << "    |       |      " << endl;

           }
      }
} 


In other words, when I output it each space shows as a 0 instead of as 1, 2, 3, 4 etc
Last edited on
Well, you are printing array elements in positions 0, 1, 2, etc, not the indexes. This code will print whatever the array contains, which I assume is filled with zeros since that's what it displays
The array is filled with {1, 2, 3, 4, 5, 6, 7, 8, 9}
Uh, ok if the array is initialized correctly then I'm clueless.
On a side note, I assume the array has size 3x3. In this case your loops are slightly wrong: the last cycle will have c/r == 3, and valid indexes are 0, 1 and 2
You're printing the entire board every loop. You need something more like this:
1
2
3
4
5
6
7
8
for (int i = 0; i < 3; i++)
{
  for (int j = 0; j < 3; j++)
  {
    std::cout << space[i][j] << " ";
  }
  std::cout << std::endl;
}


The lines will be a little trickier. You might want to make a simpler display than what you have. Depends on how much time you have to do the other parts of the program I guess.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 3; i++)
{
  for (int j = 0; j < 3; j++)
  {
    std::cout << space[i][j];
    if (j < 2) std::cout << "|"
  }
  std::cout << std::endl;
  if (i < 2)
  {
    std::cout << "-----"
    std::cout << std::endl;
  }
}


or something, will take some playing around
Last edited on
Hm yeah you're right about that, didn't even notice I put a 3. Thank you for that!

That made it slightly better. Instead of showing all 0's now though, when I output it it displays the board filled with all 1's, then all 2's, then all 3's, etc until it gets to 9 then stops.

The looping isn't the problem though, I just don't know what to do to make it display 1-9 in each respective space.
You were absolutely right. I simplified my display and used something similar to what you said and it worked perfectly. Just need to tinker with the lines a little bit.

Thank you very much, this little issue was really nagging me and preventing me from finishing this!
Topic archived. No new replies allowed.