Tic Tac Toe program need a fix for the Drawing of the game.

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;
}
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
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
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.
Please, repost the code with the code tags ^^'
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <string>
using namespace std;

int matrix[3][3];
int row, column;
char check(void);
string name1;
string name2;

void display(void);
void player1_move(void);
void player2_move(void);
void init_matrix(void);
void reset();

bool finished(int matrix[3][3]);


int main(void)
{
    char done;
    char play;
    bool CONTINUE = true;

    cout << "This is a game of Tic Tac Toe.\n";
            cout << "First player is 'X', Second player is '0'.\n\n";
            cout << "Enter name for first player: ";
            cin >> name1;
            cout << "Enter name for second player: ";
            cin >> name2;

    while (CONTINUE)
        {


            done =  ' ';
            init_matrix();

            do {
                    display();
                    player1_move();
                    done = check();
                    if(done!= ' ') break;
                    display();
                    player2_move();
                    done = check();
                    if(done!= ' ') break;

                    reset();

            } while(done== ' ');


            if(done=='X')
                cout<< name1 << " wins.\n";
            else
            {
                if(done!=' ')
                {
                    cout<< name2 << " wins."<<endl;
                }
                else
                {
                    cout << "It's a Tie.\n";
                }
            }

            display();
            cout << "Play again? Y/N" << endl;
            cin >> play;
            if ( play=='N' || play=='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<< name1 << " please enter the Row by Column coordinates for your 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<< name2 << " please enter the Row by Column coordinates for your 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];

    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] != ' ')

    return ' ';
}

bool finished(int matrix[3][3])
{
    for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
                {
                    if (matrix[i][j] == ' ')
                        return false;
                }
        }
    return true;
}

void reset()
{
    row = 0;
    column = 0;
}
Ok here is the working code
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <string>
using namespace std;

int matrix[3][3];
int row, column;
int check(void);
string name1;
string name2;

void display(void);
void player1_move(void);
void player2_move(void);
void init_matrix(void);
void reset();

bool finished(int matrix[3][3]);


int main(void)
{
    int done;
    char play;
    bool CONTINUE = true;

    cout << "This is a game of Tic Tac Toe.\n";
            cout << "First player is 'X', Second player is '0'.\n\n";
            cout << "Enter name for first player: ";
            cin >> name1;
            cout << "Enter name for second player: ";
            cin >> name2;

    while (CONTINUE)
        {


            done =  5;
            init_matrix();

            do {
                    display();
                    player1_move();
                    done = check();
                    if(done!= 5) break;
                    display();
                    player2_move();
                    done = check();
                    if(done!= 5) break;

                    reset();

            } while(done== 5);


            if(done==1)
                cout<< name1 << " wins.\n";
            else if(done ==2)
                {
                    cout<< name2 << " wins."<<endl;
                }
            else if(done ==0)
                {
                    cout << "It's a Tie.\n";
                }
            

            display();
            cout << "Play again? Y/N" << endl;
            cin >> play;
            if ( play=='N' || play=='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] = 0;
}

void display(void)
{
    int t;
    char matrix1[3][3];
    for(int i = 0; i < 3;i++)
    for(int c= 0; c < 3; c++)
    matrix1[i][c] = (matrix[i][c] == 0 ? ' ' : (matrix[i][c] == 1 ? 'X' : 'O'));
    
    for(t=0; t<3; t++)
        {

            cout << "\t\t " << matrix1[t][0] << " | " << matrix1[t][1] << " | " << matrix1[t][2];

            if(t!=2)
            cout << "\n\t\t---|---|---\n";
        }
    cout << endl<<endl;
}

void player1_move(void)
{
    int x, y;

    cout<< name1 << " please enter the Row by Column coordinates for your move: ";
    cin >> x >> y;

    x--; y--;

    if(matrix[x][y]!= 0){
        cout<<"Invalid move, try again.\n";
        player1_move();
       }
    else matrix[x][y] = 1;
}

void player2_move(void)
{
   int x, y;

    cout<< name2 << " please enter the Row by Column coordinates for your move: ";
    cin >> x >> y;

    x--; y--;

    if(matrix[x][y]!= 0){
        cout<<"Invalid move, try again.\n";
        player1_move();
       }
    else matrix[x][y] = 2;
}

int check(void)
{
    int i;

    for(i=0; i<3; i++)
        if(matrix[i][0] != 0 && 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] != 0 && matrix[0][i]==matrix[1][i] &&
            matrix[0][i]==matrix[2][i])
            return matrix[0][i];


    if(matrix[0][0] != 0 && matrix[0][0]==matrix[1][1] &&
        matrix[1][1]==matrix[2][2])
            return matrix[0][0];

    if(matrix[0][2] != 0 && matrix[0][2]==matrix[1][1] &&
        matrix[1][1]==matrix[2][0])
            return matrix[0][2];

    if(matrix[0][0] != 0 && matrix[0][1] != 0 && matrix[0][2] != 0 && matrix[1][0] != 0 &&
        matrix[1][1] != 0 && matrix[1][2] != 0 && matrix[2][0] != 0 && matrix[2][1] != 0 && matrix[2][2] != 0)
    return 0;
    
    return 5;
}

bool finished(int matrix[3][3])
{
    for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
                {
                    if (matrix[i][j] == ' ')
                        return false;
                }
        }
    return true;
}

void reset()
{
    row = 0;
    column = 0;
}


Your mistake is that you matrix was an int but you intelized it with X and O

Thanks for the help. Really appreciate it.
Topic archived. No new replies allowed.