-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVisualizer.java
More file actions
61 lines (60 loc) · 1.8 KB
/
MapVisualizer.java
File metadata and controls
61 lines (60 loc) · 1.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
package agh.cs.lab2;
/**
* The map visualizer converts the {@link IWorldMap} map into a string
* representation.
*
* @author apohllo
*
*/
public class MapVisualizer {
/**
* Convert selected region of the map into a string. It is assumed that the
* indices of the map will have no more than two characters (including the
* sign).
*
* @param map
* The map to convert.
* @param lowerLeft
* The lower left corner of the region that is dumped.
* @param upperRight
* The upper right corner of the region that is dumped.
* @return String representation of the selected region of the map.
*/
public String dump(IWorldMap map, Position lowerLeft, Position upperRight) {
StringBuilder builder = new StringBuilder();
for (int i = upperRight.y + 1; i >= lowerLeft.y - 1; i--) {
if (i == upperRight.y + 1) {
builder.append(" y\\x ");
for (int j = lowerLeft.x; j < upperRight.x + 1; j++) {
builder.append(String.format("%2d", j));
}
builder.append(System.lineSeparator());
}
builder.append(String.format("%3d: ", i));
for (int j = lowerLeft.x; j <= upperRight.x + 1; j++) {
Position currentPosition = new Position(j, i);
if (j <= upperRight.x) {
if (i < lowerLeft.y || i > upperRight.y) {
builder.append("--");
} else {
builder.append("|");
if (map.isOccupied(currentPosition)) {
Object object = map.objectAt(currentPosition);
builder.append(object.toString());
} else {
builder.append(" ");
}
}
} else {
if (i < lowerLeft.y || i > upperRight.y) {
builder.append("-");
} else {
builder.append("|");
}
}
}
builder.append(System.lineSeparator());
}
return builder.toString();
}
}