Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
tic-tac-toe.c File Reference

Tic-Tac-Toe game implementation in C More...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for tic-tac-toe.c:

Functions

static void singlemode ()
 Implementation of game vs computer. More...
 
static void doublemode ()
 Implementation of game vs another player. More...
 
static void placex (int m)
 Update table by placing an X More...
 
static void place ()
 Update table by placing an O More...
 
static void placey (int e1)
 Update table by placing an O More...
 
int checkwin ()
 Implementation of win conditon checker for 'X' or 'O' whenever the table is updated. More...
 
int main ()
 Main program function. More...
 

Variables

static char game_table [9]
 Tic-Tac-Toe table, so basically we are using variable 'game_table' as the table(size:3X3) and updating it regularly.
 

Detailed Description

Tic-Tac-Toe game implementation in C

Author
vivekboss99
Krishna Vedala

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.

Function Documentation

◆ checkwin()

int checkwin ( )

Implementation of win conditon checker for 'X' or 'O' whenever the table is updated.

Returns
-1: if 'X' won
-2: if 'O' won
0: if there is no win condition for 'X' or 'O'
324 {
325  if (game_table[0] == game_table[1] && game_table[1] == game_table[2])
326  {
327  if (game_table[0] == 'x' && game_table[1] == 'x' &&
328  game_table[2] == 'x')
329  {
330  return -1;
331  }
332 
333  if (game_table[0] == 'o' && game_table[1] == 'o' &&
334  game_table[2] == 'o')
335  {
336  return -2;
337  }
338  }
339  else if (game_table[0] == game_table[4] && game_table[4] == game_table[8])
340  {
341  if (game_table[0] == 'x' && game_table[4] == 'x' &&
342  game_table[8] == 'x')
343  {
344  return -1;
345  }
346 
347  if (game_table[0] == 'o' && game_table[4] == 'o' &&
348  game_table[8] == 'o')
349  {
350  return -2;
351  }
352  }
353  else if (game_table[0] == game_table[3] && game_table[3] == game_table[6])
354  {
355  if (game_table[0] == 'x' && game_table[3] == 'x' &&
356  game_table[6] == 'x')
357  {
358  return -1;
359  }
360 
361  if (game_table[0] == 'o' && game_table[3] == 'o' &&
362  game_table[6] == 'o')
363  {
364  return -2;
365  }
366  }
367  else if (game_table[3] == game_table[4] && game_table[4] == game_table[5])
368  {
369  if (game_table[3] == 'x' && game_table[4] == 'x' &&
370  game_table[5] == 'x')
371  {
372  return -1;
373  }
374 
375  if (game_table[3] == 'o' && game_table[4] == 'o' &&
376  game_table[5] == 'o')
377  {
378  return -2;
379  }
380  }
381  else if (game_table[6] == game_table[7] && game_table[7] == game_table[8])
382  {
383  if (game_table[6] == 'x' && game_table[7] == 'x' &&
384  game_table[8] == 'x')
385  {
386  return -1;
387  }
388 
389  if (game_table[6] == 'o' && game_table[7] == 'o' &&
390  game_table[8] == 'o')
391  {
392  return -2;
393  }
394  }
395  else if (game_table[1] == game_table[4] && game_table[4] == game_table[7])
396  {
397  if (game_table[1] == 'x' && game_table[4] == 'x' &&
398  game_table[7] == 'x')
399  {
400  return -1;
401  }
402 
403  if (game_table[1] == 'o' && game_table[4] == 'o' &&
404  game_table[7] == 'o')
405  {
406  return -2;
407  }
408  }
409  else if (game_table[2] == game_table[5] && game_table[5] == game_table[8])
410  {
411  if (game_table[2] == 'x' && game_table[5] == 'x' &&
412  game_table[8] == 'x')
413  {
414  return -1;
415  }
416 
417  if (game_table[2] == 'o' && game_table[5] == 'o' &&
418  game_table[8] == 'o')
419  {
420  return -2;
421  }
422  }
423  else if (game_table[2] == game_table[4] && game_table[4] == game_table[6])
424  {
425  if (game_table[2] == 'x' && game_table[4] == 'x' &&
426  game_table[6] == 'x')
427  {
428  return -1;
429  }
430 
431  if (game_table[2] == 'o' && game_table[4] == 'o' &&
432  game_table[6] == 'o')
433  {
434  return -2;
435  }
436  }
437  return 0;
438 }

◆ doublemode()

void doublemode ( )
static

Implementation of game vs another player.

Returns
None
155 {
156  int m;
157  int e1;
158  int k = 0;
159  int doublemode_table_count=0;
160 
161  for (int i = 0; i < 3; i++)
162  {
163  for (int j = 0; j < 3; j++)
164  {
165  printf("%c ", game_table[k]);
166  k++;
167  }
168 
169  printf("\n");
170  }
171  for (int x = 1; x < 10; x++)
172  {
173  k = 0;
174 
175  printf("PLAYER1 - where would you like to place 'x' : ");
176  scanf("%d", &m);
177 
178  placex(m);
179  if(doublemode_table_count<4)
180  {
181  printf("PLAYER2 - where would you like to place 'o' : ");
182  scanf("%d", &e1);
183 
184  placey(e1);
185  }
186 
187  for (int i = 0; i < 3; i++)
188  {
189  for (int j = 0; j < 3; j++)
190  {
191  printf("%c ", game_table[k]);
192  k++;
193  }
194 
195  printf("\n");
196  }
197  doublemode_table_count++;
198  int o = checkwin();
199 
200  if (o == -1 || o == -2)
201  {
202  if (o == -1)
203  {
204  printf("Player 1 WIN\n");
205  }
206  if (o == -2)
207  {
208  printf("Player 2 WIN\n");
209  }
210 
211  break;
212  }
213  if (doublemode_table_count==4)
214  {
215  printf("\nDRAW ");
216  break;
217  }
218  }
219 }
Here is the call graph for this function:

