| 1 | import java.io.*;
|
|---|
| 2 | import java.util.*;
|
|---|
| 3 |
|
|---|
| 4 | public class Map {
|
|---|
| 5 | private Location[][] grid;
|
|---|
| 6 |
|
|---|
| 7 | public Map(int x, int y) {
|
|---|
| 8 | grid = new Location[x][y];
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | public Map(String landFile, String structFile, HashMap<LandType, Land> landMap, HashMap<StructureType, Structure> structMap) {
|
|---|
| 12 | try {
|
|---|
| 13 | int length, height, x, y;
|
|---|
| 14 | String str, loc;
|
|---|
| 15 | BufferedReader in = new BufferedReader(new FileReader(landFile));
|
|---|
| 16 |
|
|---|
| 17 | str = in.readLine();
|
|---|
| 18 | length = Integer.parseInt(str.substring(0, str.indexOf("x")));
|
|---|
| 19 | height = Integer.parseInt(str.substring(str.indexOf("x")+1));
|
|---|
| 20 | grid = new Location[length][height];
|
|---|
| 21 |
|
|---|
| 22 | for(x=0; x<height; x++) {
|
|---|
| 23 | str = in.readLine();
|
|---|
| 24 | for(y=0; y<length; y++) {
|
|---|
| 25 | if(str.indexOf(",") == -1)
|
|---|
| 26 | loc = str;
|
|---|
| 27 | else {
|
|---|
| 28 | loc = str.substring(0, str.indexOf(","));
|
|---|
| 29 | str = str.substring(str.indexOf(",")+1);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | if(loc.equals("o")) {
|
|---|
| 33 | loc = "Ocean";
|
|---|
| 34 | }else if(loc.equals("1")) {
|
|---|
| 35 | loc = "Grass";
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | grid[y][x] = new Location(landMap.get(LandType.valueOf(loc)), structMap.get(StructureType.None));
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | in.close();
|
|---|
| 43 |
|
|---|
| 44 | in = new BufferedReader(new FileReader(structFile));
|
|---|
| 45 |
|
|---|
| 46 | while((loc = in.readLine()) != null) {
|
|---|
| 47 | str = in.readLine();
|
|---|
| 48 | x = Integer.valueOf(str.substring(0, str.indexOf(",")));
|
|---|
| 49 | y = Integer.valueOf(str.substring(str.indexOf(",")+1));
|
|---|
| 50 |
|
|---|
| 51 | grid[x-1][y-1].setStruct(structMap.get(StructureType.valueOf(loc)));
|
|---|
| 52 | }
|
|---|
| 53 | } catch(IOException ioe) {
|
|---|
| 54 | ioe.printStackTrace();
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public Location getLoc(int x, int y) {
|
|---|
| 59 | return grid[x][y];
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public void setLoc(Location loc, int x, int y) {
|
|---|
| 63 | grid[x][y] = loc;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public int getLength() {
|
|---|
| 67 | return grid.length;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public int getHeight() {
|
|---|
| 71 | if(grid.length>0)
|
|---|
| 72 | return grid[0].length;
|
|---|
| 73 | else
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|