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