Spaces:
Running
Running
File size: 9,819 Bytes
6bd73f4 | 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 | Next, let's write the GameBoard class.
The class has the following features:
1
Attribute 'size', which must have the 'final' modifier.
The class can also have other attributes as needed, but size is mandatory.
2
Constructor that takes the size of the game board (an integer) as a parameter
3
Method boolean addPieces(Piece piece, int number), which tries to add the given number of pieces to the board.
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.
Otherwise, the method returns false and does nothing.
4
Method int freeSquares(), which returns the number of free squares on the board as an integer
5
Method Piece lastAdded(), which returns the piece used in the last successful addition
6
Method getSize(), which returns the size of the game board. The size cannot change after initialization
import java.util.Random;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) {
final Random r = new Random();
System.out.println("Testing the GameBoard class...");
System.out.println("Test 1");
GameBoard board = new GameBoard(10);
System.out.println("GameBoard created!");
Piece black = new Piece(Color.BLACK);
Piece white = new Piece(Color.WHITE);
try {
Field size = board.getClass().getDeclaredField("size");
System.out.println("size is private and final: " + (size.getModifiers() > 17));
} catch (NoSuchFieldException e) {
System.out.println("GameBoard class does not have an attribute size!");
} catch (SecurityException e) {}
System.out.println("getSize returns " + board.getSize());
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 3 black...");
System.out.println("Addition successful: " + board.addPieces(black, 3));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 3 white...");
System.out.println("Addition successful: " + board.addPieces(white, 3));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 2 black...");
System.out.println("Addition successful: " + board.addPieces(black, 2));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 3 white...");
System.out.println("Addition successful: " + board.addPieces(white, 3));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Test 2");
board = new GameBoard(5);
System.out.println("GameBoard created!");
System.out.println("getSize returns " + board.getSize());
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 3 black...");
System.out.println("Addition successful: " + board.addPieces(black, 3));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 3 white...");
System.out.println("Addition successful: " + board.addPieces(white, 3));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
System.out.println("Playing 2 black...");
System.out.println("Addition successful: " + board.addPieces(black, 2));
System.out.println("Last added: " + (board.lastAdded().getColor() == 1 ? "WHITE" : "BLACK"));
System.out.println("Free squares: " + board.freeSquares());
System.out.println("");
}
}
//ADD
// round1 - ArrayList
// AND SQUARE GAMEBOARD
// class GameBoard {
// public final int size;
// public ArrayList<Piece> pieces;
// public Piece lastAdded;
//
// public GameBoard(int size) {
// this.size = size;
// this.pieces = new ArrayList<Piece>();
// }
//
// public boolean addPieces(Piece piece, int number) {
// // gameboard is 2D
// // so size = 1D of the gameboard
// // assume gameboard has dimensions OR num_squares = size x size
// // approach 1
// // if (this.pieces.size() + number > this.size * this.size) {
// // approach 2 - using freeSquares()
// if (number > this.freeSquares()) {
// return false;
// }
//
// // add pieces to the gameboard
// for (int i = 0; i < number; i++) {
// this.pieces.add(piece);
//
// // track last piece
// if (i == number - 1) {
// this.lastAdded = piece;
// }
// }
// return true;
//
// }
//
// public int getSize() {
// return this.size;
// }
//
// public int freeSquares() {
//this.pieces.size() = arraylist length
// return this.size * this.size - this.pieces.size();
// }
//
// public Piece lastAdded() {
// return this.lastAdded;
// }
//
// }
// round2 - NO ArrayList
// AND SQUARE GAMEBOARD
// class GameBoard {
// private final int size;
// private Piece[] board;
// private int pieceCount;
// private Piece lastAdded;
//
// public GameBoard(int size) {
// this.size = size;
// this.board = new Piece[size * size];
// }
//
// public boolean addPieces(Piece piece, int number) {
// // gameboard is 2D
// // so size = 1D of the gameboard
// // assume gameboard has dimensions OR num_squares = size x size
// // approach 1
// // if (this.pieces.size() + number > this.size * this.size) {
// // approach 2 - using freeSquares()
// if (number > this.freeSquares()) {
// return false;
// }
//
// // add pieces to the gameboard
// for (int i = 0; i < number; i++) {
// board[pieceCount + i] = piece;
//
// // track last piece
// if (i == number - 1) {
// this.lastAdded = piece;
// }
// }
// return true;
//
// }
//
// public int getSize() {
// return this.size;
// }
//
// public int freeSquares() {
// // count number of pieces on the board
// int count = 0;
// for (Piece piece : board) {
// if (piece != null) {
// count++;
// }
// }
// this.pieceCount = count;
// return this.size * this.size - this.pieceCount;
// }
//
// public Piece lastAdded() {
// return this.lastAdded;
// }
//
// }
// round3 - NO ArrayList
// AND 1D, i.e. NOT SQUARE GAMEBOARD
class GameBoard {
private final int size;
private Piece[] board;
private int pieceCount;
private Piece lastAdded;
public GameBoard(int size) {
this.size = size;
this.board = new Piece[size];
}
public boolean addPieces(Piece piece, int number) {
// gameboard is 1D
// so size = 1D of the gameboard
// assume gameboard has 1D dims = 1 x size
// approach 2 - using freeSquares()
if (number > this.freeSquares()) {
return false;
}
// add pieces to the gameboard
for (int i = 0; i < number; i++) {
board[pieceCount + i] = piece;
// track last piece
if (i == number - 1) {
this.lastAdded = piece;
}
}
return true;
}
public int getSize() {
return this.size;
}
public int freeSquares() {
// count number of pieces on the board
int count = 0;
for (Piece piece : board) {
if (piece != null) {
count++;
}
}
this.pieceCount = count;
return this.size - this.pieceCount;
}
public Piece lastAdded() {
return this.lastAdded;
}
}
Testing the GameBoard class...
Test 1
GameBoard created!
size is private and final: true
getSize returns 10
Free squares: 10
Playing 3 black...
Addition successful: true
Last added: BLACK
Free squares: 7
Playing 3 white...
Addition successful: true
Last added: WHITE
Free squares: 4
Playing 2 black...
Addition successful: true
Last added: BLACK
Free squares: 2
Playing 3 white...
Addition successful: false
Last added: BLACK
Free squares: 2
Test 2
GameBoard created!
getSize returns 5
Free squares: 5
Playing 3 black...
Addition successful: true
Last added: BLACK
Free squares: 2
Playing 3 white...
Addition successful: false => because GameBoard can only accept 5 pieces, but adding 3 more white pieces becomes 6, so 'false'/ cant add to gameboard
Last added: BLACK
Free squares: 2
Playing 2 black...
Addition successful: true => adding 2 more pieces -> we get a total of 5 pieces (max size of gameboard) -> so ok
Last added: BLACK
Free squares: 0
|