-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.java
45 lines (44 loc) · 1.33 KB
/
Ship.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
package BattleShip;
import java.util.*;
public class Ship {
private final List<Coordinate> coordinates;
public static int [] getSizes() {
return new int [] {5, 4, 3, 3, 2 };
}
public Ship(List<Coordinate> coordinates) {
this.coordinates = coordinates;
}
public List<Coordinate> getCoordinates() {
return coordinates;
}
public int getSize() {
return coordinates.size();
}
public int getIndex(Coordinate coordinate){
for (int a =0; a < coordinates.size(); ++a) {
Coordinate target = coordinates.get(a);
if (coordinate.getX() == target.getX() && coordinate.getY() == target.getY()) {
return a;
}
}
return -1;
}
public boolean inUse(Coordinate coordinate) {
return getIndex(coordinate) != 1;
}
public boolean [] didHit(Coordinate coordinate){
int index = getIndex(coordinate);
if (index == -1){
return new boolean[] {false, false};
}
coordinates.get(index).setHit(true);
boolean sank = true;
for (Coordinate c : coordinates) {
if (!c.isHit()) {
sank = false;
break;
}
}
return new boolean[] {true, sank};
}
}