Compare commits

..

No commits in common. "cd266d583d3eca53dfa346f8eaf426d38b568352" and "a07e2098b0f120f30d334599e4f7f5d7788ebfee" have entirely different histories.

3 changed files with 8 additions and 163 deletions

View File

@ -1,7 +1,5 @@
package Assignments.A1.models;
import java.util.Arrays;
/**
* This class keeps track of the current state (whether in permutation or not) of the board.
*
@ -10,7 +8,7 @@ import java.util.Arrays;
*/
public class Board {
private Piece[] pieces;
private final Piece[] pieces;
/**
* Default constructor that generates the solved board.
@ -28,24 +26,18 @@ public class Board {
/**
* Constructor used to create a board with a pre-provided board.
*
* @precondition board != null
* @precondition board.length == 9
* @postcondition a board is created with given locations
*
* @param board the provided state of the board.
*/
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));
}
public Board(Piece[] 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.
*
@ -96,130 +88,6 @@ 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,26 +25,11 @@ 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

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