Java 15 Puzzle Homework: Setup Visual Part & Restrictions

  • Comp Sci
  • Thread starter prosteve037
  • Start date
  • Tags
    Java Puzzle
In summary, The end of year assignment for a Java class is to write a program for the 15 Puzzle. The assignment is split into two parts: setting up the visual part of the puzzle and setting up the actions/events of the puzzle. The project requires the use of "for" loops and does not allow "if" statements. The student has completed the first half of the puzzle and is seeking help for the second half, which involves creating a button that will shuffle the puzzle tiles and allowing any adjacent tile to switch places with the empty space on click. The student has encountered a problem where the same set of shuffled tiles appear when the button is clicked continuously.
  • #1
prosteve037
110
3

Homework Statement


My class is required to write the Java source code for a 15 Puzzle program as an end of the year assignment. http://en.wikipedia.org/wiki/15_puzzle" is a link to what the 15 Puzzle is, for those who aren't familiar with its name.

The assignment is split into two parts; setting up the visual part of the puzzle and setting up the actions/events of the puzzle.

Currently, I'm in the first half of the puzzle. The second half will be assigned at a later date, and I will most likely end up having to post again for help :P

Homework Equations


This project requires "for" loops, and I was also told that "if" statements aren't allowed.

How much these restrictions will inhibit anyone's ability to find a helpful solution, I don't know. I myself am not familiar with "if" statements since my professor has yet to cover that part of the course.

The Attempt at a Solution


All current code:

NOTE: The first two sections of code was code that was given to us already to start us off; these aren't to be altered. The last section of code is what I have been writing.

Code:
package code;

public class Driver {
	public static void main(String[] args) {
		new FifteenPuzzle("images/butterflySmall.png");
	}
}

Code:
package support;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class HelpfulImageMethods {
	
	
	/**
	 * @param filePath
	 * @return The BufferedImage object created from the data in the file
	 *         located at filePath; if no image data can be loaded, null
	 *         is returned.
	 */
	public static BufferedImage loadImage(String filePath) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File(filePath));
		}
		catch (IOException e) {
			System.err.println("I could not load the file \'"+filePath+"'.  Sorry.");
		}
		return img;
	}

	/**
	 * @param img
	 * @param sx
	 * @param sy
	 * @param imageWidth
	 * @param imageHeight
	 * @return a BufferedImage object which is cut out from the BufferedImage
	 *         object 'img'.  The returned image is the sub-image of 'img' whose
	 *         upper-left corner is at (sx,sy) and whose width is imageWidth,
	 *         and whose height is imageHeight.
	 */
	public static BufferedImage createSubImage(BufferedImage img, int sx, int sy, int imageWidth,
			int imageHeight) {
		BufferedImage subImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
		Graphics g = subImage.getGraphics();
		int dx = 0;
		int dy = 0;
		g.drawImage(img,dx, dy, dx+imageWidth, dy+imageHeight,
                        sx, sy, sx+imageWidth, sy+imageHeight,
                        null);
		g.dispose();
		return subImage;
	}
}
This last section of code is the code that the student is supposed to write himself/herself, ie. the problem lies in this section.

Code:
package code;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import support.HelpfulImageMethods;public class FifteenPuzzle {			
	
	private JFrame _window;
	private BufferedImage _wholeImage;
	
	public FifteenPuzzle(String filePath) {
		_wholeImage = HelpfulImageMethods.loadImage(filePath);
		_window = new JFrame("15 Puzzle");
		
		Container p = _window.getContentPane();
		p.setLayout(null);
		p.setPreferredSize(new Dimension(300,300));
		
		this.subImageAdder();
		
		_window.pack();
		_window.setVisible(true);
		_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public List<BufferedImage> subImageCreator(){
		List<BufferedImage> list = new LinkedList<BufferedImage>();
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col = col+1){
			BufferedImage rowImages = HelpfulImageMethods.createSubImage(_wholeImage, 0, col*subImageHeight, subImageWidth, subImageHeight);
			list.add(rowImages);
		}
		
		for (int row = 0; row < 4; row = row+1){
			BufferedImage columnImages = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, 0, subImageWidth, subImageHeight);
			list.add(columnImages);
		}
		
		return list;
	}
	
	public void subImageAdder(){
		Iterator<BufferedImage> i = this.subImageCreator().iterator();
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col = col+1){
			ImageIcon icon = new ImageIcon(i.next());
			JLabel label = new JLabel(icon);
			label.setBounds(2, col*(2+subImageHeight), subImageWidth, subImageHeight);
			_window.add(label);
		}
		
		for (int row = 0; row < 4; row = row+1){
			ImageIcon icon = new ImageIcon(i.next());
			JLabel label = new JLabel(icon);
			label.setBounds(row*(2+subImageWidth), 2, subImageWidth, subImageHeight);
			_window.add(label);
		}
	}
	
}

