Exemplo n.º 1
0
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((location == null) ? 0 : location.hashCode());
   result = prime * result + ((owner == null) ? 0 : owner.hashCode());
   return result;
 }
Exemplo n.º 2
0
 @Override
 public boolean equals(Object obj) {
   if (this == obj) return true;
   if (obj == null) return false;
   if (getClass() != obj.getClass()) return false;
   Road other = (Road) obj;
   if (location == null) {
     if (other.location != null) return false;
   } else if (!location.equals(other.location)) return false;
   if (owner == null) {
     if (other.owner != null) return false;
   } else if (!owner.equals(other.owner)) return false;
   return true;
 }
Exemplo n.º 3
0
  private void SetMapManager(JsonObject map) {
    JsonArray hexes = map.getAsJsonArray("hexes");
    JsonArray ports = map.getAsJsonArray("ports");
    JsonArray roads = map.getAsJsonArray("roads");
    JsonArray settlements = map.getAsJsonArray("settlements");
    JsonArray cities = map.getAsJsonArray("cities");
    JsonObject robber = map.getAsJsonObject("robber");
    int radius = map.getAsJsonPrimitive("radius").getAsInt();

    // Return data structures
    HashMap<HexLocation, Hex> hexes_r = new HashMap<>();
    HashMap<VertexLocation, Settlement> settlements_r = new HashMap<VertexLocation, Settlement>();
    HashMap<EdgeLocation, Port> ports_r = new HashMap<EdgeLocation, Port>();
    HashMap<EdgeLocation, Road> roads_r = new HashMap<EdgeLocation, Road>();

    // Get Hex info
    for (int i = 0; i < hexes.size(); i++) {
      JsonObject hex = hexes.get(i).getAsJsonObject();
      JsonObject location = hex.getAsJsonObject("location");
      String resource = "DESERT";
      int number = 0;

      int x = location.getAsJsonPrimitive("x").getAsInt();
      int y = location.getAsJsonPrimitive("y").getAsInt();

      // if hex doesn't have resource and number it is desert hex
      if (hex.has("resource") && hex.has("number")) {
        resource = hex.getAsJsonPrimitive("resource").getAsString();
        number = hex.getAsJsonPrimitive("number").getAsInt();
      }

      // compile into Map structure
      Hex entry = new Hex(x, y, resource.toUpperCase(), number);
      hexes_r.put(entry.location, entry);
    }

    // Get Port info
    for (int i = 0; i < ports.size(); i++) {
      JsonObject port = ports.get(i).getAsJsonObject();
      JsonObject location = port.getAsJsonObject("location");
      String resource = "THREE";

      int x = location.getAsJsonPrimitive("x").getAsInt();
      int y = location.getAsJsonPrimitive("y").getAsInt();

      // if port doesn't have resource than it is 3:1 and those are for any resource
      if (port.has("resource")) resource = port.getAsJsonPrimitive("resource").getAsString();
      String direction = port.getAsJsonPrimitive("direction").getAsString();
      int ratio = port.getAsJsonPrimitive("ratio").getAsInt();

      // compile into Map structure
      HexLocation hexLocation = new HexLocation(x, y);
      EdgeLocation edgeLocation =
          new EdgeLocation(hexLocation, EdgeDirection.valueOf(Direction.shortToLong(direction)));
      // edgeLocation = edgeLocation.getNormalizedLocation();
      Port entry = new Port(edgeLocation, PortType.valueOf(resource.toUpperCase()), ratio);
      ports_r.put(entry.location, entry);
    }

    // Get road info
    for (int i = 0; i < roads.size(); i++) {
      JsonObject road = roads.get(i).getAsJsonObject();
      JsonObject location = road.getAsJsonObject("location");

      int x = location.getAsJsonPrimitive("x").getAsInt();
      int y = location.getAsJsonPrimitive("y").getAsInt();
      int owner = road.getAsJsonPrimitive("owner").getAsInt();
      String direction = location.getAsJsonPrimitive("direction").getAsString();

      // compile into Map structure
      HexLocation hexLocation = new HexLocation(x, y);
      EdgeLocation edgeLocation =
          new EdgeLocation(hexLocation, EdgeDirection.valueOf(Direction.shortToLong(direction)));
      edgeLocation = edgeLocation.getNormalizedLocation();
      Road entry = new Road(edgeLocation, owner);
      roads_r.put(entry.location, entry);
    }

    // Get settlement info
    for (int i = 0; i < settlements.size(); i++) {
      JsonObject settlement = settlements.get(i).getAsJsonObject();
      JsonObject location = settlement.getAsJsonObject("location");

      int x = location.getAsJsonPrimitive("x").getAsInt();
      int y = location.getAsJsonPrimitive("y").getAsInt();
      int owner = settlement.getAsJsonPrimitive("owner").getAsInt();
      String direction = location.getAsJsonPrimitive("direction").getAsString();

      // compile into Map structure
      HexLocation hexLocation = new HexLocation(x, y);
      VertexLocation vertexLocation =
          new VertexLocation(
              hexLocation, VertexDirection.valueOf(Direction.shortToLong(direction)));
      vertexLocation = vertexLocation.getNormalizedLocation();
      Settlement entry = new Settlement(vertexLocation, owner);
      settlements_r.put(entry.location, entry);
    }

    // Get city info
    for (int i = 0; i < cities.size(); i++) {
      JsonObject city = cities.get(i).getAsJsonObject();
      JsonObject location = city.getAsJsonObject("location");

      int x = location.getAsJsonPrimitive("x").getAsInt();
      int y = location.getAsJsonPrimitive("y").getAsInt();
      int owner = city.getAsJsonPrimitive("owner").getAsInt();
      String direction = location.getAsJsonPrimitive("direction").getAsString();

      // compile into Map structure
      HexLocation hexLocation = new HexLocation(x, y);
      VertexLocation vertexLocation =
          new VertexLocation(
              hexLocation, VertexDirection.valueOf(Direction.shortToLong(direction)));
      vertexLocation = vertexLocation.getNormalizedLocation();
      Settlement entry = new Settlement(vertexLocation, owner);
      entry.makeCity();
      settlements_r.put(entry.location, entry);
    }

    int x = robber.getAsJsonPrimitive("x").getAsInt();
    int y = robber.getAsJsonPrimitive("y").getAsInt();

    this.mapManager =
        new MapManager(hexes_r, settlements_r, ports_r, roads_r, new HexLocation(x, y), radius);
  }