-
Notifications
You must be signed in to change notification settings - Fork 1
/
tictactoe.c
94 lines (86 loc) · 2.41 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
#include <stdio.h>
char board[3][3];
int i,j,count=0;
void initialize_board()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
board[i][j]=' ';
}
void print_board()
{
printf("The current table:-\n");
printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}
int check_win()
{
for(i=0;i<3;i++)
{
if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0] !=' ')
if(board[i][0]=='X')
return 1;
else
return 2;
if(board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i] !=' ')
if(board[0][i]=='X')
return 1;
else
return 2;
}
if(board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[0][0] !=' ')
if(board[0][0]=='X')
return 1;
else
return 2;
if(board[0][2]==board[1][1] && board[1][1]==board[2][0] && board[0][2] !=' ')
if(board[0][2]=='X')
return 1;
else
return 2;
return 0;
}
int main()
{
int player=1,choice,r,c;
char mark;
initialize_board();
do
{
count++;
printf("\nEnter your numbers according to the orientation below:-\n");
printf(" 1 | 2 | 3 \n");
printf("-----------\n");
printf(" 4 | 5 | 6 \n");
printf("-----------\n");
printf(" 7 | 8 | 9 \n\n\n");
print_board();
player = (player % 2) ? 1: 2;
printf("Player %d, Enter a number from 1 to 9 to place your mark: ",player);
scanf("%d",&choice);
printf("\n");
r = --choice / 3;
c = choice % 3;
if(count>=10)
{
printf("Draw!\n");
return 0;
}
if(board[r][c]==' ')
{
mark = (player==1)? 'X' : 'O';
board[r][c] = mark;
player++;
}
else
printf("Invalid Move, Try Again!\n");
}while(check_win()==0);
print_board();
if(check_win()==1)
printf("Player 1(X) wins!\n");
else if(check_win()==2)
printf("Player 2(O) wins!\n");
}