-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.java
240 lines (216 loc) · 8.19 KB
/
World.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.*;
import java.util.Scanner;
/**
* World describes the world of the game, including the array of rooms contained
* within the world. It also includes variables such as start location, goal
* location, and general world structure. It implements the singleton design
* pattern, as there is only ever one instance of World in any given game.
* @author Alison Mayer
* @date April 4, 2012
* @version 1.1
*/
public class World
{
private static String worldPathName;
private static String objectPathName;
public static Room [][][] worldRooms;
public static ItemBag worldItems;
private static int startLocationX;
private static int startLocationY;
private static int startLocationZ;
private static World world;
//private constructor; World implements a singleton design pattern
private World()
{
worldRooms = new Room[10][10][10];
setWorldRooms();
}
/**
* Returns a single world room.
*
* @param x the x-coordinate of the room
* @param y the y-coordinate of the room
* @param z the z-coordinate of the room
* @return an instance of Room
*/
public static Room getRoom(int x, int y, int z)
{
return worldRooms[x][y][z];
}
/**
* Returns the singleton World to user. If there is no instance of World,
* constructs a new World.
*
* @return world, the instance of World
*/
public static World getWorld()
{
if (world == null)
world = new World();
return world;
}
/**
* Passes the path name of a world file to readWorldFile.
*/
private static void setWorldRooms()
{
worldPathName = "ExampleRooms1.txt";
objectPathName = "ExampleObjects1.txt";
readWorldFile(worldPathName);
readObjectFile(objectPathName);
}
/**
* Reads in the file defining the rooms in the world.
* File should be in the following format:
*
* "TOTALNUMBEROFROOMS<br />
* 1 1 # 1 # 1 # This is a description of room 1. #F#F#F#F#T#F<br />
* 2 1 # 2 # 1 # This is a description of room 2. #F#F#T#F#F#F<br />
* 3 2 # 2 # 1 # This is a description of room 3. #F#F#T#F#F#F<br />
* ...<br />
* 1000 x # y # z # This is a description of room 1000. #NExit#SExit#EExit#WExit#UpExit#DownExit"
*
* The first number is the room ID, and the last three numbers are the room's
* x, y, and z coordinates, respectively. Worlds cannot exceed 1000 rooms
* (i.e. a 10x10x10 three dimension array).
*
* @param pathname, the path to a user-specified world file.
*/
private static void readWorldFile (String pathname)
{
try {
Scanner scanNumberOfRooms = new Scanner(new File (pathname));
Scanner scanRooms = new Scanner(new File (pathname));
int totalRooms;
int currentRoom;
totalRooms = scanNumberOfRooms.nextInt();
scanNumberOfRooms.close();
scanRooms.nextInt();
for (int i = 0; i < totalRooms; i++)
{
currentRoom = scanRooms.nextInt();
if (i+1 == currentRoom)
{
Room r = processLine (scanRooms.nextLine());
if (r != null)
worldRooms[r.getXCoord()][r.getYCoord()][r.getZCoord()] = r;
else
System.out.println("Error reading World file. Check file format.");
}
else
System.out.println("Error reading world file. Check file format.");
}
System.out.println("Loaded world file.");
}
catch (FileNotFoundException ex)
{
System.out.println("File not found.");
}
}
/**
* Reads a line of text from a world file. Properly formatted world
* files contain all data for one room in each line. If the world
* file is formatted properly, processLine will return a Room with
* data read from the input parameter.
*
* @param aLine, a String of data
* @return r, a newly contructed Room. Returns null if the world file is malformed.
*/
private static Room processLine (String aLine)
{
Scanner scan = new Scanner (aLine);
scan.useDelimiter("#");
if (scan.hasNext())
{
String xCoord = scan.next();
String yCoord = scan.next();
String zCoord = scan.next();
int x = Integer.parseInt(xCoord.trim());
int y = Integer.parseInt(yCoord.trim());
int z = Integer.parseInt(zCoord.trim());
String description = scan.next();
boolean [] exits = new boolean[6];
for (int j = 0; j < 6; j++)
{
if (scan.next().compareTo("T") == 0)
exits[j] = true;
}
Room r = new Room(x, y, z, description, exits);
return r;
}
else
return null;
}
/**
* Reads in a file that contains information about the objects contained within the world.
* File should be in the following format:<br />
* <br /><code>
* NUMBEROFITEMS nameOfItem1#item1IDX#item1IDY#item1IDZ#This is a description of Item 1.#item1UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* nameOfItem2#item2IDX#item2IDY#item2IDZ#This is a description of Item 2.#item2UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* nameOfItem3#item3IDX#item3IDY#item3IDZ#This is a description of Item 3.#item3UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* ...<br />
* nameOfItemN#itemNIDX#itemNIDY#itemNIDZ#This is a description of Item N.#itemNUseEffect#someItem1#someItem2#someItem3</code>
*
* @param pathname the path to a user-specified object file.
*/
public static void readObjectFile (String pathname)
{
try {
Scanner scanItems = new Scanner (new File(pathname));
int numCurrentItems;
numCurrentItems = scanItems.nextInt();
worldItems = new ItemBag();
for (int i = 0; i < numCurrentItems; i++)
{
Item item = processObjectFileLine(scanItems.nextLine());
worldItems.addItem(item);
int locatedAtX = item.getIDX();
int locatedAtY = item.getIDY();
int locatedAtZ = item.getIDZ();
worldRooms[locatedAtX][locatedAtY][locatedAtZ].setAItem(item);
}
System.out.println("Loaded object file.");
}
catch (FileNotFoundException ex)
{
System.out.println("File not found.");
}
}
/**
* Reads a line of text from a object file. Properly formatted object
* files contain all data for a item in one line. If the object
* file is formatted properly, processLine will return a Item with
* data read from the input parameter.
*
* @param aLine a String of data
* @return i, a newly contructed Item. Returns null if the world file is malformed.
*/
public static Item processObjectFileLine (String aLine)
{
Scanner scan = new Scanner (aLine);
scan.useDelimiter("#");
if (scan.hasNext())
{
String itemName = scan.next().trim();
String itemIDX = scan.next();
String itemIDY = scan.next();
String itemIDZ = scan.next();
int idX = Integer.parseInt(itemIDX.trim());
int idY = Integer.parseInt(itemIDY.trim());
int idZ = Integer.parseInt(itemIDZ.trim());
String itemDescription = scan.next().trim();
String itemUseEffect = scan.next().trim();
String numberOfItemsUsedWith = scan.next();
int numOfItemsUsedWith = Integer.parseInt(numberOfItemsUsedWith.trim());
String [] itemsUsedWith = new String [numOfItemsUsedWith];
for (int j = 0; j < itemsUsedWith.length; j++)
{
itemsUsedWith[j] = scan.next();
}
Item i = new Item (itemName, idX, idY, idZ, itemDescription, itemUseEffect, itemsUsedWith);
return i;
}
else
return null;
}
}