Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Game error - Swing (http://www.go4expert.com/forums/game-error-swing-t17723/)

Tragar 28May2009 04:51

Game error - Swing
 
Hello everyone, I have been trying to make a sliding puzzle game where you would move blocks into an empty space and solve the puzzle. Like this sort of except I am using numbers and want people to solve the numbers to 1 to 9.

However there are two bad things happening, first of all, when i would move a block into the empty space I first start with then try to move it back it would give me an out of bounds exception as you will see if you try. The second problem is that at first the game appears to work fine but after a few moves, certain numbers duplicate or even just disapear and I have no clue why thats happening. Could someone please help me? Thank you. Here is the code.

Code:

// The "Project" class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;
class Project extends JPanel implements ActionListener
{
    static JFrame c = new JFrame ("Sliding Puzzle");
    JButton[] myButton = new JButton [9];

    public int currentlyblank = 8;
    public int[] theNumbers = {1, 2, 3, 4, 5, 6, 7, 8};
    public Project ()
    {
        c.setSize (400, 400);
        c.getContentPane ().setLayout (new GridLayout (3, 3));
        for (int i = 0 ; i < myButton.length - 1 ; i++)
        {
            myButton [i] = new JButton ((i + 1) + "");
            myButton [i].addActionListener (this);
            myButton [i].setFont (new Font ("Dialog", Font.PLAIN, 100));
            myButton [i].setEnabled (false);
            c.getContentPane ().add (myButton [i]);
        }
        myButton [8] = new JButton ("");
        myButton [8].addActionListener (this);
        myButton [8].setFont (new Font ("Dialog", Font.PLAIN, 100));
        myButton [8].setEnabled (false);
        c.getContentPane ().add (myButton [8]);
        c.setVisible (true);
        myButton [7].setEnabled (true);
        myButton [5].setEnabled (true);
    }


    public void actionPerformed (ActionEvent c)
    {
        if (c.getSource () == myButton [0])
        {
            myButton [0].setText ("");
            myButton [0].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [0] + "");
            currentlyblank = 0;
        }
        if (c.getSource () == myButton [1])
        {
            myButton [1].setText ("");
            myButton [1].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [1] + "");
            currentlyblank = 1;
        }
        if (c.getSource () == myButton [2])
        {
            myButton [2].setText ("");
            myButton [2].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [2] + "");
            currentlyblank = 2;
        }
        if (c.getSource () == myButton [3])
        {
            myButton [3].setText ("");
            myButton [3].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [3] + "");
            currentlyblank = 3;
        }
        if (c.getSource () == myButton [4])
        {
            myButton [4].setText ("");
            myButton [4].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [4] + "");
            currentlyblank = 4;
        }
        if (c.getSource () == myButton [5])
        {
            myButton [5].setText ("");
            myButton [5].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [5] + "");
            currentlyblank = 5;
        }
        if (c.getSource () == myButton [6])
        {
            myButton [6].setText ("");
            myButton [6].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [6] + "");
            currentlyblank = 6;
        }
        if (c.getSource () == myButton [7])
        {
            myButton [7].setText ("");
            myButton [7].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [7] + "");
            currentlyblank = 7;
        }
        if (c.getSource () == myButton [8])
        {
            myButton [8].setText ("");
            myButton [8].setEnabled (false);
            myButton [currentlyblank].setText (theNumbers [8] + "");
            currentlyblank = 8;
        }
        myButton [0].setEnabled (false);
        myButton [1].setEnabled (false);
        myButton [2].setEnabled (false);
        myButton [3].setEnabled (false);
        myButton [4].setEnabled (false);
        myButton [5].setEnabled (false);
        myButton [6].setEnabled (false);
        myButton [7].setEnabled (false);
        myButton [8].setEnabled (false);
        if (currentlyblank == 0)
        {
            myButton [1].setEnabled (true);
            myButton [3].setEnabled (true);
        }
        if (currentlyblank == 1)
        {
            myButton [0].setEnabled (true);
            myButton [2].setEnabled (true);
            myButton [4].setEnabled (true);
        }
        if (currentlyblank == 2)
        {
            myButton [1].setEnabled (true);
            myButton [5].setEnabled (true);
        }
        if (currentlyblank == 3)
        {
            myButton [0].setEnabled (true);
            myButton [4].setEnabled (true);
            myButton [6].setEnabled (true);
        }
        if (currentlyblank == 4)
        {
            myButton [1].setEnabled (true);
            myButton [3].setEnabled (true);
            myButton [5].setEnabled (true);
            myButton [7].setEnabled (true);
        }
        if (currentlyblank == 5)
        {
            myButton [2].setEnabled (true);
            myButton [4].setEnabled (true);
            myButton [8].setEnabled (true);
        }
        if (currentlyblank == 6)
        {
            myButton [3].setEnabled (true);
            myButton [7].setEnabled (true);
        }
        if (currentlyblank == 7)
        {
            myButton [4].setEnabled (true);
            myButton [6].setEnabled (true);
            myButton [8].setEnabled (true);
        }
        if (currentlyblank == 8)
        {
            myButton [7].setEnabled (true);
            myButton [5].setEnabled (true);
        }
    }


    public static void main (String[] args) throws Exception
    {
        new Project ();
    }
}



All times are GMT +5.5. The time now is 03:10.