TurkuBasicOOPinJava / Week 6: Methods of OO Programming /16. The Last Piece, Part 3: The Game Board
KaiquanMah's picture
Create 16. The Last Piece, Part 3: The Game Board
6bd73f4 verified
raw
history blame
9.82 kB
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