-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.java
More file actions
163 lines (144 loc) · 3.85 KB
/
Copy pathShip.java
File metadata and controls
163 lines (144 loc) · 3.85 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
package finalproject;
import java.util.Arrays;
import cse131.NotYetImplementedException;
/**
* @author Mariah Yelenick and Adam Kern
* @version 11/18/18
*/
public class Ship {
private int topLeftX;
private int topLeftY;
private int length;
private boolean isHorizontal;
private boolean[] xa;
private int[] xCoordinates;
private int[] yCoordinates;
/**
* Create an instance of the ship class, recording all necessary information
* into any instance variables you choose to create
*
* @param topLeftX
* the x coordinate of the ship's uppermost, leftmost space
* @param topLeftY
* the y coordinate of the ship's uppermost, leftmost space
* @param length
* the number of spaces the ship occupies
* @param isHorizontal
* if true, the ship is placed horizontally, else the ship is placed
* vertically
*/
public Ship(int topLeftX, int topLeftY, int length, boolean isHorizontal) {
this.topLeftX = topLeftX;
this.topLeftY = topLeftY;
this.length = length;
this.isHorizontal = isHorizontal;
this.xCoordinates = new int[length];
this.yCoordinates = new int[length];
this.xa = new boolean[length];
Arrays.fill(xa, false);
if (isHorizontal == true) {
for (int i = 0; i < length; i++) {
this.xCoordinates[i] = topLeftX + i;
this.yCoordinates[i]=topLeftY;
}
} else {
for (int i = 0; i < length; i++) {
this.yCoordinates[i] = topLeftY + i;
}
Arrays.fill(xCoordinates, topLeftX);
}
}
public int[] getxCoordinates() {
return xCoordinates;
}
public int[] getyCoordinates() {
return yCoordinates;
}
public boolean getIsHorizontal() {
return isHorizontal;
}
public int getLength() {
return length;
}
/**
* Check if the ship has been sunk This should only be true if the ship was hit
* in all the spaces it occupies
*
* @return true if the ship has been sunk
*/
public boolean isSunk() {
for(int i = 0; i < length; ++i) {
if(!this.xa[i]==true) {
return false;
}
}
return true;
}
@Override
public String toString() {
return "Ship [topLeftX=" + topLeftX + ", topLeftY=" + topLeftY + ", length=" + length + ", isHorizontal="
+ isHorizontal + ", xa=" + Arrays.toString(xa) + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (isHorizontal ? 1231 : 1237);
result = prime * result + length;
result = prime * result + topLeftX;
result = prime * result + topLeftY;
result = prime * result + Arrays.hashCode(xa);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ship other = (Ship) obj;
if (isHorizontal != other.isHorizontal)
return false;
if (length != other.length)
return false;
if (topLeftX != other.topLeftX)
return false;
if (topLeftY != other.topLeftY)
return false;
if (!Arrays.equals(xa, other.xa))
return false;
return true;
}
/**
* Check if the ship occupies a space at (x, y) Note: Be sure to update the hits
* array so that you can check if the ship is sunk!
*
* @param x
* the x coordinate to shoot at
* @param y
* the y coordinate to shoot at
* @return true if this ship occupies that spot (hit), false otherwise (miss)
*/
public boolean isHit(int x, int y) {
for (int i = 0; i < length; ++i) {
if (this.xCoordinates[i] == x && this.yCoordinates[i] == y) {
this.xa[i] = true;
return true;
}
}
return false;
}
public boolean checkOverlap(Ship other) {
for (int i = 0; i < this.length; ++i) {
for (int j = 0; j < other.length; ++j) {
if (this.getxCoordinates()[i] == other.getxCoordinates()[j]
&& this.getyCoordinates()[i] == other.getyCoordinates()[j]) {
return true;
}
}
}
return false;
}
}