Depricated Piece.java and modified functionality to work under an int[].
This commit is contained in:
parent
b6bbbef44b
commit
ec0c41975d
@ -25,19 +25,9 @@ public class Driver {
|
||||
System.out.println(board);
|
||||
System.out.println(BoardGenerator.isSolvable(board));
|
||||
|
||||
Piece[] pieces = new Piece[9];
|
||||
pieces[0] = new Piece(0,7);
|
||||
pieces[1] = null;
|
||||
pieces[2] = new Piece(2,5);
|
||||
pieces[3] = new Piece(3,1);
|
||||
pieces[4] = new Piece(4,2);
|
||||
pieces[5] = new Piece(5,4);
|
||||
pieces[6] = new Piece(6,6);
|
||||
pieces[7] = new Piece(7,3);
|
||||
pieces[8] = new Piece(8,8);
|
||||
|
||||
board = new Board(pieces);
|
||||
System.out.println(BoardGenerator.isSolvable(board));
|
||||
Board goal = new Board();
|
||||
System.out.println(goal);
|
||||
System.out.println(BoardGenerator.isSolvable(goal));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package Assignments.A1.models;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* This class keeps track of the current state (whether in permutation or not) of the board.
|
||||
@ -11,7 +10,7 @@ import java.util.Random;
|
||||
*/
|
||||
public class Board {
|
||||
|
||||
private Piece[] pieces;
|
||||
private int[] pieces;
|
||||
|
||||
/**
|
||||
* Default constructor that generates the solved board.
|
||||
@ -20,10 +19,16 @@ public class Board {
|
||||
* @postcondition a solved board is created.
|
||||
*/
|
||||
public Board() {
|
||||
this.pieces = new Piece[9];
|
||||
for (int index = 1; index < 9; index++) {
|
||||
this.pieces[index-1] = new Piece(index-1, index);
|
||||
}
|
||||
this.pieces = new int[9];
|
||||
this.pieces[0] = 1;
|
||||
this.pieces[1] = 2;
|
||||
this.pieces[2] = 3;
|
||||
this.pieces[3] = 8;
|
||||
this.pieces[5] = 4;
|
||||
this.pieces[6] = 7;
|
||||
this.pieces[7] = 6;
|
||||
this.pieces[8] = 5;
|
||||
this.pieces[4] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,11 +43,9 @@ public class Board {
|
||||
if (board == null) {
|
||||
throw new IllegalArgumentException("The board must be valid an initialized.");
|
||||
}
|
||||
this.pieces = new Piece[9];
|
||||
this.pieces = new int[9];
|
||||
for (int index = 0; index < 9; index++) {
|
||||
if (board.getPiece(index) != null) {
|
||||
this.pieces[index] = new Piece(board.getPiece(index));
|
||||
}
|
||||
this.pieces[index] = board.getPiece(index);
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +57,7 @@ public class Board {
|
||||
*
|
||||
* @param pieces the provided state.
|
||||
*/
|
||||
public Board(Piece[] pieces) {
|
||||
public Board(int[] pieces) {
|
||||
if (pieces.length != 9) {
|
||||
throw new IllegalArgumentException("The pieces list must be size 9.");
|
||||
}
|
||||
@ -70,25 +73,24 @@ public class Board {
|
||||
* @param loc the location being checked.
|
||||
*
|
||||
* @return If the location has a piece, True
|
||||
* If the location is null, False
|
||||
* If the location is 0, False
|
||||
*/
|
||||
public boolean isTaken(int loc) {
|
||||
if (invalidLocation(loc)) {
|
||||
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8");
|
||||
}
|
||||
return (this.pieces[loc].getValue() != 0);
|
||||
return (this.pieces[loc] != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value in the array of pieces.
|
||||
*
|
||||
* @param loc the location of the desired piece.
|
||||
* @precondition loc >= 0 & loc <= 8
|
||||
* @postcondition none
|
||||
* @param loc the location of the desired piece.
|
||||
*
|
||||
* @return the array of pieces.
|
||||
* @return the pieces at that location.
|
||||
*/
|
||||
public Piece getPiece(int loc) {
|
||||
public int getPiece(int loc) {
|
||||
if (invalidLocation(loc)) {
|
||||
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8");
|
||||
}
|
||||
@ -105,7 +107,7 @@ public class Board {
|
||||
*/
|
||||
public int getOpenLocation() {
|
||||
for (int index = 0; index < 9; index++) {
|
||||
if (this.pieces[index] == null) {
|
||||
if (this.pieces[index] == 0) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@ -113,36 +115,19 @@ public class Board {
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes two values on the board and swaps them if possible.
|
||||
* Takes two values on the board and swaps them.
|
||||
*
|
||||
* @precondition board.get(first).validMove(second) == true
|
||||
* || board.get(second).validMove(first) == true
|
||||
* @precondition none
|
||||
* @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;
|
||||
public void swap(int first, int second) {
|
||||
int temp = this.pieces[first];
|
||||
this.pieces[first] = this.pieces[second];
|
||||
this.pieces[second] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,17 +138,14 @@ public class Board {
|
||||
*
|
||||
* @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;
|
||||
public int[] getPieces() {
|
||||
int[] ordered = new int[8];
|
||||
int iterations = 0;
|
||||
for (int curr : this.pieces) {
|
||||
if (curr == 0) {
|
||||
continue;
|
||||
}
|
||||
ordered[iterations] = curr;
|
||||
}
|
||||
return ordered;
|
||||
}
|
||||
@ -179,15 +161,9 @@ public class 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;
|
||||
}
|
||||
hashValues[i] = this.pieces[i];
|
||||
}
|
||||
|
||||
return Arrays.hashCode(hashValues);
|
||||
}
|
||||
|
||||
@ -211,10 +187,9 @@ public class Board {
|
||||
if (this.getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Piece[] other = ((Board) o).getPiecesInOrder();
|
||||
Piece[] ordered = this.getPiecesInOrder();
|
||||
for (int curr = 0; curr < ordered.length; curr++) {
|
||||
if (ordered[curr] != other[curr]) {
|
||||
Board other = (Board) o;
|
||||
for (int curr : this.pieces) {
|
||||
if (curr != other.pieces[curr]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -229,11 +204,7 @@ public class Board {
|
||||
public String toString() {
|
||||
String result = "";
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (pieces[i] == null) {
|
||||
result += 0 + " ";
|
||||
} else {
|
||||
result += pieces[i] + " ";
|
||||
}
|
||||
result += pieces[i] + " ";
|
||||
if ((i+1) % 3 == 0) {
|
||||
result += "\n";
|
||||
}
|
||||
|
@ -1,8 +1,5 @@
|
||||
package Assignments.A1.models;
|
||||
|
||||
import Assignments.A1.models.Board;
|
||||
import Assignments.A1.models.Piece;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@ -23,19 +20,17 @@ public class BoardGenerator {
|
||||
*/
|
||||
public static Board generateBoard() {
|
||||
|
||||
// Creates the initial board with default values.
|
||||
Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
List<Integer> random = new ArrayList<>(Arrays.asList(values));
|
||||
Collections.shuffle(random);
|
||||
|
||||
Random gen = new Random();
|
||||
int spaceLoc = gen.nextInt(9);
|
||||
Piece[] pieces = new Piece[9];
|
||||
int[] pieces = new int[9];
|
||||
for (int curr = 0; curr < values.length; curr++) {
|
||||
if (curr < spaceLoc) {
|
||||
pieces[curr] = new Piece(curr,random.get(curr));
|
||||
} else {
|
||||
pieces[curr+1] = new Piece(curr+1,random.get(curr));
|
||||
}
|
||||
pieces[curr] = random.get(curr);
|
||||
|
||||
}
|
||||
|
||||
// Checks if the board is solveable.
|
||||
@ -44,14 +39,11 @@ public class BoardGenerator {
|
||||
return generated;
|
||||
} else { // If not it swaps the last two values (ignoring the space)
|
||||
if (spaceLoc == 8) {
|
||||
pieces[7].setLocation(6);
|
||||
pieces[6].setLocation(7);
|
||||
generated.swap(6,7);
|
||||
} else if (spaceLoc == 7) {
|
||||
pieces[8].setLocation(6);
|
||||
pieces[6].setLocation(8);
|
||||
generated.swap(6,8);
|
||||
} else {
|
||||
pieces[7].setLocation(6);
|
||||
pieces[6].setLocation(7);
|
||||
generated.swap(7,8);
|
||||
}
|
||||
generated = new Board(pieces);
|
||||
}
|
||||
@ -78,17 +70,17 @@ public class BoardGenerator {
|
||||
|
||||
// Holds the number of inversions
|
||||
int inversions = 0;
|
||||
Piece[] ordered = board.getPiecesInOrder();
|
||||
int[] ordered = board.getPieces();
|
||||
|
||||
// Counts the number of inversions
|
||||
for (int index = 0; index < 8; index++) {
|
||||
for (int invers = index+1; invers < 8; invers++) {
|
||||
if (ordered[index].getValue() > ordered[invers].getValue()) {
|
||||
if (ordered[index] > ordered[invers]) {
|
||||
inversions++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (inversions % 2 == 0);
|
||||
return (inversions % 2 == 1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,7 +5,9 @@ package Assignments.A1.models;
|
||||
*
|
||||
* @author Jonathan Turner
|
||||
* @version CS3642 - Spring 2024
|
||||
* @deprecated to prevent further complexity
|
||||
*/
|
||||
@Deprecated
|
||||
public class Piece {
|
||||
|
||||
private int loc;
|
||||
|
Loading…
Reference in New Issue
Block a user