
To the right are three red buttons and to the left are three green buttons. The space in the middle is "empty". In order to pass the assignment you must demonstrate that you can make the green and the red change places, i.e. move all the green to the right and move all the red to the left, only by applying the rules: When you click a button (or choose a position) it is "moved" to the empty place if the empty place is next to the clicked one or there is exactly one button (position) between the clicked and the empty.
Model.
Test your Model class in an application, either by writing a
main method in the class itself or by building a small separate
test program (with only a main method). Running the test should look something
like this.
GGG RRR Choose 0-6: 2 GG GRRR Choose 0-6: 4 GGRG RR Choose 0-6:The
Model class must have two methods which can be
invoked
move(2); // if possible, make a move with square 2, otherwise do nothing get(5); // return the colour (0, 1 or 2) of square 5It is clever to use a variable
empty to store the index of the empty square.
The methods move() and get() can be
called from the text-based test application and
later from an applet which will be your View (and Control).
Button (or JButton)
with a metod set() to be used as follows.
square[i].set(0); //Button turns white square[i].set(1); //Button turns green square[i].set(2); //Button turns redCreate seven objects of the class. Use a
GridLayout with
seven positions to display the buttons.
The Square class is an inner class of the applet.
Each square must have a listener object (an object of type
ActionListener) attached to it.
Consider the following possibilites for the listener class:
Outer class, inner class, anonymous class or an already
existing container class.
You may have a single method actionPerformed(ActionEvent e)
which takes care of the events of all seven buttons.
In this case, how do you find out which button has caused the event ?
One way to do it is to use a for-loop and test all
square[i] and check which one is equal to
e.getSource().
Extra assignment: Let frogs jump to the right and let
penguins jump to the left.
A fun applet by (student)............................................................................
enjoyed (teacher)............................................... on (date) ..........................