This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this work? The 15-puzzle using swing.

 
Bhuturam Pandey
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I wrote this program to do the 15 Puzzle using swing. It compiles properly and the display comes up okay. But the blank button and the numbered buttons are supposed to swap on click if they are neighbours (north, south,eat or west). That's not happening. And I have no idea why. Can you guys help me?
Also, how do I get the frame displayed at teh centre of the screen?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleFrame extends JFrame implements ActionListener {
private Container gframe;
private JButton[] ButtArray = new JButton[16];
public PuzzleFrame() {
setDefaultCloseOperation(PuzzleFrame.EXIT_ON_CLOSE);
setSize(400,400);
setTitle("Headache");
gframe = this.getContentPane();
gframe.setLayout(new GridLayout(4,4));
for(int i = 0;i < 15;i++) {
ButtArray[i] = new JButton(""+(1+i));
this.getContentPane().add(ButtArray[i]);
ButtArray[i].addActionListener(this);
}
ButtArray[15] = new JButton("");
this.getContentPane().add(ButtArray[15]);
ButtArray[15].addActionListener(this);
}
public int north(int i) {
if ((i<4)&&(i>=0)) return -1;
else return (i-4);
}
public int south(int i) {
if ((i>11)&&(i<16)) return -1;
else return (i+4);
}
public int west(int i) {
if ((i==0)||(i==4)||(i==8)||(i==12)) return -1;
else return (i-1);
}
public int east (int i) {
if ((i==3)||(i==7)||(i==11)||(i==15)) return -1;
else return (i+1);
}
public void astionPerformed(ActionEvent click) {
Object source = click.getSource();
for(int i = 0; i<16;i++) {
if (source == ButtArray[i]) {
if (north(i) != (-1)) {
if ButtArray[north(i)].getText().equals("")) {
ButtArray[north(i)].setText(ButtArray[i].getText());
ButtArray[i].setText("");
}}
else if (south(i) != (-1)) {
if ButtArray[south(i)].getText().equals("")) {
ButtArray[south(i)].setText(ButtArray[i].getText());
ButtArray[i].setText("");
}}
else if (east(i) != (-1)) {
if ButtArray[east(i)].getText().equals("")) {
ButtArray[east(i)].setText(ButtArray[i].getText());
ButtArray[i].setText("");
}}
else if (west(i) != (-1)) {
if ButtArray[west(i)].getText().equals("")) {
ButtArray[west(i)].setText(ButtArray[i].getText());
ButtArray[i].setText("");
}}
repaint();
}
}
}
public static void main(String[] args) {
PuzzleFrame pfr = new PuzzleFrame();
pfr.setvisible(true);
}
}
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is in actionPerformed() where you check for squares in all directions and only when there is a square found do you check if that square is the blank square. Consider the situation where there is a square north and when it is checked is found to be nonblank - does your code as it now stands then go on to check the squares south, east, and west? The answer is no. You want to change that.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing/AWT forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic