001 import java.util.Set; 002 import java.util.HashMap; 003 import java.util.Iterator; 004 005 /** 006 * Class Room - a room in an adventure game. 007 * 008 * This class is part of the "World of Zuul" application. 009 * "World of Zuul" is a very simple, text based adventure game. 010 * 011 * A "Room" represents one location in the scenery of the game. It is 012 * connected to other rooms via exits. For each existing exit, the room 013 * stores a reference to the neighboring room. 014 * 015 * @author Michael Kolling and David J. Barnes 016 * @version 2011.07.31 017 */ 018 019 public class Room 020 { 021 private String description; 022 private HashMap<String, Room> exits; // stores exits of this room. 023 024 /** 025 * Create a room described "description". Initially, it has no exits. 026 * "description" is something like "in a kitchen" or "in an open court 027 * yard". 028 */ 029 public Room(String description) 030 { 031 this.description = description; 032 exits = new HashMap<String, Room>(); 033 } 034 035 /** 036 * Define an exit from this room. 037 */ 038 public void setExit(String direction, Room neighbor) 039 { 040 exits.put(direction, neighbor); 041 } 042 043 /** 044 * Return the description of the room (the one that was defined in the 045 * constructor). 046 */ 047 public String getShortDescription() 048 { 049 return description; 050 } 051 052 /** 053 * Return a long description of this room, in the form: 054 * You are in the kitchen. 055 * Exits: north west 056 */ 057 public String getLongDescription() 058 { 059 return "You are " + description + ".\n" + getExitString(); 060 } 061 062 /** 063 * Return a string describing the room's exits, for example 064 * "Exits: north west". 065 */ 066 private String getExitString() 067 { 068 String returnString = "Exits:"; 069 Set<String> keys = exits.keySet(); 070 for(Iterator<String> iter = keys.iterator(); iter.hasNext(); ) { 071 returnString += " " + iter.next(); 072 } 073 return returnString; 074 } 075 076 /** 077 * Return the room that is reached if we go from this room in direction 078 * "direction". If there is no room in that direction, return null. 079 */ 080 public Room getExit(String direction) 081 { 082 return exits.get(direction); 083 } 084 } 085