-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleShipGUI.java
300 lines (266 loc) · 10.1 KB
/
BattleShipGUI.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package battleship;
/**
*
* @author j9neave
*/
public class BattleShipGUI extends javax.swing.JFrame {
/**
* Creates new form BattleShipGUI
*/
public BattleShipGUI() {
initComponents();
Grid displayGrid = new Grid(Team.FRIENDLY);
Team.FRIENDLY.setDisplayGrid(displayGrid);
friendlyPanel.add(displayGrid);
friendlyPanel.revalidate();
friendlyPanel.repaint();
displayGrid = new Grid(Team.ENEMY);
Team.ENEMY.setDisplayGrid(displayGrid);
enemyPanel.add(displayGrid);
enemyPanel.revalidate();
enemyPanel.repaint();
boolean[][] grid = generateGrid();
grid[1][1] = true;
grid[1][2] = true;
grid[1][3] = true;
grid[1][4] = true;
grid[1][5] = true;
grid[4][5] = true;
grid[5][5] = true;
grid[6][5] = true;
grid[7][5] = true;
grid[6][0] = true;
grid[6][1] = true;
grid[6][2] = true;
grid[0][7] = true;
grid[1][7] = true;
grid[2][7] = true;
grid[3][2] = true;
grid[3][3] = true;
boolean[][] egrid = generateGrid();
Team.ENEMY.setTeamShipGrid(placeShipsRandom(egrid));
Team.FRIENDLY.setTeamShipGrid(grid);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
friendlyPanel = new javax.swing.JPanel();
enemyPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("BattleShip");
setPreferredSize(new java.awt.Dimension(720, 480));
java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
layout.columnWidths = new int[] {0, 5, 0, 5, 0, 5, 0};
layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0};
getContentPane().setLayout(layout);
friendlyPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Friendly Waters"));
friendlyPanel.setLayout(new java.awt.BorderLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(friendlyPanel, gridBagConstraints);
enemyPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Enemy Waters"));
enemyPanel.setLayout(new java.awt.BorderLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(enemyPanel, gridBagConstraints);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BattleShipGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BattleShipGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BattleShipGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BattleShipGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new BattleShipGUI().setVisible(true);
}
});
}
static boolean[][] generateGrid() {
boolean[][] grid = new boolean[8][8];
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y< grid.length; y++) {
grid[x][y] = false;
}
}
return grid;
}
static boolean[][] placeShipsRandom(boolean[][] grid) {
boolean invalid_grid = false;
do {//repeats until a valid grid is generated where all ships are placed, none overlap, touch or extend past the side
if (invalid_grid) grid = generateGrid();
invalid_grid = false;
//placing 5 long ship
//decides whether to place vertical or horizontal
if ((int)Math.round(Math.random()) == 0) {
//code for vertical placement
int startx = (int)Math.floor(Math.random()*8);
int starty = (int)Math.floor(Math.random()*3);
grid[starty][startx] = true;
grid[starty+1][startx] = true;
grid[starty+2][startx] = true;
grid[starty+3][startx] = true;
grid[starty+4][startx] = true;
} else {
//code for horizontal placement
int startx = (int)Math.floor(Math.random()*3);
int starty = (int)Math.floor(Math.random()*8);
grid[starty][startx] = true;
grid[starty][startx+1] = true;
grid[starty][startx+2] = true;
grid[starty][startx+3] = true;
grid[starty][startx+4] = true;
}
//placing 4 long ship
//decides whether to place vertical or horizontal
if ((int)Math.round(Math.random()) == 0) {
//code for vertical
int startx = (int)Math.floor(Math.random()*8);
int starty = (int)Math.floor(Math.random()*4);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty+1][startx]) invalid_grid = true;
if (grid[starty+2][startx]) invalid_grid = true;
if (grid[starty+3][startx]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty+1][startx] = true;
grid[starty+2][startx] = true;
grid[starty+3][startx] = true;
} else {
//code for horizontal
int startx = (int)Math.floor(Math.random()*4);
int starty = (int)Math.floor(Math.random()*8);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty][startx+1]) invalid_grid = true;
if (grid[starty][startx+2]) invalid_grid = true;
if (grid[starty][startx+3]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty][startx+1] = true;
grid[starty][startx+2] = true;
grid[starty][startx+3] = true;
}
//placing 3 long ship a
//decides vertical or horizontal
if ((int)Math.round(Math.random()) == 0) {
//code for vertical
int startx = (int)Math.floor(Math.random()*8);
int starty = (int)Math.floor(Math.random()*5);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty+1][startx]) invalid_grid = true;
if (grid[starty+2][startx]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty+1][startx] = true;
grid[starty+2][startx] = true;
} else {
//code for horizontal
int startx = (int)Math.floor(Math.random()*5);
int starty = (int)Math.floor(Math.random()*8);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty][startx+1]) invalid_grid = true;
if (grid[starty][startx+2]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty][startx+1] = true;
grid[starty][startx+2] = true;
}
//placing 3 long ship b
//decides vertical or horizontal
if ((int)Math.round(Math.random()) == 0) {
//code for vertical
int startx = (int)Math.floor(Math.random()*8);
int starty = (int)Math.floor(Math.random()*5);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty+1][startx]) invalid_grid = true;
if (grid[starty+2][startx]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty+1][startx] = true;
grid[starty+2][startx] = true;
} else {
//code for horizontal
int startx = (int)Math.floor(Math.random()*5);
int starty = (int)Math.floor(Math.random()*8);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty][startx+1]) invalid_grid = true;
if (grid[starty][startx+2]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty][startx+1] = true;
grid[starty][startx+2] = true;
}
//placing 2 long ship
//decides vertical or horizontal
if ((int)Math.round(Math.random()) == 0) {
//code for vertical
int startx = (int)Math.floor(Math.random()*8);
int starty = (int)Math.floor(Math.random()*6);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty+1][startx]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty+1][startx] = true;
} else {
//code for horizontal
int startx = (int)Math.floor(Math.random()*6);
int starty = (int)Math.floor(Math.random()*8);
if (grid[starty][startx]) invalid_grid = true;
if (grid[starty][startx+1]) invalid_grid = true;
grid[starty][startx] = true;
grid[starty][startx+1] = true;
}
} while (invalid_grid);
return grid;
}
static void logGrid(boolean[][] grid) {
System.out.println("----------------");
for (int x = 0; x < grid.length; x++) {
System.out.print("|");
for (int y = 0; y< grid.length; y++) {
System.out.print(grid[x][y] + "|");
}
System.out.print("\n----------------\n");
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel enemyPanel;
private javax.swing.JPanel friendlyPanel;
// End of variables declaration//GEN-END:variables
}