Here is a snapshot of what my current code results in:

15Puzzle.jpg


So as you can see, the code's not doing what I need it to just yet. This is where I have been stumped for some time.

Also, we need to have each sub-image of the whole image 2 pixels apart from neighbouring sub-images. This explains the small spaces in-between the sub-images in the picture, except for some reason the spaces don't appear around the edges of the first (top-left most) sub-image.

Any advice or help would be greatly appreciated.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
In your code, you have two separate for loops, one for rows and one for columns, in your subimageCreator and subimageAdder methods.

The way I would approach this is to use an inner for loop nested inside an outer for loop, like this:
Code:
for(int row = 0; row < 4; row++)
{
  for(int col = 0; col < 4; col++)
  {
    // create subimage for window at position (row, col)
    // add subimage at position (row, col)
  }
}
 
  • #3
Mark44 said:
In your code, you have two separate for loops, one for rows and one for columns, in your subimageCreator and subimageAdder methods.

The way I would approach this is to use an inner for loop nested inside an outer for loop, like this:
Code:
for(int row = 0; row < 4; row++)
{
  for(int col = 0; col < 4; col++)
  {
    // create subimage for window at position (row, col)
    // add subimage at position (row, col)
  }
}

Yes, thank you. That's what I ended up doing for the first part. Now comes the real tricky, second part of the assignment.

For this next part we have to write the code so that the following things occur:

1.) On a click of a button, the puzzle starts a new game*. This entails getting the picture, "slicing it up", shuffling it, and then putting it all onto the game board (JPanel/JFrame).

*If the button is clicked continuously, the puzzle must keep shuffling/scrambling the pieces so that the puzzle keeps resetting, starting a new game.

2.) After that's done, we have to set it up so that any tile adjacent/next-to the empty/blank tile will switch spots with the empty space, on clicking that tile.


So far I've completed part of 1.), my only problem being that if I keep clicking the button, the same set of shuffled tiles show. In other words, if I keep clicking the "Start New Game" button the puzzle just stays on the same arrangement of tiles as if I had only clicked on the button once.


Here's the code that I've written so far:

"Driver" Class - Starts the game.

Code:
package code;

public class Driver {
	public static void main(String[] args) {
		new Game("images/butterflySmall.png");
	}
}



"FifteenPuzzle" Class - From the first part of the assignment, has a method that "slices up" the provided image into tiles and another method that adds the tiles onto a JPanel.

Code:
package code;

import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import support.HelpfulImageMethods;

public class FifteenPuzzle {			
	
	private JPanel _puzzlePanel;
	private BufferedImage _wholeImage;
	
	public FifteenPuzzle(String filePath, JPanel puzzlePanel) {
		_wholeImage = HelpfulImageMethods.loadImage(filePath);
		_puzzlePanel = puzzlePanel;
		_puzzlePanel.setLayout(null);
	}
	
