Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Set;
- /**
- * Exericise 8.8
- * Class Room - a room in an adventure game.
- *
- * This class is part of the "World of Zuul" application.
- * "World of Zuul" is a very simple, text based adventure game.
- *
- * A "Room" represents one location in the scenery of the game. It is
- * connected to other rooms via exits. The exits are labelled north,
- * east, south, west. For each direction, the room stores a reference
- * to the neighboring room, or null if there is no exit in that direction.
- *
- * @author Michael Kölling and David J. Barnes
- * @version 2016.02.29
- */
- public class Room
- {
- private String description;
- private HashMap<String, Room> exits; // stores exits of this room.
- /**
- * Create a room described "description". Initially, it has
- * no exits. "description" is something like "a kitchen" or
- * "an open court yard".
- * @param description The room's description.
- */
- public Room(String description)
- {
- this.description = description;
- exits = new HashMap<>();
- }
- /**
- * Define the exits of this room.
- * @param direction The direction of the exit.
- * @param neighbor The room in the given direction.
- */
- public void setExits(String direction, Room neighbor)
- {
- exits.put(direction, neighbor);
- }
- /**
- * Return the room that is reached if we go from this room in direction
- * "direction." If there is no room in that direction, return null.
- * @param direction The exit's direction.
- * @return The room in the given direction.
- */
- public Room getExit(String direction)
- {
- return exits.get(direction);
- }
- /**
- * @return The description of the room
- * (the one that was defined in the constructor).
- */
- public String getDescription()
- {
- return description;
- }
- /**
- * Return a description of the room's exits,
- * for example "Exits: north west."
- * @return A description of the available exits.
- */
- public String getExitString()
- {
- String returnString = "Exits:";
- Set<String> keys = exits.keySet();
- for(String exit : keys) {
- returnString += " " + exit;
- }
- return returnString;
- }
- /**
- * Return a long description of this room, of the form:
- * You are in the kitchen.
- * Exits: north west
- * @return A description of the room, including exits.
- */
- public String getLongDescription()
- {
- return "You are " + description + ".\n" + getExitString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement