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

room58

By: ace on Aug 11th, 2010  |  syntax: None  |  size: 2.95 KB  |  hits: 29  |  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.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Set;
  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.    /**
  29. * Create a room described "description". Initially, it has
  30. * no exits. "description" is something like "a kitchen" or
  31. * "an open court yard".
  32. */
  33. public Room(String description)
  34. {
  35. this.description = description;
  36. exits = new HashMap<String,Room>();
  37. items = new ArrayList<Item>();
  38. }
  39.  
  40. /**
  41. * Define an exit from this room.
  42. */
  43. public void setExit(String direction, Room neighbor)
  44. {
  45. exits.put(direction, neighbor);
  46. }
  47.  
  48.  
  49. /**
  50. * Define the exits of this room. Every direction either leads
  51. * to another room or is null (no exit there).
  52. */
  53.  
  54. /**
  55. * Return the room that is reached if we go from this room in
  56. * direction "direction". If there is no room in that
  57. * direction, return null.
  58. */
  59. public Room getExit(String direction)
  60. {
  61. return exits.get(direction);
  62. }
  63.  
  64. /**
  65. * Return the description of the room (the one that was defined
  66. * in the constructor).
  67. */
  68. public String getDescription()
  69. {
  70. return description;
  71. }
  72.  
  73. public String getExitString()
  74.     {
  75.        String exitString = "";
  76.        Set<String> exitSet = exits.keySet();
  77.         for(String s : exitSet){
  78.             exitString = exitString + s +" ";
  79.     }
  80.     return exitString;
  81. }
  82. /**
  83.      * Create a room described "description". Initially, it has
  84.      * no exits. "description" is something like "a kitchen" or
  85.      * "an open court yard".
  86.      */
  87.    
  88. /**
  89. * Return a long description of this room, on the form:
  90. *
  91. You are in the kitchen.
  92. *
  93. Exits: north west
  94. */
  95. public String getLongDescription()
  96. {
  97. String itemDescriptions = "";
  98.  
  99. for(Item item : items)
  100. {
  101.        
  102.            itemDescriptions = itemDescriptions + item.getItemDescription() + " ";
  103.     }
  104.    
  105.  
  106.  
  107.  
  108. return "You are " + description + "." +itemDescriptions + "\n" + getExitString();
  109. }
  110.  
  111. /**
  112.  * sets items for rooms
  113.  */
  114.  
  115.   public void setItems(Item item)
  116.         {
  117.             items.add(item);
  118. }
  119.    
  120.     public boolean checkItems(String itemName)
  121.         {
  122.            
  123.           for (Item item : items)
  124.           {
  125.               if (item.getItemName().equals(itemName))
  126.                 {
  127.                     return true;
  128.                 }
  129.                
  130.             }
  131.                 return false;
  132.            
  133.         }
  134.  
  135.  
  136. }