-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMap.java
More file actions
265 lines (220 loc) · 8.8 KB
/
Map.java
File metadata and controls
265 lines (220 loc) · 8.8 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
/**
* Map.java
* This class uses a graph to represent a map that the player can move through. Contains the player's current situation,
* the last situation they visited, and the boss fight. Items and fights are respawned when the player leaves a situation.
* The map is marked as cleared if the boss is defeated.
* @author Emma Shumadine, Lily Orth-Smith (primary), Rachel Zhang
*/
import java.util.*;
public class Map extends AdjListsGraph<Situation> {
private Situation currentSituation;
private Situation lastSituation = null;
private Situation beginning;
private Situation bossRoom;
/**
* Creates a map with an initial situation and a boss room
* @param initialSituation the starting situation
* @param boss the boss room
*/
public Map(Situation initialSituation, Situation boss) {
super();
this.addVertex(initialSituation);
this.addVertex(boss);
currentSituation = initialSituation;
beginning = initialSituation;
bossRoom = boss;
}
/**
* Returns true if the boss has been defeated, and false otherwise
* @return true if the boss has been defeated
*/
public boolean isCleared() {
for(GameCharacter enemy: bossRoom.getFight().getEnemies()) {
if(enemy.getCurrentHP() != 0) {
return false;
}
}
return true;
}
/**
* Changes lastSituation to currentSituation and currentSitatuion to newSituation, throws IllegalMoveException if newSituation is not an adjacent situation.
* Resets (respawns all enemies and heal items in) the situation that the player is leaving
* @param newSituation situation to set currentSituation to
*/
public void changeSituation (Situation newSituation) throws IllegalMoveException {
if (isAdjacent(newSituation)) {
lastSituation = currentSituation;
currentSituation = newSituation;
if(!lastSituation.equals(bossRoom)) {
lastSituation.reset();
}
} else {
throw new IllegalMoveException();
}
}
/**
* Sets the current situation to the beginning
*/
public void goToBeginning() {
currentSituation = beginning;
}
/** Gets the situation in the adjacent situations at index adjacencyIndex, and changes to that
* @param adjacencyIndex index in the array of ADJACENT situations (NOT the index of the vertex in the graph)
*/
public void changeSituation(int adjacencyIndex) throws IllegalMoveException {
Situation[] adjacentSituations = getAdjArray();
changeSituation(adjacentSituations[adjacencyIndex]);
}
/** Returns true if the given situation is adjacent to current situation
* @param s situation to check
* @return whether the situation is adjacent to currentSituation
*/
public boolean isAdjacent(Situation s) {
LinkedList<Situation> adj = getAdjacent();
return adj.contains(s);
}
/** Gets a LinkedList of the adjacent situations
* @return adjacent situations
*/
public LinkedList<Situation> getAdjacent() {
return getPredecessors(currentSituation);
}
/** Gets an array of the adjacent situations
* @return adjacent situations
*/
public Situation[] getAdjArray() {
LinkedList<Situation> adjacentSituations = getAdjacent();
Situation[] adjArray = new Situation[adjacentSituations.size()];
adjArray = adjacentSituations.toArray(adjArray);
return adjArray;
}
/** Gets an array of the names of the adjacent situations
* @return names of the adjacent situations
*/
public String[] getAdjNames() {
LinkedList<Situation> adjacent = getAdjacent();
String[] names = new String[adjacent.size()];
//if last situation is null, you can't go back, so won't put Go Back as an option
if (lastSituation == null) {
for (int i = 0; i < names.length; i++) {
String name = adjacent.get(i).getName();
names[i] = adjacent.get(i).getName();
}
//if it isn't null, replace lastSituation's name with "Go back"
} else {
for (int i = 0; i < names.length; i++) {
String name = adjacent.get(i).getName();
if (name.equals(lastSituation.getName())) {
names[i] = "Go back";
} else {
names[i] = adjacent.get(i).getName();
}
}
}
return names;
}
/** Gets an array of the descriptions of the adjacent situations
* @return descriptions of the adjacent situations
*/
public String[] getAdjDescriptions() {
LinkedList<Situation> adjacent = getAdjacent();
String[] descriptions = new String[adjacent.size()];
for (int i = 0; i < descriptions.length; i++) {
String description = adjacent.get(i).getDescription();
descriptions[i] = description;
}
return descriptions;
}
/**
* Returns the situation containing the boss fight
* @return the situation containing the boss fight
*/
public Situation getBossRoom() {
return bossRoom;
}
/** Gets the current situation
* @return the current situation
*/
public Situation getCurrentSituation() {
return currentSituation;
}
//not sure how to deal w case where lastSituation is null
/** Gets the last situation
* @return the last situation
*/
public Situation getLastSituation() {
return lastSituation;
}
/** Provides a String representation of the Map Object
* @return a String representation of the Map
*/
public String toString() {
String mapString = "Current Situation: " + currentSituation;
if (lastSituation != null) {
mapString += "\nLast Situation: " + lastSituation;
}
mapString += "\n" + super.toString();
return mapString;
}
public static void main (String[] args) {
Situation s1 = new Situation("s1", "the first situation");
Situation s2 = new Situation("s2", "the 2 situation");
Situation s3 = new Situation("s3", "the 3 situation");
Situation s4 = new Situation("s4", "the 4 situation");
Map myMap = new Map(s1, s4);
System.out.println(myMap);
myMap.addVertex(s2);
myMap.addVertex(s3);
System.out.println();
myMap.addEdge(s1, s2);
myMap.addEdge(s2, s3);
myMap.addEdge(s1, s4);
System.out.println(myMap);
System.out.println("Testing isAdjacent())");
System.out.println("s2 (true): " + myMap.isAdjacent(s2));
System.out.println("s3 (false): " + myMap.isAdjacent(s3));
System.out.println("s4 (true): " + myMap.isAdjacent(s4));
System.out.println("Testing getAdjacent()");
System.out.println(myMap.getAdjacent());
System.out.println("Testing getAdjArray())");
System.out.println(myMap.getAdjArray());
System.out.println("Testing getAdjNames())");
String[] adjNames = myMap.getAdjNames();
for (int i=0; i<adjNames.length; i++) {
System.out.println(adjNames[i]);
}
System.out.println("Testing getAdjDescriptions())");
String[] adjDescriptions = myMap.getAdjDescriptions();
for (int i=0; i<adjDescriptions.length; i++) {
System.out.println(adjDescriptions[i]);
}
System.out.println("Testing getCurrentSituation()");
System.out.println(myMap.getCurrentSituation());
System.out.println("Testing getLastSituation()");
System.out.println(myMap.getLastSituation());
System.out.println("Trying to change situation to s3 (should throw error) ");
try {
myMap.changeSituation(s3);
} catch (IllegalMoveException e) {
System.out.println("Couldn't move to s3 because of " + e);
}
System.out.println("Move to s2: ");
myMap.changeSituation(s2);
System.out.println("Current Situation (s2): " + myMap.getCurrentSituation());
System.out.println("Last Situation (s1): " + myMap.getLastSituation());
System.out.println("Names of adjacent situations (should have a go back: ");
String[] adjNames2 = myMap.getAdjNames();
for (int i=0; i<adjNames2.length; i++) {
System.out.println(adjNames2[i]);
}
System.out.println("Names of adjacent descriptions (should NOT have a go back): ");
String[] adjDescriptions2 = myMap.getAdjDescriptions();
for (int i=0; i<adjDescriptions2.length; i++) {
System.out.println(adjDescriptions2[i]);
}
System.out.println("Testing changeSituation using index (index = 1, current should go to s3) ");
myMap.changeSituation(1);
System.out.println("Current situation: " + myMap.getCurrentSituation());
System.out.println("Last situation: " + myMap.getLastSituation());
}
}