-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleshipGame.java
More file actions
346 lines (257 loc) · 8.38 KB
/
Copy pathBattleshipGame.java
File metadata and controls
346 lines (257 loc) · 8.38 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
// Mr. L
// Jacob Schnettler - 1 Player Battleship.
// 1/30/2023
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Random;
import java.applet.*;
// 0 = nothing
// 6 = miss
// 7 = hit
// 1 = aircraft carrier (5)
// 2 = destroyer (4)
// 3 = battleship (3)
// 4 = submarine (3)
// 5 = patrol boat (2)
public class BattleshipGame extends Applet implements MouseListener, ActionListener, KeyListener
{
// Styling
int paddingLeft = 150;
int paddingTop = 100;
int boxSize = 45;
int logCount = 20;
// Game Data
int board[][] = new int[10][10]; // Stores ships
int rotation = 0; // 0 = top - bottom , 1 = left to right
boolean settingShips = false; // when active enable the hover function, allow the user to use mouse to place ships. Also Press R to rotate
boolean attachMode = true; // if user can hit another ship.
boolean showTitle = true;
boolean gameOver = false;
Button startGameBTN = new Button();
AudioClip loadingMusic;
// emmitted on key press to rotate ship.
public void rotateShips()
{
if (rotation == 0)
rotation = 1;
else
rotation = 0;
}
// toggles the mouse hover selection.
public void chooseShipLocations()
{
settingShips = true;
// Mouse over and
}
// Temporary
public void fillArray()
{
board[1][0] = 1;
board[1][1] = 1;
board[1][2] = 1;
board[1][3] = 1;
board[1][4] = 1;
board[3][1] = 2;
board[3][2] = 2;
board[3][3] = 2;
board[3][4] = 2;
board[1][7] = 3;
board[2][7] = 3;
board[3][7] = 3;
board[8][5] = 4;
board[8][6] = 4;
board[8][7] = 4;
board[9][8] = 5;
board[9][9] = 5;
}
// on startup to begin game and set data.
public void startGame()
{
chooseShipLocations();
}
public void init()
{
this.setLayout(null);
this.addMouseListener(this);
this.addKeyListener(this);
this.resize(1400, 700);
loadingMusic = this.getAudioClip(this.getCodeBase(), "imgs/cod.wav");
startGameBTN.addActionListener(this);
startGameBTN.setLabel("Start Game");
startGameBTN.setBounds(600, 400, 150, 50);
fillArray();
this.add(startGameBTN);
}
// When a user clicks with mouse to "hit" ship.
public void hitShip(int x, int y)
{
int shipType = board[x][y];
if (!(shipType == 6 || shipType == 7)) {
if (shipType != 0) {
board[x][y] = 7;
} else {
board[x][y] = 6;
}
}
repaint();
}
//
public void mouseClicked(MouseEvent e) {
if (gameOver) return;
int mouseX = e.getX();
int mouseY = e.getY();
int x = (mouseX - paddingLeft) / (boxSize);
int y = (mouseY - paddingTop) / (boxSize);
hitShip(y, x);
}
public void keyPressed(KeyEvent e) {
if (!settingShips || gameOver) return;
int code = e.getKeyCode();
if (code == e.VK_R)
{
rotateShips();
}
}
public void showTitleScreen()
{
showTitle = true;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0, 0, 1400, 700);
g.setColor(Color.white);
int sunkenShips = 0;
int totalClicks = 0;
for (int i = 0; i <= 4; i++)
{
String label = "";
int shipSize = 0;
boolean sunk = false;
if (i == 0) {
label = "Aircraft Carrier";
} else if (i == 1) {
label = "Destroyer";
} else if (i == 2) {
label = "Battleship";
} else if (i == 3) {
label = "Submarine";
} else if (i == 4) {
label = "Patrol Boat";
}
for (int r = 0; r <= 9; r++)
{
for (int c = 0; c <= 9; c++)
{
int shipType = board[r][c];
if (shipType == (i + 1)) {
shipSize++;
}
}
}
if (shipSize == 0) {
sunkenShips++;
}
}
for (int r = 0; r <= 9; r++)
{
for (int c = 0; c <= 9; c++)
{
int shipType = board[r][c];
if (shipType == 6 || shipType == 7) {
totalClicks++;
}
}
}
gameOver = sunkenShips == 5;
if (showTitle) {
g.setFont(new Font("Ariel", 1, 30));
g.drawString("Battleship", 600, 250);
g.setFont(new Font("Ariel", 1, 20));
g.drawString("By: Jacob Schnettler", 570, 300);
} else {
g.setColor(Color.white);
g.setFont(new Font("Ariel", 1, 15));
g.drawString(" The objective is to guess were my battleships are located ",625,450);
g.drawString(" and try to sink my whole fleet in the least amount of moves.",625,500);
g.drawString(" When you hit my ship, you will be notifed, when you miss, you ",625,550);
g.drawString(" will also be notifed. The game ends when all ships are sunk.",625,600);
g.setFont(new Font("Ariel", 1, 30));
g.drawString("Battleship", 600, 50);
g.setFont(new Font("Ariel", 1, 20));
g.drawString("By: Jacob Schnettler", 570, 75);
g.setFont(new Font("Ariel", 1, 25));
g.drawString("Ships Left:", 625, 125);
g.drawString("Rules", 625, 400);
g.drawString("" + totalClicks + " Total Clicks", 150, 80);
g.setFont(new Font("Ariel", 1, 20));
for (int i = 0; i <= 4; i++)
{
String label = "";
int shipSize = 0;
boolean sunk = false;
if (i == 0) {
label = "Aircraft Carrier";
} else if (i == 1) {
label = "Destroyer";
} else if (i == 2) {
label = "Battleship";
} else if (i == 3) {
label = "Submarine";
} else if (i == 4) {
label = "Patrol Boat";
}
for (int r = 0; r <= 9; r++)
{
for (int c = 0; c <= 9; c++)
{
int shipType = board[r][c];
if (shipType == (i + 1)) {
shipSize++;
}
}
}
if (shipSize == 0) sunk = true;
if (sunk) {
g.drawString("" + label + " - SUNK!", 625, 175 + (i * 40));
} else {
g.drawString("" + label + " - " + shipSize + " HP", 625, 175 + (i * 40));
}
}
for (int r = 0; r <= 9; r++)
{
for (int c = 0; c <= 9; c++)
{
int shipType = board[r][c];
if (shipType == 6) {
g.setColor(Color.white);
} else if (shipType == 7) {
g.setColor(Color.red);
} else {
g.setColor(Color.blue);
}
g.fillRect(paddingLeft + boxSize *c, paddingTop + boxSize * r, boxSize, boxSize);
g.setColor(Color.black);
g.drawRect(paddingLeft + boxSize *c, paddingTop + boxSize * r, boxSize, boxSize);
}
}
}
}
public void keyReleased() {}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startGameBTN)
{
showTitle = false;
startGameBTN.setVisible(false);
loadingMusic.stop();
repaint();
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}