TIC TAC TOE GAME , BEGINNER , PROBLEMS.

Hey! I'm a beginner in C++ and am in university atm. I have a code for a tic tac toe game tht has a few problems. (1) Assigning my X and O's to the array. Also how to check winner properly, Thanks , Appreciate it.

#include <iostream>
#include <iomanip>
//setw (not working)
using namespace std;

void display(char board[3][3]);
void play(char board[3][3]);//asks 1st player to enter location
bool checkWin(char[3][3]);

int main()
{
char board[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
display (board);
play(board);



system ("pause");
return 0;
}



void display(char board[3][3])

{ cout<<" Welcome to a tic tac toe program " <<endl<<endl<<endl; //creates new lines of space

cout<<" 0 1 2\n"<<endl;
cout<<" 0 "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<endl;
cout<<" ---|---|---"<< endl;
cout<<" 1 "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<endl;
cout<<" ---|---|---"<<endl;
cout<<" 2 "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<endl;
}

void play (char board[3][3])
{

int i;
int j;
char p1='X';
char p2='O';
bool end= false;

while (!end)
{
cout<<"Player 1 is x, enter row no:"<<endl;
cin>>i;
cout<<"Player 1 enter column no:"<<endl;
cin>>j;

while ((i>3 || i<0 || j>3 || j<0)||('X' == board[i-1][j-1]||'O' == board[i-1][j-1]))
{
cout<<"Sorry you must choose elsewhere. Choose a row no:"<<endl;
cin>>i;
cout<<"Choose a column no:"<<endl;
cin>>j;
}

board[i-1][j-1] = p1;

end = checkWin(board);
if (end)
break;

cout<<"Player 2 is o, enter row no:"<<endl;
cin>>i;
cout<<"Player 2 enter column no:"<<endl;
cin>>j;
while ((i>3 || i<0 || j>3 || j<0)||('X' == board[i-1][j-1]||'O' == board[i-1][j-1]))
{
cout<<"Sorry you must choose elsewhere. Choose a row no:"<<endl;
cin>>i;
cout<<"Choose a column no:"<<endl;
cin>>j;
}

board[i-1][j-1] = p2;

end = checkWin(board);
}

}
bool checkWin(char board[3][3]) //function that checks for a winner and accepts array as parameter
{
bool end = false;

if(board[0][0] == board[1][2] && board[0][0] == board[2][0])
{cout << board[0][0] << " wins!";
end = true;}
if(board[0][1] == board[0][0] && board[0][1] == board[0][2])
{cout << board[0][0] << " wins!";
end = true;}
if(board[1][0] == board[1][1] && board[1][1] == board[1][2])
{cout << board[1][1] << " wins!";
end = true;}
if(board[1][1] == board[0][1] && board[0][1] == board[2][1])
{cout << board[1][1] << " wins!";
end = true;}
if(board[0][2] == board[2][2] && board[1][2] == board[0][2])
{cout << board[0][2] << " wins!";
end = true;}
if(board[2][0] == board[2][1] && board[2][1] == board[2][2])
{cout << board[2][1] << " wins!";
end = true;}
if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
{cout << board[1][1] << " wins!";
end = true;}
if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
{cout << board[1][1] << " wins!";
end = true;}

return(end);
}


First thing I see is in you while when you checked the row and column numbers:
while ((i>3 || i<0 || j>3 || j<0)||('X' == board[i-1][j-1]||'O' == board[i-1][j-1]))

You should check i<1 and not i<0 and same for j: j<1 and not j<0. Because as it is now, you accept column and row 0, leading to board[-1][-1] which is not possible.

Then the first check in your checkWin is wrong. I guess your checking the first column, so they should all be of the form board[i][0].

One more mistake, that leads to a direct win so far, is that you're not checking what kind of character is contained in the array. So When I tried you code, I won because your program saw a vertical line of stars ! So you should add a check that the cell contain is different than *

Hope it helps,
Here is a way to check for horizontal wins. This is a general formula that will work for n size boards, although you will have to adjust the expected totals for sum accordingly. Some minor modifications are required to use this to check verticals and diagonals. I'll leave that to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream> 

int main()
{
    int board[3][3], i, j, sum; 
    for(i = 0; i < 3; ++i) //Evaluate the board
    {
        for(j = 0; j < 3; ++j) //This evaluates the horizontals. 
        {
            sum = 0; 
            if(board[i][j] == 'x') //If there is an x, add one to sum
            {
                ++sum; //3 X in a horizontal will give a total of 3. 
            }
            else if(board[i][j] == 'o') //If there is an o, add 5 to sum. 
            {
                sum = sum + 5; //3 O in a horizontal will give a total of 15. 
            }
            //All other possible totals will not be equal to 3 or 15 (0,1,2,5,6,7,10,11). 
        }
        if(sum == 3)
        {
            std::cout << "X wins\n"; 
        }
        else if(sum == 15)
        {
            cout << "O wins\n"; 
        }
    }
}


To assign values to your array you ask the player for i and j positions already, so you need only do board[i][j] = 'x' (if player x turn) or board[i][j] = 'o' (for player o turn). I tend to use a bool turn to determine which player's turn it is.
1
2
3
4
5
6
7
8
9
10
11
12
if(turn == 0)
{
board[i][j] == 'o'; 
//Check for wins. 
turn = 1; //And then it is the other player's turn. 
}
else
{
board[i][j] == 'x';
//Check for wins. 
turn = 0;  
}

Some general tips:
-You should use "\n" instead of endl;
-You shouldn't use any calls to system().
-Please use code tags around your code as I have here. You can find them over there >>
when making your post.

Edit: Missed a closing code tag
Last edited on
Topic archived. No new replies allowed.