-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.java
More file actions
185 lines (165 loc) · 4.73 KB
/
Room.java
File metadata and controls
185 lines (165 loc) · 4.73 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.HashMap;
import java.util.Set;
/**
* Class Room - Represents a location in the game.
*
* A Room is a specific location in the game's map. Each room has a description
* and can be connected to other rooms in various directions.
*
*
* @author Florian NELCHA
*/
public class Room
{
// ### Attributes ###
private String aDescription;
private HashMap <String,Room> aExits;
private String aImageName;
private ItemList aItems;
// ### Constructor ###
/**
* Constructor to initialize the room with a description, an image, and two HashMaps with
* its exits and Items.
*
* @param pD Description of the room.
* @param pI Location of the room's image.
*/
public Room(final String pD, final String pI)
{
this.aDescription = pD;
this.aExits = new HashMap <String,Room>();
this.aImageName = pI;
this.aItems = new ItemList();
} // Room()
// ### Getters ###
/**
* Gets the description of the room.
*
* @return Description of the room.
*/
public String getDescription()
{
return this.aDescription;
} // getDescription()
/**
* Gets the room in the specified direction if it exists.
*
* @param pDirection The direction in which to look for an exit.
* @return The neighboring room in the specified direction, or null
* if it doesn't exist.
*/
public Room getExit(final String pDirection)
{
if (this.aExits.containsKey(pDirection))
{
return this.aExits.get(pDirection);
}
return null;
} // getExit()
/**
* Creates a keySet of all the exits from the current room, then constructs
* a string of it.
*
* @return A string with all available exit directions.
*/
public String getExitString()
{
String vExitString = "Exits :";
Set<String> vKeys = this.aExits.keySet();
for(String vExit : vKeys)
{
vExitString += " " + vExit;
}
return vExitString;
} // getExitString()
/**
* Returns a description of the current room, of the form :
* You are in the Control Room.
* Exits : south
*
* @return A description of the room, and its exits
*/
public String getLongDescription()
{
return "You are " + this.aDescription + "\n" + getExitString() + "\n" + getItemString();
} // getLongDescription()
/**
* Returns the name of the Room's image.
*
* @return The name of the Room's image.
*/
public String getImageName()
{
return this.aImageName;
} //getImageName()
/**
* Returns the item having the specified name.
*
* @param pName The name of the item we look for.
* @return The item having the specified name.
*/
public Item getItem(final String pName)
{
return this.aItems.getItem(pName);
} //getItem()
/**
* Creates a keySet of all the Items in the current room, then constructs
* a string of it.
*
* @return A string with all available Items.
*/
public String getItemString()
{
return "Items in the room : " + this.aItems.getItemString();
} //getItemString()
// ### Setters ###
/**
* Sets a new description for the room.
*
* @param pNewD New description to set.
*/
public void updateDescription(final String pNewD)
{
this.aDescription = pNewD;
} //updateDescription()
/**
* Sets an exit for the specified direction to a neighboring room.
*
* @param pDirection Direction of the exit.
* @param pNeighbor The neighboring room in that direction.
*/
public void setExits(final String pDirection,
final Room pNeighbor)
{
aExits.put(pDirection, pNeighbor);
} //setExits()
/**
* Adds an Item in the room
*
* @param pItem Item to add in the room.
*/
public void addItem(final Item pItem)
{
this.aItems.addItem(pItem);
} //addItems()
/**
* Remove the specified Item from the room
*
* @param pItemName Name of the Item to remove from the room.
*/
public void removeItem(final String pItemName)
{
this.aItems.removeItem(pItemName);
} //removeItem()
// ### Other Methods ###
/**
* Checks if the given room is one of the exits of this room.
*
* @param pRoom The room to check.
* @return True if the room is an exit, false otherwise.
*/
public boolean isExit(final Room pRoom)
{
return this.aExits.containsValue(pRoom);
} // isExit()
} // Room