Sign in to follow this  
Dession

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

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Dession
char 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 this post


Link to post
Share on other sites
Quote:
Original post by DevFred
char map[2][2] defines 4 squares, map[3][3] defines 9.



You're forgetting about zero. All arrays start with zero. Which means 0, 1, and 2.

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 this post


Link to post
Share on other sites
Quote:
Original post by Dession
You're forgetting about zero.

No.

Quote:
Original post by Dession
All arrays start with zero.

Yes.

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

No.

Quote:
Original post by Dession
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.

No. map[2][2] is

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

4 seperate boxes.

Just try map[3][3], please.

Share this post


Link to post
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 | yTurn
GameOver | other stuff...


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

Share this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this