Implementing hashing capabilities to the Board class.
This commit is contained in:
parent
a07e2098b0
commit
a264075c1f
@ -1,5 +1,7 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class keeps track of the current state (whether in permutation or not) of the board.
|
* 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 {
|
public class Board {
|
||||||
|
|
||||||
private final Piece[] pieces;
|
private Piece[] pieces;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor that generates the solved board.
|
* 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.
|
* 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
|
* @postcondition a board is created with given locations
|
||||||
*
|
*
|
||||||
* @param board the provided state of the board.
|
* @param board the provided state of the board.
|
||||||
*/
|
*/
|
||||||
public Board(Piece[] board) {
|
public Board(Board board) {
|
||||||
if (board.length != 9) {
|
if (board == null) {
|
||||||
throw new IllegalArgumentException("The board must be a array size of 9.");
|
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.
|
* Checks the status of a place on the board. If the location is taken or not.
|
||||||
*
|
*
|
||||||
@ -88,6 +96,103 @@ public class Board {
|
|||||||
throw new IllegalStateException("The state of the board must have 1 empty space.");
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@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. */
|
/* Private Methods Below. End of Java Docs. */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user