Question: I need help with the following question In this assignment you will implement a program that uses BFS search and A* search to solve the
I need help with the following question
In this assignment you will implement a program that uses BFS search and A* search to solve the Eight Puzzle game. Your program will take an initial board position, and then try to find a sequence of moves from the initial position to the goal position. Moves are represented by moving the blank space left, right, up, or down. For example, suppose we wanted to solve the puzzle shown above. This could be accomplished by the following sequence of moves (we represent a move as the direction the empty space moved):
1 2 3 1 2 3 1 2 3 4 0 5 R 4 5 0 D 4 5 6 7 8 6 7 8 6 7 8 0 initial goal
where R=right, L=left, U=up, D=down. So we can solve this puzzle with the sequence of moves "RD".
public class SlidingSolverDriver { public static void main(String[] args) { String puzzle = "123405786"; SlidingSolution solution = new SlidingSolver(puzzle).solvePuzzleBFS(); System.out.println(solution.getMoves()); } }
public static final SlidingSolution NO_RESPONSE = new SlidingSolution("", -123456); //Represents a null solution public static final SlidingSolution NO_SOLUTION = new SlidingSolution("", -789); private String myMoves; private int myProblemSpaceSize; public SlidingSolution(String moves, int spaceSize) { myMoves = moves; myProblemSpaceSize = spaceSize; } public String getMoves() { return myMoves; } public int getProblemSpaceSize() { return myProblemSpaceSize; } public String toString() { String toStr = "Move the empty space in the following directions: "; for(char move : myMoves.toCharArray()) { switch(move) { case 'U' : toStr += "UP "; break; case 'R' : toStr += "RIGHT "; break; case 'D' : toStr += "DOWN "; break; case 'L' : toStr += "LEFT "; break; } } return toStr; } }
import java.util.*; public class SlidingSolver { //Maximum search depth for BFS private static final int MAX_BFS_DISTANCE = 20; /* You may add instance and class variables and methods as you see fit */ /* * Constructs a SlidingSolver with the given input board. */ public SlidingSolver(String initialBoard) { /* Your code here */ } /* * Solves the puzzle by performing a breadth-first search over the puzzle space. * Returns SlidingSolution.NO_SOLUTION if the maximum BFS search depth is reached. */ public SlidingSolution solvePuzzleBFS() { /* Your code here */ return SlidingSolution.NO_RESPONSE; } /* * Solves the puzzle by performing an A* search over the puzzle space. */ public SlidingSolution solvePuzzleAStar() { /* Your code here */ return SlidingSolution.NO_RESPONSE; } /* * Requirement #2 * Solves the puzzle by performing an breadth-first search over the puzzle space using the optimization described in "requirement #2". */ public SlidingSolution solvePuzzleBFSOptimized() { /* Your code here */ return SlidingSolution.NO_RESPONSE; } /* * Requirement #2 * Solves the puzzle by performing an A* search over the puzzle space using the optimization described in "requirement #2". */ public SlidingSolution solvePuzzleAStarOptimized() { /* Your code here */ return SlidingSolution.NO_RESPONSE; } /* * Evaluates the given board. */ private int evaluateHeuristic(TileBoard p) { return TileBoard.getNumMoves(b) + TileBoard.calcManhattanDistance(b); } class TileBoardComparator implements Comparator { public int compare(TileBoard a, TileBoard b) { /* Your code here */ return 0; } } }
mport java.util.*; public class TileBoard { //String representation of the solution board private static final String goalBoard = "123456780"; //String representation of a puzzle board private String myBoard; //String representation of the list of moves that generated this board private String myMoves; /* You may add more instance and class variables and methods as you see fit - you will need to */ public TileBoard(String board) { /* Your code goes here */ } /* * Returns a list of boards that are one move away. This list *DOES NOT* contain the * previous board, as this would undo a moving we just made (see the lab documentation). */ public static List getNextBoards(TileBoard b) { /* Your code goes here */ return null; } /* * Returns the number of moves from the initial board */ public static int getNumMoves(TileBoard b) { return b.myMoves.length(); } /* * Evaluates the given board using the Manhattan distance heuristic. */ public static int calcManhattanDistance(TileBoard b) { /* Your code goes here */ return -1; } } please help thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
