forked from Haonan-Hu/Battleship-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
266 lines (246 loc) · 5.35 KB
/
GameBoard.java
File metadata and controls
266 lines (246 loc) · 5.35 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
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
/**
Battleship!
KU EECS 448 project 2
TeamName: BigSegFaultEnergy
* \Author: Chance Penner
* \Author: Markus Becerra
* \Author: Sarah Scott
* \Author: Thomas Gardner
* \Author: Haonan Hu
* \File: GameBoard.java
* \Date: 10/14/2019
* \Brief: This class serves as a the executive class of
Battleship as well as making basic gui elements.
KU EECS 448 project 1
TeamName: Poor Yorick
* \Author: Max Goad
* \Author: Jace Bayless
* \Author: Tri Pham
* \Author: Apurva Rai
* \Author: Meet Kapadia
* \File: GameBoard.java
* \Brief: This class serves as a the executive class of
Battleship as well as making basic gui elements.
*/
//Here are erternal classes that need to be imported
import java.awt.Point;
import java.util.ArrayList;
public class GameBoard
{
/*** OWN BOARD VARIABLES ***
0 = EMPTY SPACE
1 = SHIP
*** ***/
/*** OPP BOARD VARIABLES ***
0 = BLANK
1 = MISS
2 = HIT SHIP
3 = SUNK SHIP
*** ***/
private int boardSize;
private int[][] board;
private int[][] oppBoard;
private ArrayList<Ship> ships = new ArrayList<Ship>();
/*
* @ pre none
* @ param none
* @ post constuctor
* @ return none
*/
public GameBoard()
{
boardSize = 8;
board = new int[boardSize][boardSize];
oppBoard = new int[boardSize][boardSize];
}
/*
* @ pre none
* @ param a new ship
* @ post adds a new ship
* @ return none
*/
public void addShip(Ship newShip)
{
ArrayList<Point> shipCords = newShip.getShipCoordinates();
for (Point cords : shipCords) // A point representing a location in (x,y) coordinate space,specified in integer precision.
{
int x = (int) cords.getX();
int y = (int) cords.getY();
board[y][x] = 1;
}
ships.add(newShip);
}
/*
* @ pre none
* @ param x and y coordinates of the grid
* @ post fires onto the opposition
* @ return returns an error, miss, sunk, or hit message
*/
public String fire(int x, int y)
{
if (x >= boardSize || y >= boardSize || x < 0 || y < 0)
{
return "Error Bounds";
}
if (board[y][x] == 0)
{
return "Miss";
}
else if (board[y][x] == 1)
{
for (Ship ship : ships)
{
if (ship.containsCoordinate(x, y))
{
ship.hit(x, y);
if (ship.isDestroyed())
{
return "Sunk";
}
return "Hit";
}
}
}
return "Error";
}
/*
* @ pre none
* @ param none
* @ post gets the board
* @ return returns the board
*/
public int[][] getBoard()
{
return board;
}
/*
* @ pre none
* @ param none
* @ post get the player 2 board
* @ return returns player 2 board
*/
public int[][] getOppBoard()
{
return oppBoard;
}
/*
* @ pre one side's ships need to be all sunk
* @ param none
* @ post checks if the game is over
* @ return returns true or false whether or not the game is over
*/
public boolean gameOver()
{
for (Ship ship : ships)
{
if (!ship.isDestroyed())
{
return false;
}
}
return true;
}
/*
* @ pre none
* @ param x and y values of the grid
* @ post gets the ship at a specific coordinate
* @ return returns a ships coordinates
*/
public Ship shipAt(int x, int y)
{
for (Ship s : ships)
{
if (s.getShipCoordinates().contains(new Point(x, y)))
{
return s;
}
}
return null;
}
/*
* @ pre none
* @ param y and x values of the grid, ship's size, and if the ship is horizontal
* @ post checks to see if the stop is occupied
* @ return returns true or false on the spot being occuppied or not
*/
public boolean isOccupied(int x, int y, int size, boolean horizontal)
{
for (int rep = 0; rep < size; rep++)
{
if (horizontal)
{
if (x + rep > 7 || board[y][x + rep] == 1)
{
return true;
}
}
else if (!horizontal)
{
if (y + rep > 7 || board[y + rep][x] == 1)
{
return true;
}
}
}
return false;
}
/*
* @ pre none
* @ param x and y coordinates of the grid
* @ post updates the boards of hits and misses
* @ return returns miss, hit, or sunk messages
*/
public void updateOppBoard(int x, int y, String outcome)
{
if (outcome == "Miss")
{
oppBoard[y][x] = 1;
}
else if (outcome == "Hit")
{
oppBoard[y][x] = 2;
}
else if (outcome == "Sunk")
{
oppBoard[y][x] = 3;
}
}
/*
* @ pre none
* @ param none
* @ post prints the board
* @ return none
*/
public void printBoard()
{
System.out.println(" A B C D E F G H");
for (int i = 0; i < boardSize; i++)
{
System.out.print((i + 1) + " ");
for (int j = 0; j < boardSize; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println(" ");
}
}
/*
* @ pre none
* @ param none
* @ post prints the second board
* @ return none
*/
public void printOppBoard()
{
System.out.println(" A B C D E F G H");
for (int i = 0; i < boardSize; i++)
{
System.out.print((i + 1) + " ");
for (int j = 0; j < boardSize; j++)
{
System.out.print(oppBoard[i][j] + " ");
}
System.out.println(" ");
}
}
}