Ask for an alliance
In this guide, we will learn how to ask another player for an alliance, equivalent to right-clicking a player's territory and selecting "Request Alliance" in the game. We will use the Controller.request_alliance method.
The request_alliance method takes a PlayerID.
A simple pattern is to:
- Pick a neighboring player
- Avoid sending duplicate requests
- Avoid requesting alliance with players you are already allied with
In the example below, we request an alliance with our strongest real neighbor (ignoring None/null empty territory).
python
# Request one alliance if we do not already have one or a pending outgoing request.
if len(state.me.alliances) == 0 and len(state.me.outgoingAllianceRequests) == 0:
neighbors = [n for n in state.me.neighbors if n is not None and n in state.players]
if len(neighbors) > 0:
strongest_neighbor_id = max(
neighbors,
key=lambda pid: state.players[pid].totalTroops,
)
await controller.request_alliance(strongest_neighbor_id)java
// Request one alliance if we do not already have one or a pending outgoing request.
if (state.me.alliances.isEmpty() && state.me.outgoingAllianceRequests.isEmpty()) {
PlayerID strongestNeighborId = null;
double maxTroops = -1;
for (PlayerID neighborId : state.me.neighbors) {
if (neighborId != null && state.players.containsKey(neighborId)) {
double troops = state.players.get(neighborId).totalTroops;
if (troops > maxTroops) {
maxTroops = troops;
strongestNeighborId = neighborId;
}
}
}
if (strongestNeighborId != null) {
controller.requestAlliance(strongestNeighborId);
}
}Possible improvements:
- Prefer neighbors who are stronger than you (deterrence) or weaker than you (safer backline), depending on your strategy.
- Use quick chat right after sending a request (for example
help.alliance) to increase acceptance chances. - Add cooldowns so you do not repeatedly target the same player every loop.
Further reading:
