-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.java
338 lines (299 loc) · 10.6 KB
/
GameState.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @author Team Red
* GameState is a singleton class that is essentially our player.
* It keeps track of items in inventory, adventureresCurrentRoom, which dungeon is being used, and health and score.
*
* It also has the incredibly important task of ensuring the save file can Load (@throw IllegalSaveFormatException),
* contains the correct save file name, the format,
*
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class GameState {
public static class IllegalSaveFormatException extends Exception {
public IllegalSaveFormatException(String e) {
super(e);
}
}
static String DEFAULT_SAVE_FILE = "bork_save";
static String SAVE_FILE_EXTENSION = ".sav";
static String SAVE_FILE_VERSION = "Group Bork v1.0 save data";
static String ADVENTURER_MARKER = "Adventurer:";
static String CURRENT_ROOM_LEADER = "Current room: ";
static String INVENTORY_LEADER = "Inventory: ";
private static GameState theInstance;
private Dungeon dungeon;
private ArrayList<Item> inventory;
private Room adventurersCurrentRoom;
private int adventurersScore;
private int adventurersHealth;
private boolean adventurerIsDead;
private String currentTime;
static synchronized GameState instance() {
if (theInstance == null) {
theInstance = new GameState();
}
return theInstance;
}
private GameState() {
inventory = new ArrayList<Item>();
adventurersScore = 0;
adventurersHealth = 50;
adventurerIsDead = false;
currentTime = Daytime.getTime();
}
/** Restores the state of a Bork game using the parameter .sav File name. Retrieves Rooms' beenHere statuses, Rooms'
* inventories, player's current Room, and player's inventory
*
* @param filename Name of the .sav File to restore the game from
* @throws FileNotFoundException If the parameter File name is of a non-existent File
* @throws IllegalSaveFormatException If the .sav File is formatted incorrectly and cannot be parsed
* @throws Dungeon.IllegalDungeonFormatException If the corresponding .bork File is formatted incorrectly and cannot
* be parsed
*/
void restore(String filename) throws FileNotFoundException,
IllegalSaveFormatException, Dungeon.IllegalDungeonFormatException {
Scanner s = new Scanner(new FileReader(filename));
if (!s.nextLine().equals(SAVE_FILE_VERSION)) {
throw new IllegalSaveFormatException("Save file not compatible.");
}
String dungeonFileLine = s.nextLine();
if (!dungeonFileLine.startsWith(Dungeon.FILENAME_LEADER)) {
throw new IllegalSaveFormatException("No '" +
Dungeon.FILENAME_LEADER +
"' after version indicator.");
}
dungeon = new Dungeon(dungeonFileLine.substring(
Dungeon.FILENAME_LEADER.length()), false);
dungeon.restoreState(s);
s.nextLine(); // Throw away "Adventurer:".
String currentRoomLine = s.nextLine();
adventurersCurrentRoom = dungeon.getRoom(
currentRoomLine.substring(CURRENT_ROOM_LEADER.length()));
if (s.hasNext()) {
String inventoryList = s.nextLine().substring(
INVENTORY_LEADER.length());
String[] inventoryItems = inventoryList.split(",");
for (String itemName : inventoryItems) {
try {
addToInventory(dungeon.getItem(itemName));
} catch (Item.NoItemException e) {
throw new IllegalSaveFormatException("No such item '" +
itemName + "'");
}
}
}
}
/** Calls the main store(saveName : String) method with a default save File name
*
* @throws IOException In the case of DEFAULT_SAVE_FILE being a non-existent File
*/
void store() throws IOException {
store(DEFAULT_SAVE_FILE);
}
/** Saves necessary information to a .sav File
*
* @param String saveName Name of the File to save to
* @throws IOException In the case that saveName is the name of a non-existent File
*/
void store(String saveName) throws IOException {
String filename = saveName + SAVE_FILE_EXTENSION;
PrintWriter w = new PrintWriter(new FileWriter(filename));
w.println(SAVE_FILE_VERSION);
dungeon.storeState(w);
w.println(ADVENTURER_MARKER);
w.println(CURRENT_ROOM_LEADER + adventurersCurrentRoom.getTitle());
if (inventory.size() > 0) {
w.print(INVENTORY_LEADER);
for (int i=0; i<inventory.size()-1; i++) {
w.print(inventory.get(i).getPrimaryName() + ",");
}
w.println(inventory.get(inventory.size()-1).getPrimaryName());
}
w.close();
}
/** Sets the GameState instance's Dungeon to be the parameter Dungeon and sets the Adventurer's current room as the specified Dungeon's entry Room
*
* @param dungeon Dungeon to set as the GameState's current Dungeon
*/
void initialize(Dungeon dungeon) {
this.dungeon = dungeon;
adventurersCurrentRoom = dungeon.getEntry();
}
/** Returns the names of the Items currently in the player's inventory
*
* @return An ArrayList of the names of the Items in inventory
*/
ArrayList<String> getInventoryNames() {
ArrayList<String> names = new ArrayList<String>();
for (Item item : inventory) {
names.add(item.getPrimaryName());
}
return names;
}
/** Adds the parameter Item to the player's inventory
*
* @param item Item to add to inventory
*/
void addToInventory(Item item) /* throws TooHeavyException */ {
inventory.add(item);
}
/** Removes the parameter Item from the player's inventory
*
* @param item Item to remove from inventory
*/
void removeFromInventory(Item item) {
inventory.remove(item);
}
/** Returns the Item in the vicinity that has the parameter name as its primaryName
*
* @return Item The Item going by the parameter name if it is in the player's vicinity
* @param String name Name of the Item to search for
* @throws Item.NoItemException If there is no Item with the parameter name in the player's inventory or current Room
*/
Item getItemInVicinityNamed(String name) throws Item.NoItemException {
// First, check inventory.
for (Item item : inventory) {
if (item.goesBy(name)) {
return item;
}
}
// Next, check room contents.
for (Item item : adventurersCurrentRoom.getContents()) {
if (item.goesBy(name)) {
return item;
}
}
throw new Item.NoItemException();
}
/**
*
* @return Item The Item going by the parameter name if it is in the player's inventory
* @param String name Name of the Item to search for
* @throws Item.NoItem.Exception If there is no Item going by the parameter name in the player's inventory
*/
Item getItemFromInventoryNamed(String name) throws Item.NoItemException {
for (Item item : inventory) {
if (item.goesBy(name)) {
return item;
}
}
throw new Item.NoItemException();
}
/** Returns the NPC in the vicinity that has the parameter name as its name
*
* @param name Name of the NPC to find
* @return The NPC with the parameter name as its name, if one is in the adventurersCurrentRoom
* @throws NPC.NoNPCException If there is no NPC going by the parameter name in the adventurersCurrentRoom
*/
NPC getNPCInVicinityNamed(String name) throws NPC.NoNPCException
{
if(dungeon.getNPC(name).getCurrentRoom() == adventurersCurrentRoom)
{
return dungeon.getNPC(name);
}
throw new NPC.NoNPCException();
}
/** Returns the current Room of the player
*
* @return adventurersCurrentRoom
*/
Room getAdventurersCurrentRoom() {
return adventurersCurrentRoom;
}
/** Sets the player's current Room to the parameter Room
*
* @param room Room to set adventurersCurrentRoom as
*/
void setAdventurersCurrentRoom(Room room) {
adventurersCurrentRoom = room;
}
/** Returns the Dungeon currently being used by the GameState instance
*
* @return The Dungeon currently being used
*/
Dungeon getDungeon() {
return dungeon;
}
/** Adjusts the player's health by the parameter amount
*
* @param change Amount to change the player's health by (negative to increase health)
*/
void changeHealth(int change)
{
adventurersHealth += change;
if(adventurersHealth <= 0)
{
adventurerIsDead = true;
}
}
/** Adjusts the player's score by the parameter amount
*
* @param change Amount to change the player's score by (negative to reduce score)
*/
void changeScore(int change)
{
if(change == Integer.MAX_VALUE)
{
adventurersScore = Integer.MAX_VALUE;
}
else
adventurersScore += change;
}
/** Returns the player's current health
*
* @return adventurersHealth
*/
int getAdventurersHealth()
{
return adventurersHealth;
}
/** Returns whether or not the player has died
*
* @return adventurerIsDead
*/
boolean getAdventurerIsDead()
{
return adventurerIsDead;
}
/** Returns the player's current score
*
* @return adventurersScore
*/
int getAdventurersScore()
{
return adventurersScore;
}
/** Completely removes the parameter Item from the dungeon
*
* @param item Item to remove from the Dungeon
*/
void obliterateItem(Item item)
{
if(!inventory.remove(item))
{
GameState.instance().getDungeon().obliterateItem(item);
}
}
/** Completely removes the parameter itemToDestory from the Dungeon and puts the parameter itemToAdd in its place
*
* @param itemToDestroy Item to remove from the Dungeon
* @param itemToAdd The Item to replace itemToDestroy with
*/
void transformItem(Item itemToDestroy, Item itemToAdd)
{
if(!inventory.remove(itemToDestroy))
{
GameState.instance().getDungeon().transformItem(itemToDestroy, itemToAdd);
}
else
{
inventory.add(itemToAdd);
}
}
}