-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoom.java
171 lines (152 loc) · 4.75 KB
/
Room.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.util.Set;
import java.util.HashMap;
/**
* Class Room - a room in an adventure game.
*
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* A "Room" represents one location in the scenery of the game. It is
* connected to other rooms via exits. For each existing exit, the room
* stores a reference to the neighboring room.
*
* @author Fredrik Ljungdahl, Michael Kölling and David J. Barnes
* @version 2013.12.19
*/
public class Room {
private String description;
private Key key;
private HashMap<String, Room> exits; // stores exits of this room.
private HashMap<String, String> exitInfo; // stores properties of the exits
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description) {
initRoom(description, null);
}
/**
* Create a room, but clarify whether or not it has a key.
* @param description The room's description.
* @param hasKey Whether or not the room contains a key.
*/
public Room(String description, Key key) {
initRoom(description, key);
}
/**
* Actual room initialization.
*/
private void initRoom(String description, Key key) {
this.description = description;
this.key = key;
exits = new HashMap<String, Room>();
exitInfo = new HashMap<String, String>();
}
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor) {
setExit(direction, neighbor, "ok");
}
/**
* Define a locked exit.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
* @param key The key needed to open the door.
*/
public void setExit(String direction, Room neighbor, Key key) {
exitInfo.put(direction + "Key", key.toString());
setExit(direction, neighbor, "locked");
}
/**
* Define a special exit from this room (one-way, locked, etc).
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
* @param state The state of the exit (default "ok" - i.e. locked, trapped, ok)
*/
public void setExit(String direction, Room neighbor, String state) {
exits.put(direction, neighbor);
exitInfo.put(direction + "State", state);
}
/**
* @return The short description of the room
* (the one that was defined in the constructor).
*/
public String getShortDescription() {
return description;
}
/**
* Return a description of the room in the form:
* You are in the kitchen.
* Exits: north west
* @return A long description of this room
*/
public String getLongDescription() {
return "You are " + description + ".\n" + getExitString();
}
/**
* Gets the state of a specific exit
*/
public String getState(String direction) {
return exitInfo.get(direction + "State");
}
/**
* Sets the exit state.
*/
public void setState(String direction, String state) {
exitInfo.put(direction + "State", state);
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
private String getExitString() {
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction) {
return exits.get(direction);
}
/**
* Get the room key lock identifier.
*/
public String getExitKey(String direction) {
return exitInfo.get(direction + "Key");
}
/**
* Checks if the room has the key.
*/
public boolean hasKey() {
if (this.key == null) {
return false;
}
return true;
}
/**
* Get the room key.
*/
public Key getKey() {
return key;
}
/**
* Get the key identifer.
*/
public String getKeyInfo() {
return key.toString();
}
}