Ejemplo n.º 1
0
  /**
   * @param mapString : string that represents the map to be created
   * @return : a Map object to use in the game
   */
  public static Map createMap(String mapString) {
    Map map = new Map();

    // parse the map string
    try {
      JSONObject jsonMap = new JSONObject(mapString);

      // create SuperRegion objects
      JSONArray superRegions = jsonMap.getJSONArray("SuperRegions");
      for (int i = 0; i < superRegions.length(); i++) {
        JSONObject jsonSuperRegion = superRegions.getJSONObject(i);
        map.add(new SuperRegion(jsonSuperRegion.getInt("id"), jsonSuperRegion.getInt("bonus")));
      }

      // create Region object
      JSONArray regions = jsonMap.getJSONArray("Regions");
      for (int i = 0; i < regions.length(); i++) {
        JSONObject jsonRegion = regions.getJSONObject(i);
        SuperRegion superRegion = map.getSuperRegion(jsonRegion.getInt("superRegion"));
        map.add(new Region(jsonRegion.getInt("id"), superRegion));
      }

      // add the Regions' neighbors
      for (int i = 0; i < regions.length(); i++) {
        JSONObject jsonRegion = regions.getJSONObject(i);
        Region region = map.getRegion(jsonRegion.getInt("id"));
        JSONArray neighbors = jsonRegion.getJSONArray("neighbors");
        for (int j = 0; j < neighbors.length(); j++) {
          Region neighbor = map.getRegion(neighbors.getInt(j));
          region.addNeighbor(neighbor);
        }
      }
    } catch (JSONException e) {
      System.err.println("JSON: Can't parse map string: " + e);
    }

    map.sort();

    return map;
  }
Ejemplo n.º 2
0
 /**
  * Adds a double connection between given neighbor and this region
  *
  * @param neighbor
  */
 public void addNeighbor(Region neighbor) {
   if (!neighbors.contains(neighbor)) {
     neighbors.add(neighbor);
     neighbor.addNeighbor(this);
   }
 }