Java Reference
In-Depth Information
Code 6.1
continued
Selected sections of the
(badly designed) Game
class
System.out.println(
"Zuul is a new, incredibly boring adventure game." );
System.out.println( "Type 'help' if you need help." );
System.out.println();
System.out.println( "You are " +
currentRoom.getDescription());
System.out.print( "Exits: " );
if (currentRoom.northExit != null ) {
System.out.print( "north " );
}
if (currentRoom.eastExit != null ) {
System.out.print( "east " );
}
if (currentRoom.southExit != null ) {
System.out.print( "south " );
}
if (currentRoom.westExit != null ) {
System.out.print( "west " );
}
System.out.println();
}
// . . . some code omitted . . .
/**
* Try to go in one direction. If there is an exit, enter
* the new room; otherwise print an error message.
*/
private void goRoom(Command command)
{
if (!command.hasSecondWord()) {
// if there is no second word,
// we don't know where to go
System.out.println( "Go where?" );
return ;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = null ;
if (direction.equals( "north" )) {
nextRoom = currentRoom.northExit;
}
if (direction.equals( "east" )) {
nextRoom = currentRoom.eastExit;
}
Search WWH ::




Custom Search