-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
354 lines (335 loc) · 12.6 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <colors.h>
#include <windows.h>
#include <ctype.h>
#include <string.h>
#include "Struct.h"
#include "game_function.h"
int main()
{
while(1)
{
system("cls");
int menu;
setTextColor(stdout,TC_GREEN);
printf("\n \t \t\t\t\twelcome to our project:\n\n \t \t\t\t\tDOTS and BOXES:\n\n");
setTextColor(stdout,TC_CYAN);
printf("\t new game(enter:1):\n\n\t load game(enter:2):\n\n\t top ten(enter:3):\n\n\t exit(enter:4);\n\n\t enter the choose:");
setTextColor(stdout,TC_LIGHTGRAY);
menu = scanint();
system("cls");
//array of struct (name length , name, scores)
users usersarray[MAXSIZE] = {0};
//open the users file
FILE *userread = fopen("s.txt", "r");
if(userread == NULL)
{
//if the file does not exist initialize it
printf("users file was deleted\n");
userread = fopen("s.txt", "w");
fclose(userread);
userread = fopen("s.txt", "r");
}
//reading the file
int counter = 0;
while(!feof(userread))
{
fread(&usersarray[counter].namelen, sizeof(int), 1, userread);
fread(usersarray[counter].name,sizeof(char), usersarray[counter].namelen, userread);
usersarray[counter].name[usersarray[counter].namelen] = '\0';
fread(&usersarray[counter].score, sizeof(int), 1, userread);
counter++;
}
fclose(userread);
//n = number of boxes per row
//mode 1 for computer 2 for 2 players
//gamer is struct that has name , moves, score, name length
// player is the number of who will play
int n, mode;
gamer one, two;
int player, loadedtime = 0;
if(menu == 1)
{
//new game
setTextColor(stdout,TC_CYAN);
printf("\n\nenter the degree of difficulty:\nEasy (enter 1)\nMedium (enter 2)\nHard (enter 3)\nExtreme (enter 4)\n");
while(1)
{
n = scanint();
if(n>0&&n<5)//number of box must be bigger than 0 and smaller than 13 because the size of the terminal
break;
else
printf("error (invalid number)\n");
}
switch (n)
{
case 1:
n = 2;
break;
case 2:
n = 3;
break;
case 3:
n = 5;
break;
case 4:
n = 8;
break;
}
setTextColor(stdout,TC_LIGHTGRAY);
setTextColor(stdout,TC_BLUE);
printf("\none player (enter:1):\n\n");
setTextColor(stdout,TC_RED);
printf("two players (enter:2):\n");
setTextColor(stdout,TC_LIGHTGRAY);
while(1) //make sure user enter 1 or 2
{
mode= scanint();
if(mode==1||mode==2)
break;
else
printf("please enter 1 or 2\n");
}
system("cls");
setTextColor(stdout,TC_BLUE);
printf(" enter your name: ");
fflush(stdin);
gets(one.name);
strlwr(one.name);
one.lenname = strlen(one.name);
setTextColor(stdout,TC_LIGHTGRAY);
if(mode == 2)
{
setTextColor(stdout,TC_RED);
printf("enter the second name: ");
fflush(stdin);
gets(two.name);
strlwr(two.name);
setTextColor(stdout,TC_LIGHTGRAY);
}
else
strcpy(two.name, "computer");
two.lenname = strlen(two.name);
system("cls");
int size = 2*n + 2;
//initializing game array
char game[size][size];
for(int j=0; j < size; j++)
{
game[0][j] = j;
game[j][0] = j;
if(j%2 != 0)
{
for(int i=1; i < size; i=i+2)
{
game[i][j] = 254; // dot
game[i+1][j] =' ';
}
}
else if(j%2 == 0)
{
for(int i=1; i < size; i = i+2)
{
game[i][j] =' ';
game[i+1][j] =' ';
}
}
}
int totallines = 2*n*(n+1),noofmoves=0;
int movesplayed[totallines][7];//movesplayed[row][column][player made this move][row of closed box one side][col of closed box one side][row of closed box other side][col of closed box other side]
for(int i=0; i<totallines; i++)
{
for(int j=0; j < 7; j++)
movesplayed[i][j]=0;
}
one.score=0;
two.score=0;
one.moves= 0;
two.moves = 0;
player=1;
player = game_function(n, mode, size, game, totallines, movesplayed, noofmoves, player, &one.score, &two.score, one.moves, two.moves,one.lenname, one.name, two.lenname, two.name, loadedtime);
}
if(menu == 2)
{
//load saved game
int savenum;
printf("Enter the file number( 1 or 2 or 3):");
savenum= scanint();
if (savenum == 1 || savenum == 2 || savenum == 3)
{
system("cls");
char fname[5];
sprintf(fname, "%d.txt", savenum);
FILE *load = fopen(fname, "r");
if(load == NULL)
{
printf("file does not exist\n");
}
else //if the file exists
{
//if the file is empty
fseek (load, 0, SEEK_END);
int fsize = ftell(load);
if (fsize == 0)
{
fclose(load);
printf("file is empty\npress enter to go to the main menu\n");
char runkey;
scanf("%c", &runkey);
continue;
}
else
{
fclose(load);
load = fopen(fname, "r");
}
fread(&loadedtime, sizeof(int), 1, load);
fread(&n, sizeof(int), 1, load);
fread(&mode, sizeof(int), 1, load);
int size = 2 * n +2, totallines = 2*n*(n+1), noofmoves;
fread(&noofmoves, sizeof(int), 1, load);
fread(&one.moves, sizeof(int), 1, load);
fread(&two.moves, sizeof(int), 1, load);
fread(&one.score, sizeof(int), 1, load);
fread(&two.score, sizeof(int), 1, load);
fread(&player, sizeof(int), 1, load);
int movesplayed[totallines][7];
fread(movesplayed, sizeof(int), totallines * 7, load);
char game[size][size];
fread(game, sizeof(char), size * size, load);
fread(&one.lenname, sizeof(int), 1, load);
fread(one.name, sizeof(char), one.lenname, load);
one.name[one.lenname] = '\0';
//we do not save computer if mode = 1
if(mode == 2)
{
fread(&two.lenname, sizeof(int), 1, load);
fread(two.name, sizeof(char), two.lenname, load);
two.name[two.lenname] = '\0';
}
fclose(load);
if (mode == 1)
{
strcpy(two.name, "computer");
}
two.lenname = strlen(two.name);
player = game_function(n, mode, size, game, totallines, movesplayed, noofmoves, player, &one.score, &two.score, one.moves, two.moves,one.lenname, one.name, two.lenname, two.name, loadedtime);
}
}
else //if player chose wrong number
printf("is not existing file\n");
}
//to save the winer into users file after saved game of new game
if(menu == 1 || menu == 2)
{
if (player == 1)
{
int i = 0, m = 0;
while(usersarray[i].namelen != 0)
//show if the player played before
if(strcmp(one.name,usersarray[i].name) == 0)
{
m = 1;
break;
}
else
{
i++;
if(i == MAXSIZE)
{
//remove all data if it is full file
for(int j = 0; j < MAXSIZE; j++)
{
usersarray[j].namelen = 0;
}
i = 0;
}
}
if (m)
{
//if he played before choose the bigger score
if(usersarray[i].score < one.score)
{
usersarray[i].score = one.score;
}
}
else
{
usersarray[i].namelen = one.lenname;
strcpy(usersarray[i].name,one.name);
usersarray[i].score = one.score;
}
}
//if the winner is the computer so we will not save it
else if (player == 2 && mode == 2)
{
int i = 0, m = 0;
while(usersarray[i].namelen != 0)
{
if(strcmp(two.name,usersarray[i].name) == 0)
{
m = 1;
break;
}
else
{
i++;
}
}
if (m)
{
usersarray[i].score = two.score;
}
else
{
usersarray[i].namelen = two.lenname;
strcpy(usersarray[i].name,two.name);
usersarray[i].score = two.score;
}
}
//writing users array into file
FILE *userwrite = fopen("s.txt", "w");
counter = 0;
while(counter < MAXSIZE && usersarray[counter].namelen != 0)
{
fwrite(&usersarray[counter].namelen, sizeof(int), 1, userwrite);
fwrite(usersarray[counter].name,sizeof(char), usersarray[counter].namelen, userwrite);
fwrite(&usersarray[counter].score, sizeof(int), 1, userwrite);
counter++;
}
fclose(userwrite);
printf("\n\nenter 1 to return to main menu\nenter 2 to exit\n");//after match exit or return to main menu
while(1)
{
char returnkey;
scanf("%c",&returnkey);
if(returnkey=='1')
break;
else if(returnkey =='2')
return 0;
}
}
else if(menu == 3) //load top 10
{
sortusers(usersarray);
int i,j;
for(i=0; i<10; i++)
{
printf("%d.",i+1);
for(j=0; usersarray[i].name[j]!='\0'; j++)
printf("%c",usersarray[i].name[j]);
printf(" (%d)\n",usersarray[i].score);
}
printf("\npress enter to return to main menu\n");
char returnkey;
scanf("%c",&returnkey);
}
else if(menu == 4)//exit the game
return 0;
else
printf("Wrong choose\n\n");
}
return 0;
}