# Problem with my Tic-Tac-Toe game...

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here's a picture of my problem: http://i11.tinypic.com/72jiu76.png As you can see a little smiley face thing is created in [2][1], and [1][2] & [2][0] are missing the little '-'. I don't know what the problem is, since I am in no way changing them.
#include <iostream>
using namespace std;

char map[2][2];
bool xTurn, yTurn;
bool GameOver = false;

/*class X
{
public:
X(int x, int y){}
~X();
int locX, locY;
};

class Y
{
public:
Y(int x, int y) {}
~Y();
int locX, locY;
};*/

void DisplayMap()
{
int xs, ys;
cout << "Map:\n";
for (xs = 0; xs<3; xs++)
{
for(ys = 0; ys<3; ys++)
{
cout << "[" << xs << "][" << ys << "] ";
cout <<  map[xs][ys] << " ";
if(ys == 2) cout << "\n";
if(ys == 2 && xs == 2) cout << "\n\n";
}
}
/*
for (xs = 0; xs<3; xs++)
{
for(ys = 0; ys<3; ys++)
{
cout << "[" << xs << "][" << ys << "] ";
if(ys == 2) cout << "\n";
if(ys == 2 && xs == 2) cout << "\n\n";
}
}*/
}

void GameLoop()
{
if(xTurn)
{
cout << "It is player X's turn.\n";
DisplayMap();
bool xPlaced = false;
int placingX, placingY;
while(!xPlaced)
{
cout << "Which X coordinate would you like to place your X? ";
cin >> placingX;
cout << "Which Y coordinate would you like to place your X? ";
cin >> placingY;
if(map[placingX][placingY] == '-')
map[placingX][placingY] = 'X';
cout << "\n\n";
xPlaced = true;
}
xPlaced = false;
xTurn = false;
yTurn = true;
if(yTurn)
{
cout << "It is player Y's turn.\n";
DisplayMap();
bool yPlaced = false;
int placingX, placingY;
while(!yPlaced)
{
cout << "Which X coordinate would you like to place your Y? ";
cin >> placingX;
cout << "Which Y coordinate would you like to place your Y? ";
cin >> placingY;
if(map[placingX][placingY] == '-')
map[placingX][placingY] = 'Y';
cout << "\n\n";
yPlaced = true;
}
yPlaced = false;
yTurn = false;
}
if(!GameOver)
GameLoop();
}
}

int main()
{
cout << "\t\tWelcome to Dession's Tic-Tac-Toe three in a row!\n\n";
xTurn = true;
char blank = '-';
map[0][0] = blank;
map[0][1] = blank;
map[0][2] = blank;
map[1][0] = blank;
map[1][1] = blank;
map[1][2] = blank;
map[2][0] = blank;
map[2][1] = blank;
map[2][2] = blank;
GameLoop();
system("pause");
return 0;
}



##### Share on other sites
I've tried your code and I can't recreate what your screenshot is displaying. I don't see any explanation for your problem either. There are several issues with the logic going on there but that doesn't account for it.

I suggest you make a clean rebuild of the project and try that; if the problem persists and no-one else spots anything foul in the meantime then try getting yourself a more modern compiler/IDE - Visual C++ 2008 Express Edition is completely free to download from Microsoft, just remember to (freely) register the software within 30 days and that's it.

##### Share on other sites
Quote:
 Original post by Dessionchar map[2][2];

Isn't that a little small? Try

char map[3][3];

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by Dessionchar map[2][2];

Isn't that a little small? Try

char map[3][3];

Well for Tic-Tac-Toe you only need 9 squares, not 16.

##### Share on other sites
char map[2][2] defines 4 squares, map[3][3] defines 9.

##### Share on other sites
Quote:
 Original post by DevFredchar map[2][2] defines 4 squares, map[3][3] defines 9.

map[2][2] is

[0,0] [0,1] [0,2]
[1,0] [1,1] [1,2]
[2,0] [2,1] [2,2]

9 seperate boxes.

##### Share on other sites
Quote:
 Original post by DessionYou're forgetting about zero.

No.

Quote:

Yes.

Quote:
 Original post by DessionWhich means 0, 1, and 2.

No.

Quote:
 Original post by Dessionmap[2][2] is[0,0] [0,1] [0,2][1,0] [1,1] [1,2][2,0] [2,1] [2,2]9 seperate boxes.

No. map[2][2] is

[0,0] [0,1]
[1,0] [1,1]

4 seperate boxes.

##### Share on other sites
Bleh you're right. Haha I should have tried it in the first place, thanks no more little smiley things! What are those things anyways?

##### Share on other sites
When you said

char map[2][2];bool xTurn, yTurn;bool GameOver = false;

your memory looked something like this:

map[0][0] | map[0][1] | map[1][0] | map[1][1] | xTurn | yTurn | GameOver | other stuff...

But you interpreted map as a 3x3 array, somehow like that:

map[0][0] | map[0][1] | map[1][0]map[1][1] | xTurn | yTurnGameOver | other stuff...

So your other variables (xTurn, yTurn, GameOver) got interpreted as part of the map. That's what apparently changed you map.

##### Share on other sites
When you create an array the value inside the square brackets is actually the number of elements the array is to contain (in this case you want 3 elements by 3 elements), don't confuse this with the way you access elements...

When you access an element in the array you use a 0-based index, so the first element is element 0, and the third element is element 2.

So you were creating a 2x2 array but accessed it as a 3x3 array, this results in undefined behaviour which explains why I didn't have any problems but you did.

Quote:
 no more little smiley things! What are those things anyways?

It's an ASCII character, the graphic is the symbolic representation of SOH (Start Of Heading) [smile]

##### Share on other sites
Ahh I see, thanks so much you guys!