Hi, I have made my tic tac toe game that allows two people to play the game. Everything works properly, but now I would like to make the game using a two dimensional array. Here is my original code:

//tic tac toe game

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//#include <iomanip>
//using std::setw;

int main()
{
//  char square[3] [3];
    char sq1('1');
    char sq2('2');
    char sq3('3');
    char sq4('4');
    char sq5('5');
    char sq6('6');
    char sq7('7');
    char sq8('8');
    char sq9('9');
    int player(1);//player
    bool gameover(true);


do
{
      cout<< sq1 << "|" << sq2 << "|" << sq3 << "|"<<endl;
      cout<< "----"<<endl;
      cout<< sq4 << "|" << sq5 << "|" << sq6 << "|"<<endl; 
    cout<< "----"<<endl;
    cout<< sq7 << "|" << sq8 << "|" << sq9 << "|"<<endl;

    char playermark;//playersymbol
    if (player == 1)
    {
        playermark = 'X';
    }
    else
    {
        playermark = 'O';
    }

    cout<<"It is player "<< player << "'s turn."<<endl;

    bool realmove;

    do
    {
        char nextmove;
        cin>> nextmove;
        realmove = true;

        if (nextmove == '1' && sq1 == '1')
            { 
                sq1 = playermark;
            }
            else if (nextmove == '2' && sq2 == '2')
                { 
                    sq2 = playermark;
                }
            else if (nextmove == '3' && sq3 == '3')
                { 
                    sq3 = playermark;
                }
            else if (nextmove == '4' && sq4 == '4')
                { 
                    sq4 = playermark;
                }
            else if (nextmove == '5' && sq5 == '5')
                { 
                    sq5 = playermark;
                }
            else if (nextmove == '6' && sq6 == '6')
                { 
                    sq6 = playermark;
                }
            else if (nextmove == '7' && sq7 == '7')
                { 
                    sq7 = playermark;
                }
            else if (nextmove == '8' && sq8 == '8')
                { 
                    sq8 = playermark;
                }
            else if (nextmove == '9' && sq9 == '9')
                { 
                    sq9 = playermark;
                }
            else 
                {
    cout<< "You have entered a non-alid move. Please enter a valid move."<<endl;
            realmove = false;
                }
        }
    while(! realmove);
     gameover = false;
     bool wongame = true;
        //something missing here??

        if (sq1 != '1')
            {
                if (sq2 == sq1 && sq3 == sq1)
                    {
                        gameover = true;
                    }
                if (sq4 == sq1 && sq7 == sq1)
                    {
                        gameover = true;
                    }
            }
        if (sq5 != '5')
            {
                if (sq1 == sq5 && sq9 == sq5)
                    {
                        gameover = true;    
                    }
                if (sq2 == sq5 && sq8 == sq5)
                    {
                        gameover = true;
                    }
                if (sq4 == sq5 && sq6 == sq5)
                    {
                        gameover = true;
                    }
                if (sq3 == sq5 && sq7 == sq5)
                    {
                        gameover = true;
                    }
            }
        if (sq9 != '9')
            {
                if (sq3 == sq9 && sq6 == sq9)
                    {
                        gameover = true;    
                    }
                if (sq7 == sq9 && sq8 == sq9)
                    {
                        gameover = true;
                    }
            }
        if (sq1 != '1' && sq2 != '2' && sq3 != '3' && sq4 != '4' && sq5 != '5' & sq6 != '6' && sq7 != '7' && sq8 != '8' && sq9 != '9' && !gameover)
            {
                gameover = true;
                wongame = false;
            }

        if (gameover)
            {
                if (wongame)
                    {
        cout<<"Player "<< layer <<" won the game!"<<endl;                   }
        cout<< sq1 << "|" << sq2 << "|" << sq3 << "|"<<endl;
        cout<< "----"<<endl;
        cout<< sq4 << "|" << sq5 << "|" << sq6 << "|"<<endl; 
        cout<< "----"<<endl;
        cout<< sq7 << "|" << sq8 << "|" << sq9 << "|"<<endl;  

        cout<<"Would you like to play again? (y or n)"<<endl;
            char playagain;
            cin>> playagain;

            if (playagain == 'y')
            {
                gameover = false;
                char sq1('1');
                char sq2('2');
                char sq3('3');
                char sq4('4');
                char sq5('5');
                char sq6('6');
                char sq7('7');
                char sq8('8');
                char sq9('9');
            }
            player = 1;
        }
        else
            {
            if (player == 1)
                {
                player = 2;
                }
            else 
                {
                player = 1;
                }
            }
    }       while (!gameover);

return 0;

}

