Advertisement
Guest User

Untitled

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