Need help with tic tac toe game, please help.

I'm having some odd problems with this program. There's an odd smiley face thingy appearing on the board as well as the get_status function is not working properly. Copy and compile this if you want, and please tell me what the hell the problem is. I'm very new to computers and programming in general. Thanks.

#include <iostream>
#define X 'X'
#define Y 'O'

using namespace std;
void next_move(char[][3]); // player one's move
void next_mmove(char[][3]); // player two's move
bool set_value(char[][3], int row, int column); // check valid move
void display_board(char[][3]); // board
int get_status(char[][3]); // win, lose, or tie (havent finished yet)


int main()
{
char name1, nname;
cout << "enter your name player 1: ";
cin >> name1;
fflush(stdin);
cout << endl;

cout << "enter your name player 2: ";
cin >> nname;
fflush(stdin);
cout << endl;

char game[3][3];
for(int i=0; i < 3; i++)
{
for(int j=0; j<3; j++)
{ game[i][j]='/0';
}

}
for(int g=0; g<9; g++)
{ display_board(game);
cout << name1 << " make your move " << endl;
next_move(game);
display_board(game);
cout << nname << " make your move " << endl;
next_mmove(game);
display_board(game);
int Z = get_status(game);
if(Z=1)
cout << "player 1 wins";
}
}



void next_move(char Toe[][3] )
{int x, y;


do{
if(set_value(Toe, x, y)== false)
cout << "INVALID MOVE" << endl;
cout << "enter row: ";
cin >> x;
cout << endl;

cout << "enter column: ";
cin >> y;
cout << endl;
}
while(set_value(Toe, x, y)== false);



Toe[x-1][y-1] = X;
}



void next_mmove(char Toe[][3] )
{int a, b,;


do{
if(set_value(Toe, a, b)== false)
cout << "INVALID MOVE" << endl;
cout << "enter row: ";
cin >> a;
cout << endl;

cout << "enter column: ";
cin >> b;
cout << endl;
}
while(set_value(Toe, a, b)== false);



Toe[a-1][b-1] = Y;
}




bool set_value(char Tic[][3],int row, int column)
{
if (Tic[row][column] = '/0' && row <= 3 && column <=3)
return true;
else if(Tic[row][column] == 'X' || Tic[row][column] == 'Y')
return false;
else if(row > 3 || column > 3)
return false;
}


void display_board (char x[][3])
{
cout << " _________________\n";
cout << "| | | | \n";
cout << "| " << x[0][0] << " | " << x[0][1] << " | " << x[0][2] <<
" |\n";
cout << "|_____|_____|_____|\n";
cout << "| | | |\n";
cout << "| " << x[1][0] << " | " << x[1][1] << " | " << x[1][2] <<
" |\n";
cout << "|_____|_____|_____|\n";
cout << "| | | |\n";
cout << "| " << x[2][0] << " | " << x[2][1] << " | " << x[2][2] <<
" |\n";
cout << "|_____|_____|_____|\n";

}


int get_status(char b[][3])
{
if (b[0][0] == 'X' && b[0][1] == 'X' && b[0][2] == 'X')
return 1;
else if(b[1][0] == 'X' && b[1][1] == 'X' && b[1][2] == 'X')
return 1;
else if(b[2][0] == 'X' && b[2][1] == 'X' && b[2][2] == 'X')
return 1;
else if(b[0][0] =='X' && b[1][1] == 'X' && b[2][2] == 'X')
return 1;
else if(b[0][2] == 'X' && b[1][1] == 'X' && b[2][0] == 'X')
return 1;
else
return 0;

}
Last edited on
Can you use the code format tag to format your code please.

I can't really read it as presented, but I spotted this:
1
2
3
4
5
void next_move(char Toe[][3] )
{
    int x, y;
    do{
        if(set_value(Toe, x, y)== false)
What value does x and y have when set_value is called?

The same construct appears in next_mmove.

In:
1
2
3
4
5
6
7
8
9
bool set_value(char Tic[][3],int row, int column)
{
    if (Tic[row][column] = '/0' && row <= 3 && column <=3)
        return true;
    else if(Tic[row][column] == 'X' || Tic[row][column] == 'Y')
        return false;
    else if(row > 3 || column > 3)
        return false;
}
What does it mean if row or column is equal to 3?
Last edited on
well, thats not really the problem, allow me to explain a bit further. I set all board values to null, because the professors specifications said set a blank and valid spot equal to null. Now, whenever i enter the [row][column] cordinates for input, it will put an X or O respectively for player 1 and 2. Now once that spot becomes an X or O, it should no longer be null, therefore when i call the set_value function, it should recognize that as a taken position. It does not. I can replace any X with an O and vice versa. Also there are odd markings on the display board, and it messes up the entire board. another problem is the Get_status function is recognizing player one as the winner before i even complete a game, after the first round of player 1 and player 2 moves. I have no idea what to do.
Topic archived. No new replies allowed.