public List<Combat> addAttackers(Game game) { Map<Integer, Combat> engagements = new HashMap<Integer, Combat>(); // useful only for two player games - will only attack first opponent UUID defenderId = game.getOpponents(playerId).iterator().next(); List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game); // use binary digits to calculate powerset of attackers int powerElements = (int) Math.pow(2, attackersList.size()); StringBuilder binary = new StringBuilder(); for (int i = powerElements - 1; i >= 0; i--) { Game sim = game.copy(); binary.setLength(0); binary.append(Integer.toBinaryString(i)); while (binary.length() < attackersList.size()) { binary.insert(0, "0"); } for (int j = 0; j < attackersList.size(); j++) { if (binary.charAt(j) == '1') { setStoredBookmark( sim .bookmarkState()); // makes it possible to UNDO a declared attacker with costs // from e.g. Propaganda if (!sim.getCombat() .declareAttacker(attackersList.get(j).getId(), defenderId, playerId, sim)) { sim.undo(playerId); } } } if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) { logger.debug("simulating -- found redundant attack combination"); } else if (logger.isDebugEnabled()) { logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size()); } } return new ArrayList<Combat>(engagements.values()); }
@Override public void selectAttackers(Game game, UUID attackingPlayerId) { // useful only for two player games - will only attack first opponent // logger.info("select attackers"); UUID defenderId = game.getOpponents(playerId).iterator().next(); List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game); // use binary digits to calculate powerset of attackers int powerElements = (int) Math.pow(2, attackersList.size()); int value = rnd.nextInt(powerElements); StringBuilder binary = new StringBuilder(); binary.append(Integer.toBinaryString(value)); while (binary.length() < attackersList.size()) { binary.insert(0, "0"); // pad with zeros } for (int i = 0; i < attackersList.size(); i++) { if (binary.charAt(i) == '1') { setStoredBookmark( game .bookmarkState()); // makes it possible to UNDO a declared attacker with costs from // e.g. Propaganda if (!game.getCombat() .declareAttacker(attackersList.get(i).getId(), defenderId, playerId, game)) { game.undo(playerId); } } } actionCount++; }