I HAVE HAD TO POST THIS SEVERAL TIMES. PLEASE READ THE WHOLE QUESTION BEFORE TRY
ID: 3676816 • Letter: I
Question
I HAVE HAD TO POST THIS SEVERAL TIMES. PLEASE READ THE WHOLE QUESTION BEFORE TRYING TO ANSWER THIS. DO NOT COPY AND PASTE I REALLY DO NEED HELP. IF YOU CAN'T DO IT DON'T TRY AND ANSWER.
I am having a lot of trouble figuring out how to solve this problem. I don't have a problem making the puzzle, but rather how to make a solver. I have given the assigned program below and I am hoping that someone can give me an eample IN JAVA on how to tackle solving this problem the way theacher has asked. I am very lost and I have no idea how to make this work. Please I will give anything to someone who can help me get this code written.
Here is what I was given by the teacher:
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:
1234
5678
9ABC
D.EF
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:
1234
5678
9ABC
DEF.
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:
Path: E F
Cost: 29
Passoff
Write a program that solves the weighted 4x4 sliding puzzle using an iterative deepening search.
When the program starts, it should print out your name, and state that it is a sliding puzzle solver. The user must also be able to specify the input file name.
The program must read in 4x4 sliding puzzles from a file in the format described earlier.
Your program must print out an optimal solution (sequence of moves, and total cost) by tracking which square is moved into the blank space at each move.
In addition to the solution to the puzzle, the program must print out how many total recursive calls were made to find the solution.
Turn in your code and executable files to D2L, and then pass off your program directly to the instructor.
Explanation / Answer
package Siliding_Puzzle;
public class Siliding_Puzzle
{
private int[][] board;
private int[][] Win_State;
public Siliding_Puzzle()
{
this.board = new int[4][4];
this.Win_State = new int[4][4];
int num = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
this.board[i][j] = num + 1;
num++;
}
}
board[3][3] = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
this.Win_State[i][j] = board[i][j];
}
}
}
public boolean Over() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] != Win_State[i][j]) {
return false;
}
}
}
return true;
}
public void move(int x) {
moveloop:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == x) {
if (i >= 0 && i < 3) {
if (board[i+1][j] == 0) {
board[i+1][j] = board[i][j];
board[i][j] = 0;
break moveloop;
}
}
if (i == 3) {
if (board[i-1][j] == 0) {
board[i-1][j] = board[i][j];
board[i][j] = 0;
break moveloop;
}
}
if (j >= 0 && j < 3) {
if (board[i][j+1] == 0) {
board[i][j+1] = board[i][j];
board[i][j] = 0;
break moveloop;
}
}
if (j == 3) {
if (board[i][j-1] == 0) {
board[i][j-1] = board[i][j];
board[i][j] = 0;
break moveloop;
}
}
}
}
}
}
public String toString() {
String s = new String();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] < 10) {
s = s + (board[i][j] + " ");
}
if (board[i][j] >= 10) {
s = s + (board[i][j] + " ");
}
}
s = s + ' ';
}
return s;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.