READ ALL OF THIS TO SOLVE THE PROGRAM!!!!!!!!! The sliding puzzle (https://en.wi
ID: 3676564 • Letter: R
Question
READ ALL OF THIS TO SOLVE THE PROGRAM!!!!!!!!!
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. PLEASE READ ALL OF THIS TO SOLVE THE PROGRAM!!!!!!!!! I am confused and need help writing everything. Thank you!
Explanation / Answer
I have written the code for the game. working code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class puzzle_panel extends JPanel {
private static final long serialVersionUID = 4501938941435763747L;
private static final int SIDE = 4;
private static final int FONT_SIZE = 40;
private static int LEVEL = 10;
private Game game;
private Font font;
private Color background, foreground, borderColor;
private int slotWidth, slotHeight, slotXOffset, slotYOffset;
private int inX, inY, xOffset, yOffset;
private boolean dragged;
public puzzle_panel() {
game = new Game(SIDE, LEVEL);
font = new Font(Font.SERIF, Font.BOLD, FONT_SIZE);
slotWidth = slide_puzzle.WIDTH / SIDE;
slotHeight = slide_puzzle.HEIGHT / SIDE;
slotXOffset = slotWidth / 2 - FONT_SIZE / 4;
slotYOffset = slotHeight / 2 + FONT_SIZE / 3;
background = new Color(123, 155, 232);
foreground = new Color(123, 255, 132);
borderColor = new Color(223, 255, 132);
dragged = false;
setFocusable(true);
requestFocus();
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if(dragged) {
inX = e.getXOnScreen() - xOffset;
inY = e.getYOnScreen() - yOffset;
slide_puzzle.frame.setLocation(inX, inY);
}
else {
dragged = true;
xOffset = e.getX();
yOffset = e.getY();
}
}
@Override
public void mouseMoved(MouseEvent e) {
dragged = false;
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_ESCAPE:
slide_puzzle.frame.dispose();
break;
default:
game.keyPressed(key);
repaint();
if(game.isCorrect()) {
JOptionPane.showMessageDialog(slide_puzzle.frame, "You Win!");
LEVEL += 10;
game = new Game(SIDE, LEVEL);
repaint();
}
}
}
});
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setFont(font);
g2d.setColor(background);
g2d.fillRect(0, 0, getWidth(), getHeight());
drawBorders(g2d);
g2d.setColor(foreground);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(game.getValueAt(j, i) != 0)
g2d.drawString(""+game.getValueAt(j, i), slotXOffset + j * slotWidth,
slotYOffset + i * slotHeight);
}
}
}
private void drawBorders(Graphics2D g2d) {
g2d.setColor(borderColor);
for(int i = 0; i < 3; i++)
g2d.drawRect(i, i, slide_puzzle.WIDTH - 1 - 2 * i, slide_puzzle.HEIGHT - 1 - 2 * i);
for(int i = 1; i < 4; i++) {
int level = i * slotHeight;
g2d.drawLine(0, level, slide_puzzle.WIDTH, level);
g2d.drawLine(0, level + 1, slide_puzzle.WIDTH, level + 1);
g2d.drawLine(0, level + 2, slide_puzzle.WIDTH, level + 2);
g2d.drawLine(level, 0, level, slide_puzzle.HEIGHT);
g2d.drawLine(level + 1, 0, level + 1, slide_puzzle.HEIGHT);
g2d.drawLine(level + 2, 0, level + 2, slide_puzzle.HEIGHT);
}
}
}
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class slide_puzzle {
public static final int WIDTH = 512;
public static final int HEIGHT = 512;
public static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame = new JFrame("Sliding Puzzle");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setUndecorated(true);
frame.setSize(WIDTH, HEIGHT);
puzzle_panel spp = new puzzle_panel();
frame.add(spp);
frame.setVisible(true);
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.