-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.java
239 lines (205 loc) · 5.6 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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Reads and contains in memory the map of the game.
*
* @author: The unnamed tutor.
*/
public class Map {
private char[][] map;
private String mapName;
private int goldToWin;
public static final int LOOK_RADIUS = 5;
/**
* @return : Gold required to exit the current map.
*/
protected int getGoldToWin() {
return goldToWin;
}
/**
* @return : The look window around a players coordinates
*/
protected char[][] look(int x, int y) {
char[][] reply = new char[LOOK_RADIUS][LOOK_RADIUS];
for (int i = 0; i < LOOK_RADIUS; i++) {
for (int j = 0; j < LOOK_RADIUS; j++) {
int posX = x + j - LOOK_RADIUS/2;
int posY = y + i - LOOK_RADIUS/2;
if (posX >= 0 && posX < getMapWidth() &&
posY >= 0 && posY < getMapHeight()){
reply[j][i] = map[posY][posX];
}
else{
reply[j][i] = '#';
}
}
}
return reply;
}
/**
* @return : The name of the current map.
*/
public String getMapName() {
return mapName;
}
/**
* @return : The width of the current map.
*/
public int getMapWidth() {
return map[0].length;
}
/**
* @return : The height of the current map.
*/
public int getMapHeight() {
return map.length;
}
/**
* Retrieves a tile on the map. If the location requested is outside bounds of the map, it returns 'X' wall.
*
* @param x : x coordinates of the tile.
* @param y : y coordinates of the tile.
* @return : What the tile at the location requested contains.
*/
public char getTile(int x, int y) {
if (y < 0 || x < 0 || y >= map.length || x >= map[0].length){
return '#';
}
return map[y][x];
}
/**
* Replaces a tile on the map if the location requested is outside bounds of the map.
*
* @param x : x coordinates of the tile.
* @param y : y coordinates of the tile.
* @return : What the tile at the location requested contains.
*/
public void replaceTile(int x, int y, char with) {
if (y < 0 || x < 0 || y >= map.length || x >= map[0].length){
// not in bounds
}
else{
map[y][x] = with;
}
}
/**
* Reads the map from file.
*
* @param : Name of the map's file.
*/
public void readMap(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("maps","example_map.txt")));
map = loadMap(reader);
}
catch (FileNotFoundException e1) {
System.err.println("default file example_map.txt not found");
System.exit(-1);
}
catch (IOException e) {
System.err.println("map not valid");
System.exit(-1);
}
}
/**
* Reads the map from file.
*
* @param : BufferedReader for the map file.
* @return : The map as a 2D char array
*/
private char[][] loadMap(BufferedReader reader) throws IOException{
boolean error = false;
ArrayList<char[]> tempMap = new ArrayList<char[]>();
int width = -1;
String in = reader.readLine();
if (in.startsWith("name")){
error = setName(in);
}
in = reader.readLine();
if (in.startsWith("win")){
error = setWin(in);
}
in = reader.readLine();
if (in.charAt(0) == '#' && in.length() > 1){
width = in.trim().length();
}
while (in != null && !error){
char[] row = new char[in.length()];
if (in.length() != width){
error = true;
}
for (int i = 0; i < in.length(); i++){
row[i] = in.charAt(i);
}
tempMap.add(row);
in = reader.readLine();
}
if (error) {
setName("");
setWin("");
return null;
}
char[][] map = new char[tempMap.size()][width];
for (int i=0;i<tempMap.size();i++){
char[] mapRow = tempMap.get(i);
for (int j = 0; j < mapRow.length; j++) {
}
map[i] = mapRow;
}
return map;
}
/**
* Sets the win condition for the game
* checking to make sure the line from the map file is valid
*
* @param : line of the map file which contains the win condition
* @return : boolean value indicating if there was an error
*/
private boolean setWin(String in) {
if (!in.startsWith("win ")){
return true;
}
int win = 0;
try {
win = Integer.parseInt(in.split(" ")[1].trim());
} catch (NumberFormatException n){
System.err.println("the map does not contain a valid win criteria!");
}
if (win < 0){
return true;
}
this.goldToWin = win;
return false;
}
/**
* Sets the name of the maps
* checking to make sure the line from the map file is valid
*
* @param : line of the map file which contains the name of the map
* @return : boolean value indicating if there was an error
*/
private boolean setName(String in) {
if (!in.startsWith("name ") && in.length() < 4){
return true;
}
String name = in.substring(4).trim();
if (name.length() < 1){
return true;
}
this.mapName = name;
return false;
}
public char[][] getMap() {
char[][] returnMap = new char[getMapHeight()][getMapWidth()];
for (int i=0;i<getMapHeight();i++){
for (int j = 0; j < getMapWidth(); j++) {
returnMap[i][j] = map[i][j];
}
}
return returnMap;
}
}