-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictaktoe.c
108 lines (99 loc) · 2.49 KB
/
tictaktoe.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
#include<stdio.h>
#include<conio.h>
void display(int board[3][3])
{
int i, j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("%c\t",board[i][j]);
}
printf("\n");
}
}
void input(int board[3][3])
{
int complete = 0;
char fill;
char player;
int time = 0;
int inp;
while(!complete && time!=9)
{
if (time%2 == 0)
{
player = 'A';
fill = 'X';
}
else
{
player ='B';
fill = 'O';
}
printf("player %c chhose position to put %c",player, fill);
scanf("%d",&inp);
switch (inp)
{
case 1: board[0][0] = fill;
break;
case 2: board[0][1] = fill;
break;
case 3: board[0][2] = fill;
break;
case 4: board[1][0] = fill;
break;
case 5: board[1][1] = fill;
break;
case 6: board[1][2] = fill;
break;
case 7: board[2][0] = fill;
break;
case 8: board[2][1] = fill;
break;
case 9: board[2][2] = fill;
break;
}
display(board);
complete = check(board);
if(complete==1)
{
printf("player %c won", player);
}
time++;
}
if (time == 9 && complete == 0)
printf("game draw");
}
int check(int board[3][3])
{
if ((board[0][0]==board[1][1] && board[1][1]==board[2][2]) || (board[0][2]==board[1][1] && board[1][1]==board[2][0]))
{
return 1;
}
else
{
if((board[0][0]==board[0][1] && board[0][1]==board[0][2]) ||
(board[1][0]==board[1][1] && board[1][1]==board[1][2]) ||
(board[2][0]==board[2][1] && board[2][1]==board[2][2]))
{
return 1;
}
else if((board[0][0]==board[1][0] && board[1][0]==board[2][0]) ||
(board[0][1]==board[1][1] && board[1][1]==board[2][1]) ||
(board[0][2]==board[1][2] && board[1][2]==board[2][2]))
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
int board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
display(board);
input(board);
}