-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.c
69 lines (65 loc) · 1.83 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
#include <stdint.h>
#include "game.h"
// List of the highest scores, sorted high->low
Score scores[MAX_SCORES];
// Constructor for struct Score.
Score init_Score(char *name, int score){
int i;
Score s;
for(i = 0; i < 3; i++)
s.name[i] = name[i];
s.name[3] = 0;
s.score = score;
return s;
}
// Internal method.
// Inserts Score s at the right position.
// Then recursively moves all scores below down one step.
void _add_Score(Score s, int i_from){
int i;
Score tmp;
for(i = i_from; i < MAX_SCORES; i++){
tmp = scores[i]; // tmp = the value to be replaced & moved
if(s.score < tmp.score) continue;
scores[i] = s;
if(tmp.name[0]) // if tmp is a non-null entry, move it further down
_add_Score(tmp, i+1);
break;
}
}
// Inserts Score s into the leaderboard.
void add_Score(Score s){_add_Score(s,0);}
// Returns amount of non-null scoreboard entries
int get_scores_len(){
int i, c = 0;
for(i = 0; i < MAX_SCORES; i++)
if(scores[i].name[0])
c++;
return c;
}
// Returns string with all scoreboard entries separated by newlines.
char *get_scores_page(){
static char str[MAX_SCORES * 14];
char * cp;
int i, ro = 0;
uint8_t r, p;
Score s;
for(i = 0; i < sizeof(str)-1; i++)
str[i] = ' ';
for(r = 0; r < MAX_SCORES; r++){
s = scores[r];
if(s.score == 0 && s.name[0] == 0)break;
// Row format = "pp. NNN sssss\n"
// insert scoreboard position
p = r + 1;
if(p < 10) str[ro+1] = p + '0';
else insert(itoaconv(p), str, ro, 0);
str[ro+2] = '.';
insert(s.name, str, ro+4, 0); // insert name
insert(itoaconv(s.score), str, ro+8, 0); // insert score
str[ro+13] = '\n';
ro += 14;
}
str[ro] = 0;
return str;
}