-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.java
159 lines (137 loc) · 3.29 KB
/
score.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
152
153
154
155
156
157
158
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.io.*;
import javax.swing.*;
public class score{
public score(int current_time){
JOptionPane.showMessageDialog(null,
"You have won the game. Total time: " + current_time,
"YOU WIN!", JOptionPane.PLAIN_MESSAGE);
boolean highScore = false;
List<Integer> list = new ArrayList<Integer>();
List<String> names = new ArrayList<String>();
File file = new File("topten.txt");
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(file));
String text = null;
int counter = 0;
while((text = reader.readLine()) != null && counter < 10){
list.add(Integer.parseInt(text));
counter++;
}
names.add(text);
while((text = reader.readLine()) != null){
names.add(text);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
if(reader != null){
reader.close();
}
}
catch(IOException e){
}
}
int index = 0;
for(int i = 0; i < list.size(); i++){
if(list.get(i) > current_time){
list.remove(i);
list.add(i, current_time);
highScore = true;
index = i;
break;
}
}
/*for(int i = 0; i < list.size(); i++){
System.out.println(names.get(i) + " " + list.get(i));
}*/
if(highScore){
String s = (String)JOptionPane.showInputDialog(null, "New High Score!\n"
+"Enter your name:\n", "High Score", JOptionPane.PLAIN_MESSAGE, null, null, "Bob");
if(s == null){
s = "null";
}
names.remove(index);
names.add(index, s);
try{
PrintWriter writer = new PrintWriter(file);
for(int i = 0; i < list.size(); i++){
writer.println(list.get(i));
}
for(int i = 0; i < names.size(); i++){
writer.println(names.get(i));
}
writer.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
public static void showTopTen(){
File file = new File("topten.txt");
BufferedReader reader = null;
List<String> nums = new ArrayList<String>();
List<String> names = new ArrayList<String>();
String temp = "";
try{
reader = new BufferedReader(new FileReader(file));
String text = null;
int counter = 0;
while((text = reader.readLine()) != null && counter < 10){
nums.add(text);
counter++;
}
names.add(text);
while((text = reader.readLine()) != null ){
names.add(text);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
if(reader != null){
reader.close();
}
}
catch(IOException e){
}
}
for(int i = 0; i < nums.size(); i++){
temp = temp + names.get(i) + " " + nums.get(i) + "\n";
}
JOptionPane.showMessageDialog(null,
"High Scores:\n" + temp,
"Top Ten", JOptionPane.PLAIN_MESSAGE);
}
public static void resetScores(){
File file = new File("topten.txt");
try{
PrintWriter writer = new PrintWriter(file);
for(int i = 0; i < 10; i++){
writer.println("999");
}
for(int i = 0; i < 10; i++){
writer.println("Bob");
}
writer.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}