-
Notifications
You must be signed in to change notification settings - Fork 0
/
Highscore.java
executable file
·105 lines (98 loc) · 3.2 KB
/
Highscore.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
package battleships;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
public class Highscore {
private LinkedList<String> highscoreList = new LinkedList<String>();
private LinkedList<String> temp = new LinkedList<String>();
// Loads highscorelist
public void highScoreList() throws IOException {
InputStream highscore = new FileInputStream(
"/home/axebo861/eclipse-workspace/Sänkaskepp/src/battleships/highscore.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(highscore));
while (reader.ready()) {
String line = reader.readLine();
if (!line.equals("")) {
highscoreList.add(line);
}
}
reader.close();
}
// checks and compares highscore
public int checkHighscoreList(int hitrate, int shots, int time) {
int place = 1;
for (String s : highscoreList) {
String[] splitLine = s.split(":");
String[] extractNumber = splitLine[2].split(",");
String[] extractShots = splitLine[3].split(",");
String[] extractTime = splitLine[4].split(",");
int hitrate2 = Integer.parseInt(extractNumber[0].trim());
int shots2 = Integer.parseInt(extractShots[0].trim());
int time2 = Integer.parseInt(extractTime[0].trim());
if (hitrate <= hitrate2) {
if(hitrate == hitrate2) {
if (shots < shots2) {
if (time > time2) {
return place;
}
place++;
}
place++;
}
return place;
}
place++;
}
return 0;
}
public void savingHighscore(int hitrate, int shots, String playerName, int playertime) throws IOException {
highScoreList();
int place = checkHighscoreList(hitrate, shots, playertime);
int i = place + 1;
for (String s : highscoreList) {
if (place + 48 > s.charAt(0)) {
temp.add(s);
continue;
}
String[] splitLine = s.split(":");
if (place + 48 == s.charAt(0)) {
temp.add(place + ". Name: " + playerName + ", Hitrate (Percent): " + hitrate + ", Shots fired: " + shots
+ ", Time(sec): " + playertime);
temp.add(i + ". Name:" + splitLine[1] + ":" + splitLine[2] + ":" + splitLine[3] + ":" + splitLine[4]);
continue;
}
i++;
if (i == 10)
break;
temp.add(i + ". Name:" + splitLine[1] + ":" + splitLine[2] + ":" + splitLine[3] + ":" + splitLine[4]);
}
highscoreList.clear();
highscoreList = temp;
printHighscore();
OutputStream save = new FileOutputStream(
"/home/axebo861/eclipse-workspace/Sänkaskepp/src/battleships/highscore.txt", false);
saveHighscore(save);
}
public void saveHighscore(OutputStream os) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os);
for (String s : highscoreList) {
outputStreamWriter.append(s + "\n");
}
outputStreamWriter.close();
}
public void printHighscore() {
System.out.println();
System.out.println("============================ Highscore ============================ ");
for (String s : highscoreList) {
System.out.println(s);
}
System.out.println("=================================================================== ");
System.out.println();
}
}