-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
331 lines (299 loc) · 13.1 KB
/
Main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static boolean allShipsSunk = false;
public static void main(String[] args) {
// Let's setup the boards
Player player1 = new Player("Player 1");
Player player2 = new Player("Player 2");
Player[] playerList = { player1, player2 };
// Set up turns
Scanner nextTurn = new Scanner(System.in);
// Place pieces
for (int i = 0; i < 2; i++) {
System.out.println(playerList[i].name + ", place your ships on the game field");
for (Ship shipName : playerList[i].shipList) {
playerList[i].printBoard("visible");
placeShip(playerList[i].visibleBoard, shipName);
}
playerList[i].printBoard("visible");
System.out.println("Press Enter and pass the move to another player");
nextTurn.nextLine();
}
// Play the game
for (int changeTurns = 0; !allShipsSunk; changeTurns++) {
Player currentPlayer = changeTurns % 2 == 0 ? player1 : player2;
Player otherPlayer = changeTurns % 2 == 0 ? player2 : player1;
otherPlayer.printBoard("hidden");
System.out.println("---------------------");
currentPlayer.printBoard("visible");
System.out.printf("%s, it's your turn:%n", currentPlayer.name);
takeAShot(otherPlayer);
if (!allShipsSunk) {
System.out.println("Press Enter and pass the move to another player");
nextTurn.nextLine();
}
}
nextTurn.close();
}
static class Ship {
private final String name;
int cells;
ArrayList<String> coordinates = new ArrayList<String>();
boolean isSunk = false;
public Ship(String name, int cells) {
this.name = name;
this.cells = cells;
}
public String getName() {
return name;
}
public int getLength() {
return cells;
}
}
static class Player {
private final String name;
Board visibleBoard;
Board hiddenBoard;
String whichBoard;
Ship aircraftCarrier;
Ship battleship;
Ship submarine;
Ship cruiser;
Ship destroyer;
Ship[] shipList;
public Player(String name) {
this.name = name;
this.visibleBoard = new Board();
this.hiddenBoard = new Board();
this.aircraftCarrier = new Ship("Aircraft Carrier", 5);
this.battleship = new Ship("Battleship", 4);
this.submarine = new Ship("Submarine", 3);
this.cruiser = new Ship("Cruiser", 3);
this.destroyer = new Ship("Destroyer", 2);
this.shipList = new Ship[] { aircraftCarrier, battleship, submarine, cruiser, destroyer };
}
public void printBoard(String whichBoard) {
for (int row = 0; row < 11; row++) {
for (int col = 0; col < 11; col++) {
if ("visible".equals(whichBoard)) {
System.out.print(visibleBoard.board[row][col] + " ");
} else if ("hidden".equals(whichBoard)) {
System.out.print(hiddenBoard.board[row][col] + " ");
}
}
System.out.println();
}
}
}
static class Board {
String[][] board = new String[11][11];
String[] xCord = { " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
String[] yCord = { " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
public Board() {
for (int row = 0; row < 11; row++) {
for (int col = 0; col < 11; col++) {
if (row == 0) {
board[row][col] = xCord[col];
} else if (col == 0) {
board[row][col] = yCord[row];
} else {
board[row][col] = "~";
}
}
}
}
}
static void placeShip(Board visibleBoard, Ship ship) {
Scanner scanner = new Scanner(System.in);
String[][] board = visibleBoard.board;
System.out.printf("Enter the coordinates of the %s (%d cells):%n", ship.getName(), ship.getLength());
boolean notPlaced = true;
while (notPlaced) {
String[] shipCoordinates = scanner.nextLine().split(" ");
String firstPoint = shipCoordinates[0];
String secondPoint = shipCoordinates[1];
// Horizontal
if (firstPoint.charAt(0) == secondPoint.charAt(0)) {
char rowLetter = firstPoint.charAt(0);
for (int row = 1; row < 11; row++) {
if (board[row][0].charAt(0) == rowLetter) {
int startCol = Integer.parseInt(firstPoint.substring(1));
int endCol = Integer.parseInt(secondPoint.substring(1));
// verify positive order
if (endCol < startCol) {
int temp = startCol;
startCol = endCol;
endCol = temp;
}
// Verify length
if (endCol - startCol + 1 != ship.getLength()) {
System.out.printf("Error! Wrong length of the %s! Try again:\n", ship.getName());
break;
}
// Verify it's not in proximity of others
boolean proximityAlert = false;
if ("O".equals(board[row][startCol - 1])) {
proximityAlert = true;
}
if (endCol != 10 && "O".equals(board[row][endCol + 1])) {
proximityAlert = true;
}
for (int col = startCol; col <= endCol; col++) {
if ("O".equals(board[row][col]) || "O".equals(board[row - 1][col])) {
proximityAlert = true;
break;
}
if (row != 10 && "O".equals(board[row + 1][col])) {
proximityAlert = true;
break;
}
}
// Place the ship
if (!proximityAlert) {
for (int col = startCol; col <= endCol; col++) {
board[row][col] = "O";
ship.coordinates.add(String.format("%s%d", firstPoint.substring(0, 1), col));
}
notPlaced = false;
} else {
System.out.println("Error! You placed it too close to another one. Try again:");
}
}
}
// Vertical
} else if (firstPoint.substring(1).equals(secondPoint.substring(1))) {
int col = Integer.parseInt(firstPoint.substring(1));
char startRow = firstPoint.charAt(0);
char endRow = secondPoint.charAt(0);
boolean placementStarted = false;
boolean lengthVerified = false;
// verify positive order
if (endRow < startRow) {
char temp = startRow;
startRow = endRow;
endRow = temp;
}
// Verify length
if (endRow - startRow + 1 == ship.getLength()) {
lengthVerified = true;
}
// Verify proximity to others
boolean proximityAlert = false;
for (int row = 1; row < 11; row++) {
if (board[row][0].charAt(0) >= startRow && board[row][0].charAt(0) <= endRow) {
if ("O".equals(board[row][col]) || "O".equals(board[row][col - 1])) {
proximityAlert = true;
}
if (col != 10 && "O".equals(board[row][col + 1])) {
proximityAlert = true;
}
}
if (board[row][0].charAt(0) == startRow) {
if ("O".equals(board[row - 1][col])) {
proximityAlert = true;
}
} else if (board[row][0].charAt(0) == endRow) {
if (row != 10) {
if ("O".equals(board[row + 1][col])) {
proximityAlert = true;
}
}
}
}
// Place ship
if (!proximityAlert && lengthVerified) {
for (int row = 1; row < 11; row++) {
if (board[row][0].charAt(0) == startRow) {
board[row][col] = "O";
ship.coordinates.add(String.format("%s%d", board[row][0], col));
placementStarted = true;
} else if (board[row][0].charAt(0) == endRow) {
board[row][col] = "O";
ship.coordinates.add(String.format("%s%d", board[row][0], col));
break;
} else if (placementStarted) {
board[row][col] = "O";
ship.coordinates.add(String.format("%s%d", board[row][0], col));
} else {
continue;
}
}
notPlaced = false;
} else if (!lengthVerified) {
System.out.printf("Error! Wrong length of the %s! Try again:%n", ship.getName());
} else if (proximityAlert) {
System.out.println("Error! You placed it too close to another one. Try again:");
}
} else {
System.out.println("Error! Wrong ship location! Try again:");
}
}
}
static void takeAShot(Player otherPlayer) {
Scanner scanner = new Scanner(System.in);
String[][] board = otherPlayer.visibleBoard.board;
String[][] hiddenView = otherPlayer.hiddenBoard.board;
boolean completed = false;
while (!completed) {
String shot = scanner.nextLine();
if (!shot.substring(0, 1).matches("[ABCDEFGHIJ]")) {
System.out.println("Error! You entered the wrong coordinates! Try again:");
continue;
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (board[i][0].charAt(0) == shot.charAt(0)) {
if (j == Integer.parseInt(shot.substring(1))) {
if ("O".equals(board[i][j]) || "X".equals(board[i][j])) {
board[i][j] = "X";
hiddenView[i][j] = "X";
boolean sunkShip = checkSunkShip(shot, otherPlayer.shipList);
boolean allShipsSunk = checkAllShipsSunk(otherPlayer.shipList);
if (allShipsSunk) {
System.out.println("You sank the last ship. You won. Congratulations!");
} else if (sunkShip) {
System.out.println("You sank a ship!");
} else {
System.out.println("You hit a ship!");
}
completed = true;
} else {
board[i][j] = "M";
hiddenView[i][j] = "M";
System.out.println("You missed!");
completed = true;
}
}
}
}
}
}
}
static boolean checkSunkShip(String shot, Ship[] shipList) {
for (Ship shipName : shipList) {
if (!shipName.isSunk) {
for (int i = 0; i < shipName.coordinates.size(); i++) {
if (shot.equals(shipName.coordinates.get(i))) {
shipName.coordinates.remove(i);
}
}
if (shipName.coordinates.size() == 0) {
shipName.isSunk = true;
return true;
}
}
}
return false;
}
static boolean checkAllShipsSunk(Ship[] shipList) {
for (Ship shipName : shipList) {
if (!shipName.isSunk) {
return false;
}
}
allShipsSunk = true;
return true;
}
}