Skip to content

Commit 43f70a2

Browse files
Add files via upload
1 parent 7e7a80f commit 43f70a2

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

tic-tac-toe-game-in-c/tic tac toe.c

+284
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<stdlib.h>
4+
#include <windows.h>
5+
6+
int board[10] = {2,2,2,2,2,2,2,2,2,2};
7+
int turn = 1,flag = 0;
8+
int player,comp;
9+
10+
void menu();
11+
void go(int n);
12+
void start_game();
13+
void check_draw();
14+
void draw_board();
15+
void player_first();
16+
void put_X_O(char ch,int pos);
17+
COORD coord= {0,0}; // this is global variable
18+
//center of axis is set to the top left cornor of the screen
19+
void gotoxy(int x,int y)
20+
{
21+
coord.X=x;
22+
coord.Y=y;
23+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
24+
}
25+
26+
void main()
27+
{
28+
system("cls");
29+
menu();
30+
getch();
31+
32+
}
33+
34+
void menu()
35+
{
36+
int choice;
37+
system("cls");
38+
printf("\n--------MENU--------");
39+
printf("\n1 : Play with X");
40+
printf("\n2 : Play with O");
41+
printf("\n3 : Exit");
42+
printf("\nEnter your choice:>");
43+
scanf("%d",&choice);
44+
turn = 1;
45+
switch (choice)
46+
{
47+
case 1:
48+
player = 1;
49+
comp = 0;
50+
player_first();
51+
break;
52+
case 2:
53+
player = 0;
54+
comp = 1;
55+
start_game();
56+
break;
57+
case 3:
58+
exit(1);
59+
default:
60+
menu();
61+
}
62+
}
63+
64+
int make2()
65+
{
66+
if(board[5] == 2)
67+
return 5;
68+
if(board[2] == 2)
69+
return 2;
70+
if(board[4] == 2)
71+
return 4;
72+
if(board[6] == 2)
73+
return 6;
74+
if(board[8] == 2)
75+
return 8;
76+
return 0;
77+
}
78+
79+
int make4()
80+
{
81+
if(board[1] == 2)
82+
return 1;
83+
if(board[3] == 2)
84+
return 3;
85+
if(board[7] == 2)
86+
return 7;
87+
if(board[9] == 2)
88+
return 9;
89+
return 0;
90+
}
91+
92+
int posswin(int p)
93+
{
94+
// p==1 then X p==0 then O
95+
int i;
96+
int check_val,pos;
97+
98+
if(p == 1)
99+
check_val = 18;
100+
else
101+
check_val = 50;
102+
103+
i = 1;
104+
while(i<=9)//row check
105+
{
106+
if(board[i] * board[i+1] * board[i+2] == check_val)
107+
{
108+
if(board[i] == 2)
109+
return i;
110+
if(board[i+1] == 2)
111+
return i+1;
112+
if(board[i+2] == 2)
113+
return i+2;
114+
}
115+
i+=3;
116+
}
117+
118+
i = 1;
119+
while(i<=3)//column check
120+
{
121+
if(board[i] * board[i+3] * board[i+6] == check_val)
122+
{
123+
if(board[i] == 2)
124+
return i;
125+
if(board[i+3] == 2)
126+
return i+3;
127+
if(board[i+6] == 2)
128+
return i+6;
129+
}
130+
i++;
131+
}
132+
133+
if(board[1] * board[5] * board[9] == check_val)
134+
{
135+
if(board[1] == 2)
136+
return 1;
137+
if(board[5] == 2)
138+
return 5;
139+
if(board[9] == 2)
140+
return 9;
141+
}
142+
143+
if(board[3] * board[5] * board[7] == check_val)
144+
{
145+
if(board[3] == 2)
146+
return 3;
147+
if(board[5] == 2)
148+
return 5;
149+
if(board[7] == 2)
150+
return 7;
151+
}
152+
return 0;
153+
}
154+
155+
void go(int n)
156+
{
157+
if(turn % 2)
158+
board[n] = 3;
159+
else
160+
board[n] = 5;
161+
turn++;
162+
}
163+
164+
void player_first()
165+
{
166+
int pos;
167+
168+
check_draw();
169+
draw_board();
170+
gotoxy(30,18);
171+
printf("Your Turn :> ");
172+
scanf("%d",&pos);
173+
174+
if(board[pos] != 2)
175+
player_first();
176+
177+
if(pos == posswin(player))
178+
{
179+
go(pos);
180+
draw_board();
181+
gotoxy(30,20);
182+
//textcolor(128+RED);
183+
printf("Player Wins");
184+
getch();
185+
exit(0);
186+
}
187+
188+
go(pos);
189+
draw_board();
190+
start_game();
191+
}
192+
193+
void start_game()
194+
{
195+
// p==1 then X p==0 then O
196+
if(posswin(comp))
197+
{
198+
go(posswin(comp));
199+
flag = 1;
200+
}
201+
else if(posswin(player))
202+
go(posswin(player));
203+
else if(make2())
204+
go(make2());
205+
else
206+
go(make4());
207+
draw_board();
208+
209+
if(flag)
210+
{
211+
gotoxy(30,20);
212+
//textcolor(128+RED);
213+
printf("Computer wins");
214+
getch();
215+
}
216+
else
217+
player_first();
218+
}
219+
220+
void check_draw()
221+
{
222+
if(turn > 9)
223+
{
224+
gotoxy(30,20);
225+
//textcolor(128+RED);
226+
printf("Game Draw");
227+
getch();
228+
exit(0);
229+
}
230+
}
231+
232+
void draw_board()
233+
{
234+
int j;
235+
236+
for(j=9; j<17; j++)
237+
{
238+
gotoxy(35,j);
239+
printf("| |");
240+
}
241+
gotoxy(28,11);
242+
printf("-----------------------");
243+
gotoxy(28,14);
244+
printf("-----------------------");
245+
246+
for(j=1; j<10; j++)
247+
{
248+
if(board[j] == 3)
249+
put_X_O('X',j);
250+
else if(board[j] == 5)
251+
put_X_O('O',j);
252+
}
253+
}
254+
255+
void put_X_O(char ch,int pos)
256+
{
257+
int m;
258+
int x = 31, y = 10;
259+
260+
m = pos;
261+
262+
if(m > 3)
263+
{
264+
while(m > 3)
265+
{
266+
y += 3;
267+
m -= 3;
268+
}
269+
}
270+
if(pos % 3 == 0)
271+
x += 16;
272+
else
273+
{
274+
pos %= 3;
275+
pos--;
276+
while(pos)
277+
{
278+
x+=8;
279+
pos--;
280+
}
281+
}
282+
gotoxy(x,y);
283+
printf("%c",ch);
284+
}

0 commit comments

Comments
 (0)