-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpertAI.java
847 lines (755 loc) · 52.1 KB
/
ExpertAI.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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import java.util.concurrent.ExecutionException;
/**Names: J. Lai and D. Liu
* File: ExpertAI.java
* Purpose: Create the Expert AI class that includes advanced battleship strategies.
* Date: June 10, 2024
*/
public class ExpertAI extends Captain{
//declare variables to be used in the code
String name;
//Create a new grid object
private static Grid myGrid = new Grid();
/**Sets the name for the Expert AI
*
* @param name the name of the expert AI
*/
public ExpertAI(String name){
this.name = name;
}
/**returns the name of the expert AI
*
*/
public String getName(){
return name;
}
/**sets the name for the expert AI
*
*/
public void setName(String name){
this.name = name;
}
public void placeShips(){
Ship[] ships = myGrid.getShips();
int[][] directionMap = {
{0, 1},
{1, 0},
{0, -1},
{-1, 0}
};
int [][] shipPlacement = {
{6, 1, 1, 0, 6, 0, 6, 9, 1, 0, 0, 0, 1, 0, 0},
{2, 8, 1, 9, 6, 0, 0, 6, 0, 8, 0, 0, 9, 0, 0}
};
int randomShipPlacement = (int)(Math.random() * shipPlacement.length);
ships[0].setHeadX(shipPlacement[randomShipPlacement][0]);
ships[0].setHeadY(shipPlacement[randomShipPlacement][1]);
int direction = shipPlacement[randomShipPlacement][2];
for(int i = 0; i < ships[0].getLength(); i++){
// System.out.println(s.getHeadX() + directionMap[direction][0] * i + " " + (s.getHeadY() + directionMap[direction][1] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][0] * i, s.getHeadY() + directionMap[direction][1] * i));
myGrid.setGridState(ships[0].getHeadX() + directionMap[direction][0] * i, ships[0].getHeadY() + directionMap[direction][1] * i, Character.toString(ships[0].getSignature()));
if(i == ships[0].getLength()-1){
ships[0].setTailX(ships[0].getHeadX() + directionMap[direction][0] * i);
ships[0].setTailY(ships[0].getHeadY() + directionMap[direction][1] * i);
}
}
ships[1].setHeadX(shipPlacement[randomShipPlacement][3]);
ships[1].setHeadY(shipPlacement[randomShipPlacement][4]);
direction = shipPlacement[randomShipPlacement][5];
for(int i = 0; i < ships[1].getLength(); i++){
// System.out.println(s.getHeadX() + directionMap[direction][1] * i + " " + (s.getHeadY() + directionMap[direction][1] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][1] * i, s.getHeadY() + directionMap[direction][1] * i));
myGrid.setGridState(ships[1].getHeadX() + directionMap[direction][0] * i, ships[1].getHeadY() + directionMap[direction][1] * i, Character.toString(ships[1].getSignature()));
if(i == ships[1].getLength()-1){
ships[1].setTailX(ships[1].getHeadX() + directionMap[direction][0] * i);
ships[1].setTailY(ships[1].getHeadY() + directionMap[direction][1] * i);
}
}
ships[2].setHeadX(shipPlacement[randomShipPlacement][6]);
ships[2].setHeadY(shipPlacement[randomShipPlacement][7]);
direction = shipPlacement[randomShipPlacement][8];
for(int i = 0; i < ships[2].getLength(); i++){
// System.out.println(s.getHeadX() + directionMap[direction][2] * i + " " + (s.getHeadY() + directionMap[direction][2] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][2] * i, s.getHeadY() + directionMap[direction][2] * i));
myGrid.setGridState(ships[2].getHeadX() + directionMap[direction][0] * i, ships[2].getHeadY() + directionMap[direction][1] * i, Character.toString(ships[2].getSignature()));
if(i == ships[2].getLength()-1){
ships[2].setTailX(ships[2].getHeadX() + directionMap[direction][0] * i);
ships[2].setTailY(ships[2].getHeadY() + directionMap[direction][1] * i);
}
}
ships[3].setHeadX(shipPlacement[randomShipPlacement][9]);
ships[3].setHeadY(shipPlacement[randomShipPlacement][10]);
direction = shipPlacement[randomShipPlacement][11];
for(int i = 0; i < ships[3].getLength(); i++){
// System.out.println(s.getHeadX() + directionMap[direction][3] * i + " " + (s.getHeadY() + directionMap[direction][3] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][3] * i, s.getHeadY() + directionMap[direction][3] * i));
myGrid.setGridState(ships[3].getHeadX() + directionMap[direction][0] * i, ships[3].getHeadY() + directionMap[direction][1] * i, Character.toString(ships[3].getSignature()));
if(i == ships[3].getLength()-1){
ships[3].setTailX(ships[3].getHeadX() + directionMap[direction][0] * i);
ships[3].setTailY(ships[3].getHeadY() + directionMap[direction][1] * i);
}
}
ships[4].setHeadX(shipPlacement[randomShipPlacement][12]);
ships[4].setHeadY(shipPlacement[randomShipPlacement][13]);
direction = shipPlacement[randomShipPlacement][14];
for(int i = 0; i < ships[4].getLength(); i++){//i is a block variable
// System.out.println(s.getHeadX() + directionMap[direction][4] * i + " " + (s.getHeadY() + directionMap[direction][4] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][4] * i, s.getHeadY() + directionMap[direction][4] * i));
myGrid.setGridState(ships[4].getHeadX() + directionMap[direction][0] * i, ships[4].getHeadY() + directionMap[direction][1] * i, Character.toString(ships[4].getSignature()));
if(i == ships[4].getLength()-1){
ships[4].setTailX(ships[4].getHeadX() + directionMap[direction][0] * i);
ships[4].setTailY(ships[4].getHeadY() + directionMap[direction][1] * i);
}
}
// for(Ship s : ships){
// boolean validPlacement = false;
// int direction = 4;
// int[][] directionMap = {
// {0, 1},
// {1, 0},
// {0, -1},
// {-1, 0}
// };
// while(!validPlacement){
// validPlacement = false;
// s.setHeadX((int)(Math.random()*10));
// s.setHeadY((int)(Math.random()*10));
// direction = (int)(Math.random()*4);
// for(int i = 0; i < s.getLength(); i++){
// int currentX = s.getHeadX() + (directionMap[direction][0] * i);
// int currentY = s.getHeadY() + (directionMap[direction][1] * i);
// if(currentX < 0 || currentX > 9){
// validPlacement = false;
// // System.out.println("Refresh because of X");
// break;
// }else if(currentY < 0 || currentY > 9){
// validPlacement = false;
// // System.out.println("Refresh because of Y");
// break;
// }else if(!myGrid.getGridState(currentX, currentY).equals("N")){
// validPlacement = false;
// // System.out.println("Refresh because occupied");
// // System.out.println(currentX + " " + currentY);
// break;
// }else{
// // System.out.println("Valid placement");
// // System.out.println(currentX + " " + currentY);
// validPlacement = true;
// }
// }
// }
// for(int i = 0; i < s.getLength(); i++){
// // System.out.println(s.getHeadX() + directionMap[direction][0] * i + " " + (s.getHeadY() + directionMap[direction][1] * i) + " " + s.getSignature() + " " + myGrid.getGridState(s.getHeadX() + directionMap[direction][0] * i, s.getHeadY() + directionMap[direction][1] * i));
// myGrid.setGridState(s.getHeadX() + directionMap[direction][0] * i, s.getHeadY() + directionMap[direction][1] * i, Character.toString(s.getSignature()));
// if(i == s.getLength()-1){
// s.setTailX(s.getHeadX() + directionMap[direction][0] * i);
// s.setTailY(s.getHeadY() + directionMap[direction][1] * i);
// }
// }
// }
}
/**Returns the coordinate of a target to be struck.
*
*/
public int[] target(Grid enemyGrid){
//create a new array to store coordinates of the target
int[] targetCoordinates = new int[2];
boolean unfinishedHits = false;
int[] unfinishedHit = {-1, -1};
for(int i = 0; i < 10; i++){//i is a block variable
for(int j = 0; j < 10; j++){//j is a block variable
if(enemyGrid.getGridStatus(i, j) == 2){//if true, there is a unsunk ship that is hit
unfinishedHits = true;
unfinishedHit[0] = i;//sets the y coordinate of the unfinished hit
unfinishedHit[1] = j;//sets the x coordinate of the unfinished hit
}
}
}
System.out.println("unfinished hit at " + unfinishedHit[0] + " " + unfinishedHit[1]);
if(!unfinishedHits){//if true, use heat map strategy
//declare and initialize 2D array for heat map
int[][] heatMap = createHeatMap(enemyGrid);
System.out.println("Search mode");
for(int i = 0; i < 10; i++){//i is a block variable
for(int j = 0; j < 10; j++){//j is a block variable
System.out.print(heatMap[i][j] + " ");
}
System.out.println();
}
//declaring and initializing variables to store the first index of the 2D array as the max
int maxX = 0;
int maxY = 0;
for(int i = 0; i < 10; i++){//i is a block variable
for(int j = 0; j < 10; j++){//j is a block variable
double dice = Math.random();//variable that selects a random number of either 0 or 1
if(dice > 0.5){//if true, then if the coordinate is greater than or equal to the max then save those coordinates
if(heatMap[i][j] >= heatMap[maxX][maxY]){//if true, store coordinate of the new max number
maxX = i;
maxY = j;
}
}else{
if(heatMap[i][j] > heatMap[maxX][maxY]){//if true, store coordinate of the new max number
maxX = i;
maxY = j;
}
}
}
}
//save the coordinate with the highest probability in target coordinate array
targetCoordinates[0] = maxX;
targetCoordinates[1] = maxY;
}else{//if there is a unfinsihed boat, attack in straight line
System.out.println("Destroy mode");
//2D array for set directions: up, right, down, left
int[][] direction = {
{1, 0},
{0, 1},
{-1, 0},
{0, -1}
};
if(checkAdjacentHit(unfinishedHit, enemyGrid)){
System.out.println("adjacent hits");
//declare arrays to be used in the code
int[] adjacentCoordinates = new int[2];
int[] shipDirection = new int[2];
//loop to go through the different directions
for(int i = 0; i < 4; i++){//i is a block variable
if(unfinishedHit[0] + direction[i][0] < 0 || unfinishedHit[0] + direction[i][0] > 9 || unfinishedHit[1] + direction[i][1] < 0 || unfinishedHit[1] + direction[i][1] > 9){
}else if(enemyGrid.getGridStatus(unfinishedHit[0] + direction[i][0], unfinishedHit[1] + direction[i][1]) == 2){//if true, store coordinates for the adjacent hit
adjacentCoordinates[0] = unfinishedHit[0] + direction[i][0];
adjacentCoordinates[1] = unfinishedHit[1] + direction[i][1];
shipDirection[0] = direction[i][0];
shipDirection[1] = direction[i][1];
}
}
if(adjacentCoordinates[0] + shipDirection[0] < 0 || adjacentCoordinates[0] + shipDirection[0] > 9 || adjacentCoordinates[1] + shipDirection[1] < 0 || adjacentCoordinates[1] + shipDirection[1] > 9 || enemyGrid.getGridStatus(adjacentCoordinates[0] + shipDirection[0], adjacentCoordinates[1] + shipDirection[1]) != 0){
int count = 1;
boolean keepGoing = true;
while(enemyGrid.getGridStatus(adjacentCoordinates[0] - shipDirection[0] * count, adjacentCoordinates[1] - shipDirection[1] * count) == 2){// != 0
count++;
keepGoing = true;
if(enemyGrid.getGridStatus(adjacentCoordinates[0] - shipDirection[0] * count, adjacentCoordinates[1] - shipDirection[1] * count) == 1){
keepGoing = false;
}
}
if(keepGoing){
targetCoordinates[0] = adjacentCoordinates[0] - (shipDirection[0] * count);
targetCoordinates[1] = adjacentCoordinates[1] - (shipDirection[1] * count);
}else{
if(adjacentCoordinates[0] - shipDirection[0] < 0 || adjacentCoordinates[0] - shipDirection[0] > 9 || adjacentCoordinates[1] - shipDirection[1] < 0 || adjacentCoordinates[1] - shipDirection[1] > 9 || enemyGrid.getGridStatus(adjacentCoordinates[0] - shipDirection[0], adjacentCoordinates[1] - shipDirection[1]) != 0){
int c = 1;
while(enemyGrid.getGridStatus(adjacentCoordinates[0] + shipDirection[0] * c, adjacentCoordinates[1] + shipDirection[1] * c) == 2){// != 0
c++;
}
targetCoordinates[0] = adjacentCoordinates[0] + (shipDirection[0] * c);
targetCoordinates[1] = adjacentCoordinates[1] + (shipDirection[1] * c);
}else{
targetCoordinates[0] = adjacentCoordinates[0] - shipDirection[0];
targetCoordinates[1] = adjacentCoordinates[1] - shipDirection[1];
}
}
}else{
targetCoordinates[0] = adjacentCoordinates[0] + shipDirection[0];
targetCoordinates[1] = adjacentCoordinates[1] + shipDirection[1];
}
}else{
Ship[] s = enemyGrid.getShips();
int shipType = Character.getNumericValue(enemyGrid.getGridState(unfinishedHit[0], unfinishedHit[1]).charAt(0))-23;
System.out.println(shipType);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
System.out.print(enemyGrid.getGridStatus(i, j));
}
System.out.println();
}
int[][] tempGridStatus = enemyGrid.getEntireGridStatus();
tempGridStatus[unfinishedHit[0]][unfinishedHit[1]] = 0;
int[][] hitHeatmap = createHitHeatMap(unfinishedHit, shipType, tempGridStatus);
tempGridStatus[unfinishedHit[0]][unfinishedHit[1]] = 2;
int maxValue = 0;
int maxX = -1;
int maxY = -1;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
if(i == unfinishedHit[0] && j == unfinishedHit[1]){
continue;
}
System.out.print(hitHeatmap[i][j]);
if(hitHeatmap[i][j] > maxValue){
maxX = i;
maxY = j;
maxValue = hitHeatmap[i][j];
}
}
System.out.println();
}
System.out.println(maxX + " " + maxY);
targetCoordinates[0] = maxX;
targetCoordinates[1] = maxY;
// int maxX = -1;
// int maxY = -1;
// int maxHeat = 0;
// for(int i = 0; i < 4; i++){
// if(unfinishedHit[0] + direction[i][0] < 0 || unfinishedHit[0] + direction[i][0] > 9 || unfinishedHit[1] + direction[i][1] < 0 || unfinishedHit[1] + direction[i][1] > 9){
// }else if(heatMap[unfinishedHit[0] + direction[i][0]][unfinishedHit[1] + direction[i][1]] > maxHeat){
// maxHeat = heatMap[unfinishedHit[0] + direction[i][0]][unfinishedHit[1] + direction[i][1]];
// maxX = unfinishedHit[0] + direction[i][0];
// maxY = unfinishedHit[1] + direction[i][1];
// }
// }
// targetCoordinates[0] = maxX;
// targetCoordinates[1] = maxY;
}
}
return targetCoordinates;
}
/**Creates a 2D heat map that displays the probability of a ship in each grid
*
* @param enemyGrid The grid that the method will create a heat map of
* @return a 2D array of integer number that represents the probability of a ship being in each coordinate
*/
public static int [][] createHeatMap(Grid enemyGrid){
//declare variables to be used
Ship[] ship = enemyGrid.getShips();
boolean destroyer = ship[0].getAlive();
boolean submarine = ship[1].getAlive();
boolean cruiser = ship[2].getAlive();
boolean battleship = ship[3].getAlive();
boolean carrier = ship[4].getAlive();
int[][] guesses = enemyGrid.getEntireGridStatus();
int [][] grid = new int[10][10];
//check what boats are remaining
if (destroyer==true){//if destroyer is alive, then add up probability
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid.length - 1; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row,col+1, guesses) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates
grid[row][col] += 1;
grid[row][col+1] += 1;
}
}
}
for (int row = 0; row < grid.length - 1; row++){//row is a block variable
for ( int col = 0; col < grid.length; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row+1,col, guesses) == true){//does the same but in vertical direction
//increment both coordinates
grid[row][col] += 1;
grid[row+1][col] += 1;
}
}
}
}
if (submarine==true){//if submarine is alive, then add up probability
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid.length - 2; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row,col+1, guesses) == true && emptySquare(row,col+2, guesses) == true){//if there are three adjacent empty squares in the horizontal direction
//increment all coordinates
grid[row][col] += 1;
grid[row][col+1] += 1;
grid[row][col+2] += 1;
}
}
}
for (int row = 0; row < grid.length - 2; row++){//row is a block variable
for ( int col = 0; col < grid.length; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row+1,col, guesses) == true && emptySquare(row+2,col, guesses) == true){//does the same but in vertical direction
//increment all coordinates
grid[row][col] += 1;
grid[row+1][col] += 1;
grid[row+2][col] += 1;
}
}
}
}
if (cruiser==true){//if cruiser is alive, then add up probability
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid.length - 2; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row,col+1, guesses) == true && emptySquare(row,col+2, guesses) == true){//if there are three adjacent empty squares in the horizontal direction
//increment all coordinates
grid[row][col] += 1;
grid[row][col+1] += 1;
grid[row][col+2] += 1;
}
}
}
for (int row = 0; row < grid.length - 2; row++){//row is a block variable
for ( int col = 0; col < grid.length; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row+1,col, guesses) == true && emptySquare(row+2,col, guesses) == true){//does the same but in vertical direction
//increment all coordinates
grid[row][col] += 1;
grid[row+1][col] += 1;
grid[row+2][col] += 1;
}
}
}
}
if (battleship==true){//if battleship is alive, then add up probability
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid.length - 3; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row,col+1, guesses) == true && emptySquare(row,col+2, guesses) == true && emptySquare(row,col+3, guesses) == true){//if there are four adjacent empty squares in the horizontal direction
//increment all coordinates
grid[row][col] += 1;
grid[row][col+1] += 1;
grid[row][col+2] += 1;
grid[row][col+3] += 1;
}
}
}
for (int row = 0; row < grid.length - 3; row++){//row is a block variable
for ( int col = 0; col < grid.length; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row+1,col, guesses) == true && emptySquare(row+2,col, guesses) == true && emptySquare(row+3,col, guesses) == true){//does the same but in vertical direction
//increment all coordinates
grid[row][col] += 1;
grid[row+1][col] += 1;
grid[row+2][col] += 1;
grid[row+3][col] += 1;
}
}
}
}
if (carrier==true){//if carrier is alive, then add up probability
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid.length - 4; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row,col+1, guesses) == true && emptySquare(row,col+2, guesses) == true && emptySquare(row,col+3, guesses) == true && emptySquare(row,col+4, guesses) == true){//if there are five adjacent empty squares in the horizontal direction
//increment all coordinates
grid[row][col] += 1;
grid[row][col+1] += 1;
grid[row][col+2] += 1;
grid[row][col+3] += 1;
grid[row][col+4] += 1;
}
}
}
for (int row = 0; row < grid.length - 4; row++){//row is a block variable
for ( int col = 0; col < grid.length; col++){//col is a block variable
if(emptySquare(row,col,guesses) == true && emptySquare(row+1,col, guesses) == true && emptySquare(row+2,col, guesses) == true && emptySquare(row+3,col, guesses) == true && emptySquare(row+4,col, guesses) == true){//does the same but in vertical direction
//increment all coordinates
grid[row][col] += 1;
grid[row+1][col] += 1;
grid[row+2][col] += 1;
grid[row+3][col] += 1;
grid[row+4][col] += 1;
}
}
}
}
return grid;
}
/**Create a new heat map of the possibilities around a hit ship
*
* @param coordOfHit coordinate of hit
* @param shipType type of ship
* @param enemyGrid the grid that the method will create a heat map of
* @return a 2D array of the probabilities
*/
public static int[][]createHitHeatMap(int [] coordOfHit, int shipType, int[][] enemyGrid){
//declare and initialize variables to be used
boolean destroyer = shipType==0;
boolean submarine = shipType==1;
boolean cruiser = shipType==2;
boolean battleship = shipType==3;
boolean carrier = shipType==4;
int [][] grid = new int[10][10];
//set all coordinates in the grid to 0
for (int row = 0; row < grid.length; row++){//row is a block variable
for (int col = 0; col < grid[row].length; col++){//col is a block variable
grid[row][col]=0;
}
}
//create heat map depending on boat
if (destroyer==true){//if destroyer is hit, then add up probability for the squares around it
if(coordOfHit[1]+1 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
}
if(coordOfHit[1]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
}
//add probability vertically
if(coordOfHit[0]+1 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
}
if(coordOfHit[0]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
}
}
if (submarine==true){//if submarine is hit, then add up probability for the squares around it
if(coordOfHit[1]+2 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
}
if(coordOfHit[1]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
}
if(coordOfHit[1]+1 <= 10 && coordOfHit[1]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
}
//add probability vertically
if(coordOfHit[0]+2 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+1 <= 10 && coordOfHit[0]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
}
}
if (cruiser==true){//if cruiser is hit, then add up probability for the squares around it
if(coordOfHit[1]+2 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
}
if(coordOfHit[1]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
}
if(coordOfHit[1]+1 <= 10 && coordOfHit[1]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
}
//add probability vertically
if(coordOfHit[0]+2 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+1 <= 10 && coordOfHit[0]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
}
}
if (battleship==true){//if battleship is hit, then add up probability for the squares around it
if(coordOfHit[1]+3 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+3, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
grid[coordOfHit[0]][coordOfHit[1]+3] += 1;
}
if(coordOfHit[1]-3 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-3, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
grid[coordOfHit[0]][coordOfHit[1]-3] += 1;
}
if(coordOfHit[1]+2 <= 10 && coordOfHit[1]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
}
if(coordOfHit[1]+1 <= 10 && coordOfHit[1]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
}
//add probability vertically
if(coordOfHit[0]+3 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+3,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
grid[coordOfHit[0]+3][coordOfHit[1]] += 1;
}
if(coordOfHit[0]-3 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-3,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
grid[coordOfHit[0]-3][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+1 <= 10 && coordOfHit[0]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+2 <= 10 && coordOfHit[0]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
}
}
if (carrier==true){//if carrier is hit, then add up probability for the squares around it
if(coordOfHit[1]+4 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+3, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+4, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
grid[coordOfHit[0]][coordOfHit[1]+3] += 1;
grid[coordOfHit[0]][coordOfHit[1]+4] += 1;
}
if(coordOfHit[1]-4 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-3, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-4, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
grid[coordOfHit[0]][coordOfHit[1]-3] += 1;
grid[coordOfHit[0]][coordOfHit[1]-4] += 1;
}
if(coordOfHit[1]+2 <= 10 && coordOfHit[1]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
}
if(coordOfHit[1]+1 <= 10 && coordOfHit[1]-3 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-3, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
grid[coordOfHit[0]][coordOfHit[1]-2] += 1;
grid[coordOfHit[0]][coordOfHit[1]-3] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
}
if(coordOfHit[1]+3 <= 10 && coordOfHit[1]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+1, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+2, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]+3, enemyGrid) == true && emptySquare(coordOfHit[0],coordOfHit[1]-1, enemyGrid) == true){//if there are two adjacent empty squares in the horizontal direction
//increment both coordinates (Horizontal)
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]][coordOfHit[1]+1] += 1;
grid[coordOfHit[0]][coordOfHit[1]+2] += 1;
grid[coordOfHit[0]][coordOfHit[1]+3] += 1;
grid[coordOfHit[0]][coordOfHit[1]-1] += 1;
}
//add probability vertically
if(coordOfHit[0]+4 <= 10 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+3,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+4,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
grid[coordOfHit[0]+3][coordOfHit[1]] += 1;
grid[coordOfHit[0]+4][coordOfHit[1]] += 1;
}
if(coordOfHit[0]-4 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-3,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-4,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
grid[coordOfHit[0]-3][coordOfHit[1]] += 1;
grid[coordOfHit[0]-4][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+2 <= 10 && coordOfHit[0]-2 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+3 <= 10 && coordOfHit[0]-1 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+3,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
grid[coordOfHit[0]+2][coordOfHit[1]] += 1;
grid[coordOfHit[0]+3][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
}
if(coordOfHit[0]+1 <= 10 && coordOfHit[0]-3 >= 0 && emptySquare(coordOfHit[0],coordOfHit[1],enemyGrid) == true && emptySquare(coordOfHit[0]-1,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-2,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]-3,coordOfHit[1], enemyGrid) == true && emptySquare(coordOfHit[0]+1,coordOfHit[1], enemyGrid) == true){//does the same but in vertical direction
//increment both coordinates
grid[coordOfHit[0]][coordOfHit[1]] += 1;
grid[coordOfHit[0]-1][coordOfHit[1]] += 1;
grid[coordOfHit[0]-2][coordOfHit[1]] += 1;
grid[coordOfHit[0]-3][coordOfHit[1]] += 1;
grid[coordOfHit[0]+1][coordOfHit[1]] += 1;
}
}
return grid;
}
/**Checks if the coordinates enterered is unknown
*
* @param grid the grid that you are attacking and is used to check coordinate
* @param row the row of the coordinate being checked
* @param col the column of the coordinate being checked
* @return whether the coordinate entered is unknown
*/
public static boolean emptySquare(int row, int col, int [][]grid) {
try{
if(grid[row][col]==0) {//if true, coordinate on the grid is empty
return true;
}
else {
return false;
}
}catch(Exception e){
return false;
}
}
/**checks if there is hits adjacent to each other
*
* @param enemyGrid the grid used ot check for adjacent hits
* @param coordOfHit the coordinate of the hit
* @return whether there is a hit adjacent to each other
*/
public static boolean checkAdjacentHit(int[]coordOfHit, Grid enemyGrid) {
//declaring and initializing 2D array to enemy's grid
int[][]guesses = enemyGrid.getEntireGridStatus();
boolean adjacent=false;
if(coordOfHit[0]-1 >= 0 && guesses[coordOfHit[0]][coordOfHit[1]]==2 && guesses[coordOfHit[0]-1][coordOfHit[1]]==2 && myGrid.getGridState(coordOfHit[0], coordOfHit[1]).equals(myGrid.getGridState(coordOfHit[0] - 1, coordOfHit[1]))) {//if true, there is an adjacent hit above original hit
adjacent=true;
}
else if(coordOfHit[1]+1 < 10 && guesses[coordOfHit[0]][coordOfHit[1]]==2 && guesses[coordOfHit[0]][coordOfHit[1]+1]==2 && myGrid.getGridState(coordOfHit[0], coordOfHit[1]).equals(myGrid.getGridState(coordOfHit[0], coordOfHit[1] + 1))) {//if true, there is an adjacent hit to the right of original hit
adjacent=true;
}
else if(coordOfHit[0]+1 < 10 && guesses[coordOfHit[0]][coordOfHit[1]]==2 && guesses[coordOfHit[0]+1][coordOfHit[1]]==2 && myGrid.getGridState(coordOfHit[0], coordOfHit[1]).equals(myGrid.getGridState(coordOfHit[0] + 1, coordOfHit[1]))) {//if true, there is an adjacent hit below original hit
adjacent=true;
}
else if(coordOfHit[1]-1 >= 0 && guesses[coordOfHit[0]][coordOfHit[1]]==2 && guesses[coordOfHit[0]][coordOfHit[1]-1]==2 && myGrid.getGridState(coordOfHit[0], coordOfHit[1]).equals(myGrid.getGridState(coordOfHit[0], coordOfHit[1] - 1))) {//if true, there is an adjacent hit to the left of original hit
adjacent=true;
}
return adjacent;
}
/**returns the grid
*
*/
public Grid getGrid(){
return myGrid;
}
/**returns whether it is AI or not
*
*/
public boolean isAI(){
return true;
}
}