The sliding puzzle (https://en.wikipedia.org/wiki/Sliding_puzzle) is a game in w
ID: 3673771 • Letter: T
Question
The sliding puzzle (https://en.wikipedia.org/wiki/Sliding_puzzle) is a game in which tiles are arranged on a 2D array of cells. One cell in the array is open. Moves are made by sliding one of the tiles into the open slot. the objective is to make a sequence of moves that puts the tiles in a goal state (in order). The sliding puzzle also is a great example of a problem that can be solved easily by recursion, but is hard to solve by iteration.
In this assignment, you will solve a weighted variant of the 4x4 sliding puzzle using an interative deepening search. In the normal sliding puzzle, each move has the same cost. However, in this variant of the problem, the cost of moving each piece is proportional to the value on the piece.
Iterative Deepening Search
Iterative deepening search is a recursive method to solve problems made up of a sequence of moves. The idea is to try all move sequences of length or cost 1, then all sequences of length 2, and so forth, until a solution is found. When a solution is found, the recursion unwinds, retracing the moves that were needed to solve the problem.
Input file format
The input format will be a text file which has 4 lines. giving the initial configuration of the board. The files will use Hexadecimal digits (0-9,A-F) to represent the values 1-15, and a dot (.) to represent the blank space. For example:
The goal is to successively move squares up, down, right, or left into the blank spot until the goal configuration is reached, while incurring the least cost. For our purposes, the goal configuration is as follows:
For the example above, the goal can be reached by moving the E piece and then F piece left one position, with a total cost of 29 (15 for moving the F, and 14 for moving the E).
Instructions
Use an iterative deepening search to solve the weighted 4x4 sliding puzzle problem. When a solution is found, print out the sequence of moves, as well as the total path cost. A move can be specified by listing which square was moved into the blank spot. For the example above, the solution to the example above could be printed as:
This is my assignment, I need help writing the whole code out. If you could write all of the code out, that would be so helpful. Thank you!
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author sagun
*
*/
public class PuzzleSolver {
private String puzzleInputFileName;
private char puzzle[][];
public PuzzleSolver(String puzzleInputFileName) {
this.puzzleInputFileName = puzzleInputFileName;
puzzle=new char[4][4];
}
public void loadPuzzle()
{
try {
FileInputStream inputFile=new FileInputStream(puzzleInputFileName);
BufferedReader reader=new BufferedReader(new InputStreamReader(inputFile));
String line;
int linesRead=0;
while((line=reader.readLine())!=null)
{
for(int i=0;i<line.length();i++)
{
puzzle[linesRead][i]=line.charAt(i);
System.out.println("val:"+puzzle[linesRead][i]);
}
linesRead++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void solvePuzzle()
{
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PuzzleSolver solver=new PuzzleSolver("./src/puzzle/input");
solver.loadPuzzle();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.