Break alliances
In this guide, we will learn how to break an existing alliance with another player. This is equivalent to manually breaking the alliance from the game UI. We will use the Controller.break_alliance method.
The break_alliance method takes a PlayerID, corresponding to your ally.
Breaking an alliance is usually a strategic move when:
- You are allied with all neighbors and cannot expand
- An ally is becoming too strong
- You want to switch targets and open a new attack path
In the example below, we follow a simple strategy: if we are allied with all non-empty neighbors, we break alliance with the weakest ally.
python
# If all real neighbors are allies, break alliance with the weakest one.
real_neighbors = [n for n in state.me.neighbors if n is not None]
if len(real_neighbors) > 0 and all(
any(ally.other(state.me.id) == n for ally in state.me.alliances)
for n in real_neighbors
):
weakest_ally = min(
state.me.alliances,
key=lambda ally: (
state.players[ally.other(state.me.id)].totalTroops
if ally.other(state.me.id) in state.players
else float("inf")
),
)
weakest_ally_id = weakest_ally.other(state.me.id)
await controller.break_alliance(weakest_ally_id)java
// If all real neighbors are allies, break alliance with the weakest one.
boolean alliedWithAllNeighbors = false;
List<PlayerID> realNeighbors = new ArrayList<>();
for (PlayerID neighbor : state.me.neighbors) {
if (neighbor != null) {
realNeighbors.add(neighbor);
}
}
if (!realNeighbors.isEmpty()) {
alliedWithAllNeighbors = true;
for (PlayerID neighbor : realNeighbors) {
boolean isAlly = false;
for (SimplifiedAlliance ally : state.me.alliances) {
if (ally.other(state.me.id).value.equals(neighbor.value)) {
isAlly = true;
break;
}
}
if (!isAlly) {
alliedWithAllNeighbors = false;
break;
}
}
}
if (alliedWithAllNeighbors && !state.me.alliances.isEmpty()) {
SimplifiedAlliance weakestAlly = null;
double minTroops = Double.MAX_VALUE;
for (SimplifiedAlliance ally : state.me.alliances) {
PlayerID allyId = ally.other(state.me.id);
double troops = Double.MAX_VALUE;
if (state.players.containsKey(allyId)) {
troops = state.players.get(allyId).totalTroops;
}
if (troops < minTroops) {
minTroops = troops;
weakestAlly = ally;
}
}
if (weakestAlly != null) {
controller.breakAlliance(weakestAlly.other(state.me.id));
}
}Note that breaking an alliance comes with a debuff that reduces your defenses for a few seconds. Use wisely.
Possible improvements:
- Add a cooldown so you do not break and re-form alliances too often.
- Break only if you have enough troops to capitalize immediately.
- Prefer breaking with allies that are far from your base if your goal is safer expansion.
Further reading:
SimplifiedAllianceController.break_allianceSimplifiedPlayer(see theisTraitorfield)
