-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleship.java
More file actions
329 lines (230 loc) · 9.31 KB
/
Copy pathBattleship.java
File metadata and controls
329 lines (230 loc) · 9.31 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
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
import java.util.Random;
import java.util.Scanner;
public class Battleship {
public static int readCoordinate(Scanner playerInput, String prompt) {
System.out.print(prompt);
int value = playerInput.nextInt();
while (value < 0 || value > 9) {
System.out.println("Error. Please enter a coordinate between 0 & 9");
System.out.print(prompt);
value = playerInput.nextInt();
}
return value;
}
public static void main(String[] args) {
int [][] map = new int[10][10];
//Intro
System.out.println("** Welcome to Battle Ships game ** \n\n Right now, the sea is empty...\n" );
int leftNum = 0;
int rightNum = 0;
System.out.println(" 0123456789");
//nested for loop to create map
for (int row = 0; row < map.length; row++) {
System.out.print(leftNum + " |");
leftNum++; // ascending column on the left
for (int col = 0; col < map[row].length; col++) {
map[row][col] = 0; //set every value in 2D array to 0. Player ships will be 1. Comp ships 2?
System.out.print(" ");
}
System.out.print("| " + rightNum);
rightNum++; // ascending column on the right
System.out.println();
}
System.out.println(" 0123456789");
System.out.print("\n\n"); //Create some space
//Now to deploy the players ships
Scanner playerInput = new Scanner(System.in);
//asking the player where to place his ships
System.out.println("Place your 5 ships!");
for (int p1Ship = 0; p1Ship < 5; p1Ship++) {
int p1ShipNum = p1Ship + 1;
int x = readCoordinate(playerInput, "Enter X coordinate for your ship " + p1ShipNum + ":");
int y = readCoordinate(playerInput, "Enter Y coordinate for your ship " + p1ShipNum + ":");
//Still inside the for loop.
//if the ship is already placed in that spot, need to throw error, and ask for
//a new position for that same ship.
if (map[x][y] == 1) {
System.out.println("Error. There is already a ship in that spot.");
System.out.println("Please pick a new spot for ship " + p1ShipNum );
p1Ship = p1Ship -1;
}
map[x][y] = 1; // Spots where p1 ship is located is a 1, empty spots are 0
}
//Now to show the player where he has deployed his ships
System.out.println("\nHere are your ships coordinates: ");
System.out.println(" 0123456789");
//nested for loop to re-create map to show updated values, set leftNum & rightNum = 0 again.
leftNum = 0;
rightNum = 0;
for (int row = 0; row < map.length; row++) {
System.out.print(leftNum + " |");
leftNum++; // ascending column on the left
for (int col = 0; col < map[row].length; col++) {
//if statement to show @ ship location, if value=1 there is a ship there.
if (map[row][col] == 0) {
System.out.print(" ");
}else if (map[row][col] == 1) {
System.out.print("@");
}
}
System.out.print("| " + rightNum);
rightNum++; // ascending column on the right
System.out.println();
}
System.out.println(" 0123456789");
//Now to place computer ships
System.out.println("\nComputer is deploying ships.");
Random compNum = new Random();
for (int p2Ship = 0; p2Ship < 5; p2Ship++) {
int x = compNum.nextInt(10); //random number between 1-10
int y = compNum.nextInt(10); //random number between 1-10
//Still inside the for loop.
//if a ship is already placed in that spot, need computer to pick new spot
if (map[x][y] == 1 || map[x][y] == 2) {
p2Ship = p2Ship -1;
}
map[x][y] = 2; // Spots where a ship is located is a 1, empty spots are 0, player 1 spots are 1.
int p2ShipNum = p2Ship + 1;
System.out.println(p2ShipNum + ". Ship DEPLOYED");
}
//Test to see if computer placed ships and where
//Commented out, un-comment to test placed ships
/*
System.out.println(" 0123456789");
//nested for loop to re-create map to show updated values, set leftNum & rightNum = 0 again.
leftNum = 0;
rightNum = 0;
for (int row = 0; row < map.length; row++) {
System.out.print(leftNum + " |");
leftNum++; // ascending column on the left
for (int col = 0; col < map[row].length; col++) {
//if statement to show @ p1 ship location, and C at comp ship location
if (map[row][col] == 0) {
System.out.print(" ");
}else if (map[row][col] == 1) {
System.out.print("@");
}else if (map[row][col] == 2) {
System.out.print("C");
}
}
System.out.print("| " + rightNum);
rightNum++; // ascending column on the right
System.out.println();
}
System.out.println(" 0123456789");
*/
//All ships are placed. Time to play.
System.out.println("--------------------");
System.out.println("BATTLE!");
//0 is empty spot
//1 is p1 ship location
//2 is computer ship location
//3 is sunk p1 ship
//4 is sunk computer ship
//5 is p1 miss
//6 is a computer miss
boolean gameRunning = true;
int turnNum = 1;
int p1RemainingShips = 0;
int compRemainingShips = 0;
while (gameRunning) {
//Player's turn
System.out.println("Turn " + turnNum);
System.out.println("YOUR TURN");
//P1 turn to attack
int xAttack = readCoordinate(playerInput, "Enter X coordinate: ");
int yAttack = readCoordinate(playerInput, "Enter Y coordinates: ");
// avoid p1 attacking the same spot, ask for another attack
// just have p1 repeat attack unless its empty or its a enemy ship
while(map[xAttack][yAttack] == 3 | map[xAttack][yAttack] == 4 | map[xAttack][yAttack] == 5 ) {
if(map[xAttack][yAttack] == 3) {
System.out.println("Sorry, that is one of your sunk ships, try again");
}
if(map[xAttack][yAttack] == 4) {
System.out.println("Sorry, you already sunk that ship");
}
if(map[xAttack][yAttack] == 5) {
System.out.println("Sorry, you attacked that coordinate before, it was a miss");
}
xAttack = readCoordinate(playerInput, "Enter X coordinate: ");
yAttack = readCoordinate(playerInput, "Enter Y coordinates: ");
}
if (map[xAttack][yAttack] == 1) {
// this is where a p1 ship is placed. sunk own ship
System.out.println("Oh no, you sunk your own ship! Watch your fire! ");
map[xAttack][yAttack] = 3;
}else if (map[xAttack][yAttack] == 2) {//this is where an enemy ship is located
System.out.println("BOOM! You sunk an enemy ship!");
map[xAttack][yAttack] = 4;
}else {//miss
System.out.println("Sorry, you missed");
map[xAttack][yAttack] = 5;
}
//Computer turn to attack
System.out.println("\nCOMPUTER'S TURN");
xAttack = compNum.nextInt(10);
yAttack = compNum.nextInt(10);
while(map[xAttack][yAttack] == 6 | map[xAttack][yAttack] == 4) { // avoid the computer attacking the same spot
xAttack = compNum.nextInt(10);
yAttack = compNum.nextInt(10);
}
if (map[xAttack][yAttack] == 1) {
// this is where a p1 ship is placed, computer hits.
System.out.println("The Computer sunk one of your ships!");
map[xAttack][yAttack] = 3;
}else if (map[xAttack][yAttack] == 2) {//this is where a computer ship is located
System.out.println("The Computer sunk one of its own ships");
map[xAttack][yAttack] = 4;
}else {//miss
System.out.println("Computer missed");
map[xAttack][yAttack] = 6;
}
//Now show the map again, with hits and misses.
System.out.println();
System.out.println(" 0123456789");
//nested for loop to re-create map to show updated values, set leftNum & rightNum = 0 again.
leftNum = 0;
rightNum = 0;
p1RemainingShips = 0;
compRemainingShips = 0;
for (int row = 0; row < map.length; row++) {
System.out.print(leftNum + " |");
leftNum++; // ascending column on the left
for (int col = 0; col < map[row].length; col++) {
//if statement to show @ ship location, if value=1 there is a ship there.
if (map[row][col] == 1) {
System.out.print("@");
p1RemainingShips++;
}else if (map[row][col] == 3) {
System.out.print("X");
}else if (map[row][col] == 4) {
System.out.print("!");
}else if (map[row][col] == 5) {
System.out.print("-");
}else if (map[row][col] == 2) {
System.out.print(" ");
compRemainingShips++;
}else{
System.out.print(" ");
}
}
System.out.print("| " + rightNum);
rightNum++; // ascending column on the right
System.out.println();
}
System.out.println(" 0123456789");
System.out.println("Your ships: " + p1RemainingShips + " | Computer Ships: " + compRemainingShips);
System.out.println("Key: (@ = your ship) (X = your sunk ship) (! = computer sunk ship) (- = miss)");
System.out.println("----------------------------------------------------");
turnNum++;
if (compRemainingShips == 0) {
System.out.println("Hooray! You win the battle! ");
gameRunning = false;
}else if (p1RemainingShips == 0) {
System.out.println("The computer has beat you. It is becoming sentient. SkyNet is taking over");
gameRunning = false;
}
}
playerInput.close();
}
}