-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveAdventure.java
163 lines (107 loc) · 4.53 KB
/
SaveAdventure.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
/*
* SaveAdventure - This class lets the user save an ongoing Adventure to be loaded later. Five
* files are created; rooms, objects, synonyms, hints, and inventory.
*
* File: SaveAdventure.java
* @author Micah Stairs, William Fiset
*
*/
import acm.program.*;
import java.io.*;
import java.util.*;
public abstract class SaveAdventure{
static void save(ConsoleProgram console){
console.print("Please type a name for your saved game.\n> ");
String name = console.readLine();
try{
/** Save all of the rooms to file **/
PrintWriter writerRooms = new PrintWriter(name + "Rooms.txt", "UTF-8");
// Save current room into the file first
writeRoom(writerRooms, getRoom(Adventure.currentRoomNumber));
// Save all of the other rooms
for(AdvRoom room : Adventure.rooms){
// Save the room as long as it's not the current room
if(room.getNumber() != Adventure.currentRoomNumber)
writeRoom(writerRooms, room);
}
writerRooms.close();
/** Save all of the objects to file **/
PrintWriter writerObjects = new PrintWriter(name + "Objects.txt", "UTF-8");
// Iterate through each room, searching for objects to store
for(AdvRoom room : Adventure.rooms){
// Iterate through this room's list of objects, writing them to file
for(int i = 0; i < room.getObjectCount(); i++)
writeObject(writerObjects, room.getObject(i), room.getNumber());
}
// Iterate through the inventory, storing each object
for(AdvObject obj : Adventure.inventory)
writeObject(writerObjects, obj, Adventure.currentRoomNumber);
writerObjects.close();
/** Save all of the synonyms to file **/
PrintWriter writerSynonyms = new PrintWriter(name + "Synonyms.txt", "UTF-8");
Set<String> keys = Adventure.synonyms.keySet();
// Iterate through each key in the synonyms HashMap, storing the key and its associated value
for(String key : keys)
writerSynonyms.println(key + "=" + Adventure.synonyms.get(key));
writerSynonyms.close();
/** Save all of the inventory objects to file **/
PrintWriter writerInventory = new PrintWriter(name + "Inventory.txt", "UTF-8");
// Iterate through object in the inventory, storing it's name and description
for(AdvObject obj : Adventure.inventory){
writerInventory.println(obj.getName());
writerInventory.println(obj.getDescription());
writerInventory.println();
}
writerInventory.close();
/** Save which rooms have had a hint used in **/
PrintWriter writerHints = new PrintWriter(name + "Hints.txt", "UTF-8");
// Iterate through each room, storing the indexes of those rooms which a hint has been used
for(AdvRoom room : Adventure.rooms)
if(room.hintHasBeenUsed())
writerHints.println(room.getNumber());
writerHints.close();
}
catch(IOException e){}
console.print("The game was saved as '" + name + "' . You may continue playing your adventure.");
}
private static void writeRoom(PrintWriter writerRooms, AdvRoom room){
// Write room index number
writerRooms.println(room.getNumber());
// Write room name
writerRooms.println(room.getName());
// Write room description
for(String line : room.getDescription())
writerRooms.println(line);
// Write separator
writerRooms.println("-----");
// Write motion map, one entry at a time
String line = null;
for(AdvMotionTableEntry entry : room.getAdvMotionTableEntries()){
line = entry.getDirection() + " " + entry.getDestinationRoom();
// Don't write the key if it's null
if(entry.getKeyName() != null)
line += "/" + entry.getKeyName();
writerRooms.println(line);
}
// Write empty line
writerRooms.println();
}
private static void writeObject(PrintWriter writerObjects, AdvObject obj, int roomNumber){
// Write object's name
writerObjects.println(obj.getName());
// Write object's description
writerObjects.println(obj.getDescription());
// Write the object's room number (not necessarily its initial location)
writerObjects.println(roomNumber);
// Write empty line
writerObjects.println();
}
/** Returns a room of the specified index, returning null if it does not exist **/
private static AdvRoom getRoom(int roomNumber){
// Iterate through each room, checking to see if it's index matches the specified room number
for(int i = 0; i < Adventure.rooms.size(); i++)
if(Adventure.rooms.get(i).getNumber() == roomNumber)
return Adventure.rooms.get(i);
return null;
}
}