-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.c
328 lines (283 loc) · 8.21 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
/*******************************************************************
START OF PROJECT
********************************************************************/
// Defining preprocessor directives
#include <stdio.h>
#include <stdlib.h>
// Defining Constants
#define X 'X'
#define O '0'
#define WAIT 4
/*
Creating node structure which stores the following values:
[int] position: which stores the position of the character within the board.
[char] character: which stores the character placed within the block inside the board.
[struct] node *next: pointer that stores the address of the next node.
*/
struct node
{
int position;
char character;
struct node *next;
}*start = NULL;
// Declaring and Initlalizing prev_node which is used while creating the linked list
struct node *prev_node = NULL;
// This function clears the console screen and prints the ASCII art of this script
void printWelcomeMessage()
{
system("clear");
printf("'########:'####::'######:::::'########::::'###:::::'######:::::'########::'#######::'########:\n");
printf("... ##..::. ##::'##... ##::::... ##..::::'## ##:::'##... ##::::... ##..::'##.... ##: ##.....::\n");
printf("::: ##::::: ##:: ##:::..:::::::: ##:::::'##:. ##:: ##:::..:::::::: ##:::: ##:::: ##: ##:::::::\n");
printf("::: ##::::: ##:: ##::::::::::::: ##::::'##:::. ##: ##::::::::::::: ##:::: ##:::: ##: ######:::\n");
printf("::: ##::::: ##:: ##::::::::::::: ##:::: #########: ##::::::::::::: ##:::: ##:::: ##: ##...::::\n");
printf("::: ##::::: ##:: ##::: ##::::::: ##:::: ##.... ##: ##::: ##::::::: ##:::: ##:::: ##: ##:::::::\n");
printf("::: ##::::'####:. ######:::::::: ##:::: ##:::: ##:. ######:::::::: ##::::. #######:: ########:\n");
printf(":::..:::::....:::......:::::::::..:::::..:::::..:::......:::::::::..::::::.......:::........::\n");
printf("\n");
printf("--------------------\n");
printf("| CREATORS: |\n");
printf("| Arifa Arif |\n");
printf("| Apoorv Gupta |\n");
printf("| Vishesh Dvivedi |\n");
printf("--------------------\n");
printf("\n");
}
/*
This function is used to append node to the linked list
Required parameters:
[int] position: The value of position to be stored within the new node
[char] character: The character value to be stored within the new node
*/
void insertNode(int position, char character)
{
struct node *current_node = NULL;
current_node = (struct node*)malloc(sizeof(struct node));
if(current_node == NULL)
{
printf("\n[ERROR] Memory cannot be allocated.\n");
exit(0);
}
else
{
current_node->position = position;
current_node->character = character;
current_node->next = NULL;
prev_node->next = current_node;
prev_node = current_node;
}
}
/*
This function is used to create the linked list which stores the value of board
*/
void createBoard()
{
start = (struct node*)malloc(sizeof(struct node));
if(start == NULL)
{
printf("\n[ERROR] Memory cannot be allocated.\n");
exit(0);
}
else
{
start->position = 1;
start->character = '1';
start->next = NULL;
prev_node = start;
}
for (int i=2; i<10; i++)
{
insertNode(i, i+'0');
}
}
/*
This function is used to check if a certain position within the board is empty or not_eq
Required parameters:
[int] position: The position within the board to be checked.
Return:
[int] : Returns 1 if position is not occupied, else 0
*/
int checkPosition(int position)
{
struct node *prev_node = start;
while (prev_node!= NULL)
{
if(prev_node->position == position)
{
if ((prev_node->character==X) || (prev_node->character==O))
{
printf("\n[ERROR] Position %d is already occupied. Please choose another position.\n\n", position);
sleep(WAIT);
return 0;
}
else
{
return 1;
}
}
prev_node = prev_node->next;
}
}
/*
This function is used to insert a character at a certain position within the board.
Required parameters:
[int] position: The position to store the character within the board.
[char] character: The character to insert at the position.
*/
void insertCharacterAtPosition(int position, char character)
{
struct node *prev_node = start;
while (prev_node!= NULL)
{
if(prev_node->position == position)
{
prev_node->character = character;
}
prev_node = prev_node->next;
}
}
/*
This function is used to check whether any user has won the game or not.
Return:
[int] : Returns 1 if any user wins, else 0 if no user won.
*/
int checkWin()
{
struct node *prev_node = start;
char charList[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
for(int i=0; i<9; i++)
{
charList[i] = prev_node->character;
prev_node = prev_node->next;
}
// Checking for horizontal victory
for(int i=0; i<9; i+=3)
{
if ((charList[i] == charList[i+1]) && (charList[i] == charList[i+2]))
{
return 1;
}
}
// Checking for vertical victory
for(int i=0; i<3; i++)
{
if ((charList[i] == charList[i+3]) && (charList[i+3] == charList[i+6]))
{
return 1;
}
}
// Checking for diagonal victory
if ((charList[0] == charList[4]) && (charList[4] == charList[8]))
{
return 1;
}
else if ((charList[2] == charList[4]) && (charList[4] == charList[6]))
{
return 1;
}
return 0;
}
/*
This function is used to display the welcome message along with the game board.
*/
void display()
{
printWelcomeMessage();
struct node *prev_node;
if(start == NULL)
{
printf("The list is empty");
}
else
{
int counter=0;
prev_node = start;
printf("\n");
while(prev_node!=NULL)
{
for (int i=0; i<3; i++)
{
printf(" | | \n");
for (int j=0; j<3; j++)
{
if (j != 2)
{
printf(" %c |", prev_node->character);
prev_node = prev_node->next;
counter ++;
}
else
{
printf(" %c ", prev_node->character);
prev_node = prev_node->next;
counter++;
}
}
if (counter != 9)
{
printf("\n_____|_____|_____\n");
}
else
{
printf("\n | | \n");
}
}
}
}
}
/*
Main function of the script.
*/
void main()
{
int counter = 0, position=0, num=0;
char character, choice[100];
createBoard();
while (counter < 9)
{
display();
num = counter % 2;
if (num == 0)
{
character = X;
}
else
{
character = O;
}
printf("\nPlayer %d, enter your choice: ", num+1);
gets(choice);
position = atoi(choice);
if ((position >= 1) && (position <= 9))
{
if (checkPosition(position))
{
insertCharacterAtPosition(position, character);
if(checkWin())
{
display();
printf("\nConguratulations !!! Player %d won !!!", num+1);
break;
}
}
else
{
continue;
}
counter++;
}
else
{
printf("\n[ERROR] Invalid input...Please enter a number between 1 and 9\n");
sleep(WAIT);
}
}
if(counter == 9)
{
display();
printf("\nThe match ended with a DRAW !!! Better Luck Next Time !!!");
}
}
/*******************************************************************
END OF PROJECT
********************************************************************/