-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScores.pde
170 lines (146 loc) · 5.88 KB
/
HighScores.pde
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
163
164
165
166
167
168
169
170
/***********************************************************************************************************
* Class: HighScores
*
* Authors: Scott Nicol
*
* Function: Creates the high scores object
*
* Imports: javax.swing.*
*
* Methods: addScore() - Submits a high score be checked for admission to the high scores list
* loadScores() - Loads the saved high scores from the data/highscores.txt file
* saveScores() - Saves the updated high scores list to data/highscores.txt file
* displayScores() - Displays the high scores list to the screen
*
************************************************************************************************************/
import javax.swing.*;
class HighScores {
String[] names = new String[10];
int[] scores = new int[10];
String name;
/***********************************************************************************************************
* Method: Constructor
*
* Authors: Scott Nicol
*
* Function: Create a new high scores object
*
* Parameters: None
*
* Notes: Loads the saved high scores from data/highscores.txt upon creation
*
************************************************************************************************************/
public HighScores()
{
loadScores();
}
/***********************************************************************************************************
* Method: addScore()
*
* Authors: Scott Nicol
*
* Function: Create a new high scores object
*
* Parameters: int score
*
* Notes: Submits a score to be checked by an algorithm to determine if it should be added to the high
* scores list.
*
************************************************************************************************************/
public void addScore(int score)
{
for(int i = 0; i < scores.length; i++)
{
if(score > scores[i])
{
for(int j = scores.length - 1; j > i; j--)
{
scores[j] = scores[j-1];
names[j] = names[j-1];
}
while(name == null || name.length() < 1)
{
name = JOptionPane.showInputDialog(frame, "Please input your name:", "Congratulations! You made a high score!", JOptionPane.INFORMATION_MESSAGE);
}
scores[i] = score;
names[i] = name.trim();
break;
}
}
saveScores();
}
/***********************************************************************************************************
* Method: loadScores()
*
* Authors: Scott Nicol
*
* Function: Loads the saved high scores from the text file
*
* Parameters: None
*
* Notes: Loads the saved high scores from the data/highscores.txt file and then stores them in an array.
*
************************************************************************************************************/
private void loadScores()
{
String lines[] = loadStrings("data/highscores.txt");
for(int i = 0; i < scores.length; i++)
{
names[i] = lines[i * 2];
scores[i] = Integer.parseInt(lines[(i * 2) + 1]);
}
}
/***********************************************************************************************************
* Method: saveScores()
*
* Authors: Scott Nicol
*
* Function: Saves the updated high scores to text file
*
* Parameters: None
*
* Notes: Takes the updated high scores from an the array and then saves them data/highscores.txt.
*
************************************************************************************************************/
private void saveScores()
{
String[] lines = new String[20];
for(int i = 0; i < scores.length; i++)
{
lines[i * 2] = names[i];
lines[(i * 2) + 1] = str(scores[i]);
}
//Save the names and scores to file
saveStrings("data/highscores.txt", lines);
}
/***********************************************************************************************************
* Method: displayScores()
*
* Authors: Scott Nicol
*
* Function: Displays the high scores to the screen
*
* Parameters: None
*
* Notes: Displays the high scores to the screen with formatting.
*
************************************************************************************************************/
public void displayScores()
{
textSize(30);
text("HIGH SCORES", 260, 40);
text("--------------------------------------", 20, 70);
text("1", 20, 100); text(names[0], 250, 100); textAlign(RIGHT); text(scores[0], 680, 100); textAlign(LEFT);
text("2", 20, 140); text(names[1], 250, 140); textAlign(RIGHT); text(scores[1], 680, 140); textAlign(LEFT);
text("3", 20, 180); text(names[2], 250, 180); textAlign(RIGHT); text(scores[2], 680, 180); textAlign(LEFT);
text("4", 20, 220); text(names[3], 250, 220); textAlign(RIGHT); text(scores[3], 680, 220); textAlign(LEFT);
text("5", 20, 260); text(names[4], 250, 260); textAlign(RIGHT); text(scores[4], 680, 260); textAlign(LEFT);
text("6", 20, 300); text(names[5], 250, 300); textAlign(RIGHT); text(scores[5], 680, 300); textAlign(LEFT);
text("7", 20, 340); text(names[6], 250, 340); textAlign(RIGHT); text(scores[6], 680, 340); textAlign(LEFT);
text("8", 20, 380); text(names[7], 250, 380); textAlign(RIGHT); text(scores[7], 680, 380); textAlign(LEFT);
text("9", 20, 420); text(names[8], 250, 420); textAlign(RIGHT); text(scores[8], 680, 420); textAlign(LEFT);
text("10", 20, 460); text(names[9], 250, 460); textAlign(RIGHT); text(scores[9], 680, 460); textAlign(LEFT);
text("--------------------------------------", 20, 490);
text("N - New Game", 350, 650);
}
}