-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.java
103 lines (94 loc) · 2.84 KB
/
Map.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
import java.io.*;
import java.util.*;
import java.awt.*;
public class Map {
private int[][] map;
public Map(String filePath) {
map = new int[CONSTANTS.ROWS][CONSTANTS.COLS];
parseFile(filePath);
};
// Parse map from file
public void parseFile(String filePath) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
String line;
int i = 0, j = 0;
while ((line = reader.readLine()) != null) {
j = 0;
String[] parts = line.split(",");
for (String part : parts) {
if (j >= CONSTANTS.COLS) {
throw new WrongInputFormatException("Wrong input file format: " + filePath + " LINE:" + i);
}
map[i][j] = Integer.parseInt(part);
j++;
}
i++;
// Check if out of bound
if (i >= CONSTANTS.ROWS) {
break;
}
}
if (i != CONSTANTS.ROWS || j != CONSTANTS.COLS) {
throw new WrongInputFormatException("Wrong input file format: " + filePath + " LESS DATA THAN EXPECTED");
}
} catch (IOException e) {
e.printStackTrace();
} catch (WrongInputFormatException e) {
System.out.println(e);
}
}
public void print() {
for (int i = 0; i < CONSTANTS.ROWS; i++) {
for (int j = 0; j < CONSTANTS.COLS; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public Color getColor(int row, int column) {
int altitude = map[row][column];
int red, green, blue;
if (altitude <= 0) {
red = 0;
green = 0;
blue = 255;
}
else if (altitude <= 200) {
red = 60;
green = 179;
blue = 113;
}
else if (altitude <=400) {
red = 46;
green = 139;
blue = 87;
}
else if (altitude <= 700) {
red = 34;
green = 139;
blue = 34;
}
else if (altitude <= 1500) {
red = 222;
green = 184;
blue = 135;
}
else if (altitude <= 3500) {
red = 205;
green = 133;
blue = 63;
}
else {
red = 145;
green = 80;
blue = 20;
}
return new Color(red, green, blue);
}
public int getMapHeightAtLocation(PrecisePosition p) {
int column = (int) (p.getX() / CONSTANTS.TILE_SIZE_IN_MILES);
int row = (int) (p.getY() / CONSTANTS.TILE_SIZE_IN_MILES);
return map[row][column];
}
}