
Ants AI Challenge. Beginner's Guide
- Transfer
The strategies implemented in the starter pack are just a starting point for creating your own bot, in fact, these are some of the worst strategies. At the same time, the starter pack includes useful features to help you develop a better strategy. This article will guide you through a series of package enhancements. With each completed step, your bot will become smarter, and your rating will begin to grow.
For this article, we will use the Java start-up package (translator's note: we also need Python, since the tools - a bot tester and a set of useful scripts are written on it, links at the end of the article). You will also need tools.
Create a directory in which you will work with the starter package (bot) and tools. You should get something like this:
Make sure that everything works as it should by running the test game (use the shell script play_one_game.cmd or play_one_game.sh depending on your OS). Among the tools is the “playgame.py” utility, which will help us test our bot. Its use is clearly shown in the shell script play_one_game.cmd (play_one_game.sh).
Now let's make our shell script that will use the new map and our bot.
For Windows: create tutorial.cmd
For Linux: create tutorial.sh
after which we assign rights
We prescribe in it:
Do not forget that the Java bot needs to be compiled. Both bots - and HunterBot and the bot reproduced in the textbook will be determined, i.e. with the same input, they will produce the same moves. This means that if you fully follow the instructions, you will get exactly the same result as in the textbook.
Let's launch our bots using shell script and see how the bot from the starter pack works.
You should see a similar output:
The game lasted only 8 steps, which is suspiciously fast. It looks like player 0 (our bot) was destroyed. After the end of the game, the browser should start to show in the visualizer what happened.

You can see that the strategy of the original bot is terrible - it destroyed itself by pushing two ants. Well, this will be our first improvement.
To prevent collisions, do the following:
To track information where our ants are, use the HashMap structure efficiently. This data structure will store the position of the ants and will allow you to check whether this position is already used. Each HashMap key value will contain a Tile object. Tile - an object with the position of the object on the map. The key is the position of the proposed step, and the value is the current position. This way we can check the HashMap before taking a step to make sure the two ants do not go to the same point. With each movement, we will update the HashMap.
Since this check is useful later in the tutorial, we will make a function that checks whether a given step is possible. It will return a boolean value (true or false).
We will omit the comments in the original bot and post the new code:
The function
- it
-
-
- HashMap with orders is used to track planned steps. In the expression, the
Finally, we will launch our bot again and see what happened.

Better, but still bad. Only one ant comes out and fights with HunterBot, however we avoided suicide, and this is already an improvement. In addition, we have created an auxiliary function, which will be useful to us in the future.
The starter pack can be found here .
Tools for Windows , for Linux / MacOS .
Translated Articles: Ants Tutorial , Step 1: Avoiding collisions .
PS Java sites just started appearing on the site (they are now completed in step 1), so for now I have only finished translating to the end of the first step.
PPS If I liked the translation, I will continue translating it as I add Java examples to the next steps.
PPPS Thanks to an unknown Habr-friend for an invite and an opportunity to submit an article for general viewing.
Background
For this article, we will use the Java start-up package (translator's note: we also need Python, since the tools - a bot tester and a set of useful scripts are written on it, links at the end of the article). You will also need tools.
Install
Create a directory in which you will work with the starter package (bot) and tools. You should get something like this:
C:\aichallenge>tree
Folder PATH listing
C:.
+----tools
+---mapgen
+---maps
| +---maze
| +---multi_hill_maze
| +---symmetric_random_walk
+---sample_bots
| +---csharp
| +---java
| +---php
| | +---tests
| +---python
+---submission_test
+---visualizer
+---data
| +---img
+---js
C:\aichallenge>dir /b
AbstractSystemInputParser.java
AbstractSystemInputReader.java
Aim.java
Ants.java
Bot.java
Ilk.java
make.cmd
Makefile
Manifest.txt
MyBot.java
Order.java
Tile.java
tools
Check
Make sure that everything works as it should by running the test game (use the shell script play_one_game.cmd or play_one_game.sh depending on your OS). Among the tools is the “playgame.py” utility, which will help us test our bot. Its use is clearly shown in the shell script play_one_game.cmd (play_one_game.sh).
Create a shell script for testing the bot
Now let's make our shell script that will use the new map and our bot.
For Windows: create tutorial.cmd
C:\aichallenge>notepad tutorial.cmd
For Linux: create tutorial.sh
user@localhost:~$ gedit tutorial.sh
after which we assign rights
user@localhost:~$ chmod u+x tutorial.sh
We prescribe in it:
python tools/playgame.py "java -jar MyBot.jar" "python tools/sample_bots/python/HunterBot.py" --map_file tools/maps/example/tutorial1.map --log_dir game_logs --turns 60 --scenario --food none --player_seed 7 --verbose -e
- The first two options are the bots that we launch. We use our bot and HunterBot as an opponent.
- The --map_file option sets the map to use. Choose a map with 2 anthills.
- The option --log_dir sets the place where the view file and, optionally, the bot's input / output logs and its errors will be saved.
- The --turns option sets the maximum number of moves for a game. Since no additional data is required, let us dwell on 60 steps.
- The --scenario option allows you to arrange the starting points with food on the map. We specify the placement of food for this tutorial.
- The --food none option allows you to turn off the appearance of food during the game. Again, this option is used as part of the tutorial.
- The --player_seed option ensures that you can get the same result as in this tutorial. HunterBot uses this value to initialize the random number generator, so that it will always take the same steps. (Note: if you want to use game replays to debug your bot, then you should also apply this strategy).
- The --verbose option will display general statistics on the game, which allows you to monitor the progress of the game.
- The --e option directs all bot errors to the console, so if you make a mistake while studying, you can see a message about it.
Do not forget that the Java bot needs to be compiled. Both bots - and HunterBot and the bot reproduced in the textbook will be determined, i.e. with the same input, they will produce the same moves. This means that if you fully follow the instructions, you will get exactly the same result as in the textbook.
Let's launch our bots using shell script and see how the bot from the starter pack works.
You should see a similar output:
running for 60 turns
ant_count c_turns climb? cutoff food r_turn ranking_bots s_alive s_hills score w_turn winning
turn 0 stats: [1,1,0] 0 [1,1] - 18 0 None [1,1] [1,1] [1,1] 0 None
turn 1 stats: [1,1,0] 0 [1,1] - 16 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 2 stats: [2,1,0] 0 [1,1] - 16 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 3 stats: [2,2,0] 0 [1,1] - 15 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 4 stats: [2,3,0] 0 [1,1] - 14 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 5 stats: [2,4,0] 0 [1,1] - 14 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 6 stats: [2,4,0] 0 [1,1] - 14 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 7 stats: [2,4,0] 0 [1,1] - 14 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 8 bot 0 eliminated
turn 8 stats: [0,4,0] 0 [0,1] - 14 1 [0,0] [0,1] [1,1] [1,1] 1 [0,1]
score 1 3
status eliminated survived
playerturns 8 8
The game lasted only 8 steps, which is suspiciously fast. It looks like player 0 (our bot) was destroyed. After the end of the game, the browser should start to show in the visualizer what happened.

You can see that the strategy of the original bot is terrible - it destroyed itself by pushing two ants. Well, this will be our first improvement.
Step 1. Avoid Collisions
Plan
To prevent collisions, do the following:
- Prevent ants from moving on to other ants.
- Prevent 2m ants from moving to the same point.
- Track information about all of our ants.
Implementation
To track information where our ants are, use the HashMap structure efficiently. This data structure will store the position of the ants and will allow you to check whether this position is already used. Each HashMap key value will contain a Tile object. Tile - an object with the position of the object on the map. The key is the position of the proposed step, and the value is the current position. This way we can check the HashMap before taking a step to make sure the two ants do not go to the same point. With each movement, we will update the HashMap.
Since this check is useful later in the tutorial, we will make a function that checks whether a given step is possible. It will return a boolean value (true or false).
The code
We will omit the comments in the original bot and post the new code:
public boolean doMoveDirection(Ants ants, HashMap orders, Tile antLoc, Aim direction) {
// Track all moves, prevent collisions
Tile newLoc = ants.getTile(antLoc, direction);
if (ants.getIlk(newLoc).isUnoccupied() && !orders.containsKey(newLoc)) {
ants.issueOrder(antLoc, direction);
orders.put(newLoc, antLoc);
return true;
} else {
return false;
}
}
@Override
public void doTurn() {
HashMap orders = new HashMap();
Ants ants = getAnts();
for (Tile myAnt : ants.getMyAnts()) {
for (Aim direction : Aim.values()) {
if (doMoveDirection(ants, orders, myAnt, direction)) {
break;
}
}
}
}
The function
doMoveDirection
obtains the ant position (Tile object), and the direction (Aim object - N | E | S | W) and tries to complete the step. This function is located outside the function doTurn
, so we must give it an Ants and HashMap object with our orders. We will use some predefined functions from the bot that can help us: - it
ants.getTile(Tile, Aim)
accepts the position (Tile object) and direction (Aim object) and returns a new position (Tile object). She will take care of locking the card, so we don’t need to worry about that. -
ants.getIlk(Tile)
- takes a position (Tile object) and returns an Ilk object (type of map point in a given position). Next, we call the isUnoccupied()
Ilk object function to make sure that it is possible to make a move. -
Ilk.isUnoccupied
takes a position and lets us know if this move is possible. This is better than early Ilk.isPassable
, because it will not allow you to go to food or other ants. - HashMap with orders is used to track planned steps. In the expression, the
if
condition !orders.containsKey(newLoc)
checks for the existence of the specified key and helps to avoid collisions.results
Finally, we will launch our bot again and see what happened.
C:\aichallenge>tutorial.cmd
running for 60 turns
ant_count c_turns climb? cutoff food r_turn ranking_bots s_alive s_hills score w_turn winning
turn 0 stats: [1,1,0] 0 [1,1] - 18 0 None [1,1] [1,1] [1,1] 0 None
turn 1 stats: [1,1,0] 0 [1,1] - 16 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
turn 2 stats: [2,1,0] 0 [1,1] - 16 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
...
turn 60 stats: [1,5,0] 0 [1,1] - 12 1 [0,0] [1,1] [1,1] [1,1] 1 [0,1]
score 1 1
status survived survived
playerturns 60 60

Better, but still bad. Only one ant comes out and fights with HunterBot, however we avoided suicide, and this is already an improvement. In addition, we have created an auxiliary function, which will be useful to us in the future.
References
The starter pack can be found here .
Tools for Windows , for Linux / MacOS .
Translated Articles: Ants Tutorial , Step 1: Avoiding collisions .
PS Java sites just started appearing on the site (they are now completed in step 1), so for now I have only finished translating to the end of the first step.
PPS If I liked the translation, I will continue translating it as I add Java examples to the next steps.
PPPS Thanks to an unknown Habr-friend for an invite and an opportunity to submit an article for general viewing.