I know how to declare the 2d array char square [3] [3]
but I am not really sure how to initilize the spaces so that I know where they are puting the x's and o's ... any help would be greatly appreciated. thanks!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I think this program is an excellent candidate for a 2-D array. You have a lot of repeated logic in your code, which could be shortened if you used a 2-D array. For display purposes, I think you could change this code:

cout<< sq1 << "|" << sq2 << "|" << sq3 << "|"<<endl;
cout<< "----"<<endl;
cout<< sq4 << "|" << sq5 << "|" << sq6 << "|"<<endl;
cout<< "----"<<endl;
cout<< sq7 << "|" << sq8 << "|" << sq9 << "|"<<endl;

to something like this:

for (int i = 0; i < 3; i++)
{
     for (int j = 0; j < 3; j++)
     {
          cout << square[i][j];
     }
     cout << endl;
     if (i != 2)
     {
          cout << "-----" << endl;
     }
}

This is what I have changed my program to. I am not even really sure if im on the right track, but it does not work. any suggestions?? Thanks!

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

char won(void);
int main()
{
    char square[3] [3];
    int player(1);//player
    bool gameover(true);
    do
{
    for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            cout<< square[r][c];
        }
        cout<<endl;
        if (r != 2)
        {
            cout<<"-----"<<endl;
        }
    }
    char playermark;//playersymbol
    if (player == 1)
    {
        playermark = 'X';
    }
    else
    {
        playermark = 'O';
    }

    cout<<"It is player "<< player << "'s turn."<<endl;
    cout<<"Please enter the row and column coordinates for your move."<<endl;
    bool realmove;

    do
    {
        int row;
        int col;
        cin>> row;
        cin>> col;
        realmove = true;

        if (square[row][col] != ' ' )
        { 
    cout<<"Invalid move, please enter a valid move."<<endl;
        }
        else 
        square[row][col] = playermark;
                    }
        while(! realmove);
         gameover = false;

         char won(void)
    {
         int n;

for (n = 0; n < 3; n++)
if (square[i][0]==square[i][1] && square [i][0]==square[i][2])
return square [i][0];
    for (n = 0; n < 3; n++)
    if (square[0][i]==square[1][i] && square [0][i]==square[2][i])
    return square [0][i];
                    if(square[0][0]==square[1][1]&&square[1][1]==square[2][2])
        return square[0][0];
            if(square[0][2]==square[1][1]&&square[1][1]==square[2][0])
            return square[0][2];
         return '   ';
    }

    cout<<"Would you like to play again? (y or n)"<<endl;
    char playagain;
    cin>> playagain;

        if (playagain == 'y')
        {
            gameover = false;
        }
    else
        {
        if (player == 1)
            {
            player = 2;
            }
        else 
            {
            player = 1;
            }
        }
           }

    return 0;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags to post your code in future. A lot of energy of mods is spent on telling others to do the same it is the least you can do if you expect some help

You have created the function won within main() but have not called it anywhere. You have to call a function, defining it alone will not do.

Here is your code formatted and with code tags. Do this:

[code=cplusplus] // paste your code here

[/code]

to embed your code in C++-style code tags. It preserves formatting/indentation, highlights key words, adds line numbers, etc.

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

