forked from ymarcus93/Java-Battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleship.java
More file actions
467 lines (408 loc) · 14.6 KB
/
Battleship.java
File metadata and controls
467 lines (408 loc) · 14.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import java.util.Scanner;
public class Battleship
{
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("JAVA BATTLESHIP - ** Yuval Marcus **");
System.out.println("\nPlayer SETUP:");
Player userPlayer = new Player();
setup(userPlayer);
System.out.println("Computer SETUP...DONE...PRESS ENTER TO CONTINUE...");
reader.nextLine();
reader.nextLine();
Player computer = new Player();
setupComputer(computer);
System.out.println("\nCOMPUTER GRID (FOR DEBUG)...");
computer.playerGrid.printShips();
String result = "";
while(true)
{
System.out.println(result);
System.out.println("\nUSER MAKE GUESS:");
result = askForGuess(userPlayer, computer);
if (userPlayer.playerGrid.hasLost())
{
System.out.println("COMP HIT!...USER LOSES");
break;
}
else if (computer.playerGrid.hasLost())
{
System.out.println("HIT!...COMPUTER LOSES");
break;
}
System.out.println("\nCOMPUTER IS MAKING GUESS...");
compMakeGuess(computer, userPlayer);
}
}
private static void compMakeGuess(Player comp, Player user)
{
Randomizer rand = new Randomizer();
int row = rand.nextInt(0, 9);
int col = rand.nextInt(0, 9);
// While computer already guessed this posiiton, make a new random guess
while (comp.oppGrid.alreadyGuessed(row, col))
{
row = rand.nextInt(0, 9);
col = rand.nextInt(0, 9);
}
if (user.playerGrid.hasShip(row, col))
{
comp.oppGrid.markHit(row, col);
user.playerGrid.markHit(row, col);
System.out.println("COMP HIT AT " + convertIntToLetter(row) + convertCompColToRegular(col));
}
else
{
comp.oppGrid.markMiss(row, col);
user.playerGrid.markMiss(row, col);
System.out.println("COMP MISS AT " + convertIntToLetter(row) + convertCompColToRegular(col));
}
System.out.println("\nYOUR BOARD...PRESS ENTER TO CONTINUE...");
reader.nextLine();
user.playerGrid.printCombined();
System.out.println("PRESS ENTER TO CONTINUE...");
reader.nextLine();
}
private static String askForGuess(Player p, Player opp)
{
System.out.println("Viewing My Guesses:");
p.oppGrid.printStatus();
int row = -1;
int col = -1;
String oldRow = "Z";
int oldCol = -1;
while(true)
{
System.out.print("Type in row (A-J): ");
String userInputRow = reader.next();
userInputRow = userInputRow.toUpperCase();
oldRow = userInputRow;
row = convertLetterToInt(userInputRow);
System.out.print("Type in column (1-10): ");
col = reader.nextInt();
oldCol = col;
col = convertUserColToProCol(col);
//System.out.println("DEBUG: " + row + col);
if (col >= 0 && col <= 9 && row != -1)
break;
System.out.println("Invalid location!");
}
if (opp.playerGrid.hasShip(row, col))
{
p.oppGrid.markHit(row, col);
opp.playerGrid.markHit(row, col);
return "** USER HIT AT " + oldRow + oldCol + " **";
}
else
{
p.oppGrid.markMiss(row, col);
opp.playerGrid.markMiss(row, col);
return "** USER MISS AT " + oldRow + oldCol + " **";
}
}
private static void setup(Player p)
{
p.playerGrid.printShips();
System.out.println();
int counter = 1;
int normCounter = 0;
while (p.numOfShipsLeft() > 0)
{
for (Ship s: p.ships)
{
System.out.println("\nShip #" + counter + ": Length-" + s.getLength());
int row = -1;
int col = -1;
int dir = -1;
while(true)
{
System.out.print("Type in row (A-J): ");
String userInputRow = reader.next();
userInputRow = userInputRow.toUpperCase();
row = convertLetterToInt(userInputRow);
System.out.print("Type in column (1-10): ");
col = reader.nextInt();
col = convertUserColToProCol(col);
System.out.print("Type in direction (0-H, 1-V): ");
dir = reader.nextInt();
//System.out.println("DEBUG: " + row + col + dir);
if (col >= 0 && col <= 9 && row != -1 && dir != -1) // Check valid input
{
if (!hasErrors(row, col, dir, p, normCounter)) // Check if errors will produce (out of bounds)
{
break;
}
}
System.out.println("Invalid location!");
}
//System.out.println("FURTHER DEBUG: row = " + row + "; col = " + col);
p.ships[normCounter].setLocation(row, col);
p.ships[normCounter].setDirection(dir);
p.playerGrid.addShip(p.ships[normCounter]);
p.playerGrid.printShips();
System.out.println();
System.out.println("You have " + p.numOfShipsLeft() + " remaining ships to place.");
normCounter++;
counter++;
}
}
}
private static void setupComputer(Player p)
{
System.out.println();
int counter = 1;
int normCounter = 0;
Randomizer rand = new Randomizer();
while (p.numOfShipsLeft() > 0)
{
for (Ship s: p.ships)
{
int row = rand.nextInt(0, 9);
int col = rand.nextInt(0, 9);
int dir = rand.nextInt(0, 1);
//System.out.println("DEBUG: row-" + row + "; col-" + col + "; dir-" + dir);
while (hasErrorsComp(row, col, dir, p, normCounter)) // while the random nums make error, start again
{
row = rand.nextInt(0, 9);
col = rand.nextInt(0, 9);
dir = rand.nextInt(0, 1);
//System.out.println("AGAIN-DEBUG: row-" + row + "; col-" + col + "; dir-" + dir);
}
//System.out.println("FURTHER DEBUG: row = " + row + "; col = " + col);
p.ships[normCounter].setLocation(row, col);
p.ships[normCounter].setDirection(dir);
p.playerGrid.addShip(p.ships[normCounter]);
normCounter++;
counter++;
}
}
}
private static boolean hasErrors(int row, int col, int dir, Player p, int count)
{
//System.out.println("DEBUG: count arg is " + count);
int length = p.ships[count].getLength();
// Check if off grid - Horizontal
if (dir == 0)
{
int checker = length + col;
//System.out.println("DEBUG: checker is " + checker);
if (checker > 10)
{
System.out.println("SHIP DOES NOT FIT");
return true;
}
}
// Check if off grid - Vertical
if (dir == 1) // VERTICAL
{
int checker = length + row;
//System.out.println("DEBUG: checker is " + checker);
if (checker > 10)
{
System.out.println("SHIP DOES NOT FIT");
return true;
}
}
// Check if overlapping with another ship
if (dir == 0) // Hortizontal
{
// For each location a ship occupies, check if ship is already there
for (int i = col; i < col+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
if(p.playerGrid.hasShip(row, i))
{
System.out.println("THERE IS ALREADY A SHIP AT THAT LOCATION");
return true;
}
}
}
else if (dir == 1) // Vertical
{
// For each location a ship occupies, check if ship is already there
for (int i = row; i < row+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
if(p.playerGrid.hasShip(i, col))
{
System.out.println("THERE IS ALREADY A SHIP AT THAT LOCATION");
return true;
}
}
}
return false;
}
private static boolean hasErrorsComp(int row, int col, int dir, Player p, int count)
{
//System.out.println("DEBUG: count arg is " + count);
int length = p.ships[count].getLength();
// Check if off grid - Horizontal
if (dir == 0)
{
int checker = length + col;
//System.out.println("DEBUG: checker is " + checker);
if (checker > 10)
{
return true;
}
}
// Check if off grid - Vertical
if (dir == 1) // VERTICAL
{
int checker = length + row;
//System.out.println("DEBUG: checker is " + checker);
if (checker > 10)
{
return true;
}
}
// Check if overlapping with another ship
if (dir == 0) // Hortizontal
{
// For each location a ship occupies, check if ship is already there
for (int i = col; i < col+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
if(p.playerGrid.hasShip(row, i))
{
return true;
}
}
}
else if (dir == 1) // Vertical
{
// For each location a ship occupies, check if ship is already there
for (int i = row; i < row+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
if(p.playerGrid.hasShip(i, col))
{
return true;
}
}
}
return false;
}
/*HELPER METHODS*/
private static int convertLetterToInt(String val)
{
int toReturn = -1;
switch (val)
{
case "A": toReturn = 0;
break;
case "B": toReturn = 1;
break;
case "C": toReturn = 2;
break;
case "D": toReturn = 3;
break;
case "E": toReturn = 4;
break;
case "F": toReturn = 5;
break;
case "G": toReturn = 6;
break;
case "H": toReturn = 7;
break;
case "I": toReturn = 8;
break;
case "J": toReturn = 9;
break;
default: toReturn = -1;
break;
}
return toReturn;
}
private static String convertIntToLetter(int val)
{
String toReturn = "Z";
switch (val)
{
case 0: toReturn = "A";
break;
case 1: toReturn = "B";
break;
case 2: toReturn = "C";
break;
case 3: toReturn = "D";
break;
case 4: toReturn = "E";
break;
case 5: toReturn = "F";
break;
case 6: toReturn = "G";
break;
case 7: toReturn = "H";
break;
case 8: toReturn = "I";
break;
case 9: toReturn = "J";
break;
default: toReturn = "Z";
break;
}
return toReturn;
}
private static int convertUserColToProCol(int val)
{
int toReturn = -1;
switch (val)
{
case 1: toReturn = 0;
break;
case 2: toReturn = 1;
break;
case 3: toReturn = 2;
break;
case 4: toReturn = 3;
break;
case 5: toReturn = 4;
break;
case 6: toReturn = 5;
break;
case 7: toReturn = 6;
break;
case 8: toReturn = 7;
break;
case 9: toReturn = 8;
break;
case 10: toReturn = 9;
break;
default: toReturn = -1;
break;
}
return toReturn;
}
private static int convertCompColToRegular(int val)
{
int toReturn = -1;
switch (val)
{
case 0: toReturn = 1;
break;
case 1: toReturn = 2;
break;
case 2: toReturn = 3;
break;
case 3: toReturn = 4;
break;
case 4: toReturn = 5;
break;
case 5: toReturn = 6;
break;
case 6: toReturn = 7;
break;
case 7: toReturn = 8;
break;
case 8: toReturn = 9;
break;
case 9: toReturn = 10;
break;
default: toReturn = -1;
break;
}
return toReturn;
}
}