Implemented BFS. Seems to be fully functioning. Need to fix generation of boards to prevent off chance of solved generations.
This commit is contained in:
parent
db5565c1ae
commit
e13806a179
@ -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++;
|
||||
|
@ -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<Board> visited = new HashSet<>();
|
||||
|
||||
/* Checking out new Branch for BFS Development to prevent harm to DFS existing algorithm. */
|
||||
public Board traverse(Board root) {
|
||||
PriorityQueue<BoardNode> 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<Move> 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<BoardNode> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user