-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.c
154 lines (145 loc) · 3.99 KB
/
tictactoe.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
#include <stdio.h>
char a, ar[3][3];
int b, d, count = 0, check1[100];
void setdata1(int);
void setdata2(int);
int checkdata();
int check(int x)
{
for (int i = 0; i < 9; i++)
{
if (check1[i] == x)
return 1;
}
}
int checkdata()
{
if ((ar[0][0] == 'x' && ar[0][1] == 'x' && ar[0][2] == 'x') || (ar[0][0] == 'o' && ar[0][1] == 'o' && ar[0][2] == 'o'))
return 1;
if ((ar[1][0] == 'x' && ar[1][1] == 'x' && ar[1][2] == 'x') || (ar[1][0] == 'o' && ar[1][1] == 'o' && ar[1][2] == 'o'))
return 1;
if ((ar[2][0] == 'x' && ar[2][1] == 'x' && ar[2][2] == 'x') || (ar[2][0] == 'o' && ar[2][1] == 'o' && ar[2][2] == 'o'))
return 1;
if ((ar[0][0] == 'x' && ar[1][0] == 'x' && ar[2][0] == 'x') || (ar[0][0] == 'o' && ar[1][0] == 'o' && ar[2][0] == 'o'))
return 1;
if ((ar[0][1] == 'x' && ar[1][1] == 'x' && ar[2][1] == 'x') || (ar[0][1] == 'o' && ar[1][1] == 'o' && ar[2][1] == 'o'))
return 1;
if ((ar[0][2] == 'x' && ar[1][2] == 'x' && ar[2][2] == 'x') || (ar[0][2] == 'o' && ar[1][2] == 'o' && ar[2][2] == 'o'))
return 1;
if ((ar[0][0] == 'x' && ar[1][1] == 'x' && ar[2][2] == 'x') || (ar[0][0] == 'o' && ar[1][1] == 'o' && ar[2][2] == 'o'))
{
return 1;
}
if ((ar[0][2] == 'x' && ar[1][1] == 'x' && ar[2][0] == 'x') || (ar[0][2] == 'o' && ar[1][1] == 'o' && ar[2][0] == 'o'))
{
return 1;
}
}
void orientation()
{
printf(" %c %c %c\n", ar[0][0], ar[0][1], ar[0][2]);
printf(" - - - \n");
printf(" %c %c %c\n", ar[1][0], ar[1][1], ar[1][2]);
printf(" - - - \n");
printf(" %c %c %c\n", ar[2][0], ar[2][1], ar[2][2]);
printf(" - - - \n");
}
void setdata1(int z)
{
if (z == 1)
ar[0][0] = 'x';
else if (z == 2)
ar[0][1] = 'x';
else if (z == 3)
ar[0][2] = 'x';
else if (z == 4)
ar[1][0] = 'x';
else if (z == 5)
ar[1][1] = 'x';
else if (z == 6)
ar[1][2] = 'x';
else if (z == 7)
ar[2][0] = 'x';
else if (z == 8)
ar[2][1] = 'x';
else if (z == 9)
ar[2][2] = 'x';
}
void setdata2(int x)
{
if (x == 1)
ar[0][0] = 'o';
else if (x == 2)
ar[0][1] = 'o';
else if (x == 3)
ar[0][2] = 'o';
else if (x == 4)
ar[1][0] = 'o';
else if (x == 5)
ar[1][1] = 'o';
else if (x == 6)
ar[1][2] = 'o';
else if (x == 7)
ar[2][0] = 'o';
else if (x == 8)
ar[2][1] = 'o';
else if (x == 9)
ar[2][2] = 'o';
}
void main()
{
printf("Are you ready to play tic-tac-toe (y/n)\n");
scanf("%c", &a);
if (a == 'y')
{
printf("Orientation of places with respect to numbers are \n");
printf(" 1 2 3\n");
printf(" - - - \n");
printf(" 4 5 6\n");
printf(" - - - \n");
printf(" 7 8 9\n");
printf(" - - - \n");
for (int i = 0; i < 5; i++)
{
label:
printf("Enter the position\n");
scanf("%d", &b);
if (check(b) == 1)
{
printf("The number is already entered");
goto label;
}
else
check1[i] = b;
setdata1(b);
orientation();
if (checkdata() == 1)
{
printf("YOU WON THE GAME\n");
break;
}
if (i < 4)
{
label2:
printf("Enter the position\n");
scanf("%d", &b);
if (check(b) == 1)
{
printf("The number is already entered \n");
goto label2;
}
else
check1[i + 5] = b;
setdata2(b);
orientation();
if (checkdata() == 1)
{
printf("YOU WON THE GAME\n");
break;
}
}
}
}
else
printf("Okk");
}