Repository navigation
Expand file tree
/
Copy pathFileHandler.java
More file actions
332 lines (298 loc) · 13.4 KB
/
Copy pathFileHandler.java
File metadata and controls
332 lines (298 loc) · 13.4 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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class handles reading and writing information from level files.
* @author Alex Vesely
*/
public class FileHandler {
private static final int DIAMOND_COUNT_INDEX = 0;
private static final int DIAMONDS_REQUIRED_INDEX = 1;
private static final int AMOEBA_GROWTH_RATE_INDEX = 0;
private static final int AMOEBA_SIZE_LIMIT_INDEX = 1;
/**
* Reads a grid of elements from a level file.
* @param fileName the name of the level file containing the grid data.
* @return a 2D array of strings representing the grid.
*/
public static String[][] readElementGridFromLevelFile(String fileName) {
File readFile = new File(fileName);
try {
Scanner in = new Scanner(readFile);
String[] splitGridDimensions = in.nextLine().split(" ");
int width = Integer.parseInt(splitGridDimensions[0]);
int height = Integer.parseInt(splitGridDimensions[1]);
in.nextLine(); //Skip line containing seconds left
in.nextLine(); //Skip line containing diamond information
in.nextLine(); //Skip line containing amoeba information
in.nextLine(); //skip line containing key inventory information
String[][] initialGrid = new String[height][width];
int i = 0;
while (in.hasNextLine()) {
String[] elements = in.nextLine().split(" ");
for (int j = 0; j < elements.length; j++) {
initialGrid[i][j] = elements[j];
}
i++;
}
in.close();
return initialGrid;
} catch (FileNotFoundException exception) {
System.out.println("Error in finding file");
}
return null;
}
/**
* Saves the game state to a save file associated to current player playing.
* @param gameController the game controller.
* @param currentProfile the profile of the current player.
* @param secondsRemaining the remaining seconds of the level.
* @param keyInventory the keys currently held by the player.
*/
public static void writeFile(GameController gameController, PlayerProfile currentProfile, int secondsRemaining,
ArrayList<KeyColour> keyInventory) {
Element[][] currentGrid = gameController.getGridManager().getElementGrid();
int diamondCount = gameController.getGridManager().getPlayer().getDiamondCount();
int currentLevel = currentProfile.getMaxLevelReached();
int id = currentProfile.getPlayerId();
int amoebaGrowthRate = readAmoebaGrowthRateFromLevelFile("txt/Level" + currentLevel + ".txt");
int amoebaSizeLimit = readAmoebaSizeLimitFromLevelFile("txt/Level" + currentLevel + ".txt");
String fileName = "Save" + id + ".txt";
try {
String outputFile = "txt/" + fileName;
PrintWriter out = new PrintWriter(outputFile);
out.println(currentGrid[0].length + " " + currentGrid.length);
out.println(secondsRemaining);
out.println(diamondCount + " " + gameController.getDiamondsRequired());
out.println(amoebaGrowthRate + " " + amoebaSizeLimit);
out.println(createKeyInventoryString(keyInventory));
for (int i = 0; i < currentGrid.length; i++) {
for (int j = 0; j < currentGrid[i].length; j++) {
switch (gameController.getGridManager().getElement(i, j).getName()) {
case "Player" -> out.print("*");
case "Path" -> out.print("P");
case "Dirt" -> out.print("DT");
case "Exit" -> out.print("E");
case "NormalWall" -> out.print("NW");
case "TitaniumWall" -> out.print("TW");
case "MagicWall" -> out.print("MW");
case "Boulder" -> out.print("B");
case "Diamond" -> out.print("DD");
case "Frog" -> out.print("F");
case "Amoeba" -> out.print("A");
case "REDKey" -> out.print("RK");
case "REDLockedDoor" -> out.print("RLD");
case "GREENKey" -> out.print("GK");
case "GREENLockedDoor" -> out.print("GLD");
case "YELLOWKey" -> out.print("YK");
case "YELLOWLockedDoor" -> out.print("YLD");
case "BLUEKey" -> out.print("BK");
case "BLUELockedDoor" -> out.print("BLD");
case "RAINBOWKey" -> out.print("RBK");
// If there is an explosion, load a path in its place when the save is loaded
case "Explosion" -> out.print("P");
case "FireflyLeft" -> out.print("FFL");
case "FireflyRight" -> out.print("FFR");
case "ButterflyLeft" -> out.print("BFL");
case "ButterflyRight" -> out.print("BFR");
default -> System.out.println("Error in reading Symbol");
}
if (!(j == currentGrid[i].length - 1)) {
out.print(" ");
}
}
if (i != currentGrid.length - 1) {
out.println();
}
}
out.close();
} catch (IOException e) {
System.out.println("Cannot write file");
}
}
/**
* Reads the remaining time in seconds from a level file.
* @param fileName the name of the file containing the level data.
* @return the remaining time in seconds.
* @throws RuntimeException if the file is not found.
*/
public static int readSecondsFromLevelFile(String fileName) {
File readFile = new File(fileName);
try {
Scanner in = new Scanner(readFile);
in.nextLine(); // Skip first line about grid dimensions
return Integer.parseInt(in.nextLine()); // Return seconds
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + fileName, e);
}
}
/**
* Reads the number of diamonds collected so far from a level file.
* If its from the base level file it is always 0
* If its from a level save then it's the number of diamonds collected so far
* @param fileName the level file.
* @return the number of diamonds collected so far.
*/
public static int readDiamondsCollectedFromLevelFile(String fileName) {
return readDiamondsInformationFromLevelFile(fileName, DIAMOND_COUNT_INDEX);
}
/**
* Reads the number of diamonds required to complete a specific level from the level file.
* @param fileName the level file.
* @return the number of diamonds required to complete the level.
*/
public static int readRequiredDiamondsFromLevelFile(String fileName) {
return readDiamondsInformationFromLevelFile(fileName, DIAMONDS_REQUIRED_INDEX);
}
/**
* Reads information about diamonds from a level file.
* Can read the diamonds collected or a level's required diamonds from here.
* @param fileName the level file.
* @param index the index specifying the type of diamond information (0 for collected, 1 for required).
* @return the requested diamond information.
*/
private static int readDiamondsInformationFromLevelFile(String fileName, int index) {
File readFile = new File(fileName);
try {
Scanner in = new Scanner(readFile);
in.nextLine(); // Skip first line
in.nextLine(); // Skip second line about seconds left
String[] splitDiamondInformation = in.nextLine().split(" ");
return Integer.parseInt(splitDiamondInformation[index]);
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + fileName, e);
}
}
/**
* Reads the amoeba growth rate from a level file.
* @param fileName the level file.
* @return the level's amoeba growth rate.
*/
public static int readAmoebaGrowthRateFromLevelFile(String fileName) {
return readAmoebaInformationFromLevelFile(fileName, AMOEBA_GROWTH_RATE_INDEX);
}
/**
* Reads the amoeba size limit from the level file.
* @param fileName the level file.
* @return the level's amoeba size limit.
*/
public static int readAmoebaSizeLimitFromLevelFile(String fileName) {
return readAmoebaInformationFromLevelFile(fileName, AMOEBA_SIZE_LIMIT_INDEX);
}
/**
* Reads amoeba-related information from the level file.
* @param fileName the level file.
* @param index the index specifying the type of amoeba information (0 for growthRate, 1 for sizeLimit).
* @return the requested amoeba information as an integer.
*/
private static int readAmoebaInformationFromLevelFile(String fileName, int index) {
File readFile = new File(fileName);
try (Scanner in = new Scanner(readFile)) {
in.nextLine(); // Skip first line about grid dimensions
in.nextLine(); // Skip second line about seconds left
in.nextLine(); // Skip third line about diamonds information
String[] splitAmoebaInfo = in.nextLine().split(" ");
return Integer.parseInt(splitAmoebaInfo[index]);
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + fileName, e);
} catch (IndexOutOfBoundsException | NumberFormatException e) {
throw new RuntimeException("Error parsing amoeba information from file: " + fileName, e);
}
}
/**
* Reads the key inventory from the level file.
* @param fileName the level file.
* @return the key inventory.
*/
public static ArrayList<KeyColour> readKeyInventoryFromLevelFile(String fileName) {
File readFile = new File(fileName);
ArrayList<KeyColour> keyInventory = new ArrayList<>();
try {
Scanner in = new Scanner(readFile);
in.nextLine(); // Skip first line
in.nextLine(); // Skip second line about seconds left
in.nextLine(); // Skip third line about diamond information
in.nextLine(); // Skip fourth line about amoeba information
String keyInfo = in.nextLine();
if (!keyInfo.isBlank()) {
String[] splitKeyInformation = keyInfo.split(" ");
for (String key : splitKeyInformation) {
switch (key) {
case "RK":
keyInventory.add(KeyColour.RED);
break;
case "BK":
keyInventory.add(KeyColour.BLUE);
break;
case "YK":
keyInventory.add(KeyColour.YELLOW);
break;
case "GK":
keyInventory.add(KeyColour.GREEN);
break;
default:
System.out.println("Error: can't read key " + key);
break;
}
}
}
return keyInventory;
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + fileName, e);
}
}
/**
* Creates a string representation of the key inventory.
* @param keyInventory the key inventory.
* @return a space-separated string of key codes.
*/
public static String createKeyInventoryString(ArrayList<KeyColour> keyInventory) {
StringBuilder keyInventoryString = new StringBuilder();
for (int i = 0; i < keyInventory.size(); i++) {
KeyColour key = keyInventory.get(i);
switch (key) {
case RED:
keyInventoryString.append("RK");
break;
case BLUE:
keyInventoryString.append("BK");
break;
case YELLOW:
keyInventoryString.append("YK");
break;
case GREEN:
keyInventoryString.append("GK");
break;
default:
System.out.println("Error in reading symbol: " + key);
}
if (i < keyInventory.size() - 1) {
keyInventoryString.append(" ");
}
}
return keyInventoryString.toString();
}
/**
* Deletes a player's saveFile.
* @param currentProfile the player whose saveFile is to be deleted.
*/
public static void deleteSaveFile(PlayerProfile currentProfile) {
int idToDelete = currentProfile.getPlayerId();
String filePath = "txt/Save" + idToDelete + ".txt";
Path path = Paths.get(filePath);
if (!Files.exists(path)) {
return;
}
try {
Files.delete(path);
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}