Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

roomclassitems2

By: ace on Jul 21st, 2010  |  syntax: None  |  size: 2.39 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.HashMap;
  2. import java.util.Set;
  3. /*
  4.  * Class Room - a room in an adventure game.
  5.  *
  6.  * This class is part of the "World of Zuul" application.
  7.  * "World of Zuul" is a very simple, text based adventure game.  
  8.  *
  9.  * A "Room" represents one location in the scenery of the game.  It is
  10.  * connected to other rooms via exits.  The exits are labelled north,
  11.  * east, south, west.  For each direction, the room stores a reference
  12.  * to the neighboring room, or null if there is no exit in that direction.
  13.  *
  14.  * @author  Michael Kolling and David J. Barnes
  15.  * @version 1.0 (February 2002)
  16.  */
  17.  
  18. public class Room
  19. {
  20.  
  21. private String description;
  22. private HashMap<String, Room> exits;
  23. private HashMap<Item, Room> items;
  24.  
  25.  
  26.    /**
  27. * Create a room described "description". Initially, it has
  28. * no exits. "description" is something like "a kitchen" or
  29. * "an open court yard".
  30. */
  31. public Room(String description)
  32. {
  33. this.description = description;
  34. exits = new HashMap<String,Room>();
  35. items = new HashMap<Item, Room>();
  36. }
  37.  
  38. /**
  39. * Define an exit from this room.
  40. */
  41. public void setExit(String direction, Room neighbor)
  42. {
  43. exits.put(direction, neighbor);
  44. }
  45.  
  46.  
  47. /**
  48. * Define the exits of this room. Every direction either leads
  49. * to another room or is null (no exit there).
  50. */
  51.  
  52. /**
  53. * Return the room that is reached if we go from this room in
  54. * direction "direction". If there is no room in that
  55. * direction, return null.
  56. */
  57. public Room getExit(String direction)
  58. {
  59. return exits.get(direction);
  60. }
  61.  
  62. /**
  63. * Return the description of the room (the one that was defined
  64. * in the constructor).
  65. */
  66. public String getDescription()
  67. {
  68. return description;
  69. }
  70.  
  71. public String getExitString()
  72.     {
  73.        String exitString = "";
  74.        Set<String> exitSet = exits.keySet();
  75.         for(String s : exitSet){
  76.             exitString = exitString + s +" ";
  77.     }
  78.     return exitString;
  79. }
  80. /**
  81.      * Create a room described "description". Initially, it has
  82.      * no exits. "description" is something like "a kitchen" or
  83.      * "an open court yard".
  84.      */
  85.    
  86. /**
  87. * Return a long description of this room, on the form:
  88. *
  89. You are in the kitchen.
  90. *
  91. Exits: north west
  92. */
  93. public String getLongDescription()
  94. {
  95. return "You are " + description + ".\n" + getExitString();
  96. }
  97.  
  98.    public void setItems(Item item, Room room)
  99.         {
  100.             items.put(item, room);
  101. }
  102.    
  103.  
  104.  
  105.  
  106. }