@Override
 public void saveTileToSnapshot(Tile tile, Document doc, Element tileNode) {
   if (tile.getBridge() != null) {
     Location realLoc = tile.getBridge().getRawLocation();
     tileNode.setAttribute("bridge", realLoc.toString());
   }
 }
 private boolean isBridgePlacementAllowed(Tile tile, Position p, Location bridgeLoc) {
   if (!tile.isBridgeAllowed(bridgeLoc)) return false;
   for (Entry<Location, Tile> e : getBoard().getAdjacentTilesMap(p).entrySet()) {
     Location rel = e.getKey();
     if (rel.intersect(bridgeLoc) != null) {
       Tile adjacent = e.getValue();
       char adjacentSide = adjacent.getEdge(rel.rev());
       if (adjacentSide != 'R') return false;
     }
   }
   return true;
 }
  @Override
  public void loadFromSnapshot(Document doc, Element node) {
    NodeList nl = node.getElementsByTagName("player");
    for (int i = 0; i < nl.getLength(); i++) {
      Element playerEl = (Element) nl.item(i);
      Player player = game.getPlayer(Integer.parseInt(playerEl.getAttribute("index")));
      castles.put(player, Integer.parseInt(playerEl.getAttribute("castles")));
    }

    nl = node.getElementsByTagName("castle");
    for (int i = 0; i < nl.getLength(); i++) {
      Element castleEl = (Element) nl.item(i);
      Position pos = XMLUtils.extractPosition(castleEl);
      Location loc = Location.valueOf(castleEl.getAttribute("location"));
      Castle castle = convertCityToCastle(pos, loc, true);
      boolean isNew = XMLUtils.attributeBoolValue(castleEl, "new");
      boolean isCompleted = XMLUtils.attributeBoolValue(castleEl, "completed");
      if (isNew) {
        newCastles.add(castle);
      } else if (isCompleted) {
        emptyCastles.add(castle);
      } else {
        scoreableCastleVicinity.put(castle, castle.getVicinity());
      }
    }
  }
 @Override
 public void loadTileFromSnapshot(Tile tile, Element tileNode) {
   if (tileNode.hasAttribute("bridge")) {
     Location loc = Location.valueOf(tileNode.getAttribute("bridge"));
     tile.placeBridge(loc);
   }
 }
示例#5
0
 @Override
 public String toString() {
   if (location == Location.PRISON) {
     return getClass().getSimpleName() + " " + location.toString();
   } else {
     return super.toString();
   }
 }
  public BridgeAction prepareMandatoryBridgeAction() {
    Tile tile = game.getCurrentTile();
    for (Entry<Location, Tile> entry :
        getBoard().getAdjacentTilesMap(tile.getPosition()).entrySet()) {
      Tile adjacent = entry.getValue();
      Location rel = entry.getKey();

      char adjacentSide = adjacent.getEdge(rel.rev());
      char tileSide = tile.getEdge(rel);
      if (tileSide != adjacentSide) {
        Location bridgeLoc = getBridgeLocationForAdjacent(rel);
        BridgeAction action = prepareTileBridgeAction(tile, null, bridgeLoc);
        if (action != null) return action;
        return prepareTileBridgeAction(adjacent, null, bridgeLoc);
      }
    }
    throw new IllegalStateException();
  }
 private Castle convertCityToCastle(Position pos, Location loc, boolean loadFromSnaphot) {
   Castle castle1 = replaceCityWithCastle(getBoard().get(pos), loc);
   Castle castle2 = replaceCityWithCastle(getBoard().get(pos.add(loc)), loc.rev());
   castle1.getEdges()[0] = castle2;
   castle2.getEdges()[0] = castle1;
   if (!loadFromSnaphot) {
     newCastles.add(castle1.getMaster());
   }
   game.post(new CastleDeployedEvent(game.getActivePlayer(), castle1, castle2));
   return castle1.getMaster();
 }
  private boolean isTilePlacementWithOneAdjacentBridgeAllowed(Tile tile, Position p) {
    boolean bridgeUsed = false;
    for (Entry<Location, Tile> e : getBoard().getAdjacentTilesMap(p).entrySet()) {
      Tile adjacent = e.getValue();
      Location rel = e.getKey();

      char tileSide = tile.getEdge(rel);
      char adjacentSide = adjacent.getEdge(rel.rev());

      if (tileSide != adjacentSide) {
        if (bridgeUsed) return false;
        if (tileSide != 'R') return false;

        Location bridgeLoc = getBridgeLocationForAdjacent(rel);
        if (!isBridgePlacementAllowed(adjacent, adjacent.getPosition(), bridgeLoc)) return false;
        bridgeUsed = true;
      }
    }
    return bridgeUsed; // ok if exactly one bridge is used
  }
示例#9
0
	@SuppressWarnings("unchecked")
	public List<Meeple> extractTileMeeples(Element tileEl, Game game, Position pos) throws SnapshotCorruptedException {
		NodeList nl = tileEl.getElementsByTagName("meeple");
		List<Meeple> result = Lists.newArrayList();
		for(int i = 0; i < nl.getLength(); i++) {
			Element el = (Element) nl.item(i);
			Location loc = Location.valueOf(el.getAttribute("loc"));
			Class<? extends Meeple> mt = (Class<? extends Meeple>) XmlUtils.classForName(el.getAttribute("type"));
			int playerIndex = Integer.parseInt(el.getAttribute("player"));
			Meeple meeple = game.getPlayer(playerIndex).getUndeployedMeeple(mt);
			meeple.setLocation(loc);
			meeple.setPosition(pos);
			//don't set feature here. Feature must be set after meeple deployment to correct replace ref during merge
			result.add(meeple);
		}
		return result;
	}
 @Override
 public void loadFromSnapshot(Document doc, Element node) {
   NodeList nl = node.getElementsByTagName("tunnel");
   for (int i = 0; i < nl.getLength(); i++) {
     Element el = (Element) nl.item(i);
     Position pos = XmlUtils.extractPosition(el);
     Location loc = Location.valueOf(el.getAttribute("location"));
     Road road = (Road) getBoard().get(pos).getFeature(loc);
     if (!road.isTunnelEnd()) {
       logger.error("Tunnel end does not exist.");
       continue;
     }
     Player player = game.getPlayer(Integer.parseInt(el.getAttribute("player")));
     boolean isB = "yes".equals(el.getAttribute("b"));
     road.setTunnelEnd(getTunnelId(player, isB));
     game.post(new TunnelPiecePlacedEvent(player, pos, loc, isB));
   }
 }
  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;
  }
示例#12
0
 public Location getLocation() {
   return location.rotateCW(tile.getRotation());
 }