-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoom.java
executable file
·198 lines (182 loc) · 5.46 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// YichunZhang k19012458
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Class Room - a room in an adventure game.
* This class is part of the "Underground Base Escape" application.
*
* 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 Michael Kölling, David J. Barnes and Yichun Zhang
* @version 2016.02.29
*/
public class Room
{
private String name;
private String description;
private boolean isLocked;
private HashMap<String, Room> exits; // stores exits of this room.
private ArrayList<Item> items; // stores items of this room.
/**
* Create a room with its "name" and whether it is locked
* @param name The room's name.
* @param description The room's description
* @param isLocked Whether the room is locked
*/
public Room(String name, String description, boolean isLocked)
{
this.name = name;
this.description = description;
this.isLocked = isLocked;
exits = new HashMap<>();
items = new ArrayList<>();
}
/**
* 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)
{
exits.put(direction, neighbor);
}
/**
* Define an item from this room.
* @param itemName The name of the item.
*/
public void setItem(Item itemName)
{
items.add(itemName);
}
/**
* @return A long description of the unlocked room
*/
public String getDescriptionUnlocked()
{
return "You are in a " + name;
}
/**
* @return A long description of the locked room
*/
public String getDescriptionLocked(Room lockedRoomName)
{
return "Door locked.\nYou are in front of a " + lockedRoomName.getRoomName() +
"\n" + lockedRoomName.getdescription();
}
/**
* @return Name of the room
*/
public String getRoomName()
{
return name;
}
/**
* @return Description of the room
*/
public String getdescription()
{
return description;
}
/**
* @return true, if the room is locked, false otherwise
*/
public boolean getIsLocked()
{
return isLocked;
}
/**
* Define the room is inlocked
*/
public void setUnlocked()
{
isLocked = false;
}
/**
* Combine two strings describing the room's exits and items, for example
* "Exits: north | west |
* Items: Key | EnergyStone |"
* @return Details of the room's exits and items.
*/
public String getInfo()
{
return getItemString() + "\n" + getExitString();
}
/**
* 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 a string describing the room's items, for example
* "Items: Key | EnergyStone | ".
* @return Details of the room's items.
*/
private String getItemString()
{
String itemString = "Items: ";
for(Iterator<Item> it = items.iterator(); it.hasNext(); ) {
Item item = it.next();
if(item.getIsVisible()) {
itemString += item.getName() + " | ";
}
}
return itemString;
}
/**
* Get names of all neighbour rooms of the current room
* @return neighbours, an ArrayList of all the neighbours' names
*/
public ArrayList<String> getNeighbourRooms()
{
ArrayList<String> neighbours = new ArrayList<>();
Set<String> keys = exits.keySet();
for(String exit : keys) {
neighbours.add(getExit(exit).getRoomName());
}
return neighbours;
}
/**
* 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);
}
/**
* Return whether the item can be picked up (is overweight)
* @param itemName The name of the item
* @param totalWeight Total weight of items player carry with
* @param weightLimit Maximum total weight of items player can carry with
* @return true, if the item is overweight, false otherwise
*/
public boolean pickUp(String itemName, int totalWeight, int weightLimit)
{
for(Iterator<Item> it = items.iterator(); it.hasNext(); ) {
Item item = it.next();
if(item.getName().equals(itemName)) {
boolean isOverweight = weightLimit < (totalWeight
+ item.getWeight());
if(!isOverweight) {
return item.pickUpItem();
}
}
}
return false;
}
}