	public List<BufferedImage> subImageCreator() {
		List<BufferedImage> list = new LinkedList<BufferedImage>();
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col = col+1){
			for (int row = 0; row < 4; row = row + 1) {
				BufferedImage subImages = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, col*subImageHeight, subImageWidth, subImageHeight);
				list.add(subImages);
			}
		}
		
		return list;
	}
	
	public void subImageAdder(Iterator<BufferedImage> i) {
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col = col+1){
			for (int row = 0; row < 4; row = row+1) {
				ImageIcon icon = new ImageIcon(i.next());
				JLabel label = new JLabel(icon);
				label.setBounds(2+(row*(2+subImageWidth)) , 2+(col*(2+subImageHeight)) , subImageWidth, subImageHeight);
				_puzzlePanel.add(label);
			}
		}
	}
	
	public int getWidth() {
		return _wholeImage.getWidth();
	}
	
	public int getHeight() {
		return _wholeImage.getHeight();
	}
	
}



"Game" Class - Instantiates and sets up graphical components of the game. (JFrame, JPanels, JButtons, ActionListeners, etc.)

Code:
package code;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game {
	
	public Game(String filePath) {
		JFrame window = new JFrame("15 Puzzle");
		JPanel puzzlePanel = new JPanel();
		FifteenPuzzle puzzle = new FifteenPuzzle(filePath, puzzlePanel);
		JPanel buttonPanel = new JPanel();
		
		JButton startButton = new JButton("Start New Game");
		JButton quitButton = new JButton("Quit");
		
		window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));

		StartActionListener sal = new StartActionListener(puzzle, puzzlePanel, window, buttonPanel);
		QuitActionListener qal = new QuitActionListener();
		
		startButton.addActionListener(sal);
		quitButton.addActionListener(qal);
		
		buttonPanel.add(startButton);
		buttonPanel.add(quitButton);
		
		window.add(buttonPanel);
		window.add(puzzlePanel);
		
		window.setVisible(true);
		window.pack();
		
		puzzlePanel.setVisible(false);
	}
}



"StartActionListener" Class - ActionListener for "Start New Game" button. *(I think this is where my "resetting problem" might lie.)*

Code:
package code;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class StartActionListener implements ActionListener {

	private FifteenPuzzle _puzzle;
	private JPanel _puzzlePanel;
	private JFrame _window;
	
	public StartActionListener(FifteenPuzzle puzzle, JPanel puzzlePanel, JFrame window, JPanel buttonPanel) {
		_puzzle = puzzle;
		_puzzlePanel = puzzlePanel;
		_window = window;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		List<BufferedImage> list = _puzzle.subImageCreator();
		Iterator<BufferedImage> i = list.iterator();
		
		list.remove(list.size()-1);
		Collections.shuffle(list);
		
		_puzzle.subImageAdder(i);
		_puzzlePanel.setVisible(true);
		_puzzlePanel.setPreferredSize(new Dimension(_puzzle.getWidth(),_puzzle.getHeight()));
		
		_window.pack();
	}
}



"QuitActionListener" Class - ActionListener for the "Quit" button.

Code:
package code;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class QuitActionListener implements ActionListener {

	public QuitActionListener() {
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.exit(0);
	}

}



I hope that lays out the requirements of the assignment clearly.

Any help would greatly be appreciated! Thanks!
 
  • #4
I would suggest that you get familiar with a debugger so you can test your theory that your problem is in your StartActionListener class.
 
  • #5
Mark44 said:
I would suggest that you get familiar with a debugger so you can test your theory that your problem is in your StartActionListener class.

Okay I've got the tiles moving the way I need them to. Now I just have to figure out how to get the game to keep switching up the order of the tile arrangement each time I click the "Start New Game" button.

Here's all of my edited code:

Game: All the GUI stuff, now only instantiates buttonPanel instead of both buttonPanel and puzzlePanel. Also, no FifteenPuzzle object is created on instantiation.

Code:
package code;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game {
	
	public Game(String filePath) {
		JFrame window = new JFrame("15 Puzzle");
		JPanel buttonPanel = new JPanel();
		
		JButton startButton = new JButton("Start New Game");
		JButton quitButton = new JButton("Quit");
		
		StartActionListener sal = new StartActionListener(window, filePath);
		QuitActionListener qal = new QuitActionListener();
		
		window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
		
		startButton.addActionListener(sal);
		quitButton.addActionListener(qal);
		
		buttonPanel.add(startButton);
		buttonPanel.add(quitButton);
		
		window.add(buttonPanel);
		
		window.setVisible(true);
		window.pack();
	}
}



