Need Help!!!! to solve Tic Tac Toe Problem

Hi,guys I tried to make Tic Tac Toe in a much simpler way that could easily be understand but I got failed and I need your help.

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
187
188
189
190
191
192
#include<iostream.h>
#include<conio.h>


char matrix[3][3];



char check();
void player();
void computer();
void diplay();
void x();
void o();

char comp_choice;

void main()
{


char symbol;

cout<<"Choose your symbol 'X' or 'O' :"<<endl;
cin>>symbol;

if(symbol=='X')
{
void x();
}
else
{
void O();
}

}

void x()
{
int x;

do
{
cout<<"Choose your postion from 1-9:";
cin>>x;
}

while(x<9||x>1)
{
switch(x)

case1:
matrix[0][0]='X';
break;

case2:
matrix[1][0]='X';
break;

case3:
matrix[2][0]='X';
break;

case4:
matrix[0][1]='X';
break;

case5:
matrix[1][1]='X';
break;

case6:
matrix[1][2]='X';
break;

case7:
matrix[0][2]='X';
break;

case8:
matrix[2][1]='X';
break;

case9:
matrix[2][2]='X';
break;

default:
cout<<"Invaild Move, Try Again!"<<endl;
}
}

void o()
{
int y;
do {
cout<<"Choose your postion from 1-9:";
cin>>y;}
while(y=>1||y<=9)
{
switch(y)

case1:
matrix[0][0]='O';
break;

case2:
matrix[1][0]='O';
break;

case3:
matrix[2][0]='O';
break;

case4:
matrix[0][1]='O';
break;

case5:
matrix[1][1]='O';
break;

case6:
matrix[1][2]='O';
break;

case7:
matrix[0][2]='O';
break;

case8:
matrix[2][1]='O';
break;

case9:
matrix[2][2]='O';
break;

default:
cout<<"Invaild Move, Try Again!"<<endl;
}
}


void computer()
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if (matrix[i][j]==' ')
break;
if (matrix[i][j]==' ')
break;
}

if(i*j==9)
{
cout<<"Draw";
exit(0);
}
else
matrix[i][j]='O';

void display()
{
for(temp=0;temp<3;temp++)
{
cout<<"\n---|---|---"<<matrix[temp][0]<<matrix[temp][1]<<matrix[temp][2];
}
}

char check()

{
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 ' ';
}

Last edited on
All your cin, cout, and endl 's are not being called. Preface each of them with std:: or put using namespace std; at the beginning of your file, right after your include statements. I don't recommend doing that too often, but for a smaller project like this, it's okay. Your switch statements do not have braces, and each case doesn't have braces either. Your for statements are right after another without braces as well.

In your computer() function, you should put the second for loop in braces, with the two if statements, otherwise only the first if statement is included, and the second one is tested right after the two loops conclude. I recommend putting braces around that too, because it was a little difficult for me to read at first.

Your do while loop is formatted incorrectly: everything in your while section should be put into the do section, and the while condition is put after the do {}, with a semicolon at the end. (That prevents the compiler from looking for a while loop right afterwards). You define your matrix as int matrix[3][3] but your X() and O() functions don't use them properly. A quick look tells me you're indexing in the wrong order.

(I haven't slept in 37 hours, so perhaps my eyes are fooling me...)

Either you're indexing in the wrong order, or your goal was to index them in the opposite order where matrix[0][0] would be the 9th position. I think. Again, very sleepy here. If you're still having problems after this post, just...post again and I'll try to elaborate more and maybe even write a fixed up version of the program for you.

EDIT:

You're also calling your O() and X() functions incorrectly. When you prototype, and define them, it is necessary to state the return type and the parameters, but when you call them from another function (like main()), you don't include the return type for void function calls.

void x();
in your main function, should be
x();

The same goes for your o() function.
Last edited on
Have you compiled it and checked the errors first??

1)at line 42 do..while loop
while loop will continue on if you enter x from 2-8(note:not at 1 and 9)

2)same while loop missing
;
.

3)
{
after switch

4)space between case and number.

5)same in next function

6)line 154 invalid bracket

7)line 99 what is => ?? it is
>=


8)display spelling mistake in declaration

9)breaking if statements in compute function.Use return

10) closing compute

and might be many more.........

Correct program first
well thanks guys!
Thanks for telling me my mistakes I will work on these mistakes first and then update the program.
Topic archived. No new replies allowed.