-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuScores.java
151 lines (126 loc) · 4.39 KB
/
SudokuScores.java
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
// Sudoku Project
// November 2017
// Andy Vainauskas
//
// SudokuScores manages the high scores for the various puzzle
// difficulty levels. This class is saved to a file using serialization
// so that high scores can be maintained.
//
import java.io.Serializable;
import java.text.DecimalFormat;
public class SudokuScores implements Serializable {
private DecimalFormat dFormat = new DecimalFormat("00");
private Time basicRecord;
private Time easyRecord;
private Time mediumRecord;
private Time hardRecord;
private Time expertRecord;
SudokuScores() {
basicRecord = null;
easyRecord = null;
mediumRecord = null;
hardRecord = null;
expertRecord = null;
}
// Class to abstract the record times
class Time implements Serializable {
int seconds, minutes, hours;
public Time(int seconds, int minutes, int hours) {
super();
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;
}
@Override
public String toString() {
return (dFormat.format(hours)
+ ":" + dFormat.format(minutes)
+ ":" + dFormat.format(seconds));
}
}
// Returns a string that contains all of the high scores
public String getScores() {
StringBuilder result = new StringBuilder("");
result.append("Best Times: \n");
result.append("------------ \n");
if (basicRecord == null) {
result.append("Basic: No record \n");
} else {
result.append("Basic: " + basicRecord + "\n");
}
if (easyRecord == null) {
result.append("Easy: No record \n");
} else {
result.append("Easy: " + easyRecord + "\n");
}
if (mediumRecord == null) {
result.append("Medium: No record \n");
} else {
result.append("Medium: " + mediumRecord + "\n");
}
if (hardRecord == null) {
result.append("Hard: No record \n");
} else {
result.append("Hard: " + hardRecord + "\n");
}
if (expertRecord == null) {
result.append("Expert: No record \n");
} else {
result.append("Expert: " + expertRecord + "\n");
}
return result.toString();
}
// Updates the high score time if it is faster than the record based on the difficulty
public void updateHighScore(String difficulty, SudokuTimer timer) {
if (difficulty.equals("basic")) {
if (isFaster(basicRecord, timer)) {
Time newRecord = new Time(timer.getSeconds(), timer.getMinutes(), timer.getHours());
basicRecord = newRecord;
}
}
if (difficulty.equals("easy")) {
if (isFaster(easyRecord, timer)) {
Time newRecord = new Time(timer.getSeconds(), timer.getMinutes(), timer.getHours());
basicRecord = newRecord;
}
}
if (difficulty.equals("medium")) {
if (isFaster(mediumRecord, timer)) {
Time newRecord = new Time(timer.getSeconds(), timer.getMinutes(), timer.getHours());
basicRecord = newRecord;
}
}
if (difficulty.equals("hard")) {
if (isFaster(hardRecord, timer)) {
Time newRecord = new Time(timer.getSeconds(), timer.getMinutes(), timer.getHours());
basicRecord = newRecord;
}
}
if (difficulty.equals("expert")) {
if (isFaster(expertRecord, timer)) {
Time newRecord = new Time(timer.getSeconds(), timer.getMinutes(), timer.getHours());
basicRecord = newRecord;
}
}
}
// Checks to see if a Time is faster than the current record
public boolean isFaster(Time record, SudokuTimer timer) {
if (record == null)
return true;
if (timer.getHours() < record.hours)
return true;
if (timer.getMinutes() < record.minutes)
return true;
if (timer.getSeconds() < record.seconds)
return true;
return false;
}
// Resets all of the high scores
public void reset() {
basicRecord = null;
easyRecord = null;
mediumRecord = null;
hardRecord = null;
expertRecord = null;
}
}