StartActionListener: Now creates and handles its own FifteenPuzzle object.

Code:
package code;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class StartActionListener implements ActionListener {

	private FifteenPuzzle _puzzle;
	private JFrame _window;
	private JPanel _puzzlePanel;
	
	public StartActionListener(JFrame window, String filePath) {
		FifteenPuzzle puzzle = new FifteenPuzzle(filePath);
		_puzzle = puzzle;
		_window = window;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JPanel puzzlePanel = new JPanel();
		_puzzlePanel = puzzlePanel;
		_puzzle.setPanel(puzzlePanel);
		puzzlePanel.setLayout(null);

		_puzzle.subImageCreator();
		_puzzle.subImageAdder();
		
		BlankSpaceHolder bsh = new BlankSpaceHolder();
		int subImageWidth = _puzzle.getWidth()/4;
		int subImageHeight = _puzzle.getHeight()/4;
		bsh.setX(3*(subImageWidth+2));
		bsh.setY(3*(subImageHeight+2));
		
		TileListener tl = new TileListener(_puzzlePanel, bsh, _puzzle);
		_puzzlePanel.addMouseListener(tl);
		
		_puzzlePanel.setPreferredSize(new Dimension(_puzzle.getWidth(), _puzzle.getHeight()));
		
		_window.add(_puzzlePanel);
		_window.pack();
		
	}
}



FifteenPuzzle:

Code:
package code;

import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import support.HelpfulImageMethods;

public class FifteenPuzzle {			
	
	private BufferedImage _wholeImage;
	private JPanel _puzzlePanel;
	
	public FifteenPuzzle(String filePath) {
		_wholeImage = HelpfulImageMethods.loadImage(filePath);
	}
	
	public List<BufferedImage> subImageCreator() {
		List<BufferedImage> list = new LinkedList<BufferedImage>();
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col++){
			for (int row = 0; row < 4; row++) {
				BufferedImage image = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, col*subImageHeight, subImageWidth, subImageHeight);
				list.add(image);
			}
		}
		
		list.remove(list.size() - 1);
		Collections.shuffle(list);
		return list;
	}
	
	public void subImageAdder() {
		Iterator<BufferedImage> i = this.subImageCreator().iterator();
		int subImageWidth = _wholeImage.getWidth()/4;
		int subImageHeight = _wholeImage.getHeight()/4;
		
		for (int col = 0; col < 4; col++){
			for (int row = 0; row < 4; row++) {
				if (i.hasNext()) {
					ImageIcon icon = new ImageIcon(i.next());
					Tile tile = new Tile(icon);
					tile.setBounds(row*(2+subImageWidth), col*(2+subImageHeight), subImageWidth, subImageHeight);
					_puzzlePanel.add(tile);
				}
			}
		}
	}
	
	public void setPanel(JPanel panel) {
		_puzzlePanel = panel;
	}
	
	public JPanel getPanel() {
		return _puzzlePanel;
	}
	
	public int getWidth() {
		int imageWidth = _wholeImage.getWidth();
		return imageWidth;
	}
	
	public int getHeight() {
		int imageHeight = _wholeImage.getHeight();
		return imageHeight;
	}
}



BlankSpaceHolder: Holds the x and y int values that locate the blank space.

Code:
package code;

public class BlankSpaceHolder {

	private int _x;
	private int _y;
	
	public void setX(int x) {
		_x = x;
	}
	
	public void setY(int y) {
		_y = y;
	}
	
	public int getX() {
		return _x;
	}
	
	public int getY() {
		return _y;
	}
}



Tile: Extends JLabel class, thus handling its own image piece of the BufferedImage file.

Code:
package code;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Tile extends JLabel {

	private static final long serialVersionUID = 1L;

	public Tile(ImageIcon icon) {
		super(icon);
	}

}



