Skip to content

Attack a neighbor

In this guide, we will learn how to attack a neighboring player, equivalent to clicking on that player in the web client. We will use the Controller.attack method.

The attack method takes two arguments: the ID of the player to attack, and the number of troops to send. We can only attack a neighboring player, and we must have enough troops compared to the number of troops we want to send.

In the example below, we attack the first neighboring player in our list of neighbors with 100 troops. Note that this snippet is meant to be a part of the game_loop function, and assumes that state and controller are defined.

python
# Find a neighboring player to attack
neighborId = state.me.neighbors[0]

# Attack the neighboring player with 100 troops
controller.attack(neighborId, 100)
java
// Find a neighboring player to attack
PlayerID neighborId = state.me.neighbors[0]

// Attack the neighboring player with 100 troops
controller.attack(neighborId, 100);

Improvements to this simple example include:

  • Prioritizing attacking the None/null player if there is free land to claim
  • Choosing which neighbor to attack based on the number of troops they have
  • Prioritizing attacking players that are closer to winning
  • Prioritizing attacking players that are weaker than us
  • Prioritzing attacking players that attacked us in the past
  • Prioritizing attacking players that have attacked our allies in the past
  • Control the number of troops we send based on how many troops the neighboring player has, and how many troops we have available

Have fun experimenting with different strategies! Feel free to read the API reference for more details on the attack method, and the game state reference for more details on the game state.