-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
210 lines (188 loc) · 8.43 KB
/
Main.java
File metadata and controls
210 lines (188 loc) · 8.43 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static final int DEFAULT_BOARD_SIZE = 10;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
printTitle();
String player1 = readNonEmpty(scanner, "Spieler 1 Name: ");
String player2 = readNonEmpty(scanner, "Spieler 2 Name: ");
List<ShipType> fleet = defaultFleet();
GAME game = new GAME(player1, player2, DEFAULT_BOARD_SIZE, fleet);
setupPlayer(scanner, game, 0);
setupPlayer(scanner, game, 1);
runGameLoop(scanner, game);
} finally {
scanner.close();
}
}
private static void printTitle() {
System.out.println("==========================================");
System.out.println(" SCHIFFE VERSENKEN (KONSOLE) ");
System.out.println("==========================================");
System.out.println("Eingabeformat Koordinate: A1 oder 1,1");
System.out.println("Treffer: X | Fehlschuss: o | Schiff: S");
System.out.println();
}
private static List<ShipType> defaultFleet() {
List<ShipType> fleet = new ArrayList<ShipType>();
fleet.add(new ShipType("Schlachtschiff", 5, 1));
fleet.add(new ShipType("Zerstörer", 4, 1));
fleet.add(new ShipType("U-Boot", 3, 2));
fleet.add(new ShipType("Patrouillenboot", 2, 1));
return fleet;
}
private static void setupPlayer(Scanner scanner, GAME game, int playerIndex) {
GRID grid = game.getPlayerGrid(playerIndex);
System.out.println("------------------------------------------");
System.out.println("Setup für " + grid.getUsername());
System.out.println("------------------------------------------");
System.out.println("Modus wählen: [1] Manuell [2] Zufällig");
int mode = readIntInRange(scanner, "Auswahl (1/2): ", 1, 2);
if (mode == 2) {
long seed = System.nanoTime() + playerIndex * 97L;
grid.placeShipsRandomly(game.getFleetDefinition(), seed);
System.out.println("Schiffe zufällig platziert.");
System.out.println(grid.renderOwnBoard());
waitForEnter(scanner, "Weiter mit Enter...");
clearScreenHint();
return;
}
for (ShipType type : game.getFleetDefinition()) {
for (int i = 1; i <= type.getCount(); i++) {
while (true) {
try {
System.out.println(grid.renderOwnBoard());
System.out.println("Platziere " + type.getName() + " #" + i + " (Länge " + type.getLength() + ")");
Coordinate start = readCoordinate(scanner, grid.getSize(), "Start (z.B. A1): ");
Orientation orientation = readOrientation(scanner, "Richtung [H/V]: ");
grid.placeShip(type.getName() + " #" + i, start, orientation, type.getLength());
break;
} catch (IllegalArgumentException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
}
}
System.out.println("Setup abgeschlossen für " + grid.getUsername());
System.out.println(grid.renderOwnBoard());
waitForEnter(scanner, "Weiter mit Enter...");
clearScreenHint();
}
private static void runGameLoop(Scanner scanner, GAME game) {
while (!game.isGameOver()) {
GRID ownGrid = game.getActivePlayerGrid();
GRID enemyGrid = game.getOpponentGrid();
System.out.println("------------------------------------------");
System.out.println("Am Zug: " + ownGrid.getUsername());
System.out.println("------------------------------------------");
System.out.println("Dein Feld:");
System.out.println(ownGrid.renderOwnBoard());
System.out.println("Gegnerisches Feld:");
System.out.println(enemyGrid.renderOpponentView());
ShotReport report = null;
while (report == null) {
try {
Coordinate target = readCoordinate(scanner, enemyGrid.getSize(), "Schussziel: ");
report = game.playTurn(target);
announceShot(report);
if (report.getResult() == ShotResult.ALREADY_TARGETED) {
report = null;
}
} catch (IllegalArgumentException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
if (!game.isGameOver()) {
if (report.grantsExtraTurn()) {
waitForEnter(scanner, "Treffer - gleicher Spieler schiesst weiter. Enter...");
clearScreenHint();
} else {
waitForEnter(scanner, "Zug beendet. Enter...");
clearScreenHint();
waitForEnter(scanner, "Enter für " + game.getOpponentName() + "...");
clearScreenHint();
game.switchTurn();
}
}
}
System.out.println("==========================================");
System.out.println("Spielende! Gewinner: " + game.getWinnerName());
System.out.println("==========================================");
}
private static void announceShot(ShotReport report) {
ShotResult result = report.getResult();
if (result == ShotResult.MISS) {
System.out.println(report.getCoordinate().toHumanReadable() + ": Daneben.");
return;
}
if (result == ShotResult.HIT) {
System.out.println(report.getCoordinate().toHumanReadable() + ": Treffer! Du bist nochmal dran.");
return;
}
if (result == ShotResult.SUNK) {
System.out.println(report.getCoordinate().toHumanReadable() + ": Schiff versenkt! (" + report.getSunkShipName() + ")");
if (report.isGameWon()) {
System.out.println("Alle gegnerischen Schiffe wurden versenkt.");
} else {
System.out.println("Du bist nochmal dran.");
}
return;
}
System.out.println(report.getCoordinate().toHumanReadable() + ": Feld wurde bereits beschossen.");
}
private static Orientation readOrientation(Scanner scanner, String prompt) {
while (true) {
try {
return Orientation.parse(readNonEmpty(scanner, prompt));
} catch (IllegalArgumentException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
}
private static Coordinate readCoordinate(Scanner scanner, int boardSize, String prompt) {
while (true) {
try {
return Coordinate.parse(readNonEmpty(scanner, prompt), boardSize);
} catch (IllegalArgumentException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
}
private static int readIntInRange(Scanner scanner, String prompt, int min, int max) {
while (true) {
String input = readNonEmpty(scanner, prompt);
try {
int value = Integer.parseInt(input);
if (value < min || value > max) {
throw new IllegalArgumentException("Wert ausserhalb des erlaubten Bereichs.");
}
return value;
} catch (NumberFormatException ex) {
System.out.println("Bitte eine Zahl eingeben.");
} catch (IllegalArgumentException ex) {
System.out.println("Fehler: " + ex.getMessage());
}
}
}
private static String readNonEmpty(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
String value = scanner.nextLine();
if (value != null && !value.trim().isEmpty()) {
return value.trim();
}
System.out.println("Eingabe darf nicht leer sein.");
}
}
private static void waitForEnter(Scanner scanner, String message) {
System.out.print(message);
scanner.nextLine();
}
private static void clearScreenHint() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}