-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMap.java
More file actions
64 lines (58 loc) · 1.38 KB
/
Map.java
File metadata and controls
64 lines (58 loc) · 1.38 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
import java.io.*;
public class Map {
char[][] map;
int x;
int y;
public Map(char[][] map){
if (map == null) {
map = new char[1][1]; // make a dummy map, rather than crashing on null pointer exceptions.
}
this.map = map;
this.x = map.length;
this.y = map[0].length;
}
public Map(String filename){
this( Parser.load(filename) ); //fallthrough to the normal constructor once we load the array
}
/** return the value in the cell given at x,y
* @param x x coordinate of the cell
* @param y y coordinate of the cwll
*/
public char get(int x, int y){
}
/** Set a cell at x,y position to a certain value
*/
public void set(int x, int y, char cell){
}
/** Save the map to a text file
* @param filename name of the file to save to
*/
public void save(String filename){
FileOutputStream file = null;
try {
file = new FileOutputStream(filename);
file.write( this.toString().getBytes() );
} catch (IOException e){
e.printStackTrace();
} finally {
try {
if (file != null){ file.close(); }
} catch (IOException e){
e.printStackTrace();
}
}
}
/*display this map as a 2d grid,
*/
public String toString(){
StringBuilder str = new StringBuilder();
for (int yi = 0; yi<this.y; yi++) {
for (int xi = 0; xi<this.x; xi++) {
char c = this.map[xi][yi];
str.append(c);
}
str.append("\n");
}
return str.toString();
}
}