◆ main()

int main ( )

Main program function.

Returns
0 on clean exit.
Note
No checks are included for program execution failures!
37 { srand(time(NULL));
38  int l = 0;
39  do
40  {
41  int n = 0;
42 
43  // filling the table with multiple asterisks
44  for (int i = 0; i < 9; i++) game_table[i] = '*';
45 
46  // displaying the main menu
47  printf("***************************************\n");
48  printf("*************TIC TAC TOE***************\n");
49  printf("***************************************\n");
50  printf("***********1. YOU vs COMPUTER ***********\n");
51  printf("***********2. YOU vs PLAYER ***********\n");
52  printf("***********3.EXIT *********************\n");
53  printf("Enter your choice : ");
54  scanf("%d", &n);
55 
56  switch (n) // switch case to select between single player mode or
57  // double player mode
58  {
59  case 1:
60  singlemode();
61  break;
62  case 2:
63  doublemode();
64  break;
65  default:
66  printf("THANK YOU and EXIT!");
67  }
68 
69  printf("Next game ? : ");
70  printf("Enter 1 – YES and 0 - NO ");
71  scanf("%d", &l);
72 
73  } while (l == 1);
74 
75  return 0;
76 }
Here is the call graph for this function:

◆ place()

void place ( )
static

Update table by placing an O

Returns
None
263 {
264 
265  int e = rand() % 9;
266 
267  if (e >= 0 && e <= 8)
268  {
269  if (game_table[e] != 'x' && game_table[e] != 'o')
270  {
271  game_table[e] = 'o';
272  printf("\n Computer placed at %d position\n", e + 1);
273  }
274  else
275  {
276  place();
277  }
278  }
279 }

◆ placex()

void placex ( int  m)
static

Update table by placing an X

Parameters
mlocation to place X
Returns
None
229 {
230  int n1;
231  if (m >= 1 && m <= 9)
232  {
233  if (game_table[m - 1] != 'x' && game_table[m - 1] != 'o')
234  {
235  game_table[m - 1] = 'x';
236  }
237  else
238  {
239  printf("Invalid move\n");
240 
241  printf("Enter new position : ");
242  scanf("%d", &n1);
243 
244  placex(n1);
245  }
246  }
247  else
248  {
249  printf("Invalid move \n");
250 
251  printf("Enter new position : ");
252  scanf("%d", &n1);
253 
254  placex(n1);
255  }
256 }

◆ placey()

void placey ( int  e1)
static

Update table by placing an O

Parameters
e1location to place O
Returns
None
288 {
289  int n1;
290  if (e1 >= 1 && e1 <= 9)
291  {
292  if (game_table[e1 - 1] != 'x' && game_table[e1 - 1] != 'o')
293  {
294  game_table[e1 - 1] = 'o';
295  }
296  else
297  {
298  printf("Invalid move \n");
299 
300  printf("Enter new position : ");
301  scanf("%d", &n1);
302 
303  placey(n1);
304  }
305  }
306  else
307  {
308  printf("Invalid move \n");
309 
310  printf("Enter new position : ");
311  scanf("%d", &n1);
312 
313  placey(n1);
314  }
315 }

◆ singlemode()

void singlemode ( )
static

Implementation of game vs computer.

Returns
None
84 {
85  int m;
86  int k = 0;
87  int table_fill_count=0;
88 
89  for (int i = 0; i < 3; i++)
90  {
91  for (int j = 0; j < 3; j++)
92  {
93  printf("%c ", game_table[k]);
94  k++;
95  }
96 
97  printf("\n");
98  }
99 
100  for (int x = 1; x < 10; x++)
101  {
102  k = 0;
103 
104  printf("Where would you like to place 'x' ");
105  scanf("%d", &m);
106 
107  placex(m);
108  if(table_fill_count<4)
109  {
110  place();
111  }
112 
113  for (int i = 0; i < 3; i++)
114  {
115  for (int j = 0; j < 3; j++)
116  {
117  printf("%c ", game_table[k]);
118  k++;
119 
120  }
121 
122  printf("\n");
123  }
124  table_fill_count++;
125  int o = checkwin();
126 
127  if (o == -1 || o == -2)
128  {
129  if (o == -1)
130  {
131  printf("YOU WIN\n");
132  }
133  if (o == -2)
134  {
135  printf("YOU LOSE\n");
136  }
137 
138  break;
139  }
140 
141  if (table_fill_count==4)
142  {
143  printf("\nDRAW ");
144  break;
145  }
146  }
147 }
Here is the call graph for this function:
placex
static void placex(int)
Update table by placing an X
Definition: tic-tac-toe.c:228
placey
static void placey(int)
Update table by placing an O
Definition: tic-tac-toe.c:287
checkwin
int checkwin()
Implementation of win conditon checker for 'X' or 'O' whenever the table is updated.
Definition: tic-tac-toe.c:323
game_table
static char game_table[9]
Tic-Tac-Toe table, so basically we are using variable 'game_table' as the table(size:3X3) and updatin...
Definition: tic-tac-toe.c:29
place
static void place()
Update table by placing an O
Definition: tic-tac-toe.c:262
doublemode
static void doublemode()
Implementation of game vs another player.
Definition: tic-tac-toe.c:154
singlemode
static void singlemode()
Implementation of game vs computer.
Definition: tic-tac-toe.c:83