Compare commits
5 Commits
a07e2098b0
...
cd266d583d
Author | SHA1 | Date | |
---|---|---|---|
|
cd266d583d | ||
|
0a05da12bb | ||
|
c5687291be | ||
|
56294257de | ||
|
a264075c1f |
@ -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,17 +28,23 @@ 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 = board;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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. */
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
|
8
src/Assignments/A1/resources/Constants.java
Normal file
8
src/Assignments/A1/resources/Constants.java
Normal 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;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user