-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestGridMap.java
240 lines (215 loc) · 9.62 KB
/
QuestGridMap.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
// public class for the QuestGridMap
// extends the GridMap class
public class QuestGridMap extends GridMap {
// private data members
private CellHeroLocation[] hero_locations = new CellHeroLocation[3];
// QuestGridMap constructor
public QuestGridMap(int dim) {
super(dim);
hero_locations[0] = new CellHeroLocation(7,0);
hero_locations[1] = new CellHeroLocation(7,3);
hero_locations[2] = new CellHeroLocation(7,6);
}
public class CellHeroLocation {
int hero_r;
int hero_c;
public CellHeroLocation(int r, int c) {
this.hero_r = r;
this.hero_c = c;
}
public String toString() {
return "(" + Integer.toString(hero_r) + "," + Integer.toString(hero_c) + ")";
}
}
// methods to handle player movement
public int moveUp(HeroTeam team, int heroIndex) {
if (hero_locations[heroIndex].hero_r - 1 < 0 || hero_locations[heroIndex].hero_r - 1 >= super.rowCount() || hero_locations[heroIndex].hero_c < 0 || hero_locations[heroIndex].hero_c >= super.colCount()) {
return -1;
}
if (super.check(hero_locations[heroIndex].hero_r-1 , hero_locations[heroIndex].hero_c)) {
if (scans(hero_locations[heroIndex].hero_r-1, hero_locations[heroIndex].hero_c)) {
GridMapCell cell = super.getCellAt(hero_locations[heroIndex].hero_r - 1, hero_locations[heroIndex].hero_c);
leave(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c);
this.getCellAt(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c).removeHero();
hero_locations[heroIndex].hero_r -= 1;
((Terrain) cell.getEntity()).arrive();
cell.placeHero(team.get(heroIndex));
team.get(heroIndex).setLocation(cell.getRow(), cell.getColumn());
if (cell.getEntity() instanceof Nexus) {
return 1;
} else {
return 2;
}
} else {
return -2;}
} else {
return 0;
}
}
public int moveDown(HeroTeam team, int heroIndex) {
if (hero_locations[heroIndex].hero_r + 1 < 0 || hero_locations[heroIndex].hero_r + 1 >= super.rowCount() || hero_locations[heroIndex].hero_c < 0 || hero_locations[heroIndex].hero_c >= super.colCount()) {
return -1;
}
if (super.check(hero_locations[heroIndex].hero_r+1 , hero_locations[heroIndex].hero_c)) {
if (scans(hero_locations[heroIndex].hero_r+1, hero_locations[heroIndex].hero_c)){
GridMapCell cell = super.getCellAt(hero_locations[heroIndex].hero_r + 1, hero_locations[heroIndex].hero_c);
leave(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c);
this.getCellAt(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c).removeHero();
hero_locations[heroIndex].hero_r += 1;
((Terrain) cell.getEntity()).arrive();
cell.placeHero(team.get(heroIndex));
team.get(heroIndex).setLocation(cell.getRow(), cell.getColumn());
if (cell.getEntity() instanceof Nexus) {
return 1;
} else {
return 2;
}
} else {return -2;}
} else {
return 0;
}
}
public int moveLeft(HeroTeam team, int heroIndex) {
if (hero_locations[heroIndex].hero_r < 0 || hero_locations[heroIndex].hero_r >= super.rowCount() || hero_locations[heroIndex].hero_c - 1 < 0 || hero_locations[heroIndex].hero_c - 1 >= super.colCount()) {
return -1;
}
if (super.check(hero_locations[heroIndex].hero_r , hero_locations[heroIndex].hero_c - 1)) {
if (scans(hero_locations[heroIndex].hero_r, hero_locations[heroIndex].hero_c - 1)) {
GridMapCell cell = super.getCellAt(hero_locations[heroIndex].hero_r, hero_locations[heroIndex].hero_c - 1);
leave(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c);
this.getCellAt(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c).removeHero();
hero_locations[heroIndex].hero_c -= 1;
cell.placeHero(team.get(heroIndex));
((Terrain) cell.getEntity()).arrive();
team.get(heroIndex).setLocation(cell.getRow(), cell.getColumn());
if (cell.getEntity() instanceof Nexus) {
return 1;
} else {
return 2;
}
} else {return -2;}
} else {
return 0;
}
}
public int moveRight(HeroTeam team, int heroIndex) {
if (hero_locations[heroIndex].hero_r < 0 || hero_locations[heroIndex].hero_r >= super.rowCount() || hero_locations[heroIndex].hero_c + 1 < 0 || hero_locations[heroIndex].hero_c + 1 >= super.colCount()) {
return -1;
}
if (super.check(hero_locations[heroIndex].hero_r , hero_locations[heroIndex].hero_c + 1)) {
if (scans(hero_locations[heroIndex].hero_r, hero_locations[heroIndex].hero_c+1)) {
GridMapCell cell = super.getCellAt(hero_locations[heroIndex].hero_r, hero_locations[heroIndex].hero_c + 1);
leave(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c);
this.getCellAt(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c).removeHero();
hero_locations[heroIndex].hero_c += 1;
((Terrain) cell.getEntity()).arrive();
cell.placeHero(team.get(heroIndex));
team.get(heroIndex).setLocation(cell.getRow(), cell.getColumn());
if (cell.getEntity() instanceof Nexus) {
return 1;
} else {
return 2;
} } else {
return -2;
}
} else {
return 0;
}
}
// teleports a hero
public int teleportHero(HeroTeam team, int heroIndex, int r, int c) {
if (r < 0 || r >= super.rowCount() || c < 0 || c >= super.colCount()) {
return -1;
} else if (r == 0) {
System.out.println("Moving to the enemy Nexus is not allowed.");
return -1;
} else if (hero_locations[heroIndex].hero_r == r && hero_locations[heroIndex].hero_c == c) {
System.out.println("Teleporting to the same location is not allowed.");
return -1;
}
if (super.check(r , c)) {
if (scans(r,c)) {
GridMapCell cell = super.getCellAt(r,c);
leave(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c);
this.getCellAt(hero_locations[heroIndex].hero_r,hero_locations[heroIndex].hero_c).removeHero();
hero_locations[heroIndex].hero_r = r;
hero_locations[heroIndex].hero_c = c;
((Terrain) cell.getEntity()).arrive();
cell.placeHero(team.get(heroIndex));
team.get(heroIndex).setLocation(cell.getRow(), cell.getColumn());
if (cell.getEntity() instanceof Nexus) {
return 1;
} else {
return 2;
}
} else {
return -2;
}
} else {
return 0;
}
}
// row scan to see if it is possible to move forward
public boolean scans(int r, int c) {
boolean scans;
if (c % 3 == 0) {
scans = rowScan(r,c) && rowScan(r,c+1);
} else {
scans = rowScan(r,c) && rowScan(r,c-1);
}
return scans;
}
// scans a column for an enemy
public boolean rowScan(int r, int c) {
for (int r_check = super.rowCount() - 1; r_check > 0; r_check--) {
GridMapCell current = super.getCellAt(r_check, c);
if (current.enemyCount() > 0 && r_check > r) {
return false;
}
}
return true;
}
// leave a cell
public void leave(int r, int c) {
GridMapCell cell = super.getCellAt(r, c);
((Terrain) cell.getEntity()).leave();
}
// enter a market
public void enterMarket(HeroTeam heroTeam) {
Market market = new Market("Market");
market.visit(heroTeam);
}
// returns true if there are monsters in the nexus
public boolean didMonstersReachNexus() {
for (int c = 0; c < super.colCount(); c++) {
if (getCellAt(super.rowCount() - 1, c).enemyCount() > 0) {
return true;
}
}
return false;
}
public boolean didHeroesReachNexus() {
for (int c = 0; c < super.colCount(); c++) {
if (getCellAt(0, c).heroCount() > 0) {
return true;
}
}
return false;
}
// returns locations
public CellHeroLocation[] getLocations() {
return hero_locations;
}
// to string for the QuestGridMap
public String toString() {
String s = super.toString();
s += Colors.ANSI_PURPLE_BACKGROUND + " " + Colors.ANSI_RESET + " : HERO NEXUS || ";
s += Colors.ANSI_BLUE_BACKGROUND + " " + Colors.ANSI_RESET + " : ENEMY NEXUS || ";
s += Colors.ANSI_WHITE_BACKGROUND + " " + Colors.ANSI_RESET + " : PLAIN TERRAIN || ";
s += Colors.ANSI_CYAN_BACKGROUND + " " + Colors.ANSI_RESET + " : KOULOU TERRAIN || ";
s += Colors.ANSI_YELLOW_BACKGROUND + " " + Colors.ANSI_RESET + " : CAVE TERRAIN || ";
s += Colors.ANSI_GREEN_BACKGROUND + " " + Colors.ANSI_RESET + " : BUSH TERRAIN || ";
s += Colors.ANSI_RED_BACKGROUND + " " + Colors.ANSI_RESET + " : INACCESIBLE \n";
return s;
}
}