forked from Jleetrahan/bork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoom.java
224 lines (195 loc) · 6.85 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
*
*/
/** Room is the class defining the Rooms of a bork Dungeon.
* It has a contents made up of Items, a list of Exits, a
* description, and keeps track of whether or not the player
* has entered it.
* @author Team Red
*
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
public class Room {
class NoRoomException extends Exception {}
static String CONTENTS_STARTER = "Contents: ";
private String title;
private String desc;
private boolean beenHere;
private ArrayList<Item> contents;
private ArrayList<Exit> exits;
Room(String title) {
init();
this.title = title;
}
Room(Scanner s, Dungeon d) throws NoRoomException,
Dungeon.IllegalDungeonFormatException {
this(s, d, true);
}
/** Given a Scanner object positioned at the beginning of a "room" file
entry, read and return a Room object representing it.
@param d The containing {@link edu.umw.stephen.bork.Dungeon} object,
necessary to retrieve {@link edu.umw.stephen.bork.Item} objects.
@param initState should items listed for this room be added to it?
@throws NoRoomException The reader object is not positioned at the
start of a room entry. A side effect of this is the reader's cursor
is now positioned one line past where it was.
@throws IllegalDungeonFormatException A structural problem with the
dungeon file itself, detected when trying to read this room.
*/
Room(Scanner s, Dungeon d, boolean initState) throws NoRoomException,
Dungeon.IllegalDungeonFormatException {
init();
title = s.nextLine();
desc = "";
if (title.equals(Dungeon.TOP_LEVEL_DELIM)) {
throw new NoRoomException();
}
String lineOfDesc = s.nextLine();
while (!lineOfDesc.equals(Dungeon.SECOND_LEVEL_DELIM) &&
!lineOfDesc.equals(Dungeon.TOP_LEVEL_DELIM)) {
if (lineOfDesc.startsWith(CONTENTS_STARTER)) {
String itemsList = lineOfDesc.substring(CONTENTS_STARTER.length());
String[] itemNames = itemsList.split(",");
for (String itemName : itemNames) {
try {
if (initState) {
add(d.getItem(itemName));
}
} catch (Item.NoItemException e) {
throw new Dungeon.IllegalDungeonFormatException(
"No such item '" + itemName + "'");
}
}
} else {
desc += lineOfDesc + "\n";
}
lineOfDesc = s.nextLine();
}
// throw away delimiter
if (!lineOfDesc.equals(Dungeon.SECOND_LEVEL_DELIM)) {
throw new Dungeon.IllegalDungeonFormatException("No '" +
Dungeon.SECOND_LEVEL_DELIM + "' after room.");
}
}
/** Common object initialization tasks.
*
*/
private void init() {
contents = new ArrayList<Item>();
exits = new ArrayList<Exit>();
beenHere = false;
}
String getTitle() { return title; }
void setDesc(String desc) { this.desc = desc; }
/*
* Store the current (changeable) state of this room to the writer
* passed.
*/
void storeState(PrintWriter w) throws IOException {
w.println(title + ":");
w.println("beenHere=" + beenHere);
if (contents.size() > 0) {
w.print(CONTENTS_STARTER);
for (int i=0; i<contents.size()-1; i++) {
w.print(contents.get(i).getPrimaryName() + ",");
}
w.println(contents.get(contents.size()-1).getPrimaryName());
}
w.println(Dungeon.SECOND_LEVEL_DELIM);
}
void restoreState(Scanner s, Dungeon d) throws
GameState.IllegalSaveFormatException {
String line = s.nextLine();
if (!line.startsWith("beenHere")) {
throw new GameState.IllegalSaveFormatException("No beenHere.");
}
beenHere = Boolean.valueOf(line.substring(line.indexOf("=")+1));
line = s.nextLine();
if (line.startsWith(CONTENTS_STARTER)) {
String itemsList = line.substring(CONTENTS_STARTER.length());
String[] itemNames = itemsList.split(",");
for (String itemName : itemNames) {
try {
add(d.getItem(itemName));
} catch (Item.NoItemException e) {
throw new GameState.IllegalSaveFormatException(
"No such item '" + itemName + "'");
}
}
s.nextLine(); // Consume "---".
}
}
public String describe() {
String description;
if (beenHere) {
description = title;
} else {
description = title + "\n" + desc;
}
for (Item item : contents) {
description += "\nThere is a " + item.getPrimaryName() + " here.";
}
if (contents.size() > 0) { description += "\n"; }
//changed so exits print out every time you walk into room
for (Exit exit : exits) {
description += "\n" + exit.describe();
}
beenHere = true;
return description;
}
public Room leaveBy(String dir) {
for (Exit exit : exits) {
if (exit.getDir().equals(dir)) {
return exit.getDest();
}
}
return null;
}
void addExit(Exit exit) {
exits.add(exit);
}
void add(Item item) {
contents.add(item);
}
void remove(Item item) {
contents.remove(item);
}
Item getItemNamed(String name) throws Item.NoItemException {
for (Item item : contents) {
if (item.goesBy(name)) {
return item;
}
}
throw new Item.NoItemException();
}
ArrayList<Item> getContents() {
return contents;
}
/** Removes the parameter Item from this Room if it's there
*
* @param item Item to remove from the Dungeon
* @return True if the parameter Item is in this Room and was removed, false otherwise
*/
boolean obliterateItem(Item item)
{
return contents.remove(item);
}
/** Removes the parameter itemToDestory from this Room if it's there, then replaces it with the parameter itemToAdd
*
* @param itemToDestroy Item to remove from the Room
* @param itemToAdd Item with which to replace itemToDestroy
* @return True if the parameter itemToDestory is in this Room and was replaced, false otherwise
*/
boolean transformItem(Item itemToDestroy, Item itemToAdd)
{
if(obliterateItem(itemToDestroy))
{
add(itemToAdd);
return true;
}
return false;
}
}