-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exit.java
70 lines (58 loc) · 2.04 KB
/
Exit.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
/**
*
*/
/** Exits hold information related to being able to move from one Room to another in a bork game.
* @author Team Red
*
*/
import java.util.Scanner;
public class Exit {
class NoExitException extends Exception {}
private String dir;
private Room src, dest;
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 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 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() {
}
String describe() {
return "You can go " + dir + " to " + dest.getTitle() + ".";
}
String getDir() { return dir; }
Room getSrc() { return src; }
Room getDest() { return dest; }
}