-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.java
More file actions
143 lines (94 loc) · 3.18 KB
/
Board.java
File metadata and controls
143 lines (94 loc) · 3.18 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
import BreezySwing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
public class SecondaryGUI extends GBFrame {
static JButton [][] arrayButtons = new JButton[11][11];
Computer cpu;
private String hitSound = "C://Users/Admin/workspace-SCHOOL/Battleship/Resources/hitsound.wav";
private String missSound = "C://Users/Admin/workspace-SCHOOL/Battleship/Resources/misssound.wav";
private String winSound = "C://Users/Admin/workspace-SCHOOL/Battleship/Resources/winSound.wav";
URL hitSoundURL, missSoundURL, winSoundURL;
public SecondaryGUI() {
for(int i = 1; i < arrayButtons.length; i++){
for(int j = 1; j < arrayButtons[0].length; j++){
JButton b = new JButton();
b = addButton("", i, j, 1, 1);
b.setIcon(new ImageIcon( new BufferedImage(299, 168, BufferedImage.TYPE_INT_ARGB)));
b.setBackground(Color.BLUE);
b.setToolTipText("(" + i + ", " + j + ")");
arrayButtons[i][j] = b;
// arrayButtons[i][j].setEnabled(false);
hitSoundURL = getClass().getResource("C://Users/Admin/workspace-SCHOOL/Battleship/Resources/hitsound.wav");
missSoundURL = getClass().getResource("C://Users/Admin/workspace-SCHOOL/Battleship/Resources/misssound.wav");
winSoundURL = getClass().getResource("C://Users/Admin/workspace-SCHOOL/Battleship/Resources/winSound.wav");
}
}
try {
cpu=new Computer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
messageBox(e.getMessage());
}
}
public void buttonClicked(JButton b){
for(int i =0; i < arrayButtons.length; i++){
for(int j = 1; j < arrayButtons[0].length; j++){
JButton temp;
if(arrayButtons[i][j]==b){
temp=b;
try {
cpu.getGrid().attack(i-1, j-1);
if(cpu.getGrid().checkHit(i-1, j-1)){
//messageBox(""+cpu.getGrid().getPoint(i-1, j-1).getID());
temp.setBackground(Color.RED);
cpu.getShip(cpu.getGrid().getPoint(i-1, j-1).getID()).hitShip();
play(hitSoundURL);
if(cpu.testSunk(i-1,j-1)) {
messageBox("You have sunk the enemy's " + cpu.getShip(cpu.getGrid().getPoint(i-1, j-1).getID()).getName());
if(cpu.gg()) {
play(winSoundURL);
messageBox("You have won!! Victory Royale!!");
TimeUnit.SECONDS.sleep(2);
System.exit(0);
}
}
}
else{
temp.setBackground(Color.WHITE);
play(missSoundURL);
}
Board.guess();
Board.updateGUI();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
messageBox(e.getMessage());
}
}
}}
}
public JButton [][] getArrayButtons(){
return arrayButtons;
}
public static void play(URL filename)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(filename));
clip.start();
TimeUnit.SECONDS.sleep(2);
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
}
}