diff --git a/src/Assignments/A1/models/Board.java b/src/Assignments/A1/models/Board.java index 3ec8032..d6328b2 100644 --- a/src/Assignments/A1/models/Board.java +++ b/src/Assignments/A1/models/Board.java @@ -18,7 +18,7 @@ public class Board { */ public Board() { this.pieces = new Piece[9]; - for (int index = 1; index <= 9; index++) { + for (int index = 1; index < 9; index++) { this.pieces[index-1] = new Piece(index-1, index); } } @@ -35,11 +35,19 @@ public class Board { if (board.length != 9) { throw new IllegalArgumentException("The board must be a array size of 9."); } - this.pieces = board; } - + /** + * Checks the status of a place on the board. If the location is taken or not. + * + * @precondition loc >= 0 & loc <= 8 + * @postcondition none + * @param loc the location being checked. + * + * @return If the location has a piece, True + * If the location is null, False + */ public boolean isTaken(int loc) { if (invalidLocation(loc)) { throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); @@ -48,14 +56,36 @@ public class Board { } /** - * Returns the array of pieces. + * Returns a value in the array of pieces. + * + * @precondition loc >= 0 & loc <= 8 + * @postcondition none + * @param loc the location of the desired piece. + * + * @return the array of pieces. + */ + public Piece getPiece(int loc) { + if (invalidLocation(loc)) { + throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); + } + return pieces[loc]; + } + + /** + * Checks and searches for the location of the open location and returns the index. * * @precondition none * @postcondition none - * @return the array of pieces. + * + * @return the location of the empty piece. */ - public Piece[] getPieces() { - return this.pieces; + public int getOpenLocation() { + for (int index = 0; index < 9; index++) { + if (this.pieces[index] == null) { + return index; + } + } + throw new IllegalStateException("The state of the board must have 1 empty space."); }