-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighscores.c
More file actions
92 lines (85 loc) · 3.01 KB
/
highscores.c
File metadata and controls
92 lines (85 loc) · 3.01 KB
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
#include "highscores.h"
struct play transformToPlay (char name[21], int timeSpent, int moves, int completeLevels) {
struct play temporary;
strcpy(temporary.name, name);
temporary.score = (pow(10, 3) * pow(completeLevels, 3) / (moves + (timeSpent / 2)));
return temporary;
}
void appendPlay (struct play temporary) {
FILE *binaryFile = fopen("scores.bin", "ab");
if (binaryFile == NULL) {
mvprintw(23, 3, "Unable to save.");
} else {
fwrite(&temporary, sizeof(struct play), 1, binaryFile);
fclose(binaryFile);
}
}
int searchPlayer(struct play *player) {
play score;
int found = -1;
FILE *binaryFile = fopen("scores.bin", "rb");
if (binaryFile == NULL) {
mvprintw(23, 3, "Unable to open scores.bin");
} else {
do {
fread(&score, sizeof(struct play), 1, binaryFile);
} while (strcmp(player->name, score.name) != 0 && !feof(binaryFile));
if (strcmp(player->name, score.name) == 0) {
player->score = score.score;
found = 0;
}
fclose(binaryFile);
}
return found;
}
void bubbleSort(struct play* array, int i) {
struct play x;
for (int j = 0; j < i; j++) {
for (int k = 0; k < (i - 1); k++) {
if (array[k].score < array[k + 1].score) {
x = array[k];
array[k] = array[k + 1];
array[k + 1] = x;
}
}
}
}
void showHighscores() {
char bufferString[80];
int numberOfScores;
play *scores;
FILE *binFile = fopen("scores.bin", "rb");
WINDOW *highscore = newwin(20, 76, 2, 2);
box(highscore, 0, 0);
if (binFile == NULL) {
printw("Unable to load scores.bin.");
}
fseek(binFile, 0L, SEEK_END);
numberOfScores = ftell(binFile) / sizeof(struct play);
if (numberOfScores == 0) {
printw("There aren't any highscores stored in scores.bin");
} else {
rewind(binFile);
scores = malloc(sizeof(play) * numberOfScores);
fread(scores, sizeof(struct play), numberOfScores, binFile);
bubbleSort(scores, numberOfScores);
mvwprintw(highscore, 2, 33, "Highscore");
for (int y = 0; y < 10; y++) {
snprintf(bufferString, 80, "Name: %s Score: %f", scores[y].name, scores[y].score); // Prepare the string to be printed
mvwprintw(highscore, 4 + y, (76 - strlen(bufferString)) / 2, "%s", bufferString); // Centralizes string before printing
}
free(scores);
}
fclose(binFile);
wrefresh(highscore);
delwin(highscore);
}
void addUnfinishedPlay(gameState *save) {
if (save->level == 3) {
save->score = transformToPlay (save->score.name, save->timeSpent[0] + save->timeSpent[1], save->movement[0] + save->movement[1], save->level - 1);
} else if (save->level == 2) {
save->score = transformToPlay (save->score.name, save->timeSpent[0], save->movement[0], save->level - 1);
}
if (save->level == 3 || save->level == 2)
appendPlay(save->score);
}