-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.c
279 lines (235 loc) · 7.99 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
#include <stdio.h>
// Made by Mr.Quantum (Darshan Patel)
// struct players{
// char name[20];
// }; // can upgrade to display score of the player by history file in one by one game
int check(char matrix[3][3]);
void printCanvas(char matrix[3][3]);
void updateHistory(char name1[20], char name2[20], int whoseTurn);
int findLastGameNumber();
int main()
{
char choose = 'y';
while (choose == 'y' || choose == 'Y')
{
printf("Instructions:-\n\t1) Enter '-1 -1' to terminate the game before the next time a player is asked to enter the block location\n\t2) Enter the coordinates of the block in which you wanna input (space seperated integers) when asked Coordinates\n\t3) The coordinates index starts from 1,1 to 3,3 \n4) Press H to see game History\n");
char name1[20], name2[20];
printf("Player 1 name : ");
fgets(name1, sizeof(name1), stdin); // to store the full names along with space in between
printf("Player 2 name : ");
fgets(name2, sizeof(name2), stdin);
printf("\n%s is assinged 'X'\n%s is assigned 'O'\n", name1, name2);
int win = 0;
char matrix[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}}; // So that initially the every block is different valued because here I check equal character in the blocks to decide winner.
int terminate;
int whoseTurn = 2;
int count = 0; // for counting to check whether all the blocks are occupied or not. And then check if its draw.
while (1)
{
int x, y;
if (whoseTurn == 2)
{
printf("It's %s's turn to make a move!\n", name1);
whoseTurn = 1;
int error = 0;
while (error != 1) // error handling
{
printf("\nCoordinates : ");
scanf("%d %d", &x, &y);
if (x == -1)
{
return -1; // terminate the game
}
if (((x > 3) || (x < 1)) || ((y > 3) || (y < 1)))
{
printf("Please Enter valid coordinates!");
}
else if (matrix[x - 1][y - 1] == 'X' || matrix[x - 1][y - 1] == 'O')
{
printf("The block is already occupied. Please enter valid coordinates!");
}
else
{
error = 1;
}
}
matrix[x - 1][y - 1] = 'X';
count++;
printCanvas(matrix);
}
else
{
printf("It's %s's turn to make the move", name2);
whoseTurn = 2;
int error = 0;
while (error != 1) // error handling
{
printf("\nCoordinates : ");
scanf("%d %d", &x, &y);
if (x == -1)
{
return -1;
}
if (((x > 3) || (x < 1)) || ((y > 3) || (y < 1)))
{
printf("Please Enter valid coordinates!");
}
else if (matrix[x - 1][y - 1] == 'X' || matrix[x - 1][y - 1] == 'O')
{
printf("The block is already occupied. Please enter valid coordinates!");
}
else
{
error = 1;
}
}
matrix[x - 1][y - 1] = 'O';
count++;
printCanvas(matrix);
}
int checkWin = check(matrix);
if (checkWin == 1)
{
if (whoseTurn == 1)
{
printf("\nHurray! %s wins!", name1);
}
else if (whoseTurn == 2)
{
printf("\nHurray! %s wins!", name2);
}
break;
}
else if (count == 9) /*if all place occupied*/
{
printf("Ohh! The Game ended in Draw! \nNobody wins");
break;
}
}
updateHistory(name1, name2, whoseTurn);
printf("\n\nWanna Replay the game? (y/n)[case insensitive] : ");
getchar(); // somehow some buffer newline character is being taken by scanf below, hence used this to clear any buffer
scanf("%c", &choose);
if (choose == 'n')
{
printf("\nOkay! See you again on some another beautiful day!\n\nHave a nice Day ahead!\n\n");
}
}
printf("Press any key to exit...");
getchar(); // If the user runs this in windows terminal instead, than user has a time to see the result
return 0;
}
int check(char matrix[3][3])
{
int result = 0;
for (int i = 0; i < 3; i++)
{
if (((matrix[i][0] == matrix[i][1]) && (matrix[i][1] == matrix[i][2])) || ((matrix[0][i] == matrix[1][i]) && (matrix[1][i] == matrix[2][i])))
{
result = 1;
return result;
}
}
if ((matrix[0][0] == matrix[1][1]) && (matrix[1][1] == matrix[2][2]) || ((matrix[0][2] == matrix[1][1]) && matrix[1][1] == matrix[2][0]))
{
result = 1;
return result;
}
return 0;
}
void printCanvas(char matrix[3][3])
{
for (int i = 0; i < 3; i++)
{
printf("\t\t\t");
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] != 'X' && matrix[i][j] != 'O')
{
printf(". ");
}
else
{
printf("%c ", matrix[i][j]);
}
}
printf("\n\n");
}
printf("\n");
}
void updateHistory(char name1[20], char name2[20], int whoseTurn)
{
char *winner; // pointer to store adress of winner so That I can print it in history file below
if (whoseTurn == 1)
{
winner = name1;
}
else
{
winner = name2;
}
FILE *fptr1;
fptr1 = fopen("history.txt", "a"); // opens in both read and write mode
if (fptr1 == NULL)
{
printf("Error opening game history file...\nHistory is not saved.\nProcess Terminated");
return;
}
int gameNumber = findLastGameNumber();
fprintf(fptr1, "\n{\n"
"\tGame number : %d\n"
"\tPlayer 1 : %s"
"\tPlayer 2 : %s"
"\tWho Won : %s"
"}\n",
(gameNumber+1), name1, name2, winner);
fclose(fptr1);
}
int findLastGameNumber()
{
FILE *fptr2;
fptr2 = fopen("history.txt", "r");
fseek(fptr2, 0, SEEK_END);
int position = 0;
char ch;
int count=0;
while (1) // Note that atleast one previous Entry in whole file must be there for this to work. Because This is the simplest logic I come up with. Hence I have written 0th entry by default
{
// printf("counting\n");
fseek(fptr2, -1, SEEK_CUR);
ch = fgetc(fptr2);
if (ch == '\n')
{
count++;
}
if (count == 5)
{
break;
}
fseek(fptr2, -1, SEEK_CUR); // I had to do this again because reading the cahracter by fgetc moves the pointer forward again by one position
}
int gameNumber = 0;
char TempCharNum;
for (int i = 0; i < 3; i++) //Setting maximum file history to 3 digit number as it already is beyond what anyone would actually play XD
{
fseek(fptr2, -1, SEEK_CUR);
if (fgetc(fptr2) == ' ')
{
break;
}
TempCharNum = fgetc(fptr2);
if (i == 0)
{
gameNumber += (TempCharNum - '0');
}
else
{
gameNumber += (10 * (i) * (TempCharNum - '0')); // Here I am converting character to integer
fseek(fptr2, -1, SEEK_CUR);
}
}
return gameNumber;
}