-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScreen.java
1893 lines (1835 loc) · 76.5 KB
/
GameScreen.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
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This class runs the game screen of the complete product. It allows for user interaction, game functionality, and actual gameplay.
*
* date 20201110
* @filename GameScreen.java
* @author jdalwadi, sassareymuryil, jmajmudar, hshah, vreddy
* @version 1.0
* @see assignment 4
*/
package com.homebasedefence.screen;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.util.ArrayList;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.*;
// evidence of abstraction through extending JFrame
public class GameScreen extends JFrame {
private JPanel mainPanel;
private JButton spot0;
private JButton spot1;
private JButton spot2;
private JButton spot3;
private JButton spot4;
private JButton spot5;
private JButton spot6;
private JButton spot7;
private JButton spot8;
private JButton spot9;
private JButton spot20;
private JButton spot16;
private JButton spot12;
private JButton spot10;
private JButton spot11;
private JButton spot13;
private JButton spot14;
private JButton spot19;
private JButton spot24;
private JButton spot23;
private JButton spot18;
private JButton spot17;
private JButton spot22;
private JButton spot21;
private JButton spot15;
private JButton startResetButton;
private JButton S1;
private JButton S2;
private JButton S3;
private JButton S4;
private JButton S5;
private JButton S6;
private JButton S7;
private JButton S8;
private JButton S9;
private JLabel highName;
private JLabel highScore;
private JLabel numDestroyed;
private JLabel scoreNum;
private JButton healthBar;
private JButton exitButton;
String[][] asteroidLoc = new String[25][2];
ImageIcon asteroid = new ImageIcon(getClass().getResource("/Assets/Images/asteroidIMG.png"));
ImageIcon space = new ImageIcon(getClass().getResource("/Assets/Images/spaceBG.png"));
ImageIcon fullH = new ImageIcon(getClass().getResource("/Assets/Images/fullH.jpg"));
ImageIcon sixH = new ImageIcon(getClass().getResource("/Assets/Images/sixH.jpg"));
ImageIcon fiveH = new ImageIcon(getClass().getResource("/Assets/Images/fiveH.jpg"));
ImageIcon fourH = new ImageIcon(getClass().getResource("/Assets/Images/fourH.jpg"));
ImageIcon threeH = new ImageIcon(getClass().getResource("/Assets/Images/threeH.jpg"));
ImageIcon twoH = new ImageIcon(getClass().getResource("/Assets/Images/twoH.jpg"));
ImageIcon oneH = new ImageIcon(getClass().getResource("/Assets/Images/oneH.jpg"));
ImageIcon noH = new ImageIcon(getClass().getResource("/Assets/Images/noH.jpg"));
ImageIcon defaultIcon = new ImageIcon(getClass().getResource("/Assets/Images/defaultIcon.jpg"));
ImageIcon chosenIcon = new ImageIcon(getClass().getResource("/Assets/Images/chosenIcon.png"));
int numberDestroyed;
public int score;
public int highScores;
public String names;
int numHealth;
private ArrayList<Integer> pattern = new ArrayList<>();
private ArrayList<Integer> ipattern = new ArrayList<>();
Integer[] simonPattern;
Integer[] inputPattern;
int patternSize = 3;
int round = 1;
int max = 9;
int min = 1;
int delay = 1000;
boolean simonEnabled;
public String resultOutput;
public GameScreen() {
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
this.setContentPane(mainPanel);
SimonDisable();
startResetButton.addActionListener(new ActionListener() {
// abstraction + inheritance seen here
@Override
/**
* actions to start a new game
*/
public void actionPerformed(ActionEvent e) {
// refreshes contents to new game
startResetButton.setText("Reset");
numberDestroyed = 0;
numHealth = 7;
healthBar.setIcon(fullH);
AsteroidClear();
NewAsteroid();
UpdateAsteroidLoc();
score = 0;
numDestroyed.setText("Destroyed: 0");
scoreNum.setText("Score: 0");
// to start with multiple asteroids
Asteroid("wrong");
// set high score values
HighScoreBegin();
// initializes SimonSays component
ipattern.clear();
pattern.clear();
patternSize = 3;
round = 1;
delay = 1000;
SimonSays();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
/**
* functioning exit button
*/
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
/**
* SimonSays buttons - MouseListeners for input
* Used - https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
* abstraction + inheritance seen in each SimonSays button MouseListener
*/
S1.addMouseListener(new MouseAdapter() {
//abstraction
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
// inheritance
// changes colour on pressed
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S1.setDisabledIcon(chosenIcon);
} else {
return;
}
}
//abstraction
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
//inheritance
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(1);
} else if (ipattern.size() == patternSize - 1) {
// adds to input pattern array list and checks if correct
ipattern.add(1);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S2.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(2);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(2);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S3.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(3);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(3);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S4.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S4.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(4);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(4);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S5.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S5.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(5);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(5);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S6.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S6.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(6);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(6);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S7.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S7.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(7);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(7);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S8.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S8.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(8);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(8);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
S9.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (simonEnabled == true){
super.mousePressed(e);
SimonDisable();
simonEnabled = true;
S9.setDisabledIcon(chosenIcon);
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (simonEnabled == true){
super.mouseReleased(e);
SimonEnable();
if (ipattern.size() < patternSize - 1) {
// adds to input pattern array list
ipattern.add(9);
} else if (ipattern.size() == patternSize - 1) {
ipattern.add(9);
SimonDisable();
CheckPattern();
}
} else{
return;
}
}
});
}
/**
* method that will change the score (highscore) and the name in the XML file and it will also output/print the new highscore and the name
* @param names
* @param score
*/
public void HighScore(String names, int score) {
try {
String filepath = "./Assets/XML/HighScore.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Node a = doc.getElementsByTagName("name").item(0);
a.setTextContent(names); // Modifies the name in the XML file
Node b = doc.getElementsByTagName("score").item(0);
b.setTextContent(String.valueOf(score)); // Modifies the score in the XML file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("HighScore");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
highScore.setText(eElement.getElementsByTagName("score").item(0).getTextContent()); // Prints the new high score
highName.setText(eElement.getElementsByTagName("name").item(0).getTextContent()); // Prints the new name
}
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
/**
* method that will output the highscore and the name for the game and it will do that by looking in the XML file
*/
public void HighScoreBegin() {
try {
String filepath = "./Assets/XML/HighScore.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("HighScore");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
highScore.setText(eElement.getElementsByTagName("score").item(0).getTextContent()); // Prints the high score
highScores = Integer.parseInt(eElement.getElementsByTagName("score").item(0).getTextContent());
highName.setText(eElement.getElementsByTagName("name").item(0).getTextContent()); // Prints the name
}
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
/**
* method to clear asteroids by setting icons to the clear space background
*/
public void AsteroidClear() {
spot0.setIcon(space);
spot1.setIcon(space);
spot2.setIcon(space);
spot3.setIcon(space);
spot4.setIcon(space);
spot5.setIcon(space);
spot6.setIcon(space);
spot7.setIcon(space);
spot8.setIcon(space);
spot9.setIcon(space);
spot10.setIcon(space);
spot11.setIcon(space);
spot12.setIcon(space);
spot13.setIcon(space);
spot14.setIcon(space);
spot15.setIcon(space);
spot16.setIcon(space);
spot17.setIcon(space);
spot18.setIcon(space);
spot19.setIcon(space);
spot20.setIcon(space);
spot21.setIcon(space);
spot22.setIcon(space);
spot23.setIcon(space);
spot24.setIcon(space);
}
/**
* method to check and update locations of asteroids into the array prior to processing
*/
public void UpdateAsteroidLoc() {
for (int i = 0; i < 25; i++) {
// fill in first column of array with button name
asteroidLoc[i][0] = "spot" + i;
// fill in all locations with space so they can be compared unlike null
asteroidLoc[i][1] = "space";
// check and fill in asteroid into spaces with asteroids
if (i == 0 && spot0.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 1 && spot1.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 2 && spot2.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 3 && spot3.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 4 && spot4.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 5 && spot5.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 6 && spot6.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 7 && spot7.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 8 && spot8.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 9 && spot9.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 10 && spot10.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 11 && spot11.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 12 && spot12.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 13 && spot13.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 14 && spot14.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 15 && spot15.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 16 && spot16.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 17 && spot17.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 18 && spot18.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 19 && spot19.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 20 && spot20.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 21 && spot21.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 22 && spot22.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 23 && spot23.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
if (i == 24 && spot24.getIcon().equals(asteroid)) {asteroidLoc[i][1] = "asteroid";}
}
}
/**
* method to create a new asteroid in a random location in the top row
*/
public void NewAsteroid() {
// collect new random location
int newLoc = new Random().nextInt(5);
// place new asteroid based on location
if (newLoc == 0)
spot0.setIcon(asteroid);
if (newLoc == 1)
spot1.setIcon(asteroid);
if (newLoc == 2)
spot2.setIcon(asteroid);
if (newLoc == 3)
spot3.setIcon(asteroid);
if (newLoc == 4)
spot4.setIcon(asteroid);
// call UpdateAsteroidLoc() to update location
UpdateAsteroidLoc();
}
/**
* method that deals with all things asteroid in the game
*/
public void Asteroid(String check) {
// if the method is called and the input is right
if (check.equals("right")) {
// add to the number destroyed variable
numberDestroyed++;
// calculate score based on round number and constantly add to score variable when an asteroid is destroyed
score += 5 * round;
// destroy the closest asteroid (uses linear search to identify the closest one)
if (asteroidLoc[24][1].equals("asteroid")) {
spot24.setIcon(space);
} else if (asteroidLoc[23][1].equals("asteroid")) {
spot23.setIcon(space);
} else if (asteroidLoc[22][1].equals("asteroid")) {
spot22.setIcon(space);
} else if (asteroidLoc[21][1].equals("asteroid")) {
spot21.setIcon(space);
} else if (asteroidLoc[20][1].equals("asteroid")) {
spot20.setIcon(space);
} else if (asteroidLoc[19][1].equals("asteroid")) {
spot19.setIcon(space);
} else if (asteroidLoc[18][1].equals("asteroid")) {
spot18.setIcon(space);
} else if (asteroidLoc[17][1].equals("asteroid")) {
spot17.setIcon(space);
} else if (asteroidLoc[16][1].equals("asteroid")) {
spot16.setIcon(space);
} else if (asteroidLoc[15][1].equals("asteroid")) {
spot15.setIcon(space);
} else if (asteroidLoc[14][1].equals("asteroid")) {
spot14.setIcon(space);
} else if (asteroidLoc[13][1].equals("asteroid")) {
spot13.setIcon(space);
} else if (asteroidLoc[12][1].equals("asteroid")) {
spot12.setIcon(space);
} else if (asteroidLoc[11][1].equals("asteroid")) {
spot11.setIcon(space);
} else if (asteroidLoc[10][1].equals("asteroid")) {
spot10.setIcon(space);
} else if (asteroidLoc[9][1].equals("asteroid")) {
spot9.setIcon(space);
} else if (asteroidLoc[8][1].equals("asteroid")) {
spot8.setIcon(space);
} else if (asteroidLoc[7][1].equals("asteroid")) {
spot7.setIcon(space);
} else if (asteroidLoc[6][1].equals("asteroid")) {
spot6.setIcon(space);
} else if (asteroidLoc[5][1].equals("asteroid")) {
spot5.setIcon(space);
} else if (asteroidLoc[4][1].equals("asteroid")) {
spot4.setIcon(space);
} else if (asteroidLoc[3][1].equals("asteroid")) {
spot3.setIcon(space);
} else if (asteroidLoc[2][1].equals("asteroid")) {
spot2.setIcon(space);
} else if (asteroidLoc[1][1].equals("asteroid")) {
spot1.setIcon(space);
} else if (asteroidLoc[0][1].equals("asteroid")) {
spot0.setIcon(space);
}
// call UpdateAsteroidLoc() to update locations
UpdateAsteroidLoc();
// shift asteroids down based on position
if (asteroidLoc[19][1].equals("asteroid")) {
spot19.setIcon(space);
spot24.setIcon(asteroid);
}
if (asteroidLoc[18][1].equals("asteroid")) {
spot18.setIcon(space);
spot23.setIcon(asteroid);
}
if (asteroidLoc[17][1].equals("asteroid")) {
spot17.setIcon(space);
spot22.setIcon(asteroid);
}
if (asteroidLoc[16][1].equals("asteroid")) {
spot16.setIcon(space);
spot21.setIcon(asteroid);
}
if (asteroidLoc[15][1].equals("asteroid")) {
spot15.setIcon(space);
spot20.setIcon(asteroid);
}
if (asteroidLoc[14][1].equals("asteroid")) {
spot14.setIcon(space);
spot19.setIcon(asteroid);
}
if (asteroidLoc[13][1].equals("asteroid")) {
spot13.setIcon(space);
spot18.setIcon(asteroid);
}
if (asteroidLoc[12][1].equals("asteroid")) {
spot12.setIcon(space);
spot17.setIcon(asteroid);
}
if (asteroidLoc[11][1].equals("asteroid")) {
spot11.setIcon(space);
spot16.setIcon(asteroid);
}
if (asteroidLoc[10][1].equals("asteroid")) {
spot10.setIcon(space);
spot15.setIcon(asteroid);
}
if (asteroidLoc[9][1].equals("asteroid")) {
spot9.setIcon(space);
spot14.setIcon(asteroid);
}
if (asteroidLoc[8][1].equals("asteroid")) {
spot8.setIcon(space);
spot13.setIcon(asteroid);
}
if (asteroidLoc[7][1].equals("asteroid")) {
spot7.setIcon(space);
spot12.setIcon(asteroid);
}
if (asteroidLoc[6][1].equals("asteroid")) {
spot6.setIcon(space);
spot11.setIcon(asteroid);
}
if (asteroidLoc[5][1].equals("asteroid")) {
spot5.setIcon(space);
spot10.setIcon(asteroid);
}
if (asteroidLoc[4][1].equals("asteroid")) {
spot4.setIcon(space);
spot9.setIcon(asteroid);
}
if (asteroidLoc[3][1].equals("asteroid")) {
spot3.setIcon(space);
spot8.setIcon(asteroid);
}
if (asteroidLoc[2][1].equals("asteroid")) {
spot2.setIcon(space);
spot7.setIcon(asteroid);
}
if (asteroidLoc[1][1].equals("asteroid")) {
spot1.setIcon(space);
spot6.setIcon(asteroid);
}
if (asteroidLoc[0][1].equals("asteroid")) {
spot0.setIcon(space);
spot5.setIcon(asteroid);
}
// call UpdateAsteroidLoc() to update locations
UpdateAsteroidLoc();
// call NewAsteroid() to randomly create a new asteroid in the top row
NewAsteroid();
// if method is called and input is wrong
} else if (check.equals("wrong")) {
// check if there is an asteroid in the lowest row
if (asteroidLoc[24][1].equals("asteroid")
|| asteroidLoc[23][1].equals("asteroid")
|| asteroidLoc[22][1].equals("asteroid")
|| asteroidLoc[21][1].equals("asteroid")
|| asteroidLoc[20][1].equals("asteroid")) {
// if there is lower health
numHealth--;
// set healthbar based on updated health
if (numHealth == 6) {
healthBar.setIcon(sixH);
} else if (numHealth == 5) {
healthBar.setIcon(fiveH);
} else if (numHealth == 4) {
healthBar.setIcon(fourH);
} else if (numHealth == 3) {
healthBar.setIcon(threeH);
} else if (numHealth == 2) {
healthBar.setIcon(twoH);
} else if (numHealth == 1) {
healthBar.setIcon(oneH);
}
if (numHealth == 0) {
healthBar.setIcon(noH);
}
// remove asteroid that damaged the ship (uses linear search)
if (asteroidLoc[24][1].equals("asteroid")) {
spot24.setIcon(space);
} else if (asteroidLoc[23][1].equals("asteroid")) {
spot23.setIcon(space);
} else if (asteroidLoc[22][1].equals("asteroid")) {
spot22.setIcon(space);
} else if (asteroidLoc[21][1].equals("asteroid")) {
spot21.setIcon(space);
} else if (asteroidLoc[20][1].equals("asteroid")) {
spot20.setIcon(space);
}
// call UpdateAsteroidLoc() to update locations
UpdateAsteroidLoc();
}
// shift asteroids down based on location
if (asteroidLoc[19][1].equals("asteroid")) {
spot19.setIcon(space);
spot24.setIcon(asteroid);
}
if (asteroidLoc[18][1].equals("asteroid")) {
spot18.setIcon(space);
spot23.setIcon(asteroid);
}
if (asteroidLoc[17][1].equals("asteroid")) {
spot17.setIcon(space);
spot22.setIcon(asteroid);
}
if (asteroidLoc[16][1].equals("asteroid")) {
spot16.setIcon(space);
spot21.setIcon(asteroid);
}
if (asteroidLoc[15][1].equals("asteroid")) {
spot15.setIcon(space);
spot20.setIcon(asteroid);
}
if (asteroidLoc[14][1].equals("asteroid")) {
spot14.setIcon(space);
spot19.setIcon(asteroid);
}
if (asteroidLoc[13][1].equals("asteroid")) {
spot13.setIcon(space);
spot18.setIcon(asteroid);
}
if (asteroidLoc[12][1].equals("asteroid")) {
spot12.setIcon(space);
spot17.setIcon(asteroid);
}
if (asteroidLoc[11][1].equals("asteroid")) {
spot11.setIcon(space);
spot16.setIcon(asteroid);
}
if (asteroidLoc[10][1].equals("asteroid")) {
spot10.setIcon(space);
spot15.setIcon(asteroid);
}
if (asteroidLoc[9][1].equals("asteroid")) {
spot9.setIcon(space);
spot14.setIcon(asteroid);
}
if (asteroidLoc[8][1].equals("asteroid")) {
spot8.setIcon(space);
spot13.setIcon(asteroid);
}
if (asteroidLoc[7][1].equals("asteroid")) {
spot7.setIcon(space);
spot12.setIcon(asteroid);
}
if (asteroidLoc[6][1].equals("asteroid")) {
spot6.setIcon(space);
spot11.setIcon(asteroid);
}
if (asteroidLoc[5][1].equals("asteroid")) {
spot5.setIcon(space);
spot10.setIcon(asteroid);
}
if (asteroidLoc[4][1].equals("asteroid")) {
spot4.setIcon(space);
spot9.setIcon(asteroid);
}
if (asteroidLoc[3][1].equals("asteroid")) {
spot3.setIcon(space);
spot8.setIcon(asteroid);
}
if (asteroidLoc[2][1].equals("asteroid")) {
spot2.setIcon(space);
spot7.setIcon(asteroid);
}
if (asteroidLoc[1][1].equals("asteroid")) {
spot1.setIcon(space);
spot6.setIcon(asteroid);
}
if (asteroidLoc[0][1].equals("asteroid")) {
spot0.setIcon(space);
spot5.setIcon(asteroid);
}
// call UpdateAsteroidLoc() to update asteroid locations
UpdateAsteroidLoc();
// create a New Asteroid in a random loation in the top row
NewAsteroid();
}
}
/**
* method to disable all SimonSays input buttons so they can't be pressed
*/
public void SimonDisable() {
simonEnabled = false;
S1.setDisabledIcon(defaultIcon);
S1.setEnabled(false);
S2.setDisabledIcon(defaultIcon);
S2.setEnabled(false);
S3.setDisabledIcon(defaultIcon);
S3.setEnabled(false);
S4.setDisabledIcon(defaultIcon);
S4.setEnabled(false);
S5.setDisabledIcon(defaultIcon);
S5.setEnabled(false);
S6.setDisabledIcon(defaultIcon);
S6.setEnabled(false);
S7.setDisabledIcon(defaultIcon);
S7.setEnabled(false);
S8.setDisabledIcon(defaultIcon);
S8.setEnabled(false);
S9.setDisabledIcon(defaultIcon);
S9.setEnabled(false);
}
/**
* method to enable all SimonSays input buttons so they can be pressed
*/
public void SimonEnable() {
simonEnabled = true;
S1.setEnabled(true);
S1.setIcon(defaultIcon);
S1.setDisabledIcon(defaultIcon);
S2.setEnabled(true);
S2.setIcon(defaultIcon);
S2.setDisabledIcon(defaultIcon);
S3.setEnabled(true);
S3.setIcon(defaultIcon);
S3.setDisabledIcon(defaultIcon);
S4.setEnabled(true);
S4.setIcon(defaultIcon);
S4.setDisabledIcon(defaultIcon);
S5.setEnabled(true);
S5.setIcon(defaultIcon);
S5.setDisabledIcon(defaultIcon);
S6.setEnabled(true);
S6.setIcon(defaultIcon);
S6.setDisabledIcon(defaultIcon);
S7.setEnabled(true);
S7.setIcon(defaultIcon);
S7.setDisabledIcon(defaultIcon);
S8.setEnabled(true);
S8.setIcon(defaultIcon);
S8.setDisabledIcon(defaultIcon);
S9.setEnabled(true);
S9.setIcon(defaultIcon);
S9.setDisabledIcon(defaultIcon);
}
/**
* method to create a random SimonSays pattern in an ArrayList
*/
public void SetSimonSaysPattern(){
if (patternSize == 3) {
for (int y = 0; y < patternSize; y++) {
// generate first random location
if(y == 0){
pattern.add((int)((Math.random() * ((max - min) + 1)) + min));
}
// if not the first check value right before
else {
int temp = (int) ((Math.random() * ((max - min) + 1)) + min);
// while the random location is the same as before generate a new one
while (temp == pattern.get(y - 1)) {
temp = (int) ((Math.random() * ((max - min) + 1)) + min);
}
// add to pattern
pattern.add(temp);
}
}
} else if (patternSize > 3) {
// generate random location
int temp = (int)((Math.random() * ((max - min) + 1)) + min);
// while the random location is the same as before generate a new one
while (temp == pattern.get(pattern.size()-1)){
temp = (int)((Math.random() * ((max - min) + 1)) + min);
}
// add to pattern
pattern.add(temp);
}
}
/**