permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Why doesn't this work? The 15-puzzle using swing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why doesn Watch "Why doesn New topic
Author

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

Bhuturam Pandey
Greenhorn

Joined: Feb 27, 2004
Posts: 1
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

Joined: Oct 20, 2000
Posts: 84
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to the Swing/AWT forum.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why doesn't this work? The 15-puzzle using swing.
 
Similar Threads
I need your help
Hoe can i make the rat to move?
JTextField
JTextField
June Newsletter Puzzle