Java Reference
In-Depth Information
Code 6.1
continued
Selected sections of the
(badly designed) Game
class
if (direction.equals( "south" )) {
nextRoom = currentRoom.southExit;
}
if (direction.equals( "west" )) {
nextRoom = currentRoom.westExit;
}
if (nextRoom == null ) {
System.out.println( "There is no door!" );
}
else {
currentRoom = nextRoom;
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 . . .
}
Both the printWelcome and goRoom methods contain the following lines of code:
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 ");
}
Search WWH ::




Custom Search