Nada

Lab assignment 2 - Hoppsansa

hoppbild.gif
In this assignment you will write an applet that plays a tricky game.
One purpose of the lab is to learn about components, inner classes, layout managers and event listeners and practice using them. Antoher purpose is to learn about and use the MVC pattern to structure the program. The name of the game is Hoppsansa and it looks like this.

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 - a text version of the game

You are expected to put all the game logic in the class 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 5
It 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).

An applet with an inner class for an extended button

Write a subclass of 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 red
Create 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.

frog.gif frog.gif frog.gif                 penguin.gif penguin.gif penguin.gif


A fun applet by (student)............................................................................

enjoyed (teacher)............................................... on (date) ..........................


Published by: <karlm@nada.kth.se>
Updated 2008-04-11
Technical support: <webmaster@nada.kth.se>