Aug 11, 2009 at 1:52am UTC
Hi there my program is a Tic Tac Toe game which is a Human Vs Human Game. But my program is having problem when the game comes to a draw. It doesn't stop the game or even display "It's a Draw." The game works if there is a winner and a loser only.I need help in the displaying of the Draw part and stopping the game.
The code of my program is below.
#include <iostream>
using namespace std;
int matrix[3][3];
int row, column;
char check(void);
void display(void);
void player1_move(void);
void player2_move(void);
void init_matrix(void);
void reset();
bool finished(int mat[3][3]);
int main(void)
{
char done; char c;
bool CONTINUE = true;
while (CONTINUE)
{
cout << "This is a game of Tic Tac Toe.\n";
cout << "First player is 'X', computer is '0'.\n\n";
done = ' ';
init_matrix();
do {
display();
player1_move();
done = check();
if(done!= ' ') break;
display();
player2_move();
done = check();
if(done!= ' ') break;
if (finished(matrix))
{
display();
cout << endl;
cout<<"Draw\n";
break;
}
reset();
} while(done== ' ');
if(done=='X')
cout<<"Player one wins.\n";
else
if (done ==' ')
cout<<"Player two wins."<<endl;
else
cout<<"Draw\n";
// break;
display();
cout << "Play again? Y/N" << endl;
cin >> c;
if ( c=='N' || c=='n')
CONTINUE = false;
reset();
}
return 0;
}
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
void display(void)
{
int t;
for(t=0; t<3; t++)
{
cout << "\t\t " << char(matrix[t][0]) << " | " << char(matrix[t][1]) << " | " << char(matrix[t][2]);
if(t!=2)
cout << "\n\t\t---|---|---\n";
}
cout << endl<<endl;
}
void player1_move(void)
{
int x, y;
cout<<"\nEnter X,Y coordinates for player one's move: ";
cin >> x >> y;
x--; y--;
if(matrix[x][y]!= ' '){
cout<<"Invalid move, try again.\n";
player1_move();
}
else matrix[x][y] = 'X';
}
void player2_move(void)
{
int x, y;
cout<<"\nEnter X,Y coordinates for player two's move: ";
cin >> x >> y;
x--; y--;
if(matrix[x][y]!= ' '){
cout<<"Invalid move, try again.\n";
player1_move();
}
else matrix[x][y] = '0';
}
char check(void)
{
int i;
for(i=0; i<3; i++)
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++)
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}
bool finished(int mat[3][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (mat[i][j] == ' ')
return false;
}
}
return true;
}
void reset()
{
row = 0;
column = 0;
}
Aug 11, 2009 at 9:36am UTC
In the end of checking check for this
1 2
if (matrix[0][0] != ' ' && matrix[0][1] != ' ' && matrix[0][2] != ' ' && matrix[1][0] != ' ' &&
matrix[1][1] != ' ' && matrix[1][2] != ' ' && matrix[2][0] != ' ' && matrix[2][1] != ' ' && matrix[2][3] != ' ' )
now you got a draw
Last edited on Aug 11, 2009 at 9:36am UTC
Aug 11, 2009 at 9:42am UTC
please repost it using the [code ][ /code] tags, by pressing the # button on your right. Without indentation it's really hard to read your program. Thanks.
EDIT: ops ^^'. Didn't see it's been solved already xD
Last edited on Aug 11, 2009 at 9:44am UTC
Aug 11, 2009 at 11:17am UTC
erm sorry for the inconveniece. this is actually my first time posting.
Btw the program is still having problem. Whenever there are three in a row, no matter 'X' or '0', it will output the second player as the winner.
Aug 11, 2009 at 11:23am UTC
Please, repost the code with the code tags ^^'
Aug 12, 2009 at 12:48am UTC
Thanks for the help. Really appreciate it.