-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighScore.cpp
More file actions
166 lines (140 loc) · 5.81 KB
/
highScore.cpp
File metadata and controls
166 lines (140 loc) · 5.81 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "declarations.h"
#include "prototype.h"
extern Application app;
/// This function compares the existing high score with the players score to see if it is the high score.
/// It stores the high score in a text file
/// Need to pass the users score
/// Int return type
int checkHighScore(int playerScore) {
//Declare file variable and variables
int highScore[NUMBER_OF_HIGHSCORES] = {0};
bool userIsHighScore = false;
int returnCode = 0;
int timerCount = 0;
static bool userScoreAddedToScoreLog = false;
FILE *fptr;
// Get timer count
timerCount = al_get_timer_count(app.displayTimer);
// Based on the timer count display the event
switch (timerCount) {
case 0:
// Open the high score file and copy the high scores
fptr = fopen("highScore.txt", "r");
returnCode = checkIfFileLoaded(fptr);
if (returnCode != 0) {
return returnCode;
}
for (int i = 0; i < NUMBER_OF_HIGHSCORES; i++) {
fscanf(fptr, "%d", &highScore[i]);
}
fclose(fptr);
// Print out the current high scores
al_clear_to_color(SLATEGREY);
al_draw_textf(app.arial, RED, 620, 0, ALLEGRO_ALIGN_RIGHT, "Score: %d", playerScore);
al_draw_text(app.arial, BLACK, SCREEN_WIDTH / 2, 100, ALLEGRO_ALIGN_CENTER, "The current high score is:");
printHighScore(highScore);
al_flip_display();
break;
case 1:
for (int i = 0; i < NUMBER_OF_HIGHSCORES; i++) {
if (playerScore > highScore[i]) {
// This formula took forever for me to create, I had to create models and so on
// Basically it moves every high score down one slot starting where the new high score is taking place.
// For example if the new high score is slot number 4 then high score 5 will the previous high score 4.
// I really did not want to hard code it because I wanted to make this program as flexible and changeable as possible
for (int j = (NUMBER_OF_HIGHSCORES - 1); j > i; j--) {
highScore[j] = highScore[j-1];
}
highScore[i] = playerScore;
// Print out the new high scores if the user obtained a high score
al_clear_to_color(SLATEGREY);
al_draw_textf(app.arial, RED, SCREEN_WIDTH / 2, 25, ALLEGRO_ALIGN_CENTER, "Your score is the new high score in slot %d", i + 1);
al_draw_text(app.arial, BLACK, SCREEN_WIDTH / 2, 100, ALLEGRO_ALIGN_CENTER, "The new current high score is:");
printHighScore(highScore);
// Print out the new high scores into the text file
fptr = fopen("highScore.txt", "w");
returnCode = checkIfFileLoaded(fptr);
if (returnCode != 0){
return returnCode;
}
for (int i = 0; i < NUMBER_OF_HIGHSCORES; i++) {
fprintf(fptr, "%d\n", highScore[i]);
}
fclose(fptr);
userIsHighScore = true;
al_flip_display();
}
}
// User is not a high score
if (!userIsHighScore) {
al_clear_to_color(SLATEGREY);
al_draw_text(app.arial, BLACK, SCREEN_WIDTH /2, 100, ALLEGRO_ALIGN_CENTER, "Your score is not a high score");
al_flip_display();
}
break;
case 2:
al_clear_to_color(SLATEGREY);
// Find the average high score
returnCode = averageHighscore(playerScore, userScoreAddedToScoreLog);
if (returnCode != 0) {
return returnCode;
}
al_flip_display();
break;
case 3:
al_stop_timer(app.displayTimer);
al_set_timer_count(app.displayTimer, 0);
userScoreAddedToScoreLog = false;
break;
}
return 0;
}
///`This function prints out the high scores
/// Need to pass the high score
/// No return type
void printHighScore(int highScore[]) {
for (int i = 0; i < NUMBER_OF_HIGHSCORES; i++) {
al_draw_textf(app.arial, BLACK, SCREEN_WIDTH / 2, 140+i*30, ALLEGRO_ALIGN_CENTER, "%d", highScore[i]);
}
}
/// This calculates the average scores in the score log plus the users current score
/// Need to pass the players score
/// Int return type
int averageHighscore(int playerScore, bool &userScoreAddedToScoreLog) {
//Declare variables and file
int scoreFromLog = 0;
int counter = 0;
float averageScore = 0;
int sum = 0;
int returnCode = 0;
FILE *fptr;
// Open and copy the scores from the file
fptr = fopen("scoreLog.txt", "r");
returnCode = checkIfFileLoaded(fptr);
if (returnCode != 0){
return returnCode;
}
while (fscanf(fptr, "%d", &scoreFromLog) != EOF) {
counter ++;
sum += scoreFromLog;
}
fclose(fptr);
if (!userScoreAddedToScoreLog){
// Open and append the users score to the score log
fptr = fopen("scoreLog.txt", "a");
returnCode = checkIfFileLoaded(fptr);
if (returnCode != 0) {
return returnCode;
}
fprintf(fptr, "\n%d", playerScore);
fclose(fptr);
userScoreAddedToScoreLog = true;
}
// Add the users current score to the sum and calculate average
sum += playerScore;
counter ++;
averageScore = (float)sum / (float)counter;
// Print average score
al_draw_textf(app.arial, BLACK, SCREEN_WIDTH / 2, 100, ALLEGRO_ALIGN_CENTER, "The average overall score is %.2f", averageScore);
return 0;
}