-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoom.java
More file actions
113 lines (101 loc) · 2.92 KB
/
Room.java
File metadata and controls
113 lines (101 loc) · 2.92 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
import java.util.HashSet;
import java.util.HashMap;
import java.util.Set;
/**
* 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. The exits are labelled north,
* east, south, west. For each direction, the room stores a reference
* to the neighboring room, or null if there is no exit in that direction.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Room
{
private String description;
private HashMap <String,Room> exits;
private boolean isExit = false;
private HashSet <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)
{
this.description = description;
exits = new HashMap<>();
items = new HashSet<>();
}
public Room(String description, boolean isExit)
{
this(description);
this.isExit = isExit;
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param direction The direction of the exit.
* @param neighbour The room in the given direction.
*/
public void setExits(String direction, Room neighbour)
{
exits.put(direction, neighbour);
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
public String getLongDescription()
{
return "You are " + description + "." + ((exits.size() != 0) ? ("\n" + getExitString()) : "") +
((items.size() != 0) ? ("\n"+ getItems()) : "");
}
public Room getExit (String direction)
{
return exits.get(direction);
}
public String getExitString ()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys)
{
returnString += " " + exit;
}
return returnString;
}
public boolean isExit(){
return isExit;
}
public void addItem(Item item){
items.add(item);
}
public String getItems()
{
String returnString = "Things in this room: ";
for(Item item : items)
{
returnString += item.getName() + ", ";
}
returnString = returnString.replaceAll(", $", "");
return returnString;
}
public Item getItem(String item){
for(Item i : items){
if(i.getName().equals(item)){
return i;
}
}
return null;
}
}