-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battleship.java
157 lines (132 loc) · 5.4 KB
/
Battleship.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
package summer2022.Battleship;
import summer2022.Battleship.core.Board;
import summer2022.Battleship.core.Computer;
import summer2022.Battleship.core.Location;
import summer2022.Battleship.core.Ship;
import summer2022.Battleship.enums.Direction;
import summer2022.Battleship.enums.LocationStatus;
import summer2022.Battleship.enums.ShipType;
import java.util.Random;
import java.util.Scanner;
public class Battleship {
public static Board gameBoard = new Board();
public static Computer computer;
static Scanner scanner = new Scanner(System.in);
static Random random = new Random();
public static final ShipType[] shipsNeeded = {ShipType.SMALL, ShipType.MEDIUM, ShipType.MEDIUM, ShipType.LARGE, ShipType.HUGE};
public static void playGame() {
loadNewBoard();
for (ShipType shipType : shipsNeeded) {
System.out.println();
gameBoard.printShips();
createShip(shipType);
}
gameBoard.printShips();
computer = new Computer();
computer.loadComputerBoard();
while(true) {
playerTurn();
if (computer.getBoard().allShipsSunk()) {
finishGame(true);
break;
}
computerTurn();
if (gameBoard.allShipsSunk()) {
finishGame(false);
break;
}
}
}
public static void playerTurn() {
Board opponentBoard = computer.getBoard();
System.out.println("\nComputer's Board");
opponentBoard.printForOpponent();
while (true) {
System.out.print("\nEnter a row to attack: ");
int row = scanner.nextInt();
System.out.print("Enter a column to attack: ");
int col = scanner.nextInt();
if (!Location.isInBounds(row, col)) {
System.out.println("That guess is out of bounds!");
} else {
Location loc = opponentBoard.getLocation(row, col);
if (loc.getStatus() == LocationStatus.EMPTY) {
System.out.println("\nYou missed!");
opponentBoard.attackAt(row, col);
break;
} else if (loc.getStatus() == LocationStatus.ALIVE) {
System.out.println("\nHit!");
opponentBoard.attackAt(row, col);
if (loc.getShip().isSunk()) { System.out.println("\nYou sunk the computer's " + loc.getShip().getType() + " ship!"); }
break;
} else {
System.out.println("\nYou already attacked this location!");
}
}
}
}
public static void computerTurn() {
System.out.println("\nYour Board");
gameBoard.printForOpponent();
while (true) {
// pick a random location to attack
int row = random.nextInt(10) + 1;
int col = random.nextInt(10) + 1;
Location loc = gameBoard.getLocation(row, col);
if (loc.getStatus() == LocationStatus.EMPTY) {
System.out.println("\nComputer missed!");
gameBoard.attackAt(row, col);
break;
} else if (loc.getStatus() == LocationStatus.ALIVE) {
System.out.println("\nComputer hit!");
gameBoard.attackAt(row, col);
if (loc.getShip().isSunk()) {
System.out.println("\nComputer sunk your " + loc.getShip().getType() + " ship!");
}
break;
}
}
}
public static void finishGame(boolean playerIsWinner) {
if (playerIsWinner) {
System.out.println("\nYou won!");
} else {
System.out.println("\nYou lost!");
}
System.out.print("Would you like to play again? (y/n): ");
String answer = scanner.next();
if (answer.equals("y")) {
playGame();
} else {
System.out.println("\nThanks for playing!");
}
}
private static void createShip(ShipType shipType) {
while (true) {
System.out.print("\nPlease enter the row of your " + shipType + " ship: ");
int row = scanner.nextInt();
System.out.print("Please enter the column of your " + shipType + " ship: ");
int col = scanner.nextInt();
System.out.print("Please enter the direction of your " + shipType + " ship: ");
String direction = scanner.next();
if (!Direction.getStringArray().contains(direction.toUpperCase())) {
System.out.println("Invalid direction!");
} else {
Direction dir = Direction.valueOf(direction.toUpperCase());
if (Location.isValidLocation(gameBoard, row, col, shipType, dir)) {
gameBoard.addShip(new Ship(gameBoard.getLocation(row, col), shipType, dir));
break;
} else {
System.out.println("Your ship cannot go there!");
}
}
}
}
public static void loadNewBoard() {
gameBoard = new Board();
}
public static void main(String[] args) {
System.out.println("Welcome to Battleship!");
playGame();
}
}