-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.java
executable file
·129 lines (121 loc) · 3.09 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
package battleships;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Menu implements MenuItem {
protected String title;
private Scanner scan = new Scanner(System.in);
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 {
Highscore list = new Highscore();
list.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() {
Utils.printEmpty();
System.exit(1);
}
});
mainMenu.add(new AbstractMenuItem("Play") {
public void execute() throws IOException {
Utils.printEmpty();
playGame.execute();
}
});
mainMenu.add(new AbstractMenuItem("Scoreboard") {
public void execute() throws IOException {
Utils.printEmpty();
scoreBoard.execute();
}
});
playGame.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Utils.printEmpty();
mainMenu.execute();
}
});
playGame.add(new AbstractMenuItem("Player vs computer") {
public void execute() throws IOException {
Utils.printEmpty();
game.runGame(1);
mainMenu.execute();
}
});
playGame.add(new AbstractMenuItem("Player vs player") {
public void execute() throws IOException {
Utils.printEmpty();
game.runGame(2);
mainMenu.execute();
}
});
scoreBoard.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Utils.printEmpty();
mainMenu.execute();
}
});
scoreBoard.add(new AbstractMenuItem("Show highscore") {
public void execute() throws IOException {
Utils.printEmpty();
list.printHighscore();
returnMenu.execute();
}
});
returnMenu.add(new AbstractMenuItem("Main menu") {
public void execute() throws IOException {
Utils.printEmpty();
mainMenu.execute();
}
});
mainMenu.execute();
}
}