Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Pyton or java please solve. We will implement the 8 puzzle, with one varia

ID: 3746711 • Letter: U

Question

Using Pyton or java please solve.

We will implement the 8 puzzle, with one variation. Instead of having tiles numbered 1-8 we will have one duplicate tile. 2-2-3-4-5-6-7-8 for example. Example: The 8-puzzle 2 3 6 4 5 6 Start State Goal State Page 103 Grading Rubric (50 points): 5 points for a depth-first solution . 5 points for a breadth-first solution 15 points for an A* solution 20 points for a summary of results, including: o o o Total number of steps required to find each solution (10 pts) Elapsed clock time needed to solve each puzzle (5 pts) Maximum number of concurrent states for each (5 pts) 5 points for comments in your submitted code

Explanation / Answer

Here is the code in java,

Please comment belowe for any further queries or any modifications needed. Thank you.

Main Program
--------------------------------------

import javax.swing.JFrame;


class PuzzleGame {

public static void main(String[] args) {
JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new Puzzle_GUI());
window.pack();  
window.show();  
window.setResizable(false);
}
}


Graphical User Interface
----------------------------------------------------
  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Puzzle_GUI extends JPanel {
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
public Puzzle_GUI() {
JButton New_Game_Button = new JButton("New Game");
New_Game_Button.addActionListener(new NewGameAction());
JPanel Control_panel = new JPanel();
Control_panel.setLayout(new FlowLayout());
Control_panel.add(New_Game_Button);
_puzzleGraphics = new GraphicsPanel();
this.setLayout(new BorderLayout());
this.add(Control_panel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
class GraphicsPanel extends JPanel implements MouseListener {
private static final int Num_Of_rows = 3;
private static final int Num_Of_cols = 3;
private static final int CellSize = 80; // Pixels
private Font _biggerFont;
public GraphicsPanel() {
_biggerFont = new Font("SansSerif", Font.BOLD, CellSize/2);
this.setPreferredSize(
new Dimension(CellSize * Num_Of_cols, CellSize*Num_Of_rows));
this.setBackground(Color.black);
this.addMouseListener(this);
}
public void Pint_Comp(Graphics g) {
super.Pint_Comp(g);
for (int r=0; r<Num_Of_rows; r++) {
for (int c=0; c<Num_Of_cols; c++) {
int x = c * CellSize;
int y = r * CellSize;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);
g.fillRect(x+2, y+2, CellSize-4, CellSize-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CellSize)/4);
}
}
}
}

public void mousePressed(MouseEvent e) {
int columnss = e.getX()/CellSize;
int rows = e.getY()/CellSize;
if (!_puzzleModel.moveTile(rows, columnss)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();  
}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class New_game_action implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
}

Logic/Model
------------------------------------------------

class Puzzle_Model {
private static final int Num_Of_rows = 3;
private static final int Num_Of_cols = 3;   
private tiles[][] _contents; // All tiles.
private tiles _emptyTile; // The empty space.
public Puzzle_Model() {
_contents = new tiles[Num_Of_rows][Num_Of_cols];
reset(); // Initialize and shuffle tiles.
}
  
String getFace(int rows, int columnss) {
return _contents[rows][columnss].getFace();
}
public void reset() {
for (int r=0; r<Num_Of_rows; r++) {
for (int c=0; c<Num_Of_cols; c++) {
_contents[r][c] = new tiles(r, c, "" + (r*Num_Of_cols+c+1));
}
}
_emptyTile = _contents[Num_Of_rows-1][Num_Of_cols-1];
_emptyTile.setFace(null);
for (int r=0; r<Num_Of_rows; r++) {
for (int c=0; c<Num_Of_cols; c++) {
exchangeTiles(r, c, (int)(Math.random()*Num_Of_rows)
, (int)(Math.random()*Num_Of_cols));
}
}
}
public boolean moveTile(int r, int c) {
return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<Num_Of_rows && c>=0 && c<Num_Of_cols;
}
private void exchangeTiles(int r1, int c1, int r2, int c2) {
tiles temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}
public boolean isGameOver() {
for (int r=0; r<Num_Of_rows; r++) {
for (int c=0; c<Num_Of_rows; c++) {
tiles trc = _contents[r][c];
return trc.isInFinalPosition(r, c);
}
}
  
return true;
}
}
  
class tiles {
private int _rowss;
private int _col;
private String _face;

public tiles(int rows, int columnss, String face) {
_rowss = rows;
_colss = columnss;
_face = face;
}

public void setFace(String newFace) {
_face = newFace;
}
  
public String getFace() {
return _face;
}
public boolean isInFinalPosition(int r, int c) {
return r==_rowss && c==_colss;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote