-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTIC TAC TOE in C
228 lines (189 loc) · 5.63 KB
/
TIC TAC TOE in 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
/*initializing a 2D array for creating a tic tac toe board*/
char square[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
void draw_the_board(){
printf("\n\n TIC TAC TOE \n\n");
printf("PLAYER 1 IS (X) and PLAYER 2 IS (O)\n\n");
printf("__________________________\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[0][0], square[0][1], square[0][2]);
printf("________|________|________\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[1][0], square[1][1], square[1][2]);
printf("________|________|________\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[2][0], square[2][1], square[2][2]);
printf("________|________|________\n");
}
/*This functions check the combination of each cell in the 2D array matrix , if any 3 cell matches horizontally,vertically or diagonally it returns 0*/
int check_the_winner(){
//checking combination horizontally
if(square[0][0]==square[0][1]&&square[0][1]==square[0][2]){
return 1;
}
else if (square[1][0]==square[1][1]&&square[1][1]==square[1][2]){
return 1;
}
else if (square[2][0]==square[2][1]&&square[2][1]==square[2][2]){
return 1;
}
//checking combination vertically
else if (square[0][0]==square[1][0]&&square[1][0]==square[2][0]){
return 1;
}
else if (square[0][1]==square[1][1]&&square[1][1]==square[2][1]){
return 1;
}
else if (square[0][2]==square[1][2]&&square[1][2]==square[2][2]){
return 1;
}
else if (square[2][0]==square[2][1]&&square[2][1]==square[2][2]){
return 1;
}
//checking combination diagonally
else if (square[0][0]==square[1][1]&&square[1][1]==square[2][2]){
return 1;
}
else if (square[0][2]==square[1][1]&&square[1][1]==square[2][0]){
return 1;
}
/*Return 0 if no combination is found and game is drawmln*/
else if (square[0][0]!='1'&&square[0][1]!='2'&&square[0][2]!='3'&&square[1][0]!='4'&&square[1][1]!='5'&&square[1][2]!='6'&&square[2][0]!='7'&&square[2][1]!='8'&&square[2][2]!='9'){
return 0;
}
/* This else statement shows that the game is still running*/
else{
return -1;
}
return 0;
}
//The main function
int main(){
int i,player,choice;
char mark;
printf("SELECT THE PLAYER NO:");
scanf("%d",&player);
if(player<0 || player>2){
printf("INVALID SELECTION! please enter right option");
}
/*This do while places the marks at specified position at does this till check_the_winner function returns -1 i.e game is still running*/
do{
draw_the_board();
player=(player%2==0)? 2:1;
printf("PLAYER %d SELECT YOUR CHOICE:",player);
scanf("%d",&choice);
mark=(player==1)?'X':'O';
if(choice==1 && square[0][0]=='1'){
square[0][0]=mark;
}
else if(choice==2&&square[0][1]=='2'){
square[0][1]=mark;
}
else if(choice==3&&square[0][2]=='3'){
square[0][2]=mark;
}
else if(choice==4&&square[1][0]=='4'){
square[1][0]=mark;
}
else if(choice==5&&square[1][1]=='5'){
square[1][1]=mark;
}
else if(choice==6&&square[1][2]=='6'){
square[1][2]=mark;
}
else if(choice==7&&square[2][0]=='7'){
square[2][0]=mark;
}
else if(choice==8&&square[2][1]=='8'){
square[2][1]=mark;
}
else if(choice==9&&square[2][2]=='9'){
square[2][2]=mark;
}
else{
printf("==>!!INVALID OPTION!!<==");
player--;
}
/*If a player enters invalid option then to give him one more option we decrement the player*/
i=check_the_winner();
player++;
}while(i==-1);
if(i==1){
printf("==> PLAYER %d WON <==",--player);
}
else{
printf("==> GAME DRAW <==");
}
return 0;
}
//output
SELECT THE PLAYER NO:1
TIC TAC TOE
PLAYER 1 IS (X) and PLAYER 2 IS (O)
__________________________
| |
1 | 2 | 3
________|________|________
| |
4 | 5 | 6
________|________|________
| |
7 | 8 | 9
________|________|________
PLAYER 1 SELECT YOUR CHOICE:1
TIC TAC TOE
PLAYER 1 IS (X) and PLAYER 2 IS (O)
__________________________
| |
X | 2 | 3
________|________|________
| |
4 | 5 | 6
________|________|________
| |
7 | 8 | 9
________|________|________
PLAYER 2 SELECT YOUR CHOICE:2
TIC TAC TOE
PLAYER 1 IS (X) and PLAYER 2 IS (O)
__________________________
| |
X | O | 3
________|________|________
| |
4 | 5 | 6
________|________|________
| |
7 | 8 | 9
________|________|________
PLAYER 1 SELECT YOUR CHOICE:4
TIC TAC TOE
PLAYER 1 IS (X) and PLAYER 2 IS (O)
__________________________
| |
X | O | 3
________|________|________
| |
X | 5 | 6
________|________|________
| |
7 | 8 | 9
________|________|________
PLAYER 2 SELECT YOUR CHOICE:5
TIC TAC TOE
PLAYER 1 IS (X) and PLAYER 2 IS (O)
__________________________
| |
X | O | 3
________|________|________
| |
X | O | 6
________|________|________
| |
7 | 8 | 9
________|________|________
PLAYER 1 SELECT YOUR CHOICE:7
==> PLAYER 1 WON <==
[Program finished]
printf("done")