-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exe.java
224 lines (200 loc) · 6.86 KB
/
Exe.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
import java.io.*;
import java.util.Scanner;
public class Exe{
static int x = 1;
static int y = 1;
static int z = 1;
static World world;
static Player player;
static Scanner scanno = new Scanner(System.in);
static final String EOL = System.getProperty("line.separator");
public static void main (String[] args)
{
world = world.getWorld();
player = player.getPlayer();
System.out.println(
"Hello there. Welcome to Isaac and Alison's " + EOL +
"Glorious Text Adventure For Great Victory (TM & C)!" + EOL +
"To get started, please type your character's name.");
System.out.print(EOL + ">"); player.setName(readInput());
System.out.println(EOL + "Do you need some help getting started? (y/n)");
System.out.print(EOL + ">"); if(readInput().equals("y"))
{
System.out.println(EOL +
"Why hello there " + player.getName() + ". Why don't you " + EOL +
"check out your surroundings by typing 'look'?");
System.out.print(EOL + ">"); Verbs.parse(readInput());
System.out.println(EOL +
"Wow! What a cool place. Why don't you examine the" + EOL +
"box? Type 'examine box' to take a closer look at the box.");
System.out.print(EOL + ">"); Verbs.parse(readInput());
}
System.out.println(EOL +
"Alright, from here on you're on your own." + EOL +
"You can type 'help' or '?' to see a list of commands.");
while(true){
System.out.println();
System.out.print(">"); Verbs.parse(readInput());
}
}
/**
* @return the player's current [x, y, z] coordinates as an int array.
*/
public static int[] getCoords(){
return new int[] {x, y, z};
}
/**
* @return the player's current x coordinate.
*/
public static int getX(){
return x;
}
/**
* @return the player's current y coordinate.
*/
public static int getY(){
return y;
}
/**
* @return the player's current z coordinate.
*/
public static int getZ(){
return z;
}
/**
* Sets the player's current location manually.
*
* @param coords the desired [x, y, z] coordinates as an int array.
*/
public static void setCoords(int[] coords){
x = coords[0];
y = coords[1];
z = coords[2];
}
/**
* @param x2 the desired x coordinate.
*/
public static void setX(int x2){
Exe.x = x2;
}
/**
* @param y2 the desired y coordinate.
*/
public static void setY(int y2){
Exe.y = y2;
}
/**
* @param z2 the desired z coordinate.
*/
public static void setZ(int z2){
Exe.z = z2;
}
/**
* Reads the next line from System.in using a scanner.
*/
public static String readInput()
{
return scanno.nextLine();
}
/**
* Reads in a file that contains information about the previous game session.
* File should be in the following format:<br />
* <br /><code>
* NAME#SCORE#CURRENTX#CURRENTY#CURRENTZ#NUMBERCURRENTITEMS#<br />
* nameOfItem1#This is a description of Item 1.#item1UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* nameOfItem2#This is a description of Item 2.#item2UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* nameOfItem3#This is a description of Item 3.#item3UseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3<br />
* ...<br />
* nameOfItemN#This is a description of Item N.#itemNUseEffect#NUMBEROFITEMSUSEDWITH#someItem1#someItem2#someItem3</code>
*
* @param pathname the path to a user-specified save file.
*/
public static void readSaveFile (String pathname)
{
try {
Scanner scanner = new Scanner(new File (pathname));
scanner.useDelimiter("#");
String name;
String currentScore;
String currentLocationX;
String currentLocationY;
String currentLocationZ;
String numCurrentItems;
ItemBag currentItems = new ItemBag();
name = scanner.next().trim();
currentScore = scanner.next().trim();
currentLocationX = scanner.next().trim();
currentLocationY = scanner.next().trim();
currentLocationZ = scanner.next().trim();
numCurrentItems = scanner.next().trim();
int score = Integer.parseInt(currentScore);
int locX = Integer.parseInt(currentLocationX);
int locY = Integer.parseInt(currentLocationY);
int locZ = Integer.parseInt(currentLocationZ);
int numItems = Integer.parseInt(numCurrentItems);
for (int i = 0; i < numItems; i++)
{
Item item = processLine(scanner.nextLine());
currentItems.addItem(item);
}
System.out.println("Loaded save file.");
scanner.close();
}
catch (FileNotFoundException ex)
{
System.out.println("File not found.");
}
}
/**
* Reads a line of text from a save file. Properly formatted save
* files contain all data for a item in one line. If the save
* file is formatted properly, processLine will return an Item with
* data read from the input parameter.
*
* @param aLine a String of data
* @return a newly contructed Item. Returns null if the world file is malformed.
*/
public static Item processLine (String aLine)
{
Scanner scan = new Scanner (aLine);
scan.useDelimiter("#");
if (scan.hasNext())
{
String itemName = scan.next().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, itemDescription, itemUseEffect, itemsUsedWith);
scan.close();
return i;
}
else{
scan.close();
return null;
}
}
/**
* Saves game data in proper save format. File is saved as "adventure_save.txt"
*/
public static void saveFile ()
{
try
{
FileWriter newSave = new FileWriter("adventure_save.txt");
BufferedWriter newSaveOut = new BufferedWriter(newSave);
newSaveOut.write(player.getName() + "#" + player.getScore() + "#" + getX() + "#" + getY() + "#" + getZ() + "#" + player.getInv().getNumberOfItems());
newSaveOut.close();
System.out.println("Game saved.");
}
catch (IOException io)
{
System.out.println("Error saving game!");
}
}
}