Fixed Generation board to always generate a board that is solvable (fixed dfs null return).
This commit is contained in:
parent
687eb8964b
commit
156f8e0d19
@ -25,18 +25,15 @@ public class Driver {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
DFS solver = new DFS();
|
DFS solver = new DFS();
|
||||||
// Board board = new Board();
|
Board board = BoardGenerator.generateBoard();
|
||||||
// Board result = solver.dfs(board, 0);
|
Board result = solver.dfs(board, 0, new ArrayList<>());
|
||||||
// System.out.println(result);
|
int count = 0;
|
||||||
// Board board = BoardGenerator.generateBoard();
|
while (count != 100 || result != null) {
|
||||||
//
|
result = solver.dfs(board, 0, new ArrayList<>());
|
||||||
// Board result = solver.dfs(board, 0, new ArrayList<String>());
|
System.out.println(count);
|
||||||
// System.out.println(result);
|
count++;
|
||||||
//
|
board = BoardGenerator.generateBoard();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,69 +19,14 @@ public class BoardGenerator {
|
|||||||
* @return a board that is solvable.
|
* @return a board that is solvable.
|
||||||
*/
|
*/
|
||||||
public static Board generateBoard() {
|
public static Board generateBoard() {
|
||||||
|
Board ideal = new Board();
|
||||||
// Creates the initial board with default values.
|
Random random = new Random();
|
||||||
Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8};
|
int numOfMoves = random.nextInt(30,50);
|
||||||
List<Integer> random = new ArrayList<>(Arrays.asList(values));
|
for (int i = 0; i < numOfMoves; i++) {
|
||||||
Collections.shuffle(random);
|
List<Move> moves = ideal.getMoves();
|
||||||
|
Move randomMove = moves.get(random.nextInt(moves.size()));
|
||||||
Random gen = new Random();
|
ideal.swap(randomMove);
|
||||||
int spaceLoc = gen.nextInt(9);
|
|
||||||
int[] pieces = new int[9];
|
|
||||||
for (int curr = 0; curr < values.length; curr++) {
|
|
||||||
pieces[curr] = random.get(curr);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return ideal;
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,6 @@ package Assignments.A1.resources;
|
|||||||
public class Parameters {
|
public class Parameters {
|
||||||
|
|
||||||
/* Used to prevent DFS from going down only 1 branch */
|
/* 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.
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ public class DFS {
|
|||||||
|
|
||||||
public Board dfs(Board root, int depth, ArrayList<String> visited) {
|
public Board dfs(Board root, int depth, ArrayList<String> visited) {
|
||||||
counter++;
|
counter++;
|
||||||
System.out.println("Num of boards " + counter + " | Depth " + depth);
|
|
||||||
if (root.equals(solved)) {
|
if (root.equals(solved)) {
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user