Fixed Generation board to always generate a board that is solvable (fixed dfs null return).

This commit is contained in:
Jonathan Turner 2024-02-05 21:59:29 -05:00
parent 687eb8964b
commit 156f8e0d19
4 changed files with 18 additions and 77 deletions

View File

@ -25,18 +25,15 @@ public class Driver {
public static void main(String[] args) {
DFS solver = new DFS();
// Board board = new Board();
// Board result = solver.dfs(board, 0);
// System.out.println(result);
// Board board = BoardGenerator.generateBoard();
//
// Board result = solver.dfs(board, 0, new ArrayList<String>());
// System.out.println(result);
//
int[] temp = {0,8,3,5,1,6,4,7,2};
Board solvable = new Board(temp);
Board result = solver.dfs(solvable,0, new ArrayList<>());
System.out.println(result);
Board board = BoardGenerator.generateBoard();
Board result = solver.dfs(board, 0, new ArrayList<>());
int count = 0;
while (count != 100 || result != null) {
result = solver.dfs(board, 0, new ArrayList<>());
System.out.println(count);
count++;
board = BoardGenerator.generateBoard();
}
}
}

View File

@ -19,69 +19,14 @@ public class BoardGenerator {
* @return a board that is solvable.
*/
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);
int[] pieces = new int[9];
for (int curr = 0; curr < values.length; curr++) {
pieces[curr] = random.get(curr);
Board ideal = new Board();
Random random = new Random();
int numOfMoves = random.nextInt(30,50);
for (int i = 0; i < numOfMoves; i++) {
List<Move> moves = ideal.getMoves();
Move randomMove = moves.get(random.nextInt(moves.size()));
ideal.swap(randomMove);
}
// Checks if the board is solvable.
Board generated = new Board(pieces);
if (isSolvable(generated)) {
return generated;
} else { // If not it swaps the last two values (ignoring the space)
if (spaceLoc == 8) {
generated.swap(6,7);
} else if (spaceLoc == 7) {
generated.swap(6,8);
} else {
generated.swap(7,8);
}
generated = new Board(pieces);
}
// Puzzle is now solvable.
return generated;
return ideal;
}
/**
* Checks if the board is solvable by checking the number of inversions.
* If the number of inversions is even, it is solvable, if not it is not solvable.
*
* @precondition board != null
* @postcondition none
*
* @param board the board being checked
* @return if the board has even inversion, True
* if not, False
*/
public static boolean isSolvable(Board board) {
if (board == null) {
return false;
}
// Holds the number of inversions
int inversions = 0;
int[] ordered = board.pieces;
// Counts the number of inversions
for (int index = 0; index < 9; index++) {
for (int invers = index+1; invers < 9; invers++) {
if (ordered[index] > ordered[invers]) {
inversions++;
}
}
}
return (inversions % 2 == 0);
}
}

View File

@ -9,6 +9,6 @@ package Assignments.A1.resources;
public class Parameters {
/* Used to prevent DFS from going down only 1 branch */
public static final int MAX_DEPTH = 31; // Max number of moves in 8-Puzzle's are 31 moves if solvable.
public static final int MAX_DEPTH = 100; // Max number of moves in 8-Puzzle's are 31 moves if solvable.
}

View File

@ -17,7 +17,6 @@ public class DFS {
public Board dfs(Board root, int depth, ArrayList<String> visited) {
counter++;
System.out.println("Num of boards " + counter + " | Depth " + depth);
if (root.equals(solved)) {
return root;
}