Compare commits

...

5 Commits

Author SHA1 Message Date
Jonathan Turner
cd266d583d Added a method to get the pieces in order. 2024-02-03 12:39:07 -05:00
Jonathan Turner
0a05da12bb Added missing javadocs. 2024-02-03 11:52:31 -05:00
Jonathan Turner
c5687291be Implemented the capability of copying pieces from another piece. Prevents pass by reference. 2024-02-03 11:50:55 -05:00
Jonathan Turner
56294257de Implementing hashing capabilities to the Board class. 2024-02-03 11:50:13 -05:00
Jonathan Turner
a264075c1f Implementing hashing capabilities to the Board class. 2024-02-03 11:50:06 -05:00
3 changed files with 163 additions and 8 deletions

View File

@ -1,5 +1,7 @@
package Assignments.A1.models;
import java.util.Arrays;
/**
* This class keeps track of the current state (whether in permutation or not) of the board.
*
@ -8,7 +10,7 @@ package Assignments.A1.models;
*/
public class Board {
private final Piece[] pieces;
private Piece[] pieces;
/**
* Default constructor that generates the solved board.
@ -26,18 +28,24 @@ public class Board {
/**
* Constructor used to create a board with a pre-provided board.
*
* @precondition board.length == 9
* @precondition board != null
* @postcondition a board is created with given locations
*
* @param board the provided state of the board.
*/
public Board(Piece[] board) {
if (board.length != 9) {
throw new IllegalArgumentException("The board must be a array size of 9.");
public Board(Board board) {
if (board == null) {
throw new IllegalArgumentException("The board must be valid an initialized.");
}
this.pieces = new Piece[9];
for (int index = 0; index < 9; index++) {
if (board.getPiece(index) != null) {
this.pieces[index] = new Piece(board.getPiece(index));
}
}
this.pieces = board;
}
/**
* Checks the status of a place on the board. If the location is taken or not.
*
@ -88,6 +96,130 @@ public class Board {
throw new IllegalStateException("The state of the board must have 1 empty space.");
}
/**
* Takes two values on the board and swaps them if possible.
*
* @precondition board.get(first).validMove(second) == true
* || board.get(second).validMove(first) == true
* @postcondition the values are swapped
*
* @param first the first index of the values being swapped
* @param second the second index of the values being swapped
* @return if the values were swapped or not.
*/
public boolean swap(int first, int second) {
// Checks if the first piece is empty and the second is not.
if (this.pieces[first] == null && this.pieces[second] != null) {
// Checks if the moves are valid
if (this.pieces[second].validMove(first, this)) {
// Swaps the values
this.pieces[first] = this.pieces[second];
this.pieces[second] = null;
return true; // Successful Swap
}
} else if (this.pieces[second] == null && this.pieces[first] != null) { // Checks the inverse.
// Checks if the moves are valid
if (this.pieces[first].validMove(second, this)) {
// Swaps the values
this.pieces[second] = this.pieces[first];
this.pieces[first] = null;
return true; // Successful Swap
}
}
return false;
}
/**
* Gets the pieces in order in a single array of size 8.
*
* @precondition none
* @postcondition none
*
* @return the ordered list of values.
*/
public Piece[] getPiecesInOrder() {
Piece[] ordered = new Piece[8];
boolean foundSpace = false;
for (Piece curr : this.pieces) {
if (curr == null) {
foundSpace = true;
} else if (!foundSpace) {
ordered[curr.getLoc()] = curr;
} else {
ordered[curr.getLoc()-1] = curr;
}
}
return ordered;
}
/**
* Hashes the board by adding the pieces to an array and hashes the array.
*
* @precondition none
* @postcondition none
*
* @return the hash of the board.
*/
@Override
public int hashCode() {
int[] hashValues = new int[9];
for (int i = 0; i < 9; i++) {
if (this.pieces[i] != null) {
hashValues[i] = this.pieces[i].getValue();
} else {
hashValues[i] = 0;
}
}
return Arrays.hashCode(hashValues);
}
/**
* Checks if this and the other object are the same.
* @precondition none
* @postcondition they are compared.
*
* @param o the other object.
* @return if they are the same, True
* if they are not, False
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Board other = (Board) o;
for (int i = 0; i < 9; i++) {
if (this.pieces[i].getValue() != other.pieces[i].getValue()
|| this.pieces[i].getLoc() != other.pieces[i].getLoc()) {
return false;
}
}
return true;
}
/**
* Specifies a way of printing out the board with its current state.
* @return the current state formatted.
*/
@Override
public String toString() {
String result = "";
for (int i = 0; i < 9; i++) {
result += i + " ";
if ((i+1) % 3 == 0) {
result += "\n";
}
}
return result;
}
/* Private Methods Below. End of Java Docs. */

View File

@ -25,11 +25,26 @@ public class Piece {
if (value < 1 || value > 8) {
throw new IllegalArgumentException("The piece value is not valid. Valid Range: 1 <= Value <= 8");
}
this.setLoc(loc);
this.value = value;
}
/**
* Creates a new piece from an existing piece.
*
* @precondition piece != null
* @postcondition a copied piece is created.
*
* @param piece the piece being copied
*/
public Piece(Piece piece) {
if (piece == null) {
throw new IllegalArgumentException("The piece cannot be null.");
}
this.loc = piece.getLoc();
this.value = piece.getValue();
}
/**
* Moves the piece to a new location.
*

View File

@ -0,0 +1,8 @@
package Assignments.A1.resources;
public class Constants {
/* Used to prevent DFS from going down only 1 branch */
public static final int MAX_DEPTH = 100;
}