Created and added methods to the class Board.java to keep track of permutation states.

This commit is contained in:
Jonathan Turner 2024-02-02 20:25:26 -05:00
parent a1e6bad176
commit d15216acf2

View File

@ -18,7 +18,7 @@ public class Board {
*/ */
public Board() { public Board() {
this.pieces = new Piece[9]; 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); this.pieces[index-1] = new Piece(index-1, index);
} }
} }
@ -35,11 +35,19 @@ public class Board {
if (board.length != 9) { if (board.length != 9) {
throw new IllegalArgumentException("The board must be a array size of 9."); throw new IllegalArgumentException("The board must be a array size of 9.");
} }
this.pieces = board; 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) { public boolean isTaken(int loc) {
if (invalidLocation(loc)) { if (invalidLocation(loc)) {
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); 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 * @precondition none
* @postcondition none * @postcondition none
* @return the array of pieces. *
* @return the location of the empty piece.
*/ */
public Piece[] getPieces() { public int getOpenLocation() {
return this.pieces; 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.");
} }