-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHighScore.c
More file actions
110 lines (87 loc) · 1.72 KB
/
HighScore.c
File metadata and controls
110 lines (87 loc) · 1.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include "HighScore.h"
HighScore scores[NUMBER_OF_HIGH_SCORE_SLOTS]; // index 2 is the lowest score on the list, 3 is the highest
void getHighScoreList(HighScore* scoreList)
{
int i;
for (i = 0; i < NUMBER_OF_HIGH_SCORE_SLOTS; i++)
{
scoreList[i] = scores[i];
}
}
int isHighScore(int score)
{
if (scores[NUMBER_OF_HIGH_SCORE_SLOTS - 1].score > score)
{
return 0;
}
else
{
return 1;
}
}
void pushHighScore(HighScore h)
{
int i,j;
if (!isHighScore(h.score))
{
return;
}
for (i = 0; i < NUMBER_OF_HIGH_SCORE_SLOTS; i++)
{
if (h.score > scores[i].score)
{
for (j = NUMBER_OF_HIGH_SCORE_SLOTS - 1; j > i; j--)
{
scores[j] = scores[j - 1];
}
scores[i] = h;
break;
}
}
}
int loadHighScores()
{
FILE* scoresFile;
scoresFile = fopen("pp.hisc", "rb");
if (scoresFile != NULL)
{
fread((void*)scores, sizeof(HighScore), 3, scoresFile);
fclose(scoresFile);
return 1;
}
else
{
scores[0].playerCount = 1;
scores[0].name[0] = '-'; scores[0].name[1] = '-'; scores[0].name[2] = '-';
scores[0].score = 0;
scores[1].playerCount = 2;
scores[1].name[0] = '-'; scores[1].name[1] = '-'; scores[1].name[2] = '-';
scores[1].score = 0;
scores[2].playerCount = 1;
scores[2].name[0] = '-'; scores[2].name[1] = '-'; scores[2].name[2] = '-';
scores[2].score = 0;
return 0;
}
}
int saveHighScores()
{
int i;
FILE* scoresFile;
scoresFile = fopen("pp.hisc", "wb");
if (scoresFile != NULL)
{
for (i = 0; i < NUMBER_OF_HIGH_SCORE_SLOTS; i++)
{
fwrite(&(scores[i]), sizeof(HighScore), 1, scoresFile);
}
fclose(scoresFile);
return 1;
}
else
{
fprintf(stderr, "Unable to write scores file!");
return 0;
}
}