-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.java
More file actions
135 lines (120 loc) · 3.63 KB
/
Room.java
File metadata and controls
135 lines (120 loc) · 3.63 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
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
* Class Room - a room in an 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 Michael Kölling, David J. Barnes and Thomas Vakili
* @version 2013.12.12
*/
public class Room
{
private String description;
private HashMap<Direction, Room> exits;
private ArrayList<Item> items;
/**
* 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, Item ... items)
{
this.description = description;
exits = new HashMap<Direction, Room>();
this.items = new ArrayList<Item>();
for (Item item: items) {
this.items.add(item);
}
}
/**
* Connects the room and it's neighbor.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void connect(Direction direction, Room neighbor) {
// An exit should only be set once, and to one neighbor only.
assert(exits.get(direction) == null || exits.get(direction) == neighbor):
"Neighbors can't be redefined!";
if (exits.get(direction) == null && neighbor != null) {
exits.put(direction, neighbor);
neighbor.connect(direction.getOpposite(), this);
}
}
/**
* Create one way connection between to rooms.
* @param direction The direction of the exit.
* @param target The destination of the exit.
*/
public void setExit(Direction direction, Room target) {
if (target != null) {
exits.put(direction, target);
}
}
/**
* @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();
}
/**
* 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<Direction> keys = exits.keySet();
for(Direction 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(Direction direction)
{
return exits.get(direction);
}
/**
* Add an item to the room.
*
* @param item the item to add
*/
public void addItem(Item item) {
this.items.add(item);
}
/**
* Remove an item from the room.
*
* @param item to remove from the room.
*/
public void removeItem(Item item) {
items.remove(items.indexOf(item));
}
public ArrayList<Item> getItems() {
return items;
}
}