-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorld.java
More file actions
129 lines (114 loc) · 2.75 KB
/
World.java
File metadata and controls
129 lines (114 loc) · 2.75 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
import java.io.*;
public class World {
private Room[] Rooms;
public World (String filename) throws IOException{
String returnValue = "";
FileReader file = null;
String line = "0";
try {
file = new FileReader(filename);
BufferedReader reader = new BufferedReader(file);
line = reader.readLine();
int numRms = Integer.parseInt(line);
Rooms = new Room[numRms];
for(int k = 0; k < numRms; k++){
Rooms[k] = new Room();
returnValue = "";
// First, assign the room's name
line = reader.readLine();
Rooms[k].setName(line);
line = reader.readLine();
// returnValue, after this loop, holds the room desc.
while (!(line.equals("#"))) {
returnValue += line + "\n";
line = reader.readLine();
}
line = "";
// Assign the rest of the room's values
Rooms[k].setDesc(returnValue);
Rooms[k].setNroom(Integer.parseInt(reader.readLine()));
Rooms[k].setEroom(Integer.parseInt(reader.readLine()));
Rooms[k].setSroom(Integer.parseInt(reader.readLine()));
Rooms[k].setWroom(Integer.parseInt(reader.readLine()));
}
reader.close();
}
finally {
if (file != null){
file.close();
}
}
}
public int goN(int cur){
int nroom = Rooms[cur].getNroom();
if(nroom == -1){
System.out.println("You can't go that way!");
return cur;
}
else{
return nroom;
}
}
public int goE(int cur){
int eroom = Rooms[cur].getEroom();
if(eroom == -1){
System.out.println("You can't go that way!");
return cur;
}
else{
return eroom;
}
}
public int goS(int cur){
int sroom = Rooms[cur].getSroom();
if(sroom == -1){
System.out.println("You can't go that way!");
return cur;
}
else{
return sroom;
}
}
public int goW(int cur){
int wroom = Rooms[cur].getWroom();
if(wroom == -1){
System.out.println("You can't go that way!");
return cur;
}
else{
return wroom;
}
}
public void longDisplay (int cur) {
System.out.println(Rooms[cur].getName());
System.out.println(Rooms[cur].getDesc());
System.out.println(getExits(cur));
}
public void shortDisplay (int cur) {
System.out.println(Rooms[cur].getName());
}
public String getExits (int cur) {
String exits = new String("Exits: ");
boolean anyExits = false;
if(Rooms[cur].getNroom() >= 0) {
exits += "\nNorth";
anyExits = true;
}
if (Rooms[cur].getEroom() >= 0) {
exits += "\nEast";
anyExits = true;
}
if (Rooms[cur].getSroom() >= 0) {
exits += "\nSouth";
anyExits = true;
}
if (Rooms[cur].getWroom() >= 0) {
exits += "\nWest";
anyExits = true;
}
if (!anyExits) {
exits += "\nNone";
}
return exits;
}
}