-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathtic_tac_toe.c
449 lines (402 loc) · 9.74 KB
/
tic_tac_toe.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* @file tic-tac-toe.c
* @author [vivekboss99](github.com/vivekboss99)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief [Tic-Tac-Toe game](https://en.wikipedia.org/wiki/Tic-tac-toe)
* implementation in C
* @details Tic-Tac-Toe Game, where the user can decide to play with the
* computer(single player mode) or with other user(double player mode), the
* code as an array named 'game_table' which is the table and user needs to enter the
* position inside the array(from 1-9) where he/she wants to place 'X' or 'O' on the
* table.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Functions Declarations
static void singlemode();
static void doublemode();
static void placex(int); // used for placing position of X by the 1st player
static void place(); // used by the computer to place O
static void placey(int); // used in Double Player mode by the 2nd player to
// place the position of O
int checkwin(); // checks everytime when a player or computer places 'X' or 'O'
/** Tic-Tac-Toe table, so basically we are using variable 'game_table' as the table(size:3X3) and
* updating it regularly
*/
static char game_table[9];
/**
* Main program function.
* @returns 0 on clean exit.
* @note No checks are included for program execution failures!
*/
int main()
{
srand( (unsigned int)time(NULL));
int l = 0;
do
{
int n = 0;
// filling the table with multiple asterisks
for (int i = 0; i < 9; i++) game_table[i] = '*';
// displaying the main menu
printf("***************************************\n");
printf("*************TIC TAC TOE***************\n");
printf("***************************************\n");
printf("***********1. YOU vs COMPUTER ***********\n");
printf("***********2. YOU vs PLAYER ***********\n");
printf("***********3.EXIT *********************\n");
printf("Enter your choice : ");
scanf("%d", &n);
switch (n) // switch case to select between single player mode or
// double player mode
{
case 1:
singlemode();
break;
case 2:
doublemode();
break;
default:
printf("THANK YOU and EXIT!");
}
printf("Next game ? : ");
printf("Enter 1 – YES and 0 - NO ");
scanf("%d", &l);
} while (l == 1);
return 0;
}
/**
* @brief Implementation of game vs computer
*
* @returns None
*/
void singlemode()
{
int m;
int k = 0;
int table_fill_count=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c ", game_table[k]);
k++;
}
printf("\n");
}
for (int x = 1; x < 10; x++)
{
k = 0;
printf("Where would you like to place 'x' ");
scanf("%d", &m);
placex(m);
if(table_fill_count<4)
{
place();
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c ", game_table[k]);
k++;
}
printf("\n");
}
table_fill_count++;
int o = checkwin();
if (o == -1 || o == -2)
{
if (o == -1)
{
printf("YOU WIN\n");
}
if (o == -2)
{
printf("YOU LOSE\n");
}
break;
}
if (table_fill_count==4)
{
printf("\nDRAW ");
break;
}
}
}
/**
* @brief Implementation of game vs another player.
*
* @returns None
*/
void doublemode()
{
int m;
int e1;
int k = 0;
int doublemode_table_count=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c ", game_table[k]);
k++;
}
printf("\n");
}
for (int x = 1; x < 10; x++)
{
k = 0;
printf("PLAYER1 - where would you like to place 'x' : ");
scanf("%d", &m);
placex(m);
if(doublemode_table_count<4)
{
printf("PLAYER2 - where would you like to place 'o' : ");
scanf("%d", &e1);
placey(e1);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c ", game_table[k]);
k++;
}
printf("\n");
}
doublemode_table_count++;
int o = checkwin();
if (o == -1 || o == -2)
{
if (o == -1)
{
printf("Player 1 WIN\n");
}
if (o == -2)
{
printf("Player 2 WIN\n");
}
break;
}
if (doublemode_table_count==4)
{
printf("\nDRAW ");
break;
}
}
}
int check_placex(){
char input[50];
int n1;
while (1){
fgets(input,49,stdin);
if ( strlen(input) > 2 || strlen(input) == 0){
fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
continue;
}
if(sscanf(input,"%d",&n1) != 1){
fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
continue;
}
if ((game_table[n1-1] == 'x') || (game_table[n1-1]) == 'o' || (n1== 0)){
fprintf(stderr,"Already allocated, Enter number: ");
continue;
}
return n1;
}
}
/**
* @brief Update table by placing an `X`
*
* @param m location to place `X`
*
* @returns None
*/
void placex(int m)
{
int n1 = 0;
if (m >= 1 && m <= 9)
{
if (game_table[m - 1] != 'x' && game_table[m - 1] != 'o')
{
game_table[m - 1] = 'x';
}
else
{
int n = check_placex();
placex(n);
}
}
else
{
int n = check_placex();
placex(n);
}
}
/**
* @brief Update table by placing an `O`
*
* @returns None
*/
void place()
{
int e = rand() % 9;
if (e >= 0)
{
if (game_table[e] != 'x' && game_table[e] != 'o')
{
game_table[e] = 'o';
printf("\n Computer placed at %d position\n", e + 1);
}
else
{
place();
}
}
}
/**
* @brief Update table by placing an `O`
*
* @param e1 location to place `O`
*
* @returns None
*/
void placey(int e1)
{
int n1 = 0;
if (e1 >= 1 && e1 <= 9)
{
if (game_table[e1 - 1] != 'x' && game_table[e1 - 1] != 'o')
{
game_table[e1 - 1] = 'o';
}
else
{
int n = check_placex();
placex(n);
}
}
else
{
int n = check_placex();
placex(n);
}
}
/**
* @brief Implementation of win conditon checker for 'X' or 'O' whenever the table is updated
*
* @returns -1: if 'X' won
* @returns -2: if 'O' won
* @returns 0: if there is no win condition for 'X' or 'O'
*/
int checkwin()
{
if (game_table[0] == game_table[1] && game_table[1] == game_table[2])
{
if (game_table[0] == 'x' && game_table[1] == 'x' &&
game_table[2] == 'x')
{
return -1;
}
if (game_table[0] == 'o' && game_table[1] == 'o' &&
game_table[2] == 'o')
{
return -2;
}
}
else if (game_table[0] == game_table[4] && game_table[4] == game_table[8])
{
if (game_table[0] == 'x' && game_table[4] == 'x' &&
game_table[8] == 'x')
{
return -1;
}
if (game_table[0] == 'o' && game_table[4] == 'o' &&
game_table[8] == 'o')
{
return -2;
}
}
else if (game_table[0] == game_table[3] && game_table[3] == game_table[6])
{
if (game_table[0] == 'x' && game_table[3] == 'x' &&
game_table[6] == 'x')
{
return -1;
}
if (game_table[0] == 'o' && game_table[3] == 'o' &&
game_table[6] == 'o')
{
return -2;
}
}
else if (game_table[3] == game_table[4] && game_table[4] == game_table[5])
{
if (game_table[3] == 'x' && game_table[4] == 'x' &&
game_table[5] == 'x')
{
return -1;
}
if (game_table[3] == 'o' && game_table[4] == 'o' &&
game_table[5] == 'o')
{
return -2;
}
}
else if (game_table[6] == game_table[7] && game_table[7] == game_table[8])
{
if (game_table[6] == 'x' && game_table[7] == 'x' &&
game_table[8] == 'x')
{
return -1;
}
if (game_table[6] == 'o' && game_table[7] == 'o' &&
game_table[8] == 'o')
{
return -2;
}
}
else if (game_table[1] == game_table[4] && game_table[4] == game_table[7])
{
if (game_table[1] == 'x' && game_table[4] == 'x' &&
game_table[7] == 'x')
{
return -1;
}
if (game_table[1] == 'o' && game_table[4] == 'o' &&
game_table[7] == 'o')
{
return -2;
}
}
else if (game_table[2] == game_table[5] && game_table[5] == game_table[8])
{
if (game_table[2] == 'x' && game_table[5] == 'x' &&
game_table[8] == 'x')
{
return -1;
}
if (game_table[2] == 'o' && game_table[5] == 'o' &&
game_table[8] == 'o')
{
return -2;
}
}
else if (game_table[2] == game_table[4] && game_table[4] == game_table[6])
{
if (game_table[2] == 'x' && game_table[4] == 'x' &&
game_table[6] == 'x')
{
return -1;
}
if (game_table[2] == 'o' && game_table[4] == 'o' &&
game_table[6] == 'o')
{
return -2;
}
}
return 0;
}