TileListener: Handles the movement of the tiles. There shouldn't be any problems in this code, but just including it as well in case you're curious :P

Code:
package code;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class TileListener implements MouseListener {
	
	private JPanel _panel;
	private BlankSpaceHolder _bsh;
	private FifteenPuzzle _puzzle;
	
	public TileListener (JPanel panel, BlankSpaceHolder bsh, FifteenPuzzle puzzle) {
		_panel = panel;
		_bsh = bsh;
		_puzzle = puzzle;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		Point pointClicked = e.getPoint();
		Component tile = _panel.getComponentAt(pointClicked);
		Point tilePoint = tile.getLocation();
		
		int x = tilePoint.x;
		int y = tilePoint.y;
		
		int bx = _bsh.getX();
		int by = _bsh.getY();
		
		int xCup;
		int yCup;
		
		int subImageWidth = _puzzle.getWidth()/4;
		int subImageHeight = _puzzle.getHeight()/4;
		
		if ((x - bx == subImageWidth + 2) && (y - by == 0)) {
			xCup = x;
			tile.setLocation(bx,by);
			_bsh.setX(xCup);
		}
		
		else if ((bx - x == subImageWidth + 2) && (y - by == 0)) {
			xCup = x;
			tile.setLocation(bx, by);
			_bsh.setX(xCup);
		}
		
		else if ((y - by == subImageHeight + 2) && (x - bx == 0)) {
			yCup = y;
			tile.setLocation(bx, by);
			_bsh.setY(yCup);
		}
		
		else if ((by - y == subImageHeight + 2) && (x - bx == 0)) {
			yCup = y;
			tile.setLocation(bx, by);
			_bsh.setY(yCup);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}
}
 
  • #6
prosteve037 said:
Okay I've got the tiles moving the way I need them to. Now I just have to figure out how to get the game to keep switching up the order of the tile arrangement each time I click the "Start New Game" button.
I think this should be pretty easy. As I understand your code, you read image pieces from a file and store them in a linked list. This happens in one method on the FifteenPuzzle class, subImageCreator. Before this method returns, it calls a static method on the Collections class, shuffle, to shuffle the items in the linked list. I believe you could use this to create a rearranged puzzle when someone clicks Start New Game. Hope that helps!
 
  • #7
Mark44 said:
I think this should be pretty easy. As I understand your code, you read image pieces from a file and store them in a linked list. This happens in one method on the FifteenPuzzle class, subImageCreator. Before this method returns, it calls a static method on the Collections class, shuffle, to shuffle the items in the linked list. I believe you could use this to create a rearranged puzzle when someone clicks Start New Game. Hope that helps!

But wouldn't that just shuffle and rearrange all the pieces? I need to have the Start Button do that and have all the pieces put onto the JFrame with the ButtonPanel still showing.
 
  • #8
prosteve037 said:
Okay I've got the tiles moving the way I need them to. Now I just have to figure out how to get the game to keep switching up the order of the tile arrangement each time I click the "Start New Game" button.

prosteve037 said:
But wouldn't that just shuffle and rearrange all the pieces? I need to have the Start Button do that and have all the pieces put onto the JFrame with the ButtonPanel still showing.
Maybe I misunderstood, but shuffling and rearranging when the user wants to play another round was what I thought you wanted to do.

What exactly is the problem you're trying to solve? What you're asking isn't clear to me.

BTW, it looks like you have done a very nice job on this programming problem.
 
  • #9
Mark44 said:
Maybe I misunderstood, but shuffling and rearranging when the user wants to play another round was what I thought you wanted to do.

What exactly is the problem you're trying to solve? What you're asking isn't clear to me.

I actually JUST got the assignment finished today, almost right after I posted that :smile:

Initially the problem was that repeatedly clicking the "Start New Game" button would just keep adding JPanels to the JFrame, without first removing the old/initial puzzlePanel.

I solved this problem by revising my Game, StartActionListener, and FifteenPuzzle classes.

First I added a new method to the FifteenPuzzle class, called setPanel(JPanel panel): (Re)Assigns the JPanel instance variable that the FifteenPuzzle holds (_puzzlePanel).

Code:
public void setPanel(JPanel panel) {
[INDENT]_puzzlePanel = panel;[/INDENT]
}

Then I modified the Game class so that:

1.) It instantiates a "dummy" JPanel that the StartActionListener class could take in as an argument.

