-
Notifications
You must be signed in to change notification settings - Fork 4
/
TICTAC.C
300 lines (288 loc) · 5.42 KB
/
TICTAC.C
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* A simple 2D game of Tic Tac Toe */
/*Developed By : Aniket Kumar */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void board();
void com_move();
void player_move();
void win_check();
int flag[9]={0,0,0,0,0,0,0,0,0};//to mark the filled boxes
int box[9], move=0;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
while(1)
{
outtextxy(220,10,"***TIC TAC TOE***");
outtextxy(200,30,"Computer has O and you have X");
outtextxy(220,220,"Enter 0 to exit");
printf("\n");
board();
com_move();
win_check();
player_move();
win_check();
getch();
}
closegraph();
}
void win_check()
{
int win=0;// 1 is for Player and 2 is for Computer
//checks the winning conditions
if(box[0]==box[1]&&box[1]==box[2]&&box[1]==1)//first row
{
win=1;
}
if(box[0]==box[1]&&box[1]==box[2]&&box[1]==2)//first row
{
win=2;
}
if(box[0]==box[3]&&box[3]==box[6]&&box[3]==1)//first column
{
win=1;
}
if(box[0]==box[3]&&box[3]==box[6]&&box[3]==2)//first column
{
win=2;
}
if(box[3]==box[4]&&box[4]==box[5]&&box[4]==1)//second row
{
win=1;
}
if(box[3]==box[4]&&box[4]==box[5]&&box[4]==2)//second row
{
win=2;
}
if(box[6]==box[7]&&box[7]==box[8]&&box[7]==1)//third row
{
win=1;
}
if(box[6]==box[7]&&box[7]==box[8]&&box[7]==2)//third row
{
win=2;
}
if(box[1]==box[4]&&box[4]==box[7]&&box[4]==1)//second column
{
win=1;
}
if(box[1]==box[4]&&box[4]==box[7]&&box[4]==2)//second column
{
win=2;
}
if(box[2]==box[5]&&box[5]==box[8]&&box[5]==1)//third column
{
win=1;
}
if(box[2]==box[5]&&box[5]==box[8]&&box[5]==2)//third column
{
win=2;
}
if(box[2]==box[4]&&box[4]==box[6]&&box[4]==1)//right to left diagonal
{
win=1;
}
if(box[2]==box[4]&&box[4]==box[6]&&box[4]==2)//right to left diagonal
{
win=2;
}
if(box[0]==box[4]&&box[4]==box[8]&&box[4]==1)//left to right diagonal
{
win=1;
}
if(box[0]==box[4]&&box[4]==box[8]&&box[4]==2)//left to right diagonal
{
win=2;
}
//checks the win for Player
if(win==1)
{
printf("Game Over\nPlayer Wins");
getch();
exit(0);
}
//checks the win for computer
if(win==2)
{
printf("Game Over\nComputer Wins");
getch();
exit(0);
}
//for match draw
else if(move==8)
{
printf("Match Draw");
getch();
exit(0);
}
}
void board()
{
int i;
for(i=0;i<=90;i+=30)
{
line(220+i,50,220+i,140); //vertical lines
line(220,50+i,310,50+i); //horizontal lines
}
}
void com_move()//for moves of the computer
{
int i;
for(i=0;i<9;i++)
{
if(flag[i]==0&&i==0)//box 1
{
outtextxy(230,60,"O");
flag[i]=1;
box[0]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==1&&box[1]!=1)//box 2
{
outtextxy(260,60,"O");
flag[i]=1;
box[1]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==2&&box[2]!=1)//box 3
{
outtextxy(290,60,"O");
flag[i]=1;
box[2]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==3&&box[3]!=1)//box 4
{
outtextxy(230,90,"O");
flag[i]=1;
box[3]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==4&&box[4]!=1)//box 5
{
outtextxy(260,90,"O");
flag[i]=1;
box[4]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==5&&box[5]!=1)//box 6
{
outtextxy(290,90,"O");
flag[i]=1;
box[5]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==6&&box[6]!=1)//box 7
{
outtextxy(230,120,"O");
flag[i]=1;
box[6]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==7&&box[7]!=1)//box 8
{
outtextxy(260,120,"O");
flag[i]=1;
box[7]=2;
move=move+1;
break;
}
else if(flag[i]==0&&i==8&&box[8]!=1)//box 9
{
outtextxy(290,120,"O");
flag[i]=1;
box[8]=2;
move=move+1;
break;
}
}
}
void player_move()//for moves of the user
{
int i;
printf("\nEnter the box no : ");
scanf("%d", &i);
if(i==0)//for exitting the game
{
exit(0);
}
if(i==1&&flag[i-1]==0)//box 1
{
outtextxy(230,60,"X");
flag[i-1]=1;
box[0]=1;
move=move+1;
}
else if(i==2&&flag[i-1]==0)//box 2
{
outtextxy(260,60,"X");
flag[i-1]=1;
box[1]=1;
move=move+1;
}
else if(i==3&&flag[i-1]==0)//box 3
{
outtextxy(290,60,"X");
flag[i-1]=1;
box[2]=1;
move=move+1;
}
else if(i==4&&flag[i-1]==0)//box 4
{
outtextxy(230,90,"X");
flag[i-1]=1;
box[3]=1;
move=move+1;
}
else if(i==5&&flag[i-1]==0)//box 5
{
outtextxy(260,90,"X");
flag[i-1]=1;
box[4]=1;
move=move+1;
}
else if(i==6&&flag[i-1]==0)//box 6
{
outtextxy(290,90,"X");
flag[i-1]=1;
box[5]=1;
move=move+1;
}
else if(i==7&&flag[i-1]==0)//box 7
{
outtextxy(230,120,"X");
flag[i-1]=1;
box[6]=1;
move=move+1;
}
else if(i==8&&flag[i-1]==0)//box 8
{
outtextxy(260,120,"X");
flag[i-1]=1;
box[7]=1;
move=move+1;
}
else if(i==9&&flag[i-1]==0)//box 9
{
outtextxy(290,120,"X");
flag[i-1]=1;
box[8]=1;
move=move+1;
}
//if entered box no is already filled
else if(flag[i-1]==1)
{
printf("Invalid move!");
player_move();
}
}