Lab assignment 2 - Hoppsansa![]() In this assignment you will write a program 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. The program must keep track of whose turn it is (red or green) and not allow two subsequent green (or red) moves. Model - game logicsYou are expected to put all the game logics in the classModel .
The following methods are essential for the game and must be defined:
move(k); // if possible, make a move with square 2, otherwise do nothing get(k); // return the colour (e.g. 0, 1 or 2) of square kIf you find it necessary, feel free to define additional methods! A natural choice for representing the empty space and the game pieces are simply 0, 1 and 2. The Model must keep track of whose turn it is to move. It is clever to use a variable empty to store the index of the empty square.
Think carefully about the logics of the game in order to maket it
clear and short.
Please note that the Model class does not play
the game, it provides the necessary methods for playing but the actual
playing is done elsewhere.
Text version of the gameTest yourModel class in a text-application, either by writing a
main method in the class itself or by building a small separate
test program (with only a main method). The game is played by invoking
methods from the Model class.
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:Addition, april 14th: The Scanner class in package java.util is useful for reading input from a terminal window. Click here to see a small example of Scanner use: HelloQuestion.java. A component with an inner class for an extended buttonIn the grafic version of the game there must be clickable squares that can change colour. Use an extended version ofJButton
similar to the one in lab1.
The extended button should have 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 redThe Square class could be an inner class of a container class
of your choice representing the game.
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() . This solution is not accepted.
You must find of something more elegant!
You may choose yourself to implement the game as an applet or an application. Lab demonstration
Extra assignment 1:Let frogs jump to the right and let penguins jump to the left in the grafic version of the game. The game with red and green squares must be demonstrated separately and may not be replaced by the frog-penguine version. Use right click on the images below if you wish to download them!
Extra assignment 2:Extend the program so that the user gets feedback on each move, i.e. a message should be displayed saying whether the move is OK or not. If the move was erroneous, show the kind of error made! Let the Model class manage the error messages. The View part of the program must of course display the messages to the user. This extra assignment may be included in the program from start and is not necessary to present in a separate program. It is sufficient to include it in the grafic program only (but it must be in the Model).Don't forget to ask the teacher to sign the signature sheet when your lab is approved! The signature sheet (swedish: kvittensblad) is available from the course home page under Laborationer
Published by: <ann@nada.kth.se> Updated 2010-03-17 Technical support: <webmaster@nada.kth.se> |