-
Notifications
You must be signed in to change notification settings - Fork 0
/
BattleshipBoard.java
374 lines (342 loc) · 10.2 KB
/
BattleshipBoard.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class BattleshipBoard {
/** Static constant to indicate a board location is empty */
public static final char EMPTY = '.';
/** The 2D array representing the board */
private char board[][];
/** The number of battleships left */
private int numBattleshipsLeft;
/** The ID of the current ship being placed */
private int currentShipID;
/** The number of rows */
private int numRows;
/** The number of columns */
private int numCols;
/** Maintains information about the shots taken by each ship */
private ArrayList<Integer> shipInfo;
/**
* Constructor for the Battleship Board. It allows for boards of arbitrary
* rows and columns to be created. The board must be empty initially (before
* any ships are placed).
*
* @param numRows
* The number of rows
* @param numCols
* The number of columns
* @throws BattleshipException if numRows or numCols are <= 0
*/
public BattleshipBoard(int numRows, int numCols) throws BattleshipException {
if( numRows <= 0 ) {
throw new BattleshipException("Invalid number of rows");
}
if( numCols <= 0 ) {
throw new BattleshipException("Invalid number of columns");
}
this.numRows = numRows;
this.numCols = numCols;
this.board = new char[numCols][numRows];
for (int c = 0; c < numCols; c++) {
for (int r = 0; r < numRows; r++) {
this.board[c][r] = BattleshipBoard.EMPTY;
}
}
this.numBattleshipsLeft = 0;
this.currentShipID = 0;
this.shipInfo = new ArrayList<Integer>();
}
/**
* Constructor for the Battleship Board.
* File must have 10 lines, with exactly 10 characters per line.
* '.' represents EMPTY. A unique number (0, 1, 2, 3, or 4) will represent each ship.
* Example File:
* ..........
* ..........
* ...11111..
* ..02......
* ..02......
* ..02......
* ...2......
* ...33344..
* ..........
* ..........
*
* @param boardFile a text file with a 10x10 character grid, representing a battleship board
* @throws FileNotFoundException if the board file is not found
* @author richmaja
*/
public BattleshipBoard(File boardFile) throws FileNotFoundException{
this.numRows = 10;
this.numCols = 10;
this.board = new char[numCols][numRows];
this.shipInfo = new ArrayList<Integer>();
this.numBattleshipsLeft = 5;
//Add 5 entries to shipInfo
for(int i=0; i < 5; i++){
shipInfo.add(0);
}
try{
if(!boardFile.exists()) throw new FileNotFoundException("Error Reading: "+boardFile.getName());
FileReader fr = new FileReader(boardFile);
BufferedReader br = new BufferedReader(fr);
String line;
int lineCounter = 0;
while((line = br.readLine())!=null){
for(int i = 0; i < line.length(); i++){
char currentCharacter = line.charAt(i);
board[i][lineCounter] = currentCharacter;
}
lineCounter++;
}
br.close();
fr.close();
for(int c = 0; c < this.numCols; c++){
for(int r = 0; r < this.numRows; r++){
try{
int shipIdVal = Integer.parseInt(board[c][r]+"");
shipInfo.set(shipIdVal, shipInfo.get(shipIdVal)+1);
}catch(NumberFormatException e){}
}
}
} catch(IOException e){
System.out.println("Error: "+e.getMessage());
throw new FileNotFoundException();
}
}
/**
* Getter for the number of rows in the board
*
* @return The number of rows in the Battleship board
*/
public int getNumRows() {
return this.numRows;
}
/**
* Getter for the number of columns in the board
*
* @return The number of columns in the Battleship board
*/
public int getNumCols() {
return this.numCols;
}
/**
* Gets the number of Battleships left
*
* @return The number of battleships left
*/
public int getNumBattleshipsLeft() {
return this.numBattleshipsLeft;
}
/**
* Checks if the cell is occupied.
* @param col The column
* @param row The row
* @return true if the cell is occupied
*/
public boolean isOccupied(int col, int row){
if(row >= 0 && row < getNumRows() && col >= 0 && col < getNumCols()){
return board[col][row]!=EMPTY;
}
return false;
}
/**
* Returns the char value at a certain row and column of the board
* @param col The column on which the cell resides
* @param row The row on which the cell resides
* @return the char value at a certain row and column of the board
*/
public char getCellContent(int col, int row){
if(row >= 0 && row < getNumRows() && col >= 0 && col < getNumCols()){
return board[col][row];
}
return EMPTY;
}
/**
* Checks if the start and end row positions are a vertical placement of the
* battleship
*
* @param startRow
* The starting row position of the battleship
* @param endRow
* The ending row position of the battleship
* @return true if it is a vertical placement, and false otherwise
*/
private boolean isVerticalPlacement(int startRow, int endRow) {
if (startRow == endRow) {
return true;
} else {
return false;
}
}
/**
* Checks if the start and end col positions are a horizontal placement of the
* battleship
*
* @param startCol
* The starting Col position of the battleship
* @param endCol
* The ending Col position of the battleship
* @return true if it is a horizontal placement, and false otherwise
*/
private boolean isHorizontalPlacement(int startCol, int endCol) {
if (startCol == endCol) {
return true;
} else {
return false;
}
}
/**
* Checks of the ship can be legally placed without overlapping with
* existing ships
*
* @param startColumn
* The starting Column coordinate
* @param startRow
* The starting Row coordinate
* @param endColumn
* The ending Column coordinate
* @param endRow
* The ending Row coordinate
* @return true if the is a legal placement, false otherwise
*/
private boolean checkShipPlacement(int startColumn, int startRow, int endColumn,
int endRow) {
// Do a check first
if (startRow == endRow) {
for (int c = startColumn; c <= endColumn; c++) {
if (this.board[c][startRow] != BattleshipBoard.EMPTY) {
return false;
}
}
} else if (startColumn == endColumn) {
for (int r = startRow; r <= endRow; r++) {
if (this.board[startColumn][r] != BattleshipBoard.EMPTY) {
return false;
}
}
}
return true;
}
/**
* Places the battleship at the starting coordinates (startCol,startRow) and the
* end coordinates (endCol,endRow) inclusive. This means that if you place a ship
* at (1,3) to (1,5), it is placed on (1,3), (1,4) AND (1,5). Size 1 ships are
* allowed to be placed.
*
* @param startColumn
* The starting Column coordinate
* @param startRow
* The starting Row coordinate
* @param endColumn
* The ending Column coordinate
* @param endRow
* The ending Row coordinate
* @throws IndexOutOfBoundsException Thrown if the coordinates are out of bounds.
* The row coordinate is in bounds if 0 <= row < (number of rows - 1).
* The col coordinate is in bounds if 0 <= col < (number of columns - 1).
* BattleshipException
* If ship is placed diagonally
* If it overlaps with another battleship
* If startRow > endRow or startColumn > endColumn.
*/
public void placeShip(int startColumn, int startRow, int endColumn, int endRow)
throws BattleshipException, IndexOutOfBoundsException {
int shipSize = 0;
if (!isHorizontalPlacement(startColumn, endColumn)
&& !isVerticalPlacement(startRow, endRow)) {
throw new BattleshipException("Cannot place battleship diagonally");
}
if (!checkShipPlacement(startColumn, startRow, endColumn, endRow)) {
throw new BattleshipException("Battleship overlaps another battleship");
}
if( startRow > endRow ) {
throw new BattleshipException("Invalid startRow coordinates");
}
if( startColumn > endColumn ) {
throw new BattleshipException("Invalid startColumn coordinates");
}
// Then place ship
if (startRow == endRow) {
for (int c = startColumn; c <= endColumn; c++) {
this.board[c][startRow] = Character.forDigit(currentShipID,10);
shipSize++;
}
} else if (startColumn == endColumn) {
for (int r = startRow; r <= endRow; r++) {
this.board[startColumn][r] = Character.forDigit(currentShipID,10);
shipSize++;
}
}
this.shipInfo.add(new Integer(shipSize));
this.currentShipID++;
this.numBattleshipsLeft++;
}
/**
* Fires a shot at a battleship
*
* @param col
* The col coordinate of the shot
* @param row
* The row coordinate of the shot
* @return true if an enemy battleship is hit, false otherwise. If you fire twice at the same
* spot and a battleship was at that spot, this method returns true both times.
* @throws IndexOutOfBoundsException
* if row or col are out of bounds
*/
public boolean fireShot(int col, int row) throws IndexOutOfBoundsException {
if (this.board[col][row] != BattleshipBoard.EMPTY) {
int shipID = Character.digit(this.board[col][row],10);
Integer shotsLeft = this.shipInfo.get(shipID);
shotsLeft--;
if( shotsLeft == 0 ) {
this.numBattleshipsLeft--;
}
this.shipInfo.set(shipID,new Integer(shotsLeft));
return true;
} else {
return false;
}
}
/**
* Prints the board
*/
public void print() {
for (int r = 0; r < this.numRows; r++) {
StringBuffer sb = new StringBuffer(this.numCols);
for (int c = 0; c < this.numCols; c++) {
sb.append(this.board[c][r]);
}
System.out.println(sb.toString());
}
}
/**
* Returns true if game is over, false otherwise
* @return true if game is over, false otherwise
*/
public boolean isGameOver() {
if( this.numBattleshipsLeft <= 0 ) {
return true;
} else {
return false;
}
}
/**
* Main function
*
* @param args
* Command line arguments
*/
public static void main(String args[]) {
try {
System.out.println("My Main Method Is Stupid. It Does NOTHING");
} catch (Exception e) {
e.printStackTrace();
//System.out.println(e.getMessage());
System.exit(-1);
}
}
}