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:
Anonymous2007-03-03 16:03 ID:aa0Ae61k
Stupid me, it is probably obvious but just in case, I am doing this in Java.
Name:
Anonymous2007-03-03 16:05 ID:aa0Ae61k
Also,
public boolean valid(int num)
{
return num >= 0 && num <= 3;
}
is used to make sure that the buttons don't go off the sides of the grid.
is used so that I can see the buttons changing, and I'm too lazy to use a try-catch block with a Thread.sleep()
Name:
Anonymous2007-03-03 16:09 ID:fewNOl/o
NOW THAT'S A VERY GREAT SUPERB FANTASTIC JAVA CODE AND LOGIC.
Name:
Anonymous2007-03-03 16:11 ID:fewNOl/o
i can't be fucked to read this awful code to actually see what you are trying to do, but if the case is to generate a random number between 0 and 3 i'll give you a PROTIP:
use modulus op.
Name:
Anonymous2007-03-03 16:30 ID:aa0Ae61k
I decided to just say fuck the switch statements, and then did a bunch of if-else instead. Now the randomization works, yay.
Sorry for wasting space on this board lol.
Name:
Anonymous2007-03-03 17:35 ID:9TJFM9Ph
>>6
Then you should've put breaks at the end of your cases.
>>7
And wow, thanks for pointing that out, I really AM an idiot for forgetting something so simple.
Now to just figure out how to incorporate linked lists so that the movement of the boxes aren't trivial. I.e., they don't move back the spot they just came from.
class DoubleyLinkedList
{//a double linked list
int n;
Link front, rear, current;
public DoubleyLinkedList()
{
n = 0;//# of items on the queue
front = null;
rear = null;
}
public void addNode(Object obj)
{
Link temp = new Link();
temp.info = obj;
n++;
temp.next = null;
if(front == null)//first node on list
{
front = temp;
current = front;
front.previous = null;
}
else
{
rear.next = temp;
temp.previous = rear;
}
rear = temp;
current = rear;
}
public void reverse()
{
Link temp = rear;
System.out.println("List of directions printed in reverse.");
while(temp != null)
{
System.out.print(temp);
temp = temp.previous;
}
System.out.println();
}
Name:
Anonymous2007-03-05 21:24 ID:PAPFpw9W
public void print()
{
Link temp = front;
System.out.println("List of directions printed forwards.");
while(temp != null)
{
System.out.print(temp);
temp = temp.next;
}
System.out.println();
}
public void setNode() //sets the current node to the last node.
{
if(!atStart())
{
current = current.previous;
}
else
{
current = current;
}
}
public int getNode()
//returns the value of the current node and moves the pointer to the previous node. Used for retracing the steps.
{
int x = (Integer)current.info;
setNode();
return x;
}
public boolean atStart()
//determines whether the beginning of the list has been reached when traversing the list from the end to the beginning.
{
return current == front;
}
}
class Link
{
Object info;
Link previous, next;
public String toString()
{
return "" + info;
}
}
Name:
Anonymous2007-03-05 21:26 ID:PAPFpw9W
Above I have posted the whole program. Overall it works great, EXCEPT that for some reason, during the unscrambling process, the button stops RIGHT before the last move. What I mean is that it does not return to the top left corner. I've gone over the code for hours and can't find it, so I'm hoping anonymous can.
P.S. I HAVE to write it this way without using ListIterator etc or anything not used in my program, because those are the specifications for the assignment I am doing. Thanks in advance for the help.
Name:
Anonymous2007-03-05 21:51 ID:RAl4huSt
Make a unit test class for DoubleyLinkedList (sic). If there's a problem there, it should become obvious.
Name:
Anonymous2007-03-05 22:27 ID:PAPFpw9W
>>18
To be honest, I'm a total idiot, and so I have no idea what you want me to do >_<. The class I'm taking is like beginners level 2 java I guess.
Name:
Anonymous2007-03-06 0:00 ID:EXtrLV3x
>>19
You're using a doubly-linked list. How are you learning this in your second Java course? IIRC, Data Structures is not a freshman-level course.
Data structures is very much a freshman level course. It's very basic, fundamental stuff. What in the world would be freshman level if not Data Structures? You're not implying that an entire year should be wasted on simple constructs like loops and functions, are you?
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);
}
}
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy