-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.c
91 lines (82 loc) · 2.73 KB
/
score.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
#include "score.h"
void check_for_highscore(int level, int frames)
// write to "highscore.txt" if $frames < highscore for a given $level
{
FILE* h_file;
h_file = fopen("highscore.txt", "r"); // open hi-score file
if (h_file == NULL) // hi-score file doesnt exist
{
// write the basic highscore file
h_file = fopen("highscore.txt", "w");
must_init(h_file, "highscore file");
for(int i = 1; i <= LEVELS_N; i++)
{
if (i == level)
fprintf(h_file, "CPU%d: %d\n", level, frames/60);
else
fprintf(h_file, "CPU%d: -1\n", i);
}
fclose(h_file);
}
else // hi-score file exists
{
fseek (h_file, 0, SEEK_END);
int file_length = ftell (h_file); // determine its length so we can store it in a buffer
fseek (h_file, 0, SEEK_SET);
char* buffer = malloc ((file_length + EXTRA_MEMORY) * sizeof(char));
must_init(buffer, "file buffer");
strcpy(buffer, "");
char aux_str[STR_MAX];
// now, let's write the other lines exactly as it is in the file,
// but not the line $level if we need to rewrite it
int j, level_hiscore;
for (int i = 1; i <= LEVELS_N; i++) // for each line
{
fscanf(h_file, "CPU%d: %d\n", &j, &level_hiscore);
if (i != j)
{
perror("Hi-score file corrupted!");
abort();
}
if ((i == level) && (level_hiscore == -1 || frames/60 < level_hiscore))
// its the line we want to change AND we got a better score to replace
{
sprintf(aux_str, "CPU%d: %d\n", level, frames/60);
}
else // keep it the same
sprintf(aux_str, "CPU%d: %d\n", i, level_hiscore);
strcat(buffer, aux_str); // store it in the buffer
}
fclose (h_file); // stop reading
// replace it:
h_file = fopen("highscore.txt", "w");
fputs(buffer, h_file);
fclose(h_file);
}
}
void get_highscores(int* array)
// get the highscores from "highscores.txt" and store them in a given array
{
FILE* h_file;
h_file = fopen("highscore.txt", "r"); // open hi-score file
if (h_file == NULL) // hi-score file doesnt exist
{
for(int i = 0; i < LEVELS_N; i++)
{
array[i] = -1; // no values for any level!
}
}
else
{
int j;
for (int i = 1; i <= LEVELS_N; i++) // for each line
{
fscanf(h_file, "CPU%d: %d\n", &j, &array[i-1]);
if (i != j)
{
perror("Hi-score file corrupted!");
abort();
}
}
}
}