-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battleship.java
280 lines (242 loc) · 9.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
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
import java.util.Random;
import java.util.Scanner;
public class Battleship {
/*
You will be recreating the game of battleships. A player will place 5 of their ships on a 10 by 10 grid.
The computer player will deploy five ships on the same grid. Once the game starts the player and computer
take turns, trying to sink each other's ships by guessing the coordinates to "attack". The game ends when
either the player or computer has no ships left.
*/
public static String[][] playingField = new String[10][10];
public static String[][] computerShots = new String[10][10];
public static int shipCount = 0;
public static int compCount = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("**** Welcome to Battle Ships game ****");
System.out.println();
System.out.println("Right now, the sea is empty.");
System.out.println();
showMap(); // Prints the current Array data. (Shows as map!)
System.out.println();
System.out.println("Where would you like to place your ships? \t (" + shipCount + "/5 USED)");
System.out.println();
/* Step 2 - Deploy Player's Ships */
for (shipCount = 0; shipCount < 5; shipCount++) {
System.out.print("Enter X coordinate for ship " + (shipCount + 1) + ": ");
int getX = input.nextInt();
System.out.print("Enter Y coordinate for ship " + (shipCount + 1) + ": ");
int getY = input.nextInt();
System.out.println();
verifyInput(getX, getY);
}
System.out.println();
/* Step 3 - Deploy Computer's Ships */
System.out.println("Computer is deploying ships");
for (compCount = 0; compCount < 5; compCount++) {
boolean success = false;
int X = 0;
int Y = 0;
while (success == false) {
Random gen = new Random();
X = gen.nextInt(9);
Y = gen.nextInt(9);
success = checkCoords(X, Y);
}
playingField[Y][X] = "2";
System.out.println((compCount + 1) + ". ship DEPLOYED");
}
System.out.println("-------------------------------------");
System.out.println();
showMap();
System.out.println();
/* Step 4 - Battle */
System.out.println("**** BEGIN BATTLE ****");
int turnNum = 0; // Track turns.
System.out.println();
while ((shipCount != 0) && (compCount != 0)) {
if (turnNum % 2 == 0) {
boolean fired = false;
System.out.println("YOUR TURN");
while (fired == false) {
System.out.print("Enter X coordinate: ");
int X = input.nextInt();
System.out.print("Enter Y coordinate: ");
int Y = input.nextInt();
if (!validate(X, Y)) {
System.out.println("Coordinates Invalid. Try again! (0-9)");
}
if (shotAlready(X, Y)) {
System.out.println("You've shot here already. Try a new coordinate.");
} else {
fireWeapon(X, Y);
fired = true;
}
}
} else {
System.out.println();
System.out.println("COMPUTER'S TURN");
boolean goodshot = false;
int X = 0;
int Y = 0;
while (goodshot == false) {
Random shot = new Random();
X = shot.nextInt(9);
Y = shot.nextInt(9);
goodshot = compShots(X, Y); // Turn true once the shot can be made.
}
compShoots(X, Y); // Fire Computer's Weapon!
System.out.println();
showMap();
System.out.println();
System.out.println("Your ships: " + shipCount + " | Computer ships: " + compCount);
}
turnNum++;
}
System.out.println();
if (compCount == 0) {
System.out.println("Hooray! You win the battle. :)");
}
else {
System.out.println("Aw... the computer wins this time! Better luck next round!");
}
}
public static boolean compShots(int X, int Y) { // Checks computer array for shots taken.
if (computerShots[Y][X] != null) {
if (computerShots[Y][X].equals("x")) {
return false;
}
}
return true; // Take the shot!
}
public static void compShoots(int X, int Y) { // Fires on the map array, tracks on computer array.
if (computerShots[Y][X] != null) {
if (!computerShots[Y][X].equals("x")) {
if (playingField[Y][X].equals("1")) { // player ship.
System.out.println("Computer destroyed your ship at: " + X + ", " + Y + "!");
playingField[Y][X] = "x"; // Update player's map.
computerShots[Y][X] = "x"; // Update computer's record.
shipCount--;
}
if (playingField[Y][X].equals("2")) { // computer ship.
System.out.println("Computer sunk his own ship at: " + X + ", " + Y + "!");
playingField[Y][X] = "!";
computerShots[Y][X] = "x";
compCount--;
}
else {
System.out.println("Computer missed.");
computerShots[Y][X] = "x";
}
}
}
else {
System.out.println("Computer missed.");
computerShots[Y][X] = "x";
}
}
public static void fireWeapon(int X, int Y) {
String target = getTarget(X, Y);
if (target == "") {
System.out.println("Sorry, you missed!");
playingField[Y][X] = "-";
}
if (target == "1") {
System.out.println("Oh no, you sunk your own ship! :(");
playingField[Y][X] = "x";
shipCount--;
}
if (target == "2") {
System.out.println("Boom! You sunk the ship!");
playingField[Y][X] = "!";
compCount--;
}
}
public static boolean shotAlready(int X, int Y) {
if (playingField[Y][X] != null) {
if ((playingField[Y][X] == "!") || (playingField[Y][X] == "x") || (playingField[Y][X]) == "-") {
return true;
}
}
else {
return false;
}
return false;
}
public static String getTarget(int X, int Y) {
if (playingField[Y][X] != null) {
return playingField[Y][X];
}
else {
return "";
}
}
public static boolean validate(int X, int Y) {
if ((X > 9) || (X < 0)) {
return false;
}
if ((Y > 9) || (Y < 0)) {
return false;
}
return true;
}
public static void showMap() {
System.out.println(" 0123456789 ");
for (int r = 0; r < playingField.length; r++) {
System.out.print(r + " |");
for (int c = 0; c < playingField[r].length; c++) {
if (playingField[r][c] != null) {
if (playingField[r][c].equals("1")) {
System.out.print("@");
}
else if (playingField[r][c].equals("2")) {
System.out.print(" ");
}
else {
System.out.print(playingField[r][c]);
}
} else {
System.out.print(" ");
}
}
System.out.print("| " + r + "\n");
}
System.out.println(" 0123456789 ");
}
public static void verifyInput(int X, int Y) {
if ((X > 9) || (X < 0)) {
System.out.println("Your X coordinate must be 0-9.");
shipCount--;
return;
}
if ((Y > 9) || (Y < 0)) {
System.out.println("Your Y coordinate must be 0-9");
shipCount--;
return;
}
else {
if (playingField[Y][X] != null) {
if (playingField[Y][X].equals("1")) {
System.out.println("Ship already located here.");
shipCount--;
}
}
else {
playingField[Y][X] = "1";
showMap();
System.out.println();
System.out.println("Ship successfully added.");
}
}
}
public static boolean checkCoords(int X, int Y) {
if (playingField[Y][X] != null) {
if ((playingField[Y][X].equals("1")) || (playingField[Y][X].equals("2"))) {
return false;
}
} else {
return true;
}
return false;
}
}