-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleshipManager.java
More file actions
919 lines (759 loc) · 29.9 KB
/
BattleshipManager.java
File metadata and controls
919 lines (759 loc) · 29.9 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
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/**
* BattleshipManager.java 5/17/2017
*
* Rahul Kumar
* AP Computer Science
* Mrs. Hitch 3rd Period
*
*
*/
//creates Battleship Game using other classes
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class BattleshipManager extends JFrame implements ActionListener
{
//private instance variables
private ArrayList<Ship> playergrid;
private ArrayList<Ship> AIGrid;
private int[][] placement;
private int[][] AIboard;
private JLabel banner;
private JLabel background;
private Container contents;
private JTextArea textArea;
private JScrollPane scrollpane;
private JButton randomizebutton;
private JButton resetButton;
private JPanel AIpanel;
private JPanel Playerpanel;
private ImageIcon HitBattleship = new ImageIcon("HitBattleship.png");
private ImageIcon HitCarrier = new ImageIcon("HitCarrier.png");
private ImageIcon HitCruiser = new ImageIcon("HitCruiser.png");
private ImageIcon HitSub = new ImageIcon("HitSub.png");
private ImageIcon HitDestroyer = new ImageIcon("HitDestroyer.png");
private ImageIcon Splash = new ImageIcon("redX.png");
private ImageIcon water = new ImageIcon("water.gif");
private JButton[][] squares;
private JButton[][] enemysquares;
//Ships
private Ship PlayerBattleship;
private Ship PlayerCarrier;
private Ship PlayerCruiser;
private Ship PlayerDestroyer;
private Ship PlayerSubmarine;
private Ship AIBattleship;
private Ship AICarrier;
private Ship AICruiser;
private Ship AIDestroyer;
private Ship AISubmarine;
//Image Labels
private JLabel EnemyCarrierLabel;
private JLabel EnemyDestroyerLabel;
private JLabel EnemyCruiserLabel;
private JLabel EnemySubmarineLabel;
private JLabel EnemyBattleshipLabel;
private JLabel PlayerCarrierLabel;
private JLabel PlayerDestroyerLabel;
private JLabel PlayerCruiserLabel;
private JLabel PlayerSubmarineLabel;
private JLabel PlayerBattleshipLabel;
private JLabel HasSubmarine;
private JLabel HasCarrier;
private JLabel HasCruiser;
private JLabel HasBattleship;
private JLabel HasDestroyer;
private JLabel AIHasSubmarine;
private JLabel AIHasCarrier;
private JLabel AIHasCruiser;
private JLabel AIHasBattleship;
private JLabel AIHasDestroyer;
private boolean gameover = false;
//constructer
public BattleshipManager()
{
playergrid = new ArrayList<Ship>(); // initilizes grids and boards
AIGrid = new ArrayList<Ship>();
placement = new int[8][8];
AIboard = new int[8][8];
//Player ships initilized
PlayerBattleship = new Battleship();
playergrid.add(PlayerBattleship);
PlayerCarrier = new Carrier();
playergrid.add(PlayerCarrier);
PlayerCruiser = new Cruiser();
playergrid.add(PlayerCruiser);
PlayerDestroyer = new Destroyer();
playergrid.add(PlayerDestroyer);
PlayerSubmarine = new Submarine();
playergrid.add(PlayerSubmarine);
//sets board to a default of -1
for(int row = 0; row < placement.length; row++){
for(int col = 0; col < placement[row].length; col++)
{
placement[row][col] = -1;
}
}
//Goes through every Ship in the playergrid, and initlizes them on the playerboard(Polymorphism)
for(Ship e: playergrid)
{
e.init(placement);
}
//Prints board in console(Was used for debugging)
for(int row = 0; row < placement.length; row++){
for(int col = 0; col < placement[row].length; col++)
{
System.out.print(placement[row][col] + " ");
}
System.out.println();
}
//AI Ships initilized
AIBattleship = new Battleship();
AIGrid.add(AIBattleship);
AICarrier = new Carrier();
AIGrid.add(AICarrier);
AICruiser = new Cruiser();
AIGrid.add(AICruiser);
AIDestroyer = new Destroyer();
AIGrid.add(AIDestroyer);
AISubmarine = new Submarine();
AIGrid.add(AISubmarine);
//sets AIboard to a default of -1
for(int row = 0; row < AIboard.length; row++){
for(int col = 0; col < AIboard[row].length; col++)
{
AIboard[row][col] = -1;
}
}
for(Ship e: AIGrid) //Goes through every Ship in the playergrid, and initlizes them on the AIboard(Polymorphism)
{
e.init(AIboard);
}
//Creating the title image
ImageIcon image = new ImageIcon("battleship-banner.jpg");
banner = new JLabel(image);
banner.setLocation(-12,0);
banner.setSize(710, 125);
getContentPane().add(banner);
//GUI Frame
setLayout(null);
setSize(715, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Playerpanel = new JPanel();
Playerpanel.setLayout(new GridLayout(8,8));
Playerpanel.setLocation(25,150);
Playerpanel.setSize(300,300);
add(Playerpanel);
//initlizes the 2dmatrix of buttons for both the player and the AI
squares = new JButton[8][8];
enemysquares = new JButton[8][8];
ButtonHandler buttonHandler = new ButtonHandler();
//filling the button array with buttons with the default image of "water"
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
JButton button = new JButton();
button.setSize(5,5);
button.setIcon(water);
squares[i][j] = button;
Playerpanel.add(squares[i][j]);
squares[i][j].addActionListener(buttonHandler);
}
}
AIpanel = new JPanel();
AIpanel.setLayout(new GridLayout(8,8));
AIpanel.setLocation(372,150);
AIpanel.setSize(300,300);
add(AIpanel);
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
JButton button = new JButton();
button.setSize(5,5);
button.setIcon(water);
enemysquares[i][j] = button;
AIpanel.add(enemysquares[i][j]);
}
}
//Setting icons for each ship on the player side so they can see which ships are on their side
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
if(placement[i][j] == 2)
enemysquares[i][j].setIcon(HitDestroyer);
if(placement[i][j] == 3)
enemysquares[i][j].setIcon(HitCruiser);
if(placement[i][j] == 4)
enemysquares[i][j].setIcon(HitBattleship);
if(placement[i][j] == 5)
enemysquares[i][j].setIcon(HitCarrier);
if(placement[i][j] == 6)
enemysquares[i][j].setIcon(HitSub);
}
}
//Static images
ImageIcon bg = new ImageIcon("background.jpg");
background = new JLabel(bg);
background.setLocation(-100,2);
background.setSize(900, 785);
getContentPane().setBackground(Color.black);
//background.setVisible(false);
HasSubmarine = new JLabel(HitSub);
HasSubmarine.setLocation(25,463);
HasSubmarine.setSize(35,35);
getContentPane().add(HasSubmarine);
HasBattleship = new JLabel(HitBattleship);
HasBattleship.setLocation(75,463);
HasBattleship.setSize(35,35);
getContentPane().add(HasBattleship);
HasDestroyer = new JLabel(HitDestroyer);
HasDestroyer.setLocation(125,463);
HasDestroyer.setSize(35,35);
getContentPane().add(HasDestroyer);
HasCruiser = new JLabel(HitCruiser);
HasCruiser.setLocation(175,463);
HasCruiser.setSize(35,35);
getContentPane().add(HasCruiser);
HasCarrier = new JLabel(HitCarrier);
HasCarrier.setLocation(225,463);
HasCarrier.setSize(35,35);
getContentPane().add(HasCarrier);
AIHasSubmarine = new JLabel(HitSub);
AIHasSubmarine.setLocation(437,463);
AIHasSubmarine.setSize(35,35);
getContentPane().add(AIHasSubmarine);
AIHasBattleship = new JLabel(HitBattleship);
AIHasBattleship.setLocation(487,463);
AIHasBattleship.setSize(35,35);
getContentPane().add(AIHasBattleship);
AIHasDestroyer = new JLabel(HitDestroyer);
AIHasDestroyer.setLocation(537,463);
AIHasDestroyer.setSize(35,35);
getContentPane().add(AIHasDestroyer);
AIHasCruiser = new JLabel(HitCruiser);
AIHasCruiser.setLocation(587,463);
AIHasCruiser.setSize(35,35);
getContentPane().add(AIHasCruiser);
AIHasCarrier = new JLabel(HitCarrier);
AIHasCarrier.setLocation(637,463);
AIHasCarrier.setSize(35,35);
getContentPane().add(AIHasCarrier);
//Setting text area with the Documentation or default text
textArea = new JTextArea();
textArea.setFont(new Font("consolas", Font.PLAIN, 16));
textArea.setEditable(false); // cannot type text into textArea
textArea.setLineWrap(true); // wrap text when reach right border
textArea.setText("Welcome To Battleship!\n---------------------\n" +
"Battleship is a game between the Player(You) and the AI. Your board\n" +
"is on the right, and the enemy's board is on the left. Both you and\n" +
"the Computer will start off with 5 ships:\n\nList of Ships\n----------------\n" +
"1 Battleship length of 4 grid places and is represented by a box\n" +
"with a blue outline and black dot.\n" +
"1 Carrier length of 5 grid places and is represented by a box\n" +
"with a red outline and black dot.\n"+
"1 Submarine length of 3 grid places and is represented by a box\n" +
"with a yellow outline and black dot.\n" +
"1 Cruiser length of 3 grid places and is represented by a box\n" +
"with a green outline and black dot.\n" +
"1 Destroyer length of 2 grid places and is represented by a box\n" +
"with a orange outline and black dot.\n\n" +
"The Icons you see under the grid is the ships left to sink. If a ship\n" +
"such as a submarine sinks, the icon associated with the submarine\n" +
"will disappear. You and the enemy have this.\n\n" +
"Randomize Board(button) - This button will randomize the placement\n" +
"of your 5 ships. The moment you click to fire your projectile on the\n" +
"enemy board,you will no longer have access to this button.\n\n" +
"Rules- You start off. You may click anywhere to fire your projectile on the enemy board. After you fire your projectile, the computer will"
+ "fire a projectile on your board. If you hit a ship, the Icon\n" +
"associated with that ship will pop up(Example: If you hit an enemy \n" +
"Submarine, a yellow outlined box with a black dot in the middle will appear at that position). If the computer hits one of your ship, a\n" +
"explosion icon will appear at that position. Whoever sinks all the opposing sides ships, wins.\n\n" +
"CONSOLE \n---------\n\n");
//buttons
randomizebutton = new JButton("Randomize Board"); // randomizes ships positions on the playerboard
randomizebutton.setLocation(272,465);
randomizebutton.setSize(150,30);
resetButton = new JButton("Reset Game"); // resets the game
resetButton.setLocation(272,465);
resetButton.setSize(150,30);
getContentPane().add(randomizebutton); //adds buttons to panel
getContentPane().add(resetButton);
randomizebutton.addActionListener(this);
randomizebutton.setVisible(false);
resetButton.addActionListener(this);
resetButton.setVisible(false);
//sets everything on the Panel
scrollpane = new JScrollPane(textArea);
scrollpane.setLocation(23,510);
scrollpane.setSize(650, 125);
scrollpane.setBorder(BorderFactory.createLineBorder (Color.black, 2));
getContentPane().add(scrollpane);
getContentPane().add(background);
background.setVisible(false);
scrollpane.setVisible(true);
randomizebutton.setVisible(true);
background.setVisible(true);
setVisible(true);
}
//returns if the projectile fired had a ship
public boolean isHit(int row, int col)
{
if(AIboard[row][col] != -1)
return true; //had a ship
else
return false; // did not have a ship
}
//returns which kind of ship on the position
public int typeOfHit(int row, int col)
{
if(AIboard[row][col] == 2)
return 2;
if(AIboard[row][col] == 3)
return 3;
if(AIboard[row][col] == 4)
return 4;
if(AIboard[row][col] == 5)
return 5;
if(AIboard[row][col] == 6)
return 6;
else
return 0;
}
//checks if all ships return true to the method call "hasSunk"
public boolean checkIfEitherSideWon(ArrayList<Ship> parameter)
{
int numberofsunkships = 0;
for(int i = 0; i < parameter.size(); i++)
{
if(parameter.get(i).hasSank())
numberofsunkships++;
}
if(numberofsunkships == 5)
return true;
else
return false;
}
//finds a random position to fire at to the player board and loops till the position has not been shot at
public void AIAttack()
{
int randomcol = 0;
int randomrow = 0;
boolean condition = false;
ImageIcon enemyHit = new ImageIcon("EnemyHit.jpg");
while(!(condition || gameover)) // if the game is not over or the conditon(Attack was not executed before) is not met
{
randomcol = (int)(Math.random() * AIboard[0].length);
randomrow = (int)(Math.random() * AIboard.length);
//If AI Missed
if(placement[randomrow][randomcol] == -1 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(Splash);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI missed\n\n");
condition = true;
}
//if AI hit a Submarine
else if(placement[randomrow][randomcol] == 6 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(enemyHit);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI hit a Submarine!\n\n");
PlayerSubmarine.incrementHits();
if(PlayerSubmarine.hasSank()) //if the hit executed sank the player's Submarine
{
textArea.append("AI sunk your Submarine!\n");
AIHasSubmarine.setVisible(false);
if(checkIfEitherSideWon(playergrid)) // if the game is won
{
gameover = true;
textArea.append("GAME OVER. AI Won!");
resetButton.setVisible(true);
}
}
condition = true;
}
//if AI hit a Carrier
else if(placement[randomrow][randomcol] == 5 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(enemyHit);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI hit a Carrier!\n\n");
PlayerCarrier.incrementHits();
if(PlayerCarrier.hasSank()) //if the hit executed sank the player's Carrier
{
textArea.append("AI sunk your Carrier!\n");
AIHasCarrier.setVisible(false);
if(checkIfEitherSideWon(playergrid))
{
gameover = true;
textArea.append("GAME OVER. AI Won!");
resetButton.setVisible(true);
}
}
condition = true;
}
//if AI hits a battleship
else if(placement[randomrow][randomcol] == 4 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(enemyHit);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI hit a Battleship!\n\n");
PlayerBattleship.incrementHits();
if(PlayerBattleship.hasSank()) //if the hit executed sank the player's Battleship
{
textArea.append("AI sunk your Battleship!\n");
AIHasBattleship.setVisible(false);
if(checkIfEitherSideWon(playergrid))
{
gameover = true;
textArea.append("GAME OVER. AI Won!");
resetButton.setVisible(true);
}
}
condition = true;
}
//if AI hits a Cruiser
else if(placement[randomrow][randomcol] == 3 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(enemyHit);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI hit a Cruiser!\n\n");
PlayerCruiser.incrementHits();
if(PlayerCruiser.hasSank()) //if the hit executed sank the player's Cruiser
{
textArea.append("AI sunk your Cruiser!\n");
AIHasCruiser.setVisible(false);
if(checkIfEitherSideWon(playergrid))
{
gameover = true;
textArea.append("GAME OVER. AI Won!");
resetButton.setVisible(true);
}
}
condition = true;
} //if AI Hits a Destroyer
else if(placement[randomrow][randomcol] == 2 && enemysquares[randomrow][randomcol] != null)
{
enemysquares[randomrow][randomcol].setIcon(enemyHit);
textArea.append("AI fired a projectile at (" + randomrow + "," + randomcol+ ")" + "\t\tAI hit a Destroyer!\n\n");
PlayerDestroyer.incrementHits();
if(PlayerDestroyer.hasSank()) //if the hit executed sank the player's Destroyer
{
textArea.append("AI sunk your Destroyer!\n");
AIHasDestroyer.setVisible(false);
if(checkIfEitherSideWon(playergrid))
{
gameover = true;
textArea.append("GAME OVER. AI Won!");
resetButton.setVisible(true);
}
}
condition = true;
}
}
enemysquares[randomrow][randomcol] = null;
textArea.setCaretPosition(textArea.getDocument().getLength());
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == randomizebutton) //if the button press associates to the "randomizebutton"
{
//sets the board to -1
for(int row = 0; row < placement.length; row++){
for(int col = 0; col < placement[row].length; col++)
{
placement[row][col] = -1;
}
}
//reints
for(Ship e: playergrid)
{
e.init(placement);
}
//sets all to the default icon "water"
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
enemysquares[i][j].setIcon(water);
}
}
//sets the icon assoicated with each Ship with the new, reintilized board
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
if(placement[i][j] == 2)
enemysquares[i][j].setIcon(HitDestroyer);
if(placement[i][j] == 3)
enemysquares[i][j].setIcon(HitCruiser);
if(placement[i][j] == 4)
enemysquares[i][j].setIcon(HitBattleship);
if(placement[i][j] == 5)
enemysquares[i][j].setIcon(HitCarrier);
if(placement[i][j] == 6)
enemysquares[i][j].setIcon(HitSub);
}
}
}
if(event.getSource() == resetButton) //if button press associates with "resetButton"
{
gameover = false;
//sets text to the default text
textArea.setText("");
textArea.setText("Welcome To Battleship!\n---------------------\n" +
"Battleship is a game between the Player(You) and the AI. Your board\n" +
"is on the right, and the enemy's board is on the left. Both you and\n" +
"the Computer will start off with 5 ships:\n\nList of Ships\n----------------\n" +
"1 Battleship length of 4 grid places and is represented by a box\n" +
"with a blue outline and black dot.\n" +
"1 Carrier length of 5 grid places and is represented by a box\n" +
"with a red outline and black dot.\n"+
"1 Submarine length of 3 grid places and is represented by a box\n" +
"with a yellow outline and black dot.\n" +
"1 Cruiser length of 3 grid places and is represented by a box\n" +
"with a green outline and black dot.\n" +
"1 Destroyer length of 2 grid places and is represented by a box\n" +
"with a orange outline and black dot.\n\n" +
"The Icons you see under the grid is the ships left to sink. If a ship\n" +
"such as a submarine sinks, the icon associated with the submarine\n" +
"will disappear. You and the enemy have this.\n\n" +
"Randomize Board(button) - This button will randomize the placement\n" +
"of your 5 ships. The moment you click to fire your projectile on the\n" +
"enemy board,you will no longer have access to this button.\n\n" +
"Rules- You start off. You may click anywhere to fire your projectile on the enemy board. After you fire your projectile, the computer will"
+ "fire a projectile on your board. If you hit a ship, the Icon\n" +
"associated with that ship will pop up(Example: If you hit an enemy \n" +
"Submarine, a yellow outlined box with a black dot in the middle will appear at that position). If the computer hits one of your ship, a\n" +
"explosion icon will appear at that position. Whoever sinks all the opposing sides ships, wins.\n\n" +
"CONSOLE \n---------\n\n");
//sets the board to -1
for(int row = 0; row < placement.length; row++){
for(int col = 0; col < placement[row].length; col++)
{
placement[row][col] = -1;
}
}
//resets the enemy;s board to -1
for(int row = 0; row < AIboard.length; row++){
for(int col = 0; col < AIboard[row].length; col++)
{
AIboard[row][col] = -1;
}
}
//reinits both grids and ships
for(Ship e: playergrid)
{
e.init(placement);
}
for(Ship e: AIGrid)
{
e.init(AIboard);
}
//calls the resetShip method for all ships
for(Ship e: playergrid)
{
e.resetShip();
}
for(Ship e: AIGrid)
{
e.resetShip();
}
//creates new panels and resets them
remove(background);
remove(Playerpanel);
Playerpanel = new JPanel();
Playerpanel.setLayout(new GridLayout(8,8));
Playerpanel.setLocation(25,150);
Playerpanel.setSize(300,300);
add(Playerpanel);
Playerpanel.setVisible(false);
//reinits the 2d matrixes
squares = new JButton[8][8];
enemysquares = new JButton[8][8];
ButtonHandler buttonHandler = new ButtonHandler();
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
JButton button = new JButton();
button.setSize(5,5);
button.setIcon(water);
squares[i][j] = button;
Playerpanel.add(squares[i][j]);
squares[i][j].addActionListener(buttonHandler);
}
}
remove(AIpanel);
AIpanel = new JPanel();
AIpanel.setLayout(new GridLayout(8,8));
AIpanel.setLocation(372,150);
AIpanel.setSize(300,300);
add(AIpanel);
AIpanel.setVisible(false);
add(background);
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
JButton button = new JButton();
button.setSize(5,5);
button.setIcon(water);
enemysquares[i][j] = button;
AIpanel.add(enemysquares[i][j]);
}
}
for(int i=0; i <8; i++){
for(int j=0; j < 8; j++){
if(placement[i][j] == 2)
enemysquares[i][j].setIcon(HitDestroyer);
if(placement[i][j] == 3)
enemysquares[i][j].setIcon(HitCruiser);
if(placement[i][j] == 4)
enemysquares[i][j].setIcon(HitBattleship);
if(placement[i][j] == 5)
enemysquares[i][j].setIcon(HitCarrier);
if(placement[i][j] == 6)
enemysquares[i][j].setIcon(HitSub);
}
}
//shows all the labels needed for restarting the game
resetButton.setVisible(false); // hides resetButton
randomizebutton.setVisible(true);
Playerpanel.setVisible(true);
AIpanel.setVisible(true);
HasSubmarine.setVisible(true);
HasDestroyer.setVisible(true);
HasBattleship.setVisible(true);
HasCarrier.setVisible(true);
HasCruiser.setVisible(true);
AIHasSubmarine.setVisible(true);
AIHasDestroyer.setVisible(true);
AIHasBattleship.setVisible(true);
AIHasCarrier.setVisible(true);
AIHasCruiser.setVisible(true);
background.setVisible(true);
setVisible(true);
}
}
//if a button on the Panel has been pressed
public class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
randomizebutton.setVisible(false);
//if the game is not over
if(!gameover)
{
for(int i=0; i < 8; i++){
for(int j=0; j < 8; j++){
if( source == squares[i][j]){
if(isHit(i,j) && squares[i][j] != null && !gameover) //if position assoicated with the button contains a ship
{
int hit = typeOfHit(i,j);
if(hit == 2)
{
squares[i][j].setIcon(HitDestroyer);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou hit a Destroyer!\n");
AIDestroyer.incrementHits();
if(AIDestroyer.hasSank()) // if player sank the enemy's Destroyer
{
textArea.append("You sunk the AI Destroyer!\n");
HasDestroyer.setVisible(false);
if(checkIfEitherSideWon(AIGrid)) //if game is won
{
gameover = true;
textArea.append("GAME OVER. You Won!");
resetButton.setVisible(true);
}
}
}
if(hit == 3)
{
squares[i][j].setIcon(HitCruiser);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou hit a Cruiser!\n");
AICruiser.incrementHits();
if(AICruiser.hasSank()) // if player sank the enemy's Cruiser
{
textArea.append("You sunk the AI Cruiser!\n");
HasCruiser.setVisible(false);
if(checkIfEitherSideWon(AIGrid)) //if game is won
{
gameover = true;
textArea.append("GAME OVER. You Won!");
resetButton.setVisible(true);
}
}
}
if(hit == 4)
{
squares[i][j].setIcon(HitBattleship);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou hit a Battleship!\n");
AIBattleship.incrementHits();
if(AIBattleship.hasSank()) // if player sank the enemy's Battleship
{
textArea.append("You sunk the AI Battleship!\n");
HasBattleship.setVisible(false);
if(checkIfEitherSideWon(AIGrid)) //if game is won
{
gameover = true;
textArea.append("GAME OVER. You Won!");
resetButton.setVisible(true);
}
}
}
if(hit == 5)
{
squares[i][j].setIcon(HitCarrier);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou hit a Carrier!\n");
AICarrier.incrementHits();
if(AICarrier.hasSank()) // if player sank the enemy's Carrier
{
textArea.append("You sunk the AI Carrier!\n");
HasCarrier.setVisible(false);
if(checkIfEitherSideWon(AIGrid)) //if game is won
{
gameover = true;
textArea.append("GAME OVER. You Won!");
resetButton.setVisible(true);
}
}
}
if(hit == 6)
{
squares[i][j].setIcon(HitSub);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou hit a Submarine!\n");
AISubmarine.incrementHits();
if(AISubmarine.hasSank())
{
textArea.append("You sunk the AI Submarine!\n"); // if player sank the enemy's Submarine
HasSubmarine.setVisible(false);
if(checkIfEitherSideWon(AIGrid)) //if game is won
{
gameover = true;
textArea.append("GAME OVER. You Won!");
resetButton.setVisible(true);
}
}
}
try
{
AIAttack(); // shoots a projectile on the player board
}
catch(NullPointerException m) //the AiAttack sometimes return null for the button it tries, but it doesn't matter
{
System.out.println("null, but system handled it");
}
squares[i][j] = null;
}
else if(squares[i][j] != null && !gameover) //if the button or gird has a -1 and does not contain a ship
{
squares[i][j].setIcon(Splash);
textArea.append("You fired a projectile at (" + i + "," + j + ")" + "\t\tYou Missed\n");
try
{
AIAttack(); // shoots a projectile on the player board
}
catch(NullPointerException m) //the AiAttack sometimes return null for the button it tries, but it doesn't matter
{
System.out.println("null, but system handled it");
}
squares[i][j] = null;
}
return;
}
}
}
}
}
}
public static void main(String[] args) //main
{
BattleshipManager app = new BattleshipManager(); //creates a "BattleshipManager" object
}
}