Java Reference
In-Depth Information
Exercise 6.41 Add some form of time limit to your game. If a certain task is not completed
in a specified time, the player loses. A time limit can easily be implemented by counting the
number of moves or the number of entered commands. You do not need to use real time.
Exercise 6.42 Implement a trapdoor somewhere (or some other form of door that you can
only cross one way).
Exercise 6.43 Add a beamer to the game. A beamer is a device that can be charged and fired .
When you charge the beamer, it memorizes the current room. When you fire the beamer, it transports
you immediately back to the room it was charged in. The beamer could either be standard equipment
or an item that the player can find. Of course, you need commands to charge and fire the beamer.
Exercise 6.44 Add locked doors to your game. The player needs to find (or otherwise ob-
tain) a key to open a door.
Exercise 6.45 Add a transporter room. Whenever the player enters this room, he/she is
randomly transported into one of the other rooms. Note: Coming up with a good design for
this task is not trivial. It might be interesting to discuss design alternatives for this with other
students. (We discuss design alternatives for this task at the end of Chapter 9. The adventur-
ous or advanced reader may want to skip ahead and have a look.)
Exercise 6.46 Challenge exercise In the processCommand method in Game , there is a
switch statement (or a sequence of if statements) to dispatch commands when a command
word is recognized. This is not a very nice design, because every time we add a command, we
have to add a case here. Can you improve this design? Design the classes so that handling of
commands is more modular and new commands can be added more easily. Implement it. Test it.
Exercise 6.47 Add characters to the game. Characters are similar to items, but they can
talk. They speak some text when you first meet them, and they may give you some help if you
give them the right item.
Exercise 6.48 Add moving characters. These are like other characters, but every time the
player types a command, these characters can move into an adjoining room.
6.15
Executing without BlueJ
When our game is finished, we may want to pass it on to others to play. To do this, it would
be nice if people could play the game without the need to start BlueJ. To be able to do this, we
need one more thing: class methods, which in Java are also referred to as static methods .
6.15.1
Class methods
So far, all methods we have seen have been instance methods : they are invoked on an instance
of a class. What distinguishes class methods from instance methods is that class methods can be
invoked without an instance—having the class is enough.
 
 
Search WWH ::




Custom Search