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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Next, let's write the GameBoard class.
|
| 2 |
+
|
| 3 |
+
The class has the following features:
|
| 4 |
+
1
|
| 5 |
+
Attribute 'size', which must have the 'final' modifier.
|
| 6 |
+
The class can also have other attributes as needed, but size is mandatory.
|
| 7 |
+
|
| 8 |
+
2
|
| 9 |
+
Constructor that takes the size of the game board (an integer) as a parameter
|
| 10 |
+
|
| 11 |
+
3
|
| 12 |
+
Method boolean addPieces(Piece piece, int number), which tries to add the given number of pieces to the board.
|
| 13 |
+
If the addition is successful (i.e., there was enough free space on the board), the method returns true and adds the pieces to the board.
|
| 14 |
+
Otherwise, the method returns false and does nothing.
|
| 15 |
+
|
| 16 |
+
4
|
| 17 |
+
Method int freeSquares(), which returns the number of free squares on the board as an integer
|
| 18 |
+
|
| 19 |
+
5
|
| 20 |
+
Method Piece lastAdded(), which returns the piece used in the last successful addition
|
| 21 |
+
|
| 22 |
+
6
|
| 23 |
+
Method getSize(), which returns the size of the game board. The size cannot change after initialization
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import java.util.Random;
|
| 32 |
+
import java.lang.reflect.Field;
|
| 33 |
+
|
| 34 |
+
public class Test {
|
| 35 |
+
public static void main(String[] args) {
|
| 36 |
+
final Random r = new Random();
|
| 37 |
+
|
| 38 |
+
System.out.println("Testing the GameBoard class...");
|
| 39 |
+
|
| 40 |
+
System.out.println("Test 1");
|
| 41 |
+
|
| 42 |
+
GameBoard board = new GameBoard(10);
|
| 43 |
+
System.out.println("GameBoard created!");
|
| 44 |
+
|
| 45 |
+
Piece black = new Piece(Color.BLACK);
|
| 46 |
+
Piece white = new Piece(Color.WHITE);
|
| 47 |
+
|
| 48 |
+
try {
|
| 49 |
+
Field size = board.getClass().getDeclaredField("size");
|
| 50 |
+
System.out.println("size is private and final: " + (size.getModifiers() > 17));
|
| 51 |
+
} catch (NoSuchFieldException e) {
|
| 52 |
+
System.out.println("GameBoard class does not have an attribute size!");
|
| 53 |
+
} catch (SecurityException e) {}
|
| 54 |
+
|
| 55 |
+
System.out.println("getSize returns " + board.getSize());
|
| 56 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 57 |
+
System.out.println("");
|
| 58 |
+
|
| 59 |
+
System.out.println("Playing 3 black...");
|
| 60 |
+
System.out.println("Addition successful: " + board.addPieces(black, 3));
|
| 61 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 62 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 63 |
+
System.out.println("");
|
| 64 |
+
|
| 65 |
+
System.out.println("Playing 3 white...");
|
| 66 |
+
System.out.println("Addition successful: " + board.addPieces(white, 3));
|
| 67 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 68 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 69 |
+
System.out.println("");
|
| 70 |
+
|
| 71 |
+
System.out.println("Playing 2 black...");
|
| 72 |
+
System.out.println("Addition successful: " + board.addPieces(black, 2));
|
| 73 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 74 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 75 |
+
System.out.println("");
|
| 76 |
+
|
| 77 |
+
System.out.println("Playing 3 white...");
|
| 78 |
+
System.out.println("Addition successful: " + board.addPieces(white, 3));
|
| 79 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 80 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 81 |
+
System.out.println("");
|
| 82 |
+
|
| 83 |
+
System.out.println("Test 2");
|
| 84 |
+
|
| 85 |
+
board = new GameBoard(5);
|
| 86 |
+
System.out.println("GameBoard created!");
|
| 87 |
+
|
| 88 |
+
System.out.println("getSize returns " + board.getSize());
|
| 89 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 90 |
+
System.out.println("");
|
| 91 |
+
|
| 92 |
+
System.out.println("Playing 3 black...");
|
| 93 |
+
System.out.println("Addition successful: " + board.addPieces(black, 3));
|
| 94 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 95 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 96 |
+
System.out.println("");
|
| 97 |
+
|
| 98 |
+
System.out.println("Playing 3 white...");
|
| 99 |
+
System.out.println("Addition successful: " + board.addPieces(white, 3));
|
| 100 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 101 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 102 |
+
System.out.println("");
|
| 103 |
+
|
| 104 |
+
System.out.println("Playing 2 black...");
|
| 105 |
+
System.out.println("Addition successful: " + board.addPieces(black, 2));
|
| 106 |
+
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
|
| 107 |
+
System.out.println("Free squares: " + board.freeSquares());
|
| 108 |
+
System.out.println("");
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
//ADD
|
| 116 |
+
// round1 - ArrayList
|
| 117 |
+
// AND SQUARE GAMEBOARD
|
| 118 |
+
// class GameBoard {
|
| 119 |
+
// public final int size;
|
| 120 |
+
// public ArrayList<Piece> pieces;
|
| 121 |
+
// public Piece lastAdded;
|
| 122 |
+
//
|
| 123 |
+
// public GameBoard(int size) {
|
| 124 |
+
// this.size = size;
|
| 125 |
+
// this.pieces = new ArrayList<Piece>();
|
| 126 |
+
// }
|
| 127 |
+
//
|
| 128 |
+
// public boolean addPieces(Piece piece, int number) {
|
| 129 |
+
// // gameboard is 2D
|
| 130 |
+
// // so size = 1D of the gameboard
|
| 131 |
+
// // assume gameboard has dimensions OR num_squares = size x size
|
| 132 |
+
// // approach 1
|
| 133 |
+
// // if (this.pieces.size() + number > this.size * this.size) {
|
| 134 |
+
// // approach 2 - using freeSquares()
|
| 135 |
+
// if (number > this.freeSquares()) {
|
| 136 |
+
// return false;
|
| 137 |
+
// }
|
| 138 |
+
//
|
| 139 |
+
// // add pieces to the gameboard
|
| 140 |
+
// for (int i = 0; i < number; i++) {
|
| 141 |
+
// this.pieces.add(piece);
|
| 142 |
+
//
|
| 143 |
+
// // track last piece
|
| 144 |
+
// if (i == number - 1) {
|
| 145 |
+
// this.lastAdded = piece;
|
| 146 |
+
// }
|
| 147 |
+
// }
|
| 148 |
+
// return true;
|
| 149 |
+
//
|
| 150 |
+
// }
|
| 151 |
+
//
|
| 152 |
+
// public int getSize() {
|
| 153 |
+
// return this.size;
|
| 154 |
+
// }
|
| 155 |
+
//
|
| 156 |
+
// public int freeSquares() {
|
| 157 |
+
//this.pieces.size() = arraylist length
|
| 158 |
+
// return this.size * this.size - this.pieces.size();
|
| 159 |
+
// }
|
| 160 |
+
//
|
| 161 |
+
// public Piece lastAdded() {
|
| 162 |
+
// return this.lastAdded;
|
| 163 |
+
// }
|
| 164 |
+
//
|
| 165 |
+
// }
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
// round2 - NO ArrayList
|
| 175 |
+
// AND SQUARE GAMEBOARD
|
| 176 |
+
// class GameBoard {
|
| 177 |
+
// private final int size;
|
| 178 |
+
// private Piece[] board;
|
| 179 |
+
// private int pieceCount;
|
| 180 |
+
// private Piece lastAdded;
|
| 181 |
+
//
|
| 182 |
+
// public GameBoard(int size) {
|
| 183 |
+
// this.size = size;
|
| 184 |
+
// this.board = new Piece[size * size];
|
| 185 |
+
// }
|
| 186 |
+
//
|
| 187 |
+
// public boolean addPieces(Piece piece, int number) {
|
| 188 |
+
// // gameboard is 2D
|
| 189 |
+
// // so size = 1D of the gameboard
|
| 190 |
+
// // assume gameboard has dimensions OR num_squares = size x size
|
| 191 |
+
// // approach 1
|
| 192 |
+
// // if (this.pieces.size() + number > this.size * this.size) {
|
| 193 |
+
// // approach 2 - using freeSquares()
|
| 194 |
+
// if (number > this.freeSquares()) {
|
| 195 |
+
// return false;
|
| 196 |
+
// }
|
| 197 |
+
//
|
| 198 |
+
// // add pieces to the gameboard
|
| 199 |
+
// for (int i = 0; i < number; i++) {
|
| 200 |
+
// board[pieceCount + i] = piece;
|
| 201 |
+
//
|
| 202 |
+
// // track last piece
|
| 203 |
+
// if (i == number - 1) {
|
| 204 |
+
// this.lastAdded = piece;
|
| 205 |
+
// }
|
| 206 |
+
// }
|
| 207 |
+
// return true;
|
| 208 |
+
//
|
| 209 |
+
// }
|
| 210 |
+
//
|
| 211 |
+
// public int getSize() {
|
| 212 |
+
// return this.size;
|
| 213 |
+
// }
|
| 214 |
+
//
|
| 215 |
+
// public int freeSquares() {
|
| 216 |
+
// // count number of pieces on the board
|
| 217 |
+
// int count = 0;
|
| 218 |
+
// for (Piece piece : board) {
|
| 219 |
+
// if (piece != null) {
|
| 220 |
+
// count++;
|
| 221 |
+
// }
|
| 222 |
+
// }
|
| 223 |
+
// this.pieceCount = count;
|
| 224 |
+
// return this.size * this.size - this.pieceCount;
|
| 225 |
+
// }
|
| 226 |
+
//
|
| 227 |
+
// public Piece lastAdded() {
|
| 228 |
+
// return this.lastAdded;
|
| 229 |
+
// }
|
| 230 |
+
//
|
| 231 |
+
// }
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
// round3 - NO ArrayList
|
| 246 |
+
// AND 1D, i.e. NOT SQUARE GAMEBOARD
|
| 247 |
+
class GameBoard {
|
| 248 |
+
private final int size;
|
| 249 |
+
private Piece[] board;
|
| 250 |
+
private int pieceCount;
|
| 251 |
+
private Piece lastAdded;
|
| 252 |
+
|
| 253 |
+
public GameBoard(int size) {
|
| 254 |
+
this.size = size;
|
| 255 |
+
this.board = new Piece[size];
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
public boolean addPieces(Piece piece, int number) {
|
| 259 |
+
// gameboard is 1D
|
| 260 |
+
// so size = 1D of the gameboard
|
| 261 |
+
// assume gameboard has 1D dims = 1 x size
|
| 262 |
+
// approach 2 - using freeSquares()
|
| 263 |
+
if (number > this.freeSquares()) {
|
| 264 |
+
return false;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
// add pieces to the gameboard
|
| 268 |
+
for (int i = 0; i < number; i++) {
|
| 269 |
+
board[pieceCount + i] = piece;
|
| 270 |
+
|
| 271 |
+
// track last piece
|
| 272 |
+
if (i == number - 1) {
|
| 273 |
+
this.lastAdded = piece;
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
return true;
|
| 277 |
+
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
public int getSize() {
|
| 281 |
+
return this.size;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
public int freeSquares() {
|
| 285 |
+
// count number of pieces on the board
|
| 286 |
+
int count = 0;
|
| 287 |
+
for (Piece piece : board) {
|
| 288 |
+
if (piece != null) {
|
| 289 |
+
count++;
|
| 290 |
+
}
|
| 291 |
+
}
|
| 292 |
+
this.pieceCount = count;
|
| 293 |
+
return this.size - this.pieceCount;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
public Piece lastAdded() {
|
| 297 |
+
return this.lastAdded;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
Testing the GameBoard class...
|
| 311 |
+
Test 1
|
| 312 |
+
GameBoard created!
|
| 313 |
+
size is private and final: true
|
| 314 |
+
getSize returns 10
|
| 315 |
+
Free squares: 10
|
| 316 |
+
|
| 317 |
+
Playing 3 black...
|
| 318 |
+
Addition successful: true
|
| 319 |
+
Last added: BLACK
|
| 320 |
+
Free squares: 7
|
| 321 |
+
|
| 322 |
+
Playing 3 white...
|
| 323 |
+
Addition successful: true
|
| 324 |
+
Last added: WHITE
|
| 325 |
+
Free squares: 4
|
| 326 |
+
|
| 327 |
+
Playing 2 black...
|
| 328 |
+
Addition successful: true
|
| 329 |
+
Last added: BLACK
|
| 330 |
+
Free squares: 2
|
| 331 |
+
|
| 332 |
+
Playing 3 white...
|
| 333 |
+
Addition successful: false
|
| 334 |
+
Last added: BLACK
|
| 335 |
+
Free squares: 2
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
Test 2
|
| 342 |
+
GameBoard created!
|
| 343 |
+
getSize returns 5
|
| 344 |
+
Free squares: 5
|
| 345 |
+
|
| 346 |
+
Playing 3 black...
|
| 347 |
+
Addition successful: true
|
| 348 |
+
Last added: BLACK
|
| 349 |
+
Free squares: 2
|
| 350 |
+
|
| 351 |
+
Playing 3 white...
|
| 352 |
+
Addition successful: false => because GameBoard can only accept 5 pieces, but adding 3 more white pieces becomes 6, so 'false'/ cant add to gameboard
|
| 353 |
+
Last added: BLACK
|
| 354 |
+
Free squares: 2
|
| 355 |
+
|
| 356 |
+
Playing 2 black...
|
| 357 |
+
Addition successful: true => adding 2 more pieces -> we get a total of 5 pieces (max size of gameboard) -> so ok
|
| 358 |
+
Last added: BLACK
|
| 359 |
+
Free squares: 0
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
|