char won(void);
int main()
{
    char square[3] [3];
    int player(1);//player
    bool gameover(true);
    do
    {
        for (int r = 0; r < 3; r++)
        {
            for (int c = 0; c < 3; c++)
            {
                cout<< square[r][c];
            }
            cout<<endl;
            if (r != 2)
            {
                cout<<"-----"<<endl;
            }
        }
        char playermark;//playersymbol
        if (player == 1)
        {
            playermark = 'X';
        }
        else
        {
            playermark = 'O';
        }

        cout<<"It is player "<< player << "'s turn."<<endl;
        cout<<"Please enter the row and column coordinates for your move."<<endl;
        bool realmove;

        do
        {
            int row;
            int col;
            cin>> row;
            cin>> col;
            realmove = true;

            if (square[row][col] != ' ' )
            {
                cout<<"Invalid move, please enter a valid move."<<endl;
            }
            else
                square[row][col] = playermark;
        }
        while(! realmove);
        gameover = false;

        char won(void)
        {
            int n;

            for (n = 0; n < 3; n++)
                if (square[i][0]==square[i][1] && square [i][0]==square[i][2])
                    return square [i][0];
            for (n = 0; n < 3; n++)
                if (square[0][i]==square[1][i] && square [0][i]==square[2][i])
                    return square [0][i];
            if(square[0][0]==square[1][1]&&square[1][1]==square[2][2])
                return square[0][0];
            if(square[0][2]==square[1][1]&&square[1][1]==square[2][0])
                return square[0][2];
            return ' ';
        }

        cout<<"Would you like to play again? (y or n)"<<endl;
        char playagain;
        cin>> playagain;

        if (playagain == 'y')
        {
            gameover = false;
        }
        else
        {
            if (player == 1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }
        }
    }

    return 0;

}

Can you explain a little more what in particular is going on? Does it not compile? Does it not run? Does it run, but incorrectly?

Along the lines of what hammerhead said, I would move your function "won" outside of "main". Also, I see the start of a "do" loop on line 12, but no corresponding "while" condition to go along with the bracket in line 94 which ends the do-while loop. In your original post, you had the line:

while (!gameover)

which I don't see in your most recent post.

I can get it to compile, but it does nothing. Its just an empty terminal window, so im not sure if I amcalling things using the correct format or what. I got myself a bit lost.

I can get it to compile, but it does nothing. Its just an empty terminal window, so im not sure if I amcalling things using the correct format or what. I got myself a bit lost.

Hmm. Seems to me that it should do SOMETHING. Whether that something is the correct thing or not is a different story. Move the entire "won" function and place the entire function after the "main" function ends. Add that "while" condition line before your "return 0" line as I mentioned in my previous post. Also, you need to initialize your character array before you use it. If it should be blank spaces at the beginning of the game, you need to assign the entire array to be blank spaces somewhere. To do that, you could change line 9 to this:

char square[3][3] = ' '; // blank space between quotes

That will at least initialize the array correctly for the first game. You may have to initialize it again if the player decides to play a second game.

Seems to me that if the program compiled and ran without errors it should have given you at least some output. Make those changes, recompile, run it again, and see if you get anything different. Post the updated code if you get the same non-results.

I triad all of that including your last post but it tells me that line 9 is an invalid initilizer.
I also got the code running before I changed line 9. but this was my output, was not right and when I went to go enter my move, the program crashed...

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

char won(void);
int main()
{
char square[3][3]= ' ';
int player(1);//player
bool gameover(true);
do
{
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
cout<< square[r][c];
}
cout<<endl;
if (r != 2)
{
cout<<"-----"<<endl;
}
}
char playermark;//playersymbol
if (player == 1)
{
playermark = 'X';
}
else
{
playermark = 'O';
}

cout<<"It is player "<< player << "'s turn."<<endl;
cout<<"Please enter the row and column coordinates for your move."<<endl;
bool realmove;

do
{
	
int row;
int col;
cin>> row;
cin>> col;
realmove = true;
cout<<won()<<endl;

if (square[row][col] != ' ' )
{ 
cout<<"Invalid move, please enter a valid move."<<endl;
}
else 
square[row][col] = playermark;
}
while(! realmove);
gameover = false;


