forked from ymarcus93/Java-Battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
310 lines (282 loc) · 9.6 KB
/
Grid.java
File metadata and controls
310 lines (282 loc) · 9.6 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
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
public class Grid
{
private Location[][] grid;
private int points;
// Constants for number of rows and columns.
public static final int NUM_ROWS = 10;
public static final int NUM_COLS = 10;
public Grid()
{
if (NUM_ROWS > 26)
{
throw new IllegalArgumentException("ERROR! NUM_ROWS CANNOT BE > 26");
}
grid = new Location[NUM_ROWS][NUM_COLS];
for (int row = 0; row < grid.length; row++)
{
for (int col = 0; col < grid[row].length; col++)
{
Location tempLoc = new Location();
grid[row][col] = tempLoc;
}
}
}
// Mark a hit in this location by calling the markHit method
// on the Location object.
public void markHit(int row, int col)
{
grid[row][col].markHit();
points++;
}
// Mark a miss on this location.
public void markMiss(int row, int col)
{
grid[row][col].markMiss();
}
// Set the status of this location object.
public void setStatus(int row, int col, int status)
{
grid[row][col].setStatus(status);
}
// Get the status of this location in the grid
public int getStatus(int row, int col)
{
return grid[row][col].getStatus();
}
// Return whether or not this Location has already been guessed.
public boolean alreadyGuessed(int row, int col)
{
return !grid[row][col].isUnguessed();
}
// Set whether or not there is a ship at this location to the val
public void setShip(int row, int col, boolean val)
{
grid[row][col].setShip(val);
}
// Return whether or not there is a ship here
public boolean hasShip(int row, int col)
{
return grid[row][col].hasShip();
}
// Get the Location object at this row and column position
public Location get(int row, int col)
{
return grid[row][col];
}
// Return the number of rows in the Grid
public int numRows()
{
return NUM_ROWS;
}
// Return the number of columns in the grid
public int numCols()
{
return NUM_COLS;
}
public void printStatus()
{
generalPrintMethod(0);
}
public void printShips()
{
generalPrintMethod(1);
}
public void printCombined()
{
generalPrintMethod(2);
}
public boolean hasLost()
{
if (points >= 17)
return true;
else
return false;
}
public void addShip(Ship s)
{
int row = s.getRow();
int col = s.getCol();
int length = s.getLength();
int dir = s.getDirection();
if (!(s.isDirectionSet()) || !(s.isLocationSet()))
throw new IllegalArgumentException("ERROR! Direction or Location is unset/default");
// 0 - hor; 1 - ver
if (dir == 0) // Hortizontal
{
for (int i = col; i < col+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
grid[row][i].setShip(true);
grid[row][i].setLengthOfShip(length);
grid[row][i].setDirectionOfShip(dir);
}
}
else if (dir == 1) // Vertical
{
for (int i = row; i < row+length; i++)
{
//System.out.println("DEBUG: row = " + row + "; col = " + i);
grid[i][col].setShip(true);
grid[i][col].setLengthOfShip(length);
grid[i][col].setDirectionOfShip(dir);
}
}
}
// Type: 0 for status, 1 for ships, 2 for combined
private void generalPrintMethod(int type)
{
System.out.println();
// Print columns (HEADER)
System.out.print(" ");
for (int i = 1; i <= NUM_COLS; i++)
{
System.out.print(i + " ");
}
System.out.println();
// Print rows
int endLetterForLoop = (NUM_ROWS - 1) + 65;
for (int i = 65; i <= endLetterForLoop; i++)
{
char theChar = (char) i;
System.out.print(theChar + " ");
for (int j = 0; j < NUM_COLS; j++)
{
if (type == 0) // type == 0; status
{
if (grid[switchCounterToIntegerForArray(i)][j].isUnguessed())
System.out.print("- ");
else if (grid[switchCounterToIntegerForArray(i)][j].checkMiss())
System.out.print("O ");
else if (grid[switchCounterToIntegerForArray(i)][j].checkHit())
System.out.print("X ");
}
else if (type == 1) // type == 1; ships
{
if (grid[switchCounterToIntegerForArray(i)][j].hasShip())
{
// System.out.print("X ");
if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 2)
{
System.out.print("D ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 3)
{
System.out.print("C ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 4)
{
System.out.print("B ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 5)
{
System.out.print("A ");
}
}
else if (!(grid[switchCounterToIntegerForArray(i)][j].hasShip()))
{
System.out.print("- ");
}
}
else // type == 2; combined
{
if (grid[switchCounterToIntegerForArray(i)][j].checkHit())
System.out.print("X ");
else if (grid[switchCounterToIntegerForArray(i)][j].hasShip())
{
// System.out.print("X ");
if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 2)
{
System.out.print("D ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 3)
{
System.out.print("C ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 4)
{
System.out.print("B ");
}
else if (grid[switchCounterToIntegerForArray(i)][j].getLengthOfShip() == 5)
{
System.out.print("A ");
}
}
else if (!(grid[switchCounterToIntegerForArray(i)][j].hasShip()))
{
System.out.print("- ");
}
}
}
System.out.println();
}
}
public int switchCounterToIntegerForArray (int val)
{
int toReturn = -1;
switch (val)
{
case 65: toReturn = 0;
break;
case 66: toReturn = 1;
break;
case 67: toReturn = 2;
break;
case 68: toReturn = 3;
break;
case 69: toReturn = 4;
break;
case 70: toReturn = 5;
break;
case 71: toReturn = 6;
break;
case 72: toReturn = 7;
break;
case 73: toReturn = 8;
break;
case 74: toReturn = 9;
break;
case 75: toReturn = 10;
break;
case 76: toReturn = 11;
break;
case 77: toReturn = 12;
break;
case 78: toReturn = 13;
break;
case 79: toReturn = 14;
break;
case 80: toReturn = 15;
break;
case 81: toReturn = 16;
break;
case 82: toReturn = 17;
break;
case 83: toReturn = 18;
break;
case 84: toReturn = 19;
break;
case 85: toReturn = 20;
break;
case 86: toReturn = 21;
break;
case 87: toReturn = 22;
break;
case 88: toReturn = 23;
break;
case 89: toReturn = 24;
break;
case 90: toReturn = 25;
break;
default: toReturn = -1;
break;
}
if (toReturn == -1)
{
throw new IllegalArgumentException("ERROR OCCURED IN SWITCH");
}
else
{
return toReturn;
}
}
}