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

room class item->ArrayList

By: ace on Jul 21st, 2010  |  syntax: None  |  size: 2.43 KB  |  hits: 47  |  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. import java.util.ArrayList;
  4. /*
  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 Kolling and David J. Barnes
  16.  * @version 1.0 (February 2002)
  17.  */
  18.  
  19. public class Room
  20. {
  21.  
  22. private String description;
  23. private HashMap<String, Room> exits;
  24. private ArrayList<Item> items;
  25.  
  26.  
  27.    /**
  28. * Create a room described "description". Initially, it has
  29. * no exits. "description" is something like "a kitchen" or
  30. * "an open court yard".
  31. */
  32. public Room(String description)
  33. {
  34. this.description = description;
  35. exits = new HashMap<String,Room>();
  36. items = new ArrayList<Item>();
  37. }
  38.  
  39. /**
  40. * Define an exit from this room.
  41. */
  42. public void setExit(String direction, Room neighbor)
  43. {
  44. exits.put(direction, neighbor);
  45. }
  46.  
  47.  
  48. /**
  49. * Define the exits of this room. Every direction either leads
  50. * to another room or is null (no exit there).
  51. */
  52.  
  53. /**
  54. * Return the room that is reached if we go from this room in
  55. * direction "direction". If there is no room in that
  56. * direction, return null.
  57. */
  58. public Room getExit(String direction)
  59. {
  60. return exits.get(direction);
  61. }
  62.  
  63. /**
  64. * Return the description of the room (the one that was defined
  65. * in the constructor).
  66. */
  67. public String getDescription()
  68. {
  69. return description;
  70. }
  71.  
  72. public String getExitString()
  73.     {
  74.        String exitString = "";
  75.        Set<String> exitSet = exits.keySet();
  76.         for(String s : exitSet){
  77.             exitString = exitString + s +" ";
  78.     }
  79.     return exitString;
  80. }
  81. /**
  82.      * Create a room described "description". Initially, it has
  83.      * no exits. "description" is something like "a kitchen" or
  84.      * "an open court yard".
  85.      */
  86.    
  87. /**
  88. * Return a long description of this room, on the form:
  89. *
  90. You are in the kitchen.
  91. *
  92. Exits: north west
  93. */
  94. public String getLongDescription()
  95. {
  96. return "You are " + description + "." + item.getItemDescription() + "\n" + getExitString();
  97. }
  98.  
  99.    public void setItems(Item item)
  100.         {
  101.             items.add(item);
  102. }
  103.    
  104.  
  105.  
  106.  
  107. }