-
Notifications
You must be signed in to change notification settings - Fork 0
/
vs_computer.c
133 lines (129 loc) · 3.72 KB
/
vs_computer.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
#include <stdio.h>
#include <windows.h>
int main()
{
int i, j, r1, c1, r2, c2, x = 0, c = 0,m=0;
char a[3][3], p1[20], p2[20];
// initialising array = ' '
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
a[i][j] = ' ';
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf(" %c ", a[i][j]);
if (j < 2)
printf("|");
}
if (i < 2)
printf("\n-----------\n");
else
printf("\n");
}
printf("Welcome to Tic Tac Toe game\n");
printf("Enter player name : ");
scanf("%s", p1);
printf("Let's start Playing\n");
for (i = 3; i >= 1; i--)
{
sleep(1);
printf("Starting in %ds\n", i);
}
while (1)
{
do
{
printf("Enter Row and column %s = ", p1);
scanf("%d", &r1);
scanf("%d", &c1);
m++;
} while ((a[r1][c1] == 'X') || (a[r1][c1] == 'O'));
c++;
a[r1][c1] = 'X';
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf(" %c ", a[i][j]);
if (j < 2)
printf("|");
}
if (i < 2)
printf("\n-----------\n");
else
printf("\n");
}
if ((a[0][0] == 'X') && (a[1][1] == 'X') && (a[2][2] == 'X'))
x = 1;
if ((a[0][2] == 'X') && (a[1][1] == 'X') && (a[2][0] == 'X'))
x = 1;
if ((a[0][0] == 'X') && (a[0][1] == 'X') && (a[0][2] == 'X'))
x = 1;
if ((a[1][0] == 'X') && (a[1][1] == 'X') && (a[1][2] == 'X'))
x = 1;
if ((a[2][0] == 'X') && (a[2][1] == 'X') && (a[2][2] == 'X'))
x = 1;
if ((a[0][0] == 'X') && (a[1][0] == 'X') && (a[2][0] == 'X'))
x = 1;
if ((a[0][1] == 'X') && (a[1][1] == 'X') && (a[2][1] == 'X'))
x = 1;
if ((a[0][2] == 'X') && (a[1][2] == 'X') && (a[2][2] == 'X'))
x = 1;
if (x == 1)
break;
do
{
srand(time(NULL));
r2 = rand() % (3);
c2 = rand() % (3);
} while ((a[r2][c2] == 'X') || (a[r2][c2] == 'O'));
c++;
a[r2][c2] = 'O';
printf("\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf(" %c ", a[i][j]);
if (j < 2)
printf("|");
}
if (i < 2)
printf("\n-----------\n");
else
printf("\n");
}
if ((a[0][0] == 'O') && (a[1][1] == 'O') && (a[2][2] == 'O'))
x = 2;
if ((a[0][2] == 'O') && (a[1][1] == 'O') && (a[2][0] == 'O'))
x = 2;
if ((a[0][0] == 'O') && (a[0][1] == 'O') && (a[0][2] == 'O'))
x = 2;
if ((a[1][0] == 'O') && (a[1][1] == 'O') && (a[1][2] == 'O'))
x = 2;
if ((a[2][0] == 'O') && (a[2][1] == 'O') && (a[2][2] == 'O'))
x = 2;
if ((a[0][0] == 'O') && (a[1][0] == 'O') && (a[2][0] == 'O'))
x = 2;
if ((a[0][1] == 'O') && (a[1][1] == 'O') && (a[2][1] == 'O'))
x = 2;
if ((a[0][2] == 'O') && (a[1][2] == 'O') && (a[2][2] == 'O'))
x = 2;
if ((x == 2))
break;
if (c == 9)
{
printf("Tie");
break;
}
}
if (x == 1)
printf("%s won the game", p1);
if (x == 2)
printf("Computer won the game");
}