Repository navigation
Expand file tree
/
Copy pathSquareSolitaireModelImpl.java
More file actions
298 lines (276 loc) · 10.2 KB
/
Copy pathSquareSolitaireModelImpl.java
File metadata and controls
298 lines (276 loc) · 10.2 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
package cs3500.marblesolitaire.model.hw04;
import cs3500.marblesolitaire.model.hw02.BoardSpot;
import cs3500.marblesolitaire.model.hw02.MarbleSolitaireModel;
/**
* Represents a square board implementation of the (@code MarbleSolitaireModel).
*/
public abstract class SquareSolitaireModelImpl implements MarbleSolitaireModel {
protected int armLength;
protected BoardSpot[][] board;
/**
* Constructor that takes in armLength, sRow, and sCol.
*
* <p>@param armLength represents the length of one edge side of the board.
*
* @param sRow represents the row of the initial empty spot.
* @param sCol represents the column of the initial empty spot.</p>
*/
public SquareSolitaireModelImpl(int armLength, int sRow, int sCol) {
if (!isValidArmLength(armLength)) {
throw new IllegalArgumentException("Arm length is invalid.");
} else if (!isValidPosition(armLength, sRow, sCol)) {
String errMsg = String.format("Invalid empty cell position (%d,%d)", sRow, sCol);
throw new IllegalArgumentException(errMsg);
} else {
this.armLength = armLength;
this.board = createBoard(armLength);
this.initializeBoard(sRow, sCol);
}
}
/**
* Fills in the null board with the correct BoardSpot in each position.
*
* <p>@param row Represents the row of the empty spot on the board.
*
* @param col Represents the column of the empty spot on the board.</p>
*/
private void initializeBoard(int row, int col) {
int dimension = 3 * this.armLength - 2;
for (int r = 0; r < dimension; r++) {
for (int c = 0; c < dimension; c++) {
//set the invalid spots
if (!(this.isValidPosition(armLength, r, c))) {
this.board[r][c] = BoardSpot.INVALID;
} else {
this.board[r][c] = BoardSpot.MARBLE;
}
}
}
this.board[row][col] = BoardSpot.EMPTY;
}
/**
* Determines whether the given position is valid for a board of the given arm length.
*
* <p>@param armLength Represents the length of the arm of the board.
*
* @param row Represents the row in the position.
* @param col Represents the column in the position.
* @return True if the position is valid, false otherwise.</p>
*/
protected boolean isValidPosition(int armLength, int row, int col) {
//Checks that the position is within the dimensions of the board.
boolean checkPositionOnBoard = ((row >= 0 && row <= 3 * armLength - 3)
&& (col >= 0 && col <= 3 * armLength - 3));
//Checks that the position is not an invalid spot.
boolean checkPositionNotInvalid;
if (row < armLength - 1 || row > 2 * armLength - 2) {
checkPositionNotInvalid = (col >= armLength - 1 && col <= 2 * armLength - 2);
} else {
checkPositionNotInvalid = true;
}
//Return the final boolean.
return checkPositionOnBoard && checkPositionNotInvalid;
}
/**
* Initializes a square board.
*
* <p>@param armLength the length of the arm (must be odd and positive).</p>
*/
private static BoardSpot[][] createBoard(int armLength) {
int dimension = 3 * armLength - 2;
BoardSpot[][] ans = new BoardSpot[dimension][dimension];
return ans;
}
/**
* Checks that the armLength is a positive odd number (must be greater than 1 since a game
* with armLength of 1 has no valid moves.)
*
* <p>@param armLength the length of the arm (must be odd and positive).</p>
*/
private static boolean isValidArmLength(int armLength) {
return (armLength > 1 && armLength % 2 == 1);
}
/**
* Move a single marble from a given position to another given position. A move is valid only if
* the from and to positions are valid. Specific implementations may place additional constraints
* on the validity of a move.
*
* <p>@param fromRow the row number of the position to be moved from (starts at 0)
*
* @param fromCol the column number of the position to be moved from (starts at 0)
* @param toRow the row number of the position to be moved to (starts at 0)
* @param toCol the column number of the position to be moved to (starts at 0)
* @throws IllegalArgumentException if the move is not possible</p>
*/
/*
* (a) the “from” and “to” positions are valid
* (b) there is a marble at the specified “from” position
* (c) the “to” position is empty
* (d) the “to” and “from” positions are exactly two positions away (horizontally or vertically)
* (e) there is a marble in the slot between the “to” and “from” positions.
*
* */
@Override
public void move(int fromRow, int fromCol, int toRow, int toCol) throws IllegalArgumentException {
//First check that the To and From positions are valid.
boolean fromIsValid = isValidPosition(this.armLength, fromRow, fromCol);
boolean toIsValid = isValidPosition(this.armLength, toRow, toCol);
//If To and From are valid, check that the move can be made.
if (!(fromIsValid && toIsValid)) {
throw new IllegalArgumentException("To or From position is invalid.");
} else {
boolean marbleAtFrom = (this.board[fromRow][fromCol] == BoardSpot.MARBLE);
boolean emptyAtTo = (this.board[toRow][toCol] == BoardSpot.EMPTY);
boolean distanceIsTwo = ((Math.abs(fromCol - toCol) == 2 && fromRow == toRow)
|| (Math.abs(fromRow - toRow) == 2 && fromCol == toCol));
//If move can be made, check that a marble is in between.
if (!(marbleAtFrom && emptyAtTo && distanceIsTwo)) {
throw new IllegalArgumentException("Move could not be made.");
} else {
int midRow;
int midCol;
if (toRow != fromRow) {
if (toRow > fromRow) {
midRow = fromRow + 1;
midCol = toCol;
} else {
midRow = fromRow - 1;
midCol = toCol;
}
} else {
if (toCol > fromCol) {
midRow = toRow;
midCol = fromCol + 1;
} else {
midRow = toRow;
midCol = fromCol - 1;
}
}
//Check that marble is actually in between
if (this.board[midRow][midCol] != BoardSpot.MARBLE) {
throw new IllegalArgumentException("No marble in between.");
} else {
this.board[fromRow][fromCol] = BoardSpot.EMPTY;
this.board[midRow][midCol] = BoardSpot.EMPTY;
this.board[toRow][toCol] = BoardSpot.MARBLE;
}
}
}
}
/**
* Determine and return if the game is over or not. A game is over if no more moves can be made.
*
* <p>@return true if the game is over, false otherwise</p>
*/
@Override
public boolean isGameOver() {
//Check that all empty spots have no two adjacent marbles in any direction.
int dimension = this.board.length;
boolean gameIsOver = true;
for (int r = 0; r < dimension; r++) {
for (int c = 0; c < dimension; c++) {
if (this.board[r][c] == BoardSpot.EMPTY) {
if (r >= 2) {
//Checks above it
gameIsOver &= (this.board[r - 1][c] != BoardSpot.MARBLE
|| this.board[r - 2][c] != BoardSpot.MARBLE);
}
if (r <= dimension - 3) {
//Checks below it
gameIsOver &= (this.board[r + 1][c] != BoardSpot.MARBLE
|| this.board[r + 2][c] != BoardSpot.MARBLE);
}
if (c >= 2) {
//Checks to the left
gameIsOver &= (this.board[r][c - 1] != BoardSpot.MARBLE
|| this.board[r][c - 2] != BoardSpot.MARBLE);
}
if (c <= dimension - 3) {
//Checks to the right
gameIsOver &= (this.board[r][c + 1] != BoardSpot.MARBLE
|| this.board[r][c + 2] != BoardSpot.MARBLE);
}
}
}
}
return gameIsOver;
}
/**
* Return a string that represents the current state of the board. The string should have one line
* per row of the game board. Each slot on the game board is a single character (O, _ or space for
* a marble, empty and invalid position respectively). Slots in a row should be separated by a
* space. Each row has no space before the first slot and after the last slot.
*
* <p>@return the game state as a string</p>
*/
@Override
public String getGameState() {
int dimension = 3 * armLength - 2;
String[] rows = new String[dimension];
//Creates an array of Strings where each element represents the row in the board
for (int r = 0; r < dimension; r++) {
rows[r] = "";
boolean readyForNewline = false; //resetting the row resets this value
for (BoardSpot c : this.board[r]) {
//if we're at the last row
if ((r == dimension - 1) && (c == BoardSpot.INVALID) && readyForNewline) {
break; // end the full loop
}
switch (c) {
case EMPTY:
//add the empty spot
readyForNewline = true;
rows[r] += "_";
break;
case MARBLE:
//add the marble, now ready for newline
readyForNewline = true;
rows[r] += "O";
break;
case INVALID:
//add an invalid if not yet ready for newline, else add a newline and reset r and c
if (!readyForNewline) {
rows[r] += " ";
} else {
continue;
}
break;
default:
throw new RuntimeException(String.format("Invalid BoardSpot: %s.", c));
}
}
}
//formats the array of Strings as a single String
String ans = "";
for (int s = 0; s < rows.length; s++) {
String tmp = "";
for (int i = 0; i < rows[s].length(); i++) {
if (i < rows[s].length() - 1) {
tmp += rows[s].charAt(i) + " ";
} else {
tmp += rows[s].charAt(i);
}
}
rows[s] = tmp;
}
ans += String.join("\n", rows);
return ans;
}
/**
* Return the number of marbles currently on the board.
*
* <p>@return the number of marbles currently on the board</p>
*/
@Override
public int getScore() {
int score = 0;
for (BoardSpot[] bArr : this.board) {
for (BoardSpot b : bArr) {
if (b == BoardSpot.MARBLE) {
score += 1;
}
}
}
return score;
}
}