Depricated Piece.java and modified functionality to work under an int[].

This commit is contained in:
Jonathan Turner 2024-02-03 14:59:42 -05:00
parent b6bbbef44b
commit ec0c41975d
4 changed files with 53 additions and 98 deletions

View File

@ -25,19 +25,9 @@ public class Driver {
System.out.println(board); System.out.println(board);
System.out.println(BoardGenerator.isSolvable(board)); System.out.println(BoardGenerator.isSolvable(board));
Piece[] pieces = new Piece[9]; Board goal = new Board();
pieces[0] = new Piece(0,7); System.out.println(goal);
pieces[1] = null; System.out.println(BoardGenerator.isSolvable(goal));
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));
} }
} }

View File

@ -1,7 +1,6 @@
package Assignments.A1.models; package Assignments.A1.models;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random;
/** /**
* 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.
@ -11,7 +10,7 @@ import java.util.Random;
*/ */
public class Board { public class Board {
private Piece[] pieces; private int[] pieces;
/** /**
* Default constructor that generates the solved board. * Default constructor that generates the solved board.
@ -20,10 +19,16 @@ public class Board {
* @postcondition a solved board is created. * @postcondition a solved board is created.
*/ */
public Board() { public Board() {
this.pieces = new Piece[9]; this.pieces = new int[9];
for (int index = 1; index < 9; index++) { this.pieces[0] = 1;
this.pieces[index-1] = new Piece(index-1, index); 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) { if (board == null) {
throw new IllegalArgumentException("The board must be valid an initialized."); 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++) { for (int index = 0; index < 9; index++) {
if (board.getPiece(index) != null) { this.pieces[index] = board.getPiece(index);
this.pieces[index] = new Piece(board.getPiece(index));
}
} }
} }
@ -54,7 +57,7 @@ public class Board {
* *
* @param pieces the provided state. * @param pieces the provided state.
*/ */
public Board(Piece[] pieces) { public Board(int[] pieces) {
if (pieces.length != 9) { if (pieces.length != 9) {
throw new IllegalArgumentException("The pieces list must be size 9."); throw new IllegalArgumentException("The pieces list must be size 9.");
} }
@ -70,25 +73,24 @@ public class Board {
* @param loc the location being checked. * @param loc the location being checked.
* *
* @return If the location has a piece, True * @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) { public boolean isTaken(int loc) {
if (invalidLocation(loc)) { if (invalidLocation(loc)) {
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); 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. * Returns a value in the array of pieces.
* *
* @param loc the location of the desired piece.
* @precondition loc >= 0 & loc <= 8 * @precondition loc >= 0 & loc <= 8
* @postcondition none * @postcondition none
* @param loc the location of the desired piece. * @return the pieces at that location.
*
* @return the array of pieces.
*/ */
public Piece getPiece(int loc) { public int getPiece(int loc) {
if (invalidLocation(loc)) { if (invalidLocation(loc)) {
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8");
} }
@ -105,7 +107,7 @@ public class Board {
*/ */
public int getOpenLocation() { public int getOpenLocation() {
for (int index = 0; index < 9; index++) { for (int index = 0; index < 9; index++) {
if (this.pieces[index] == null) { if (this.pieces[index] == 0) {
return index; 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 * @precondition none
* || board.get(second).validMove(first) == true
* @postcondition the values are swapped * @postcondition the values are swapped
* *
* @param first the first index of the values being swapped * @param first the first index of the values being swapped
* @param second the second index of the values being swapped * @param second the second index of the values being swapped
* @return if the values were swapped or not. * @return if the values were swapped or not.
*/ */
public boolean swap(int first, int second) { public void swap(int first, int second) {
// Checks if the first piece is empty and the second is not. int temp = this.pieces[first];
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[first] = this.pieces[second];
this.pieces[second] = null; this.pieces[second] = temp;
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;
} }
/** /**
@ -153,17 +138,14 @@ public class Board {
* *
* @return the ordered list of values. * @return the ordered list of values.
*/ */
public Piece[] getPiecesInOrder() { public int[] getPieces() {
Piece[] ordered = new Piece[8]; int[] ordered = new int[8];
boolean foundSpace = false; int iterations = 0;
for (Piece curr : this.pieces) { for (int curr : this.pieces) {
if (curr == null) { if (curr == 0) {
foundSpace = true; continue;
} else if (!foundSpace) {
ordered[curr.getLoc()] = curr;
} else {
ordered[curr.getLoc()-1] = curr;
} }
ordered[iterations] = curr;
} }
return ordered; return ordered;
} }
@ -179,15 +161,9 @@ public class Board {
@Override @Override
public int hashCode() { public int hashCode() {
int[] hashValues = new int[9]; int[] hashValues = new int[9];
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
if (this.pieces[i] != null) { hashValues[i] = this.pieces[i];
hashValues[i] = this.pieces[i].getValue();
} else {
hashValues[i] = 0;
} }
}
return Arrays.hashCode(hashValues); return Arrays.hashCode(hashValues);
} }
@ -211,10 +187,9 @@ public class Board {
if (this.getClass() != o.getClass()) { if (this.getClass() != o.getClass()) {
return false; return false;
} }
Piece[] other = ((Board) o).getPiecesInOrder(); Board other = (Board) o;
Piece[] ordered = this.getPiecesInOrder(); for (int curr : this.pieces) {
for (int curr = 0; curr < ordered.length; curr++) { if (curr != other.pieces[curr]) {
if (ordered[curr] != other[curr]) {
return false; return false;
} }
} }
@ -229,11 +204,7 @@ public class Board {
public String toString() { public String toString() {
String result = ""; String result = "";
for (int i = 0; i < 9; i++) { 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) { if ((i+1) % 3 == 0) {
result += "\n"; result += "\n";
} }

View File

@ -1,8 +1,5 @@
package Assignments.A1.models; package Assignments.A1.models;
import Assignments.A1.models.Board;
import Assignments.A1.models.Piece;
import java.util.*; import java.util.*;
/** /**
@ -23,19 +20,17 @@ public class BoardGenerator {
*/ */
public static Board generateBoard() { public static Board generateBoard() {
// Creates the initial board with default values.
Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8}; Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8};
List<Integer> random = new ArrayList<>(Arrays.asList(values)); List<Integer> random = new ArrayList<>(Arrays.asList(values));
Collections.shuffle(random); Collections.shuffle(random);
Random gen = new Random(); Random gen = new Random();
int spaceLoc = gen.nextInt(9); int spaceLoc = gen.nextInt(9);
Piece[] pieces = new Piece[9]; int[] pieces = new int[9];
for (int curr = 0; curr < values.length; curr++) { for (int curr = 0; curr < values.length; curr++) {
if (curr < spaceLoc) { pieces[curr] = random.get(curr);
pieces[curr] = new Piece(curr,random.get(curr));
} else {
pieces[curr+1] = new Piece(curr+1,random.get(curr));
}
} }
// Checks if the board is solveable. // Checks if the board is solveable.
@ -44,14 +39,11 @@ public class BoardGenerator {
return generated; return generated;
} else { // If not it swaps the last two values (ignoring the space) } else { // If not it swaps the last two values (ignoring the space)
if (spaceLoc == 8) { if (spaceLoc == 8) {
pieces[7].setLocation(6); generated.swap(6,7);
pieces[6].setLocation(7);
} else if (spaceLoc == 7) { } else if (spaceLoc == 7) {
pieces[8].setLocation(6); generated.swap(6,8);
pieces[6].setLocation(8);
} else { } else {
pieces[7].setLocation(6); generated.swap(7,8);
pieces[6].setLocation(7);
} }
generated = new Board(pieces); generated = new Board(pieces);
} }
@ -78,17 +70,17 @@ public class BoardGenerator {
// Holds the number of inversions // Holds the number of inversions
int inversions = 0; int inversions = 0;
Piece[] ordered = board.getPiecesInOrder(); int[] ordered = board.getPieces();
// Counts the number of inversions // Counts the number of inversions
for (int index = 0; index < 8; index++) { for (int index = 0; index < 8; index++) {
for (int invers = index+1; invers < 8; invers++) { for (int invers = index+1; invers < 8; invers++) {
if (ordered[index].getValue() > ordered[invers].getValue()) { if (ordered[index] > ordered[invers]) {
inversions++; inversions++;
} }
} }
} }
return (inversions % 2 == 0); return (inversions % 2 == 1);
} }

View File

@ -5,7 +5,9 @@ package Assignments.A1.models;
* *
* @author Jonathan Turner * @author Jonathan Turner
* @version CS3642 - Spring 2024 * @version CS3642 - Spring 2024
* @deprecated to prevent further complexity
*/ */
@Deprecated
public class Piece { public class Piece {
private int loc; private int loc;