-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameFile.java
112 lines (97 loc) · 2.99 KB
/
GameFile.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
/**
* Handles loading and saving of game files
* @author Johnson Zhou 1302442 <[email protected]>
*/
import java.util.ArrayList;
class GameFile {
// game file parsed as an arraylist of string
private ArrayList<String> fileLines;
// parsed game file particulars
private ArrayList<String> mapLines;
private String playerLine;
private ArrayList<String> monsterLines;
private ArrayList<String> itemLines;
/**
* @param fileLines as extracted by FileIO
*/
public GameFile(ArrayList<String> fileLines) {
this.fileLines = fileLines;
this.mapLines = new ArrayList<String>();
this.monsterLines = new ArrayList<String>();
this.itemLines = new ArrayList<String>();
}
/**
* Read file lines and parse map, player, monster and item information
* @throws FileIOException if file information is unexpected
*/
public void readFile() throws FileIOException {
boolean playerLoaded = false;
boolean monsterLoaded = false;
boolean itemLoaded = false;
// iterate through lines of loaded game file and segregating
// various map and entity information
for (int line = 0, mapRows = 0; line < this.fileLines.size(); line++) {
// map dimensions in the first row
if (line == 0) {
try {
mapRows = Integer.parseInt(this.fileLines.get(0).split(" ")[1]);
} catch (Exception e) {
throw new FileIOException("Could not parse file map dimensions");
}
}
String lineContents = this.fileLines.get(line);
// extract mapLines
if (line <= mapRows) {
this.mapLines.add(lineContents);
}
// extract player
if (lineContents.startsWith("player")) {
if (monsterLoaded || itemLoaded) {
throw new FileIOException("Player data out of order in file");
}
this.playerLine = lineContents;
playerLoaded = true;
}
// extract monsters
if (lineContents.startsWith("monster")) {
if (!playerLoaded || itemLoaded) {
throw new FileIOException("Monster data out of order in file");
}
this.monsterLines.add(lineContents);
monsterLoaded = true;
}
// extract items
if (lineContents.startsWith("item")) {
if (!playerLoaded && !monsterLoaded) {
throw new FileIOException("Item data out of order in file");
}
this.itemLines.add(lineContents);
itemLoaded = true;
}
}
}
/**
* @return the map lines as an ArrayList of String
*/
public ArrayList<String> getMapLines() {
return this.mapLines;
}
/**
* @return the player line as a String
*/
public String getPlayerLine() {
return this.playerLine;
}
/**
* @return the monster lines as an ArrayList of String
*/
public ArrayList<String> getMonsterLines() {
return this.monsterLines;
}
/**
* @return the item lines as an ArrayList of String
*/
public ArrayList<String> getItemLines() {
return this.itemLines;
}
}