-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.java
More file actions
86 lines (76 loc) · 2.5 KB
/
Ship.java
File metadata and controls
86 lines (76 loc) · 2.5 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
import java.util.ArrayList;
public class Ship {
private char row;
private int col;
private int length;
private String orientation;
private boolean[] hits;
public Ship(char row, int col, int length, String orientation) {
this.row = Character.toUpperCase(row);
this.col = col;
this.length = length;
this.orientation = orientation.toUpperCase();
this.hits = new boolean[length];
}
public boolean registerHit(char row, int col) {
row = Character.toUpperCase(row);
if (orientation.equals("H")) {
if (this.row == row && col >= this.col && col < this.col + length) {
hits[col - this.col] = true;
return true;
}
} else if (orientation.equals("V")) {
if (this.col == col && row >= this.row && row < this.row + length) {
hits[row - this.row] = true;
return true;
}
}
return false;
}
public boolean isSunk() {
for (boolean hit : hits) {
if (!hit) {
return false;
}
}
return true;
}
public ArrayList<int[]> getCoordinates() {
ArrayList<int[]> coordinates = new ArrayList<int[]>();
int startRow = (int) row - 65 + 1;
if (orientation.equals("H")) {
for (int i = 0; i < length; i++) {
coordinates.add(new int[] { startRow, col + i });
}
} else if (orientation.equals("V")) {
for (int i = 0; i < length; i++) {
coordinates.add(new int[] { startRow + i, col });
}
}
return coordinates;
}
public String getOrientation() {
return orientation;
}
public boolean containsCoordinate(char inRow, int inCol) {
char startRow = row;
int startCol = col;
int endCol;
char endRow;
if (getOrientation().equals("H")) {
endCol = startCol + length - 1;
endRow = startRow;
} else {
endCol = startCol;
endRow = (char) (startRow + length - 1);
}
if (inRow >= startRow && inRow <= endRow && inCol >= startCol && inCol <= endCol) {
if (getOrientation().equals("H")) {
return inRow == startRow && inCol >= startCol && inCol <= endCol;
} else {
return inCol == startCol && inRow >= startRow && inRow <= endRow;
}
}
return false;
}
}