Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The Fifteen Program

Name: Anonymous 2007-03-03 16:01 ID:aa0Ae61k

I have an assignment for a programming class where I have to use Applets to create a 4x4 grid filled with buttons numbered 0-15, where the very first button (in the top left corner) starts out "empty" with a blue background, like this:
    public void init()
    {
        int count = 0;
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0 ; j < 4; j++)
            {
                if(count == 0)
                {
                    squares[i][j].setBackground(Color.blue);
                }
                else
                {
                    squares[i][j].setBackground(Color.lightGray);
                    squares[i][j].setText(""+ count);
                }
                count++;              
            }
        }
    }

I won't ask anyone to actually do the assignment, but part of it entails making that empty button randomly move throughout the grid, up or down, left or right, at least 30 times. I am having trouble getting the empty button to actually move around, as it stops after just moving once. Heres the code involved:

public void randomize()
    {
        int i = 0;
        int j = 0;
        int k = 0;
        int ran;
        while(k < 30)
        {
            pause();
            ran = (int)(4*Math.random());
            switch (ran)
            {
                case 0: if(valid(j+1)){j++;moveSquare(i, j);k++;};
                case 1: if(valid(j-1)){j--;moveSquare(i, j);k++;};
                case 2: if(valid(i+1)){i++;moveSquare(i, j);k++;};
                case 3: if(valid(i-1)){i--;moveSquare(i, j);k++;};
            }
        }
    }

public void moveSquare (int i, int j)
    {
    JButton empty = null;
    //  find if the empty square is adjacent to the selected one.
    //  the neighbors of (i, j) are (i+1, j), (i-1, j), (i, J+1), and (i, j-1).
    //  as long as all quantities are between 0 and 3.
       
        if (i < 3 && squares [i+1][j].getBackground( ) == Color.blue)
            empty = squares [i+1][j];
        if (i > 0 && squares [i -1][j].getBackground( ) == Color.blue)
            empty = squares [i -1][j];
        if (j < 3 && squares [i][j+1].getBackground( ) == Color.blue)
            empty = squares [i][j+1];
        if (j > 0 && squares [i][j -1].getBackground( ) == Color.blue)
            empty = squares [i][j -1];   
        if (empty == null) return;    //  player pressed on a button that cannot be moved   
        else
        {
            empty.setText (squares [i][j].getText ( ));
            empty.setBackground (Color.lightGray);
            squares [i][j].setText("");
            squares [i][j].setBackground (Color.blue);
        }       
    }

Where am I messing up?

Name: ‮‮‮‮penis penis penis 2007-03-04 0:49 ID:Heaven

­>>6
Sorry for failing at life
Fix'd.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List