-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
564 lines (460 loc) · 20.2 KB
/
Copy pathMain.java
File metadata and controls
564 lines (460 loc) · 20.2 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package battleship;
import java.io.IOException;
import java.util.*;
public class Main {
static Scanner scanner = new Scanner(System.in);
static int count1 = 0;
static int count2 = 0;
static int irr;
public static void main(String[] args) {
// Declaring sea array
//Player's 1 field
String[][] Player1 = new String[11][11];
//Player's 2 field
String[][] Player2 = new String[11][11];
//Setting 5 types of ships
shipType Aircraft = new shipType();
Aircraft.setShipName("Aircraft Carrier");
Aircraft.setShipLength(5);
shipType Battleship = new shipType();
Battleship.setShipName("Battleship");
Battleship.setShipLength(4);
shipType Submarine = new shipType();
Submarine.setShipName("Submarine");
Submarine.setShipLength(3);
shipType Cruiser = new shipType();
Cruiser.setShipName("Cruiser");
Cruiser.setShipLength(3);
shipType Destroyer = new shipType();
Destroyer.setShipName("Destroyer");
Destroyer.setShipLength(2);
// Creating a starting battlefields
for (int row = 0; row <= 10; row++) {
for (int col = 0; col <= 10; col++) {
if (row == 0 && col == 0) {
Player1[row][col] = " ";
Player2[row][col] = " ";
} else if (row == 0) {
Player1[row][col] = String.format("%d", col);
Player2[row][col] = String.format("%d", col);
} else if (col == 0) {
Player1[row][col] = String.format("%c", (char) (64 + row));
Player2[row][col] = String.format("%c", (char) (64 + row));
} else {
Player1[row][col] = "~";
Player2[row][col] = "~";
}
}
}
//Placing ships on field 1
System.out.println("Player 1, place your ships on the game field\n");
printBattlefield(Player1);
//Take position!
setUnit(Player1, Aircraft);
setUnit(Player1, Battleship);
setUnit(Player1, Submarine);
setUnit(Player1, Cruiser);
setUnit(Player1, Destroyer);
promptEnterKey();
char[] allShips1 = {Aircraft.coordinates[0], Aircraft.coordinates[1], Aircraft.coordinates[2], Aircraft.coordinates[3],
Battleship.coordinates[0], Battleship.coordinates[1], Battleship.coordinates[2], Battleship.coordinates[3],
Submarine.coordinates[0], Submarine.coordinates[1], Submarine.coordinates[2], Submarine.coordinates[3],
Cruiser.coordinates[0], Cruiser.coordinates[1], Cruiser.coordinates[2], Cruiser.coordinates[3],
Destroyer.coordinates[0], Destroyer.coordinates[1], Destroyer.coordinates[2], Destroyer.coordinates[3]};
System.out.println("Player 2, place your ships on the game field\n");
//Placing ships on field 2
printBattlefield(Player2);
//Take position!
setUnit(Player2, Aircraft);
setUnit(Player2, Battleship);
setUnit(Player2, Submarine);
setUnit(Player2, Cruiser);
setUnit(Player2, Destroyer);
promptEnterKey();
//Ships' coordinates for checking if one of them or all of them destroyed or not
char[] allShips2 = {Aircraft.coordinates[0], Aircraft.coordinates[1], Aircraft.coordinates[2], Aircraft.coordinates[3],
Battleship.coordinates[0], Battleship.coordinates[1], Battleship.coordinates[2], Battleship.coordinates[3],
Submarine.coordinates[0], Submarine.coordinates[1], Submarine.coordinates[2], Submarine.coordinates[3],
Cruiser.coordinates[0], Cruiser.coordinates[1], Cruiser.coordinates[2], Cruiser.coordinates[3],
Destroyer.coordinates[0], Destroyer.coordinates[1], Destroyer.coordinates[2], Destroyer.coordinates[3]};
//while all ships aren't destroyed
for(irr = 2;;irr++) {
if(irr % 2 == 0) {
printFoggedBattlefield(Player2);
System.out.println("---------------------");
printBattlefield(Player1);
System.out.println("\n\nPlayer " + 1 + ", it's your turn:\n");
shot(Player2, allShips2);
System.out.println(count2);
promptEnterKey();
}
else if(irr % 2 == 1) {
printFoggedBattlefield(Player1);
System.out.println("---------------------");
printBattlefield(Player2);
System.out.println("\n\nPlayer " + 2 + ", it's your turn:\n");
shot(Player1, allShips1);
System.out.println(count1);
promptEnterKey();
}
if(count1 == 5 || count2 == 5)
break;
}
}
public static void promptEnterKey() {
System.out.println("Press Enter and pass the move to another player");
try {
System.in.read();} catch (IOException e) {
e.printStackTrace();
}
}
// Printing a battlefield
public static void printBattlefield(String[][] sea) {
for (int row = 0; row <= 10; row++) {
for (int col = 0; col <= 10; col++) {
System.out.print(sea[row][col] + " ");
}
System.out.println();
}
}
//printing the sea full of '~', 'X' and 'M'
public static void printFoggedBattlefield(String[][] sea) {
// Printing a battlefield
for (int row = 0; row <= 10; row++) {
for (int col = 0; col <= 10; col++) {
if( "O".equals(sea[row][col]) ) {
System.out.print("~ ");
continue;
}
System.out.print(sea[row][col] + " ");
}
System.out.println();
}
}
//Game starts, tale a shot
// public static void endOfPlacing(String[][] sea) {
// System.out.printf("\nThe game starts!\n\n");
//
// printFoggedBattlefield(sea);
//
// System.out.println("\nTake a shot!\n");
// }
//Shooting process
public static void shot(String[][] sea, char[] allShips) {
Scanner scanner = new Scanner(System.in);
try {
for (; ; ) {
// Entering input values
String shotCoordinates = scanner.next();
// Shooting Coordinates parsing process
int rowShot = shotCoordinates.charAt(0);
int colShot = 0;
if (shotCoordinates.length() == 3) {
if ((int) (shotCoordinates.charAt(1)) == 49 && (int) (shotCoordinates.charAt(2)) == 48)
colShot = 10;
} else
colShot = shotCoordinates.charAt(1);
// Converting variables
int rowShotInt = rowShot - 64;
int colShotInt;
if (colShot == 10)
colShotInt = 10;
else
colShotInt = colShot - 48;
// Shooting
try {
if(rowShotInt == 10 && colShotInt == 10) {
System.out.println("You sank the last ship. You won. Congratulations!");
count2 = 5;
count1 = 5;
break;
}
if ("X".equals(sea[rowShotInt][colShotInt])) {
System.out.println("\nYou hit a ship! Try again:\n");
continue;
}
if ("O".equals(sea[rowShotInt][colShotInt])) {
sea[rowShotInt][colShotInt] = "X";
if (!isDestroyed(sea, allShips)) {
System.out.println("\nYou hit a ship!\n");
} else if (count1 == 5 || count2 == 5) {
System.out.println("You sank the last ship. You won. Congratulations!");
break;
} else {
System.out.println("\nYou sank a ship!\n");
}
break;
} else if ("~".equals(sea[rowShotInt][colShotInt])) {
sea[rowShotInt][colShotInt] = "M";
System.out.println("\nYou missed!\n");
break;
} else {
System.out.println("\nError! You entered the wrong coordinates! Try again:\n");
}
} catch (Exception e) {
System.out.println("\nError! You entered the wrong coordinates! Try again:\n"); // it will be printed
}
}
}
catch (Exception e) {
System.out.println("\nError! You entered the wrong coordinates! Try again:\n"); // it will be printed
}
}
//Checking if a ship is destroyed or not, also counting destroyed ships
public static boolean isDestroyed(String[][] sea, char[] allShips) {
boolean result = false;
for(int i = 0; i < 20; i+=4) {
boolean notDestroyed = false;
if(allShips[i] == 0)
continue;
if (allShips[i] == allShips[i+2]) {
for (int j = allShips[i+1]; j <= allShips[i+3]; j++) {
if (sea[allShips[i]][j].equals("O")) {
notDestroyed = true;
break;
}
}
if(notDestroyed)
continue;
result = true;
if(irr % 2 == 0)
count2++;
else
count1++;
allShips[i] = 0;
return result;
}
else if (allShips[i+1] == allShips[i+3]) {
for (int j = allShips[i]; j <= allShips[i+2]; j++) {
if (sea[j][allShips[i + 1]].equals("O")) {
notDestroyed = true;
break;
}
}
if(notDestroyed)
continue;
result = true;
if(irr % 2 == 0)
count2++;
else
count1++;
allShips[i] = 0;
return result;
}
}
return result;
}
//Setting ship of the certain type to the sea
public static void setUnit(String[][] sea, shipType unit) {
System.out.printf("\nEnter the coordinates of the %s (%d cells):\n\n", unit.getShipName(), unit.getShipLength());
Scanner scanner = new Scanner(System.in);
for (; ; ) {
// Entering input values
String startCoordinates = scanner.next();
String endCoordinates = scanner.next();
// Starting Coordinates parsing process
int rowStart = startCoordinates.charAt(0);
int colStart = 0;
if(startCoordinates.length() == 3) {
if((int)(startCoordinates.charAt(1)) == 49 && (int)(startCoordinates.charAt(2)) == 48)
colStart = 10;
} else
colStart = startCoordinates.charAt(1);
// Ending Coordinates parsing process
int rowEnd = endCoordinates.charAt(0);
int colEnd = 0;
if(endCoordinates.length() == 3) {
if((int)(endCoordinates.charAt(1)) == 49 && (int)(endCoordinates.charAt(2)) == 48)
colEnd = 10;
} else
colEnd = endCoordinates.charAt(1);
//Converting variables
int rowStartInt = rowStart - 64;
int rowEndInt = rowEnd - 64;
int colStartInt;
if(colStart == 10)
colStartInt = 10;
else
colStartInt = colStart - 48;
int colEndInt;
if(colEnd == 10)
colEndInt = 10;
else
colEndInt = colEnd - 48;
//Replacing variables if condition is true
if(rowEndInt < rowStartInt) {
int temp = rowStartInt;
rowStartInt = rowEndInt;
rowEndInt = temp;
}
if(colEndInt < colStartInt) {
int temp = colStartInt;
colStartInt = colEndInt;
colEndInt = temp;
}
// Error checking
if ((rowStartInt != rowEndInt && colStartInt != colEndInt)) {
System.out.println("\nError! Wrong ship location! Try again:\n\n");
continue;
}
if ((rowEndInt - rowStartInt != unit.getShipLength() - 1 && colStartInt == colEndInt)
|| (colEndInt - colStartInt != unit.getShipLength() - 1) && rowStartInt == rowEndInt) {
System.out.printf("\nError! Wrong length of the %s! Try again:\n\n", unit.getShipName());
continue;
}
//Ship placing Errors
boolean errorFlag = false;
if (rowStartInt == rowEndInt) {
for (int j = colStartInt; j <= colEndInt; j++) {
//1st Error
if ("O".equals(sea[rowStartInt][j])) {
System.out.println("\nError! This place is already taken!\n\n");
errorFlag = true;
break;
}
//2nd Error
if((rowStartInt < 10 && rowEndInt < 10 && colStartInt < 10 && colEndInt < 10) && (
("O".equals(sea[rowStartInt - 1][j - 1]))
|| ("O".equals(sea[rowStartInt - 1][j]))
|| ("O".equals(sea[rowStartInt - 1][j + 1]))
|| ("O".equals(sea[rowStartInt][j + 1]))
|| ("O".equals(sea[rowStartInt + 1][j + 1]))
|| ("O".equals(sea[rowStartInt + 1][j]))
|| ("O".equals(sea[rowStartInt + 1][j - 1]))
|| ("O".equals(sea[rowStartInt][j - 1]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//3d Error
else if((rowStartInt == 10 && colEndInt < 10) && (
("O".equals(sea[rowStartInt - 1][j - 1]))
|| ("O".equals(sea[rowStartInt - 1][j]))
|| ("O".equals(sea[rowStartInt - 1][j + 1]))
|| ("O".equals(sea[rowStartInt][j + 1]))
|| ("O".equals(sea[rowStartInt][j - 1]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//4th Error
else if((rowStartInt == 10 && colEndInt == 10) && (
("O".equals(sea[rowStartInt - 1][j - 1]))
|| ("O".equals(sea[rowStartInt - 1][j]))
|| ("O".equals(sea[rowStartInt][j - 1]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//5th Error
else if((rowStartInt < 10 && colEndInt == 10) && (
("O".equals(sea[rowStartInt - 1][j - 1]))
|| ("O".equals(sea[rowStartInt - 1][j]))
|| ("O".equals(sea[rowStartInt][j - 1]))
|| ("O".equals(sea[rowStartInt+1][j - 1]))
|| ("O".equals(sea[rowStartInt+1][j]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
}
}
} else if (colStartInt == colEndInt) {
for (int i = rowStartInt; i <= rowEndInt; i++) {
//1 Error
if ("O".equals(sea[i][colStartInt])) {
System.out.println("\nError! This place is already taken!\n\n");
errorFlag = true;
break;
}
//2nd Error
if ((rowStartInt < 10 && rowEndInt < 10 && colStartInt < 10 && colEndInt < 10) && ("O".equals(sea[i - 1][colStartInt - 1])
|| ("O".equals(sea[i][colStartInt - 1]))
|| ("O".equals(sea[i + 1][colStartInt - 1]))
|| ("O".equals(sea[i - 1][colStartInt]))
|| ("O".equals(sea[i + 1][colStartInt]))
|| ("O".equals(sea[i - 1][colStartInt + 1]))
|| ("O".equals(sea[i + 1][colStartInt + 1]))
|| ("O".equals(sea[i][colStartInt + 1]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//3rd Error
if ((colStartInt == 10 && rowEndInt < 10) && ("O".equals(sea[i - 1][colStartInt - 1])
|| ("O".equals(sea[i][colStartInt - 1]))
|| ("O".equals(sea[i + 1][colStartInt - 1]))
|| ("O".equals(sea[i - 1][colStartInt]))
|| ("O".equals(sea[i + 1][colStartInt]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//4th Error
if ((colStartInt == 10 && rowEndInt == 10) && ("O".equals(sea[i - 1][colStartInt - 1])
|| ("O".equals(sea[i][colStartInt - 1]))
|| ("O".equals(sea[i - 1][colStartInt]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
//5th Error
if ((colStartInt < 10 && rowEndInt == 10) && ("O".equals(sea[i - 1][colStartInt - 1])
|| ("O".equals(sea[i][colStartInt - 1]))
|| ("O".equals(sea[i - 1][colStartInt]))
|| ("O".equals(sea[i - 1][colStartInt + 1]))
|| ("O".equals(sea[i][colStartInt + 1]))
)) {
System.out.println("\nError! You placed it too close to another one. Try again:\n\n");
errorFlag = true;
break;
}
}
}
if (errorFlag) {
continue;
}
// Setting the Aircraft
unit.coordinates[0] = (char) rowStartInt;
unit.coordinates[1] = (char) colStartInt;
unit.coordinates[2] = (char) rowEndInt;
unit.coordinates[3] = (char) colEndInt;
if (rowStartInt == rowEndInt) {
int j, n = 0;
for (j = colStartInt, n = 0; j <= colEndInt; j++, n++) {
sea[rowStartInt][j] = "O";
}
printBattlefield(sea);
break;
} else if (colStartInt == colEndInt) {
for (int i = rowStartInt; i <= rowEndInt; i++) {
sea[i][colStartInt] = "O";
}
printBattlefield(sea);
break;
}
}
}
}
class shipType {
String shipName;
int shipLength;
char[] coordinates = new char[4];
public void setShipName(String name) {
this.shipName = name;
}
public void setShipLength(int len) {
this.shipLength = len;
}
public String getShipName() {
return this.shipName;
}
public int getShipLength() {
return this.shipLength;
}
}