-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExit.java
More file actions
105 lines (90 loc) · 3.1 KB
/
Exit.java
File metadata and controls
105 lines (90 loc) · 3.1 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
package bork;
import java.util.Scanner;
/**
* A class that represents the means by which an adventurer leaves or enters a room.
* It includes getters for the rooms that it connects, as well as the direction
* it represents, and can describe itself.
* @author Dr. Zeitz
*/
public class Exit {
/**
* A subclass of Exception that handles exceptions that result from the user
* trying to exit a room when there is no exit.
* @author Dr. Zeitz
*/
class NoExitException extends Exception {}
private String dir;
private Room src, dest;
/**
* Constructor of exit objects that sets the direction, source room, and
* destination room of the exit, as well as adding the exit to the source
* room's hashtable.
* @param dir the direction by which the exit can be accessed
* @param src the room to be left
* @param dest the room to be entered
*/
Exit(String dir, Room src, Room dest) {
init();
this.dir = dir;
this.src = src;
this.dest = dest;
src.addExit(this);
}
/** Given a Scanner object positioned at the beginning of an "exit" file
entry, read and return an Exit object representing it.
@param s the Scanner object
@param d The dungeon that contains this exit (so that Room objects
may be obtained.)
@throws NoExitException The reader object is not positioned at the
start of an exit entry. A side effect of this is the reader's cursor
is now positioned one line past where it was.
@throws Dungeon.IllegalDungeonFormatException A structural problem with the
dungeon file itself, detected when trying to read this room.
*/
Exit(Scanner s, Dungeon d) throws NoExitException,
Dungeon.IllegalDungeonFormatException {
init();
String srcTitle = s.nextLine();
if (srcTitle.equals(Dungeon.TOP_LEVEL_DELIM)) {
throw new NoExitException();
}
src = d.getRoom(srcTitle);
dir = s.nextLine();
dest = d.getRoom(s.nextLine());
// I'm an Exit object. Great. Add me as an exit to my source Room too,
// though.
src.addExit(this);
// throw away delimiter
if (!s.nextLine().equals(Dungeon.SECOND_LEVEL_DELIM)) {
throw new Dungeon.IllegalDungeonFormatException("No '" +
Dungeon.SECOND_LEVEL_DELIM + "' after exit.");
}
}
/**
* Common object initialization tasks.
*/
private void init() {
}
/**
* Describes the exit for the user.
* @return the String description
*/
String describe() {
return "You can go " + dir + " to " + dest.getTitle() + ".";
}
/**
* Getter for the direction of the exit.
* @return the direction
*/
String getDir() { return dir; }
/**
* Getter for the source Room of the exit.
* @return the source room
*/
Room getSrc() { return src; }
/**
* Getter for the destination Room of the exit.
* @return the destination room
*/
Room getDest() { return dest; }
}