diff --git a/src/Assignments/A1/Driver.java b/src/Assignments/A1/Driver.java index 02fe969..4470e2c 100644 --- a/src/Assignments/A1/Driver.java +++ b/src/Assignments/A1/Driver.java @@ -4,6 +4,7 @@ package Assignments.A1; import Assignments.A1.models.Board; import Assignments.A1.models.BoardGenerator; +import Assignments.A1.solving_algorithms.BFS; import Assignments.A1.solving_algorithms.DFS; import java.util.ArrayList; @@ -28,12 +29,12 @@ public class Driver { int successes = 0; for (int run = 0; run < 100; run++) { Board board = BoardGenerator.generateBoard(); - DFS solver = new DFS(); +// DFS solver = new DFS(); + BFS solver = new BFS(); Date start = new Date(); - Board result = solver.dfs(board,0); + Board result = solver.traverse(board); Date end = new Date(); if (result != null) { - System.out.println("solved"); long runtime = end.getTime() - start.getTime(); timer.add(runtime); successes++; diff --git a/src/Assignments/A1/resources/Parameters.java b/src/Assignments/A1/resources/Parameters.java index 58767b7..aca21ea 100644 --- a/src/Assignments/A1/resources/Parameters.java +++ b/src/Assignments/A1/resources/Parameters.java @@ -11,4 +11,4 @@ public class Parameters { /* Used to prevent DFS from going down only 1 branch */ public static final int MAX_DEPTH = 100; // Max number of moves in 8-Puzzle's are 31 moves if solvable. -} +} \ No newline at end of file diff --git a/src/Assignments/A1/solving_algorithms/BFS.java b/src/Assignments/A1/solving_algorithms/BFS.java index 7a6dd99..656879a 100644 --- a/src/Assignments/A1/solving_algorithms/BFS.java +++ b/src/Assignments/A1/solving_algorithms/BFS.java @@ -1,7 +1,94 @@ package Assignments.A1.solving_algorithms; +import Assignments.A1.models.Board; +import Assignments.A1.models.Move; + +import java.util.HashSet; +import java.util.List; +import java.util.PriorityQueue; + public class BFS { + private final Board solved = new Board(); + private final HashSet visited = new HashSet<>(); - /* Checking out new Branch for BFS Development to prevent harm to DFS existing algorithm. */ + public Board traverse(Board root) { + PriorityQueue boards = new PriorityQueue<>(); + boards.add(new BoardNode(root)); + BoardNode node = null; + while (!boards.isEmpty()) { + node = boards.poll(); + if (visited.contains(node.board)) { + continue; + } + visited.add(node.board); + List children = node.board.getMoves(); + for (Move move : children) { + Board child = new Board(node.board); + child.swap(move); + boards.add(new BoardNode(child)); + } + } + return node.board; + } +} + +class BoardNode implements Comparable { + + public int heuristic; + public Board board; + + public BoardNode(Board board) { + this.heuristic = this.getCost(board); + this.board = board; + } + + private int getCost(Board board) { + int cost = 0; + for (int i = 0; i < 9; i++) { + int currValue = board.getPiece(i); + cost += Math.abs(i - this.idealIndex(i)); + } + return cost; + } + + private int idealIndex(int value) { + int result = -1; + switch(value) { + case 0: + result = 4; + break; + case 1: + result = 0; + break; + case 2: + result = 1; + break; + case 3: + result = 2; + break; + case 4: + result = 5; + break; + case 5: + result = 8; + break; + case 6: + result = 7; + break; + case 7: + result = 6; + break; + case 8: + result = 3; + break; + + } + return result; + } + + @Override + public int compareTo(BoardNode o) { + return Integer.compare(this.heuristic, o.heuristic); + } }