-
Notifications
You must be signed in to change notification settings - Fork 1
/
endgame.c
226 lines (208 loc) · 6.6 KB
/
endgame.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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "game.h"
#include "types.h"
int check(int array[100], int number) // check whether the repeated name has been added to the file or not
{
int result = 0;
for (int i = 0; i < 100; i++)
{
if (array[i] == number)
{
result = 1;
break;
}
}
return result;
}
void topPlayers(FILE *winners_file, unsigned long long highscores)
{
winners_file = fopen("winners.txt", "r");
char buffer[100];
char data[100][100];
char checkData[100][100];
char tempData[100][1000];
int arrtemp[100];
int score[100], i = 0, scoreIndex = 0, j, temp, line, k = 0;
if (winners_file != NULL)
{
while (fgets(buffer, 100, winners_file) != NULL)
{
for (int j = 0; j < strlen(buffer); j++)
{
if (!(buffer[j] == ':'))
{
data[i][j] = buffer[j];
}
else
{
for (j; j < strlen(buffer); j++)
{
if (isdigit(buffer[j]))
{
tempData[i][scoreIndex] = buffer[j];
score[i] = atoi(tempData[i]); // get the integer value from the new array
scoreIndex++;
}
}
}
}
if (score[i] == 0)
{
printf("File is Corrupted ERROR in Line %d\n", i + 1);
scoreIndex = 0;
continue;
}
scoreIndex = 0;
i++;
}
line = i;
for (i = 0; i < line; i++)
{
for (j = i + 1; j < line; j++)
{
if (score[i] < score[j])
{
strcpy(tempData[i], data[j]);
strcpy(data[j], data[i]);
strcpy(data[i], tempData[i]);
temp = score[j];
score[j] = score[i];
score[i] = temp;
}
}
for (j = 0; j < (strlen(data[i])); j++)
{
checkData[i][j] = tolower(data[i][j]); // converting all sorted data to lowercase for later comparison
}
}
temp = 0;
int firstAp = 0, n = 0;
FILE *SortedTopper;
SortedTopper = fopen("sortedToppers.txt", "w");
SortedTopper = fopen("sortedToppers.txt", "a");
for (i = 0; i < line; i++) // get the repeated names
{
for (j = i + 1; j < line; j++)
{
if (!strcmp(checkData[i], checkData[j]))
{
temp = 1;
if (check(arrtemp, j))
{
break;
}
arrtemp[k] = j;
k++;
if (firstAp == 1)
continue;
fprintf(SortedTopper, "%d. %s: %d\n", n + 1, data[i], score[i]);
n++;
firstAp = 1;
}
}
if (temp == 0 && !check(arrtemp, i))
{
fprintf(SortedTopper, "%d. %s: %d\n", n + 1, data[i], score[i]);
n++;
}
temp = 0;
firstAp = 0;
}
fclose(SortedTopper);
fclose(SortedTopper);
FILE * ptr1 = fopen("sortedToppers.txt", "r");
char line[102];
int counter_to_break = 0;
while (fgets(line, 100, ptr1) != NULL)
{
counter_to_break++;
if (line[strlen(line) - 1] == '\n')
{
line[strlen(line) - 1] = '\0';
}
printf("%s", line);
printf("\n");
if (counter_to_break == highscores)
{
break;
}
}
fclose(ptr1);
}
}
void declareWinner(player p1, player p2, player computer, int mode, unsigned long long highscores, int played)
{
FILE *winners_file;
winners_file = fopen("winners.txt", "a");
if (played == 0)
{
topPlayers(winners_file, highscores);
return;
}
if (mode == 1)
{
if (p1.score > p2.score)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Player one is the winner!\nPlease, Enter Your Name: ");
fgets(p1.name, 256, stdin);
reset_color();
if (p1.name[strlen(p1.name) - 1] == '\n')
{
p1.name[strlen(p1.name) - 1] = '\0';
}
fprintf(winners_file, "%s: %d\n", p1.name, p1.score);
fprintf(winners_file, "%s: %d\n", p1.name, p1.score);
}
else if (p2.score > p1.score)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p2.color);
printf("Player two is the winner!\nPlease, Enter Your Name: ");
fgets(p2.name, 256, stdin);
reset_color();
if (p2.name[strlen(p2.name) - 1] == '\n')
{
p2.name[strlen(p2.name) - 1] = '\0';
}
fprintf(winners_file, "%s: %d\n", p2.name, p2.score);
fprintf(winners_file, "%s: %d\n", p2.name, p2.score);
}
else
{
yellow_color();
printf("\t\t\t\t\tDraw !\n");
reset_color();
}
}
else
{
if (p1.score > computer.score)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Player one is the winner!\nPlease, Enter Your Name: ");
fgets(p1.name, 256, stdin);
if (p1.name[strlen(p1.name) - 1] == '\n')
{
p1.name[strlen(p1.name) - 1] = '\0';
}
fprintf(winners_file, "%s: %d\n", p1.name, p1.score);
reset_color();
}
else if (p1.score == computer.score)
{
yellow_color();
printf("\t\t\t\t\tDraw !\n");
reset_color();
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("Computer is the winner!\n");
reset_color();
}
}
fclose(winners_file);
topPlayers(winners_file, highscores);
}