and

2.) It doesn't instantiate a new FifteenPuzzle object.

Code:
package code;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class StartActionListener implements ActionListener {

	private JFrame _window;
	private String _filePath;
	private JPanel _puzzlePanel;
	
	public StartActionListener(JFrame window, String filePath, JPanel panel) {
		_window = window;
		_puzzlePanel = panel;
		_filePath = filePath;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		_window.remove(_puzzlePanel);
		JPanel puzzlePanel = new JPanel();
		_puzzlePanel = puzzlePanel;
		
		FifteenPuzzle puzzle = new FifteenPuzzle(_filePath);
		puzzle.setPanel(_puzzlePanel);
		_puzzlePanel.setLayout(null);
		
		List<BufferedImage> list = puzzle.subImageCreator();
		list.remove(list.size() - 1);
		Collections.shuffle(list);
		puzzle.subImageAdder(list);
		
		BlankSpaceHolder bsh = new BlankSpaceHolder();
		int subImageWidth = puzzle.getWidth()/4;
		int subImageHeight = puzzle.getHeight()/4;
		bsh.setX(3*(subImageWidth+2));
		bsh.setY(3*(subImageHeight+2));
		
		TileListener tl = new TileListener(_puzzlePanel, bsh, puzzle);
		_puzzlePanel.addMouseListener(tl);
		
		_puzzlePanel.setPreferredSize(new Dimension(puzzle.getWidth(), puzzle.getHeight()));
		
		_window.add(_puzzlePanel);
		_window.pack();
		
	}
}

Thus by clicking the "Start New Game" button the window would remove the "dummy" puzzlePanel, that was instantiated in the Game class, from the window object and replace it with a new JPanel that's instantiated upon click.

Clicked repeatedly, the StartActionListener would just remove from the window the object whose reference is assigned to the _puzzlePanel instance variable, ie. the last added JPanel. (Since the last added JPanel's reference is assigned to the StartActionListener's _puzzlePanel instance variable, clicking the button multiple times will just keep reassigning _puzzlePanel to new JPanel instances.)

I hope that explains my method :P

Mark44 said:
BTW, it looks like you have done a very nice job on this programming problem.

Thanks! :smile:
 

1. What is the "Java 15 Puzzle"?

The Java 15 Puzzle is a popular programming challenge where the player must rearrange 15 numbered tiles in a 4x4 grid by sliding them into the empty space. This puzzle was first created in the 1870s and has since been adapted into various forms, including a computer game.

2. What is the purpose of setting up the visual part for the Java 15 Puzzle homework?

The visual part of the Java 15 Puzzle homework is essential for players to interact with the game. It allows the player to see the current state of the puzzle, make moves, and receive feedback on their progress.

3. Are there any restrictions or limitations for the Java 15 Puzzle homework?

Yes, there are several restrictions for the Java 15 Puzzle homework. The puzzle grid must always be 4x4, and each tile should have a unique number from 1 to 15. The player can only move tiles that are adjacent to the empty space, and the game should end when the tiles are arranged in ascending order.

4. How can I set up the visual part for the Java 15 Puzzle homework?

To set up the visual part for the Java 15 Puzzle homework, you can use a graphics library such as Java Swing or JavaFX. These libraries provide tools for creating a graphical user interface (GUI) and handling user interactions. You can also refer to online tutorials or documentation for step-by-step instructions.

5. How can I test my Java 15 Puzzle homework for errors or bugs?

You can test your Java 15 Puzzle homework by playing the game and trying different inputs to see if it follows the restrictions and produces the desired outcome. You can also use debugging tools, such as breakpoints and print statements, to identify and fix any errors in your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
11K
  • Programming and Computer Science
Replies
9
Views
3K
Back
Top