-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOceanTest.java
332 lines (258 loc) · 8.37 KB
/
OceanTest.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
package battleship;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class OceanTest {
Ocean ocean;
static int NUM_BATTLESHIPS = 1;
static int NUM_CRUISERS = 2;
static int NUM_DESTROYERS = 3;
static int NUM_SUBMARINES = 4;
static int OCEAN_SIZE = 10;
@BeforeEach
void setUp() throws Exception {
ocean = new Ocean();
}
@Test
void testEmptyOcean() {
//tests that all locations in the ocean are "empty"
Ship[][] ships = ocean.getShipArray();
for (int i = 0; i < ships.length; i++) {
for (int j = 0; j < ships[i].length; j++) {
Ship ship = ships[i][j];
assertEquals("empty", ship.getShipType());
}
}
assertEquals(0, ships[0][0].getBowRow());
assertEquals(0, ships[0][0].getBowColumn());
assertEquals(5, ships[5][5].getBowRow());
assertEquals(5, ships[5][5].getBowColumn());
assertEquals(9, ships[9][0].getBowRow());
assertEquals(0, ships[9][0].getBowColumn());
}
@Test
void testPlaceAllShipsRandomly() {
//tests that the correct number of each ship type is placed in the ocean
ocean.placeAllShipsRandomly();
Ship[][] ships = ocean.getShipArray();
ArrayList<Ship> shipsFound = new ArrayList<Ship>();
int numBattlehips = 0;
int numCruisers = 0;
int numDestroyers = 0;
int numSubmarines = 0;
int numEmptySeas = 0;
for (int i = 0; i < ships.length; i++) {
for (int j = 0; j < ships[i].length; j++) {
Ship ship = ships[i][j];
if (!shipsFound.contains(ship)) {
shipsFound.add(ship);
}
}
}
for (Ship ship : shipsFound) {
if ("battleship".equals(ship.getShipType())) {
numBattlehips++;
} else if ("cruiser".equals(ship.getShipType())) {
numCruisers++;
} else if ("destroyer".equals(ship.getShipType())) {
numDestroyers++;
} else if ("submarine".equals(ship.getShipType())) {
numSubmarines++;
} else if ("empty".equals(ship.getShipType())) {
numEmptySeas++;
}
}
assertEquals(NUM_BATTLESHIPS, numBattlehips);
assertEquals(NUM_CRUISERS, numCruisers);
assertEquals(NUM_DESTROYERS, numDestroyers);
assertEquals(NUM_SUBMARINES, numSubmarines);
//calculate total number of available spaces and occupied spaces
int totalSpaces = OCEAN_SIZE * OCEAN_SIZE;
int occupiedSpaces = (NUM_BATTLESHIPS * 4)
+ (NUM_CRUISERS * 3)
+ (NUM_DESTROYERS * 2)
+ (NUM_SUBMARINES * 1);
//test number of empty seas, each with length of 1
assertEquals(totalSpaces - occupiedSpaces, numEmptySeas);
}
@Test
void testIsOccupied() {
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
Ship submarine = new Submarine();
row = 0;
column = 0;
horizontal = false;
submarine.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.isOccupied(1, 5));
//test if the vertical battleship is occupying the left bottom corner of the grid
Ship battleship = new Battleship();
row = 9;
column = 0;
horizontal = false;
battleship.placeShipAt(row,column, horizontal, ocean);
assertTrue(ocean.isOccupied(row, column));
// test if the horizontal cruiser is occupying the top right corner of grid
Ship cruiser = new Cruiser();
row = 0;
column = 9;
horizontal = true;
cruiser.placeShipAt(row,column,horizontal,ocean);
assertTrue(ocean.isOccupied(row,column));
// test if place emptySea object, method should return false
Ship empty = new EmptySea();
row = 0;
column = 1;
horizontal = false;
empty.placeShipAt(row,column,horizontal, ocean);
assertFalse(ocean.isOccupied(row, column));
}
@Test
void testShootAt() {
assertFalse(ocean.shootAt(0, 1));
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.shootAt(1, 5));
assertFalse(destroyer.isSunk());
assertTrue(ocean.shootAt(0, 5));
// shoot at empty sea objects should return false
Ship empty = new EmptySea();
row = 1;
column = 1;
horizontal = false;
empty.placeShipAt(row,column,horizontal, ocean);
assertFalse(ocean.shootAt(1,1));
// test if submarine is sunk, shootAt should return false
Ship sub = new Submarine();
row = 3;
column = 1;
horizontal = false;
sub.placeShipAt(row,column,horizontal, ocean);
assertTrue(ocean.shootAt(3, 1));
assertTrue(sub.isSunk());
assertFalse(ocean.shootAt(3, 1));
//if shoot at the same location of ship, should return true every time
Destroyer destroy = new Destroyer();
row = 5;
column = 5;
horizontal = false;
destroy.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.shootAt(5, 5));
assertTrue(ocean.shootAt(5, 5));
assertTrue(ocean.shootAt(5, 5));
}
@Test
void testGetShotsFired() {
//should be all false - no ships added yet
assertFalse(ocean.shootAt(0, 1));
assertFalse(ocean.shootAt(1, 0));
assertFalse(ocean.shootAt(3, 3));
assertFalse(ocean.shootAt(9, 9));
assertEquals(4, ocean.getShotsFired());
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
Ship submarine = new Submarine();
row = 0;
column = 0;
horizontal = false;
submarine.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.shootAt(1, 5));
assertFalse(destroyer.isSunk());
assertTrue(ocean.shootAt(0, 5));
assertTrue(destroyer.isSunk());
assertEquals(6, ocean.getShotsFired());
//test if shooting at sunk position, should also increase shots fired
assertFalse(ocean.shootAt(0, 5));
assertEquals(7, ocean.getShotsFired());
//test if shooting at empty sea objects, should increase shots fired
Ship empty = new EmptySea();
row = 1;
column = 0;
horizontal = true;
empty.placeShipAt(row, column, horizontal, ocean);
assertFalse(ocean.shootAt(1,0));
assertEquals(8, ocean.getShotsFired());
}
@Test
void testGetHitCount() {
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.shootAt(1, 5));
assertFalse(destroyer.isSunk());
assertEquals(1, ocean.getHitCount());
//shooting at empty sea obj, should not increase hit count
Ship empty = new EmptySea();
row = 1;
column = 0;
horizontal = true;
empty.placeShipAt(row, column, horizontal, ocean);
assertFalse(ocean.shootAt(1,0));
assertEquals(1, ocean.getHitCount());
//shooting a sunk ship, should not increase hit count
assertTrue(ocean.shootAt(0, 5));
assertTrue(destroyer.isSunk());
assertEquals(2, ocean.getHitCount());
assertFalse(ocean.shootAt(0, 5));
assertEquals(2, ocean.getHitCount());
}
@Test
void testGetShipsSunk() {
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
assertTrue(ocean.shootAt(1, 5));
assertFalse(destroyer.isSunk());
assertEquals(1, ocean.getHitCount());
assertEquals(0, ocean.getShipsSunk());
//if ship is sunk, shipsSunk count should increase
assertTrue(ocean.shootAt(0, 5));
assertTrue(destroyer.isSunk());
assertEquals(1, ocean.getShipsSunk());
//hitting an empty sea should not increase shipSunk
Ship empty = new EmptySea();
row = 1;
column = 0;
horizontal = true;
empty.placeShipAt(row, column, horizontal, ocean);
assertFalse(ocean.shootAt(1,0));
assertEquals(1, ocean.getShipsSunk());
}
@Test
void testGetShipArray() {
Ship[][] shipArray = ocean.getShipArray();
assertEquals(OCEAN_SIZE, shipArray.length);
assertEquals(OCEAN_SIZE, shipArray[0].length);
assertEquals("empty", shipArray[0][0].getShipType());
// adding ship to array should not change size of array
Destroyer destroyer = new Destroyer();
int row = 1;
int column = 5;
boolean horizontal = false;
destroyer.placeShipAt(row, column, horizontal, ocean);
assertEquals(OCEAN_SIZE, shipArray.length);
assertEquals(OCEAN_SIZE, shipArray[0].length);
//adding a ship should update the array
assertEquals("destroyer", shipArray[1][5].getShipType());
Ship empty = new EmptySea();
row = 1;
column = 0;
horizontal = true;
empty.placeShipAt(row, column, horizontal, ocean);
assertEquals("empty", shipArray[1][0].getShipType());
}
}