forked from Haonan-Hu/Battleship-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.java
More file actions
165 lines (151 loc) · 3.44 KB
/
Ship.java
File metadata and controls
165 lines (151 loc) · 3.44 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
164
165
/**
Battleship!
KU EECS 448 project 2
TeamName: BigSegFaultEnergy
* \Author: Chance Penner
* \Author: Markus Becerra
* \Author: Sarah Scott
* \Author: Thomas Gardner
* \Author: Haonan Hu
* \File: Ship.java
* \Date: 10/14/2019
* \Brief: This class serves as a the executive class of
Battleship
KU EECS 448 project 1
TeamName: Poor Yorick
* \Author: Max Goad
* \Author: Jace Bayless
* \Author: Tri Pham
* \Author: Apurva Rai
* \Author: Meet Kapadia
* \File: Ship.java
* \Brief: This class serves as a the executive class of
Battleship
*/
//Here are erternal classes that need to be imported
import java.awt.Point;
import java.util.ArrayList;
public class Ship
{
//Declare variables for the ship coordinates, ship size
private ArrayList<Point> shipCoordinates = new ArrayList<Point>();
private int shipSize;
private int shipPieces;
/*
* @ pre none
* @ param Ships' size
* @ post constuctor
* @ return none
*/
public Ship(int size)
{
this.shipSize = size;
this.shipPieces = size;
}
/*
* @ pre none
* @ param none
* @ post gets ship's size
* @ return returns ships size
*/
public int getSize()
{
return shipSize;
}
/*
* @ pre none
* @ param none
* @ post gets ships coordinates
* @ return returns ship's coordinates
*/
public ArrayList<Point> getShipCoordinates() {
return shipCoordinates;
}
/*
* @ pre none
* @ param x and y values of the grid
* @ post adds new coordinates of the ship
* @ return none
*/
public void addCoordinates(int x, int y)
{
shipCoordinates.add(new Point(x, y));
}
/*
* @ pre none
* @ param new x and y values of the grid
* @ post none
* @ return returns true or false on whether or not the ships are in line
*/
public boolean inline(int newX, int newY)
{
if (shipCoordinates.size() == 0)
{
return true;
}
for (Point shipPiece : shipCoordinates)
{
int x = (int) shipPiece.getX();
int y = (int) shipPiece.getY();
if (newX == x && (newY == y + 1 || newY == y - 1))
{
return true;
}
else if (newY == y && (newX == x + 1 || newX == x - 1))
{
return true;
}
}
return false;
}
/*
* @ pre the coordinate an opponenet entered to fire at
* @ param x and y values of the grid
* @ post decreases the number of ships because it was "attacked"
* @ return none
*/
public void hit(int x, int y)
{
shipPieces--;
}
/*
* @ pre none
* @ param x and y values of the grid
* @ post none
* @ return returns true or false on whether or not a specific coordinate exists or not
*/
public boolean containsCoordinate(int x, int y)
{
if (shipCoordinates.size() == 0)
{
return false;
}
if (x >= 0 && x <= 7 && y >= 0 && y <= 7)
{
for (int i = 0; i < shipCoordinates.size(); i++)
{
int cordX = (int) shipCoordinates.get(i).getX();
int cordY = (int) shipCoordinates.get(i).getY();
if (cordX == x && cordY == y)
{
return true;
}
}
}
return false;
}
/*
* @ pre the coordinate an opponenet entered to fire at a specific ship
* @ param none
* @ post none
* @ return returns true or false on whether or not the ship is destroyed or not
*/
public boolean isDestroyed()
{
if (shipPieces <= 0)
{
return true;
}
return false;
}
}