-
Notifications
You must be signed in to change notification settings - Fork 1
/
highscore.c
66 lines (57 loc) · 1.67 KB
/
highscore.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"
#include "highscore.h"
#define HIGHSCORES_PATH "highscores"
static void initHighscoreFile() {
Highscore hs[10];
int i;
for(i=0; i<10; ++i) {
strcpy(hs[i].nickname, "Computer");
hs[i].score = (i+1)*5+20;
}
highscore_sort(hs, 10);
highscore_store(hs, 10);
}
static int comparHighscore(const void* a, const void* b) {
return ((Highscore*)b)->score - ((Highscore*)a)->score;
}
extern int highscore_equals(Highscore *a, Highscore *b) {
return a->score==b->score && strcmp(a->nickname, b->nickname)==0;
}
extern void highscore_sort(Highscore* highscores, int length) {
qsort(highscores, length, sizeof(Highscore), comparHighscore);
}
extern int highscore_retrieve(Highscore* highscores, int max) {
int length = 0;
Highscore highscore;
FILE* file = fopen(HIGHSCORES_PATH, "r");
if(file==NULL) {
initHighscoreFile();
file = fopen(HIGHSCORES_PATH, "r");
}
if(file!=NULL) {
while(length<max && fscanf(file, "%d %" NICKNAME_LENGTH_STR "s", &(highscore.score), highscore.nickname)==2)
highscores[length++] = highscore;
fclose(file);
}
return length;
}
extern int highscore_store(Highscore* highscores, int length) {
int i;
FILE* file = fopen(HIGHSCORES_PATH, "w");
if(file!=NULL) {
for(i=0; i<length; ++i)
fprintf(file, "%d %s\n", highscores[i].score, highscores[i].nickname);
fclose(file);
}
else return 1;
return 0;
}
extern void highscore_print(Highscore* highscores, int length) {
int i;
printf("Place\tScore\tNickname\n");
for(i=0; i<length; ++i)
printf(" %d.\t %d\t %s\n", i+1, highscores[i].score, highscores[i].nickname);
}