cout<<"Would you like to play again? (y or n)"<<endl;
char playagain;
cin>> playagain;

if (playagain == 'y')
{
gameover = false;
}
else
{
if (player == 1)
{
player = 2;
}
else 
{
player = 1;
}
}
}
while (!gameover);

return 0;

} 
char won(void)
{
int n;
int i;
char square[3][3];

for (n = 0; n < 3; n++)
if (square[i][0]==square[i][1] && square [i][0]==square[i][2])
return square [i][0];
for (n = 0; n < 3; n++)
if (square[0][i]==square[1][i] && square [0][i]==square[2][i])
return square [0][i];
if(square[0][0]==square[1][1]&&square[1][1]==square[2][2])
return square[0][0];
if(square[0][2]==square[1][1]&&square[1][1]==square[2][0])
return square[0][2];
return ' ';
}

ˆl
-----
/
-----
o
that is what my output is, along with the prompt to enter a move, and then when I do, it crashes.

I triad all of that including your last post but it tells me that line 9 is an invalid initilizer.
I also got the code running before I changed line 9. but this was my output, was not right and when I went to go enter my move, the program crashed...

Okay, try changing this:

char square[3][3] = ' ';

to this:

char square[3][3];
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        square[i][j] = ' '; // space between quotes
    }
}

I did and ran it and got a prompt for a move.

Yeah you can get the out put and that prompt, thanks, but there is somehting else that I am missing becasue it still does not let a move be entered, it still terminates.

You have a couple of problems in this function:

char won(void)
{
    int n;
    int i;
    char square[3][3];

    for (n = 0; n < 3; n++)
        if (square[i][0]==square[i][1] && square [i][0]==square[i][2])
            return square [i][0];
    for (n = 0; n < 3; n++)
        if (square[0][i]==square[1][i] && square [0][i]==square[2][i])
            return square [0][i];
    if(square[0][0]==square[1][1]&&square[1][1]==square[2][2])
        return square[0][0];
    if(square[0][2]==square[1][1]&&square[1][1]==square[2][0])
        return square[0][2];
    return ' ';
}

One, you never initialize i when you declare it in line 4, then you use it in your loops. i thus contains who-knows-what. If who-knows-what is anything other than 0, 1, or 2, which chances are that it is, you are going to have problems since 0, 1, and 2 are the only legal array indexes here. Solution: assign i a value before you use it. Second, the 2-dimensional character array that you declare in line 5 is different from the one that you were using in main. If you want them to be the same, you have two options. One, make the square array a global variable by moving your original declaration of it:

char square[3][3];

BEFORE the main function starts: i.e. right before the line:

int main ()

Second option is to pass the square array to your "won" function as a parameter. Option 1 is the easier of the two, though many people frown on using global variables. It will work though. Regardless of which of the two options you use, delete line five above. Don't declare a square array in your "won" funciton if you intend it to be the same as the array that you use in "main".

initilized i to 0, deleted line 5 and put char square[3][3]; before int main(), still crashes. Haha I think this program is the death of me. haha, but thanks so much for your help

You have a couple of problems in this function:

char won(void)
{
    int n;
    int i;
    char square[3][3];

    for (n = 0; n < 3; n++)
        if (square[i][0]==square[i][1] && square [i][0]==square[i][2])
            return square [i][0];
    for (n = 0; n < 3; n++)
        if (square[0][i]==square[1][i] && square [0][i]==square[2][i])
            return square [0][i];
    if(square[0][0]==square[1][1]&&square[1][1]==square[2][2])
        return square[0][0];
    if(square[0][2]==square[1][1]&&square[1][1]==square[2][0])
        return square[0][2];
    return ' ';
}

It occurs to me that perhaps you want to change i to n in lines 8, 9, 11, and 12?

I doubt that has anything to do with your program crash though.

This article has been dead for over six months. Start a new discussion instead.