-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
321 lines (303 loc) · 10.4 KB
/
Map.java
File metadata and controls
321 lines (303 loc) · 10.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
import java.util.ArrayList;
import java.util.Random;
public class Map
{
private Event map[][] = new Event[21][21];
private int numberOfEvents;
/**
* Constructor for the map
*/
Map()
{
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 21; j++)
{
map[i][j] = new Event();
}
}
}
/**
* Generates a random number of events based on the param
*
* @param number Number of events to be generated
* @return True if data generated, false otherwise
*/
public boolean generateData(int number)
{
numberOfEvents = number;
if(number > (21 * 21))
{
return false;
}
Random random = new Random();
int x;
int y;
for(int i = 1; i <= number; i++)
{
while(!isOccupied(x = random.nextInt(21), y = random.nextInt(21)))
{
map[x][y] = new Event(i, random.nextInt(99) + 1);
i++;
}
}
return true;
}
/**
* Checks if location (x, y) on the map has an event on it
*
* @param x x parameter
* @param y y parameter
* @return True if it has an event, false otherwise
*/
private boolean isOccupied(int x, int y)
{
return map[x][y].getId() != 0;
}
private int coord(int x)
{
return x + 10;
}
/**
* Returns an ArrayList of the closest 5 locations or the total numbers of events if there are fewer than 5 events on the map
*
* @param x x parameter
* @param y y parameter
* @return ArrayList of the closest 5 locations or the total numbers of events if there are fewer than 5 events on the map
*/
public ArrayList<Event> getCloseLocations(int x, int y)
{
map[x][y].setDistance(0);
ArrayList<Event> events = new ArrayList<>();
int number = 5;
if(numberOfEvents < 5)
{
number = numberOfEvents;
}
diamondParse(x, y, events, number);
return events;
}
/**
* Returns an ArrayList of the closest locations based on *the number provided* or the total numbers of events if there are fewer than *the number provided* events on the map
*
* @param x x parameter
* @param y y parameter
* @param number The number of closest locations to be looked for
* @return ArrayList of the closest locations based on *the number provided* or the total numbers of events if there are fewer than *the number provided* events on the map
*/
public ArrayList<Event> getCloseLocations(int x, int y, int number)
{
map[x][y].setDistance(0);
ArrayList<Event> events = new ArrayList<>();
if(number > numberOfEvents)
{
number = numberOfEvents;
}
diamondParse(x, y, events, number);
return events;
}
/**
* Parses the matrix in a diamond shape style looking for events, this guarantees that the events found in order are the closest at the same time
*
* @param x x parameter
* @param y y parameter
* @param events The ArrayList in which the parse stores the results
* @param number The number of closest locations to be looked for
*/
private void diamondParse(int x, int y, ArrayList<Event> events, int number)
{
int limit = 1;
if(isOccupied(x, y))
{
map[x][y].setDistance(0);
events.add(map[x][y]);
}
while(events.size() < number)
{
for(int i = x, j = y - limit; j <= y; i--, j++)
{
if(i < 0 || j >= map[0].length || j < 0)
{
continue;
}
map[i][j].setDistance(limit);
if(isOccupied(i, j))
{
events.add(map[i][j]);
if(events.size() == number)
{
return;
}
}
}
for(int i = x - limit + 1, j = y + 1; i <= x; i++, j++)
{
if(i < 0 || j >= map[0].length)
{
continue;
}
map[i][j].setDistance(limit);
if(isOccupied(i, j))
{
events.add(map[i][j]);
if(events.size() == number)
{
return;
}
}
}
for(int i = x + 1, j = y + limit - 1; j >= y; i++, j--)
{
if(i < 0 || j >= map[0].length || i >= map.length)
{
continue;
}
map[i][j].setDistance(limit);
if(isOccupied(i, j))
{
events.add(map[i][j]);
if(events.size() == number)
{
return;
}
}
}
for(int i = x + limit - 1, j = y - 1; i >= x + 1; i--, j--)
{
if(i < 0 || j >= map[0].length || j < 0 || i >= map.length)
{
continue;
}
map[i][j].setDistance(limit);
if(isOccupied(i, j))
{
events.add(map[i][j]);
if(events.size() == number)
{
return;
}
}
}
limit++;
}
}
/**
* Returns the map under a string format as a matrix with spaces between event ids
*
* @return The map under a string format as a matrix with spaces between event ids
*/
public String toString()
{
StringBuilder mapDisplay = new StringBuilder();
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 21; j++)
{
mapDisplay.append(map[i][j].getId());
if(map[i][j].getId() / 10 == 0)
{
mapDisplay.append(" ");
}
if(map[i][j].getId() / 10 >= 1 && map[i][j].getId() / 10 <= 9)
{
mapDisplay.append(" ");
}
if(map[i][j].getId() / 100 >= 1 && map[i][j].getId() / 100 <= 9)
{
mapDisplay.append(" ");
}
}
mapDisplay.append("\n");
}
return mapDisplay.toString();
}
/**
* (ONLY ON LINUX terminal!) Returns the map under a string format with colour palette (maximum distance 11 due to constraints of colours) based on the distance from location searched as a matrix with spaces between event ids
*
* @return The map under a string format with colour palette (maximum distance 11 due to constraints of colours) based on the distance from location searched as a matrix with spaces between event ids
*/
public String toStringColour()
{
final String ANSI_RESET = "\u001B[0m";
final String ANSI_BLACK = "\u001B[30m";
final String ANSI_RED = "\u001B[31m";
final String ANSI_GREEN = "\u001B[32m";
final String ANSI_YELLOW = "\u001B[33m";
final String ANSI_BLUE = "\u001B[34m";
final String ANSI_PURPLE = "\u001B[35m";
final String ANSI_CYAN = "\u001B[36m";
final String ANSI_WHITE = "\u001B[37m";
StringBuilder mapDisplay = new StringBuilder();
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 21; j++)
{
switch(map[i][j].getDistance())
{
case 0:
mapDisplay.append(ANSI_RED).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 1:
mapDisplay.append(ANSI_GREEN).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 2:
mapDisplay.append(ANSI_BLUE).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 3:
mapDisplay.append(ANSI_YELLOW).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 4:
mapDisplay.append(ANSI_PURPLE).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 5:
mapDisplay.append(ANSI_CYAN).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 6:
mapDisplay.append(ANSI_GREEN).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 7:
mapDisplay.append(ANSI_BLUE).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 8:
mapDisplay.append(ANSI_YELLOW).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 9:
mapDisplay.append(ANSI_PURPLE).append(map[i][j].getId()).append(ANSI_RESET);
break;
case 10:
mapDisplay.append(ANSI_CYAN).append(map[i][j].getId()).append(ANSI_RESET);
break;
case -1:
mapDisplay.append(ANSI_BLACK).append(map[i][j].getId()).append(ANSI_RESET);
break;
default:
mapDisplay.append(map[i][j].getId());
break;
}
if(map[i][j].getId() / 10 == 0)
{
mapDisplay.append(" ");
}
if(map[i][j].getId() / 10 >= 1 && map[i][j].getId() / 10 <= 9)
{
mapDisplay.append(" ");
}
if(map[i][j].getId() / 100 >= 1 && map[i][j].getId() / 100 <= 9)
{
mapDisplay.append(" ");
}
}
mapDisplay.append("\n");
}
return mapDisplay.toString();
}
/**
* Return the map as a Event matrix
*
* @return The map as a Event matrix
*/
public Event[][] getMap()
{
return map;
}
}