Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Set;
  3. /**
  4.  * Exericise 8.8
  5.  * Class Room - a room in an adventure game.
  6.  *
  7.  * This class is part of the "World of Zuul" application.
  8.  * "World of Zuul" is a very simple, text based adventure game.  
  9.  *
  10.  * A "Room" represents one location in the scenery of the game.  It is
  11.  * connected to other rooms via exits.  The exits are labelled north,
  12.  * east, south, west.  For each direction, the room stores a reference
  13.  * to the neighboring room, or null if there is no exit in that direction.
  14.  *
  15.  * @author  Michael Kölling and David J. Barnes
  16.  * @version 2016.02.29
  17.  */
  18. public class Room
  19. {
  20.     private String description;
  21.     private HashMap<String, Room> exits;        // stores exits of this room.
  22.  
  23.     /**
  24.      * Create a room described "description". Initially, it has
  25.      * no exits. "description" is something like "a kitchen" or
  26.      * "an open court yard".
  27.      * @param description The room's description.
  28.      */
  29.     public Room(String description)
  30.     {
  31.         this.description = description;
  32.         exits = new HashMap<>();
  33.     }
  34.  
  35.     /**
  36.      * Define the exits of this room.
  37.      * @param direction The direction of the exit.
  38.      * @param neighbor The room in the given direction.
  39.      */
  40.     public void setExits(String direction, Room neighbor)
  41.     {
  42.         exits.put(direction, neighbor);
  43.     }
  44.    
  45.     /**
  46.      * Return the room that is reached if we go from this room in direction
  47.      * "direction." If there is no room in that direction, return null.
  48.      * @param direction The exit's direction.
  49.      * @return The room in the given direction.
  50.      */
  51.     public Room getExit(String direction)
  52.     {
  53.         return exits.get(direction);
  54.     }
  55.    
  56.     /**
  57.      * @return The description of the room
  58.      * (the one that was defined in the constructor).
  59.      */
  60.     public String getDescription()
  61.     {
  62.         return description;
  63.     }
  64.    
  65.     /**
  66.      * Return a description of the room's exits,
  67.      * for example "Exits: north west."
  68.      * @return A description of the available exits.
  69.      */
  70.     public String getExitString()
  71.     {
  72.         String returnString = "Exits:";
  73.         Set<String> keys = exits.keySet();
  74.         for(String exit : keys) {
  75.             returnString += " " + exit;
  76.         }
  77.         return returnString;
  78.     }
  79.    
  80.     /**
  81.      * Return a long description of this room, of the form:
  82.      *     You are in the kitchen.
  83.      *     Exits: north west
  84.      * @return A description of the room, including exits.
  85.      */
  86.     public String getLongDescription()
  87.     {
  88.         return "You are " + description + ".\n" + getExitString();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement