-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.java
executable file
·198 lines (182 loc) · 5.69 KB
/
Menu.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package battleships;
import java.io.*;
import java.util.*;
public class Menu implements MenuItem {
protected String title;
private Scanner scan = new Scanner(System.in);
private LinkedList<String> highscoreList = new LinkedList<String>();
private List<MenuItem> items;
public Menu() {
}
public Menu(String title) {
items = new ArrayList<>();
this.title = title;
}
public void add(MenuItem item) {
items.add(item);
}
public String getTitle() {
return title;
}
public void execute() throws IOException {
boolean inputCorrect = true;
int counter = -1;
MenuItem toExecute = null;
System.out.println(getTitle());
for (int i = 0; i < getTitle().length(); i++) {
String n = ("=");
System.out.print(n);
}
System.out.println();
for(MenuItem item : items) {
counter++;
if(item.getTitle().equals(this.getTitle())) {
toExecute = item;
}
System.out.println(counter + ": " + item.getTitle());
}
while (inputCorrect) {
try {
int a = scan.nextInt();
toExecute = items.get(a);
toExecute.execute();
inputCorrect = false;
}
catch (InputMismatchException | IndexOutOfBoundsException e) {
System.out.println("Incorrect input. Try again.");
scan.nextLine();
}
}
}
public void launchMenu() throws IOException {
highScoreList();
Menu mainMenu = new Menu("Main Menu");
Menu playGame = new Menu("Play");
Menu scoreBoard = new Menu("Scoreboard");
Menu returnMenu = new Menu("Return");
Game game = new Game();
System.out.println("Welcome to Battleships!");
System.out.println();
mainMenu.add(new AbstractMenuItem("Exit game") {
public void execute() {
Constants.printEmpty();
System.exit(1);
}
});
mainMenu.add(new AbstractMenuItem("Play") {
public void execute() throws IOException {
Constants.printEmpty();
playGame.execute();
}
});
mainMenu.add(new AbstractMenuItem("Scoreboard") {
public void execute() throws IOException {
Constants.printEmpty();
scoreBoard.execute();
}
});
playGame.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Constants.printEmpty();
mainMenu.execute();
}
});
playGame.add(new AbstractMenuItem("Player vs computer") {
public void execute() throws IOException {
Constants.printEmpty();
game.runGame(1);
mainMenu.execute();
}
});
playGame.add(new AbstractMenuItem("Player vs player") {
public void execute() throws IOException {
Constants.printEmpty();
game.runGame(2);
mainMenu.execute();
}
});
scoreBoard.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Constants.printEmpty();
mainMenu.execute();
}
});
scoreBoard.add(new AbstractMenuItem("Show highscore") {
public void execute() throws IOException {
Constants.printEmpty();
System.out.println("Top 10 players");
System.out.println("==============");
for(String s : highscoreList) {
System.out.println(s);
}
System.out.println();
returnMenu.execute();
}
});
returnMenu.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Constants.printEmpty();
mainMenu.execute();
}
});
mainMenu.execute();
}
public void highScoreList() throws IOException {
InputStream highscore = new FileInputStream ("/home/olibo733/eclipse-workspace/battleships/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();
}
public int checkHighscoreList(float hitrate) {
int scorePlace = 1;
for(String s : highscoreList) {
String[] splitLine = s.split(":");
int recordShots = Integer.parseInt(splitLine[2].trim());
if(hitrate <= recordShots) {
return scorePlace;
}
scorePlace++;
}
return 0;
}
public void saving(int hitrate, String playerName) throws IOException {
highScoreList();
LinkedList<String> placeholder = new LinkedList<String>();
int checkShots = checkHighscoreList(hitrate);
int i = checkShots + 1;
for(String s : highscoreList) {
if(checkShots + 48 > s.charAt(0)) {
placeholder.add(s);
continue;
}
if(checkShots + 48 == s.charAt(0)) {
placeholder.add(checkShots + ". Name: " + playerName + ", Hitrate: " + hitrate);
continue;
}
String[] splitLine = s.split(":");
placeholder.add(i + ". Name:" + splitLine[1] + ":" + splitLine[2]);
i++;
}
highscoreList.clear();
for(String s : placeholder) {
highscoreList.add(s);
}
for(String s : highscoreList) {
System.out.println(s);
}
OutputStream save = new FileOutputStream ("/home/olibo733/eclipse-workspace/battleships/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();
}
}