Ejemplo n.º 1
0
  private Castle replaceCityWithCastle(Tile tile, Location loc) {
    ListIterator<Feature> iter = tile.getFeatures().listIterator();
    City city = null;
    while (iter.hasNext()) {
      Feature feature = iter.next();
      if (feature.getLocation() == loc) {
        city = (City) feature;
        break;
      }
    }
    List<Meeple> meeples =
        new ArrayList<>(city.getMeeples()); // collection copy required!!! undeploy modify it
    for (Meeple m : meeples) {
      m.undeploy();
    }
    Castle castle = new Castle();
    castle.setTile(tile);
    castle.setId(game.idSequnceNextVal());
    castle.setLocation(loc.rotateCCW(tile.getRotation()));
    iter.set(castle);

    for (Feature f : tile.getFeatures()) { // replace also city references
      if (f instanceof Farm) {
        Farm farm = (Farm) f;
        Feature[] adjoining = farm.getAdjoiningCities();
        if (adjoining != null) {
          for (int i = 0; i < adjoining.length; i++) {
            if (adjoining[i] == city) {
              adjoining[i] = castle;
              break;
            }
          }
        }
      }
    }

    FeaturePointer fp = new FeaturePointer(tile.getPosition(), loc);
    for (Meeple m : meeples) {
      if (m.getPlayer() == game.getActivePlayer() && m.isDeploymentAllowed(castle).result) {
        m.deploy(fp);
      }
    }
    return castle;
  }