Ants Tutorial
The strategies implemented in the starter packages are only meant to serve as a starting point for making your bot. In fact, it's almost the worst strategy to use. The starter packages also come with some useful functions to help you develop a smarter strategy. This page will walk you through a series of improvements. With each step that you complete, your bot will get smarter and you should notice your ranking start to rise.
Prerequisites
For this tutorial, we will be using the Python starter package. In order to use python, you must have a python interpreter downloaded and installed on your machine. See Getting Started with Python.
Note: The tools come with the game engine written in python. You will need to install a python interpreter to run the game engine regardless of which language you are programming in.
You'll also want to download the tools and install them on your machine.
Setting Up
Create a folder to put both the tools and the starter bot and unzip both files in that location. You should have something that looks like this:
C:\aichallenge>tree Folder PATH listing C:. +----tools +---mapgen +---maps | +---example | +---maze | +---multi_hill_maze | +---symmetric_random_walk +---sample_bots | +---csharp | +---java | +---php | | +---tests | +---python +---submission_test +---visualizer +---data | +---img +---js
C:\aichallenge>dir /b ants.py MyBot.py tools
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
Testing
Now lets make sure everything is working by running a test game. The tools comes with a utility called "playgame.py" that will help up test our bot. It also comes with an example script called "play_one_game.cmd" to show you how to use it.
C:\aichallenge>tools\play_one_game.cmd running for 500 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,1,1,0] 0 [1,1,1,1] - 20 0 None [1,1,1,1] [1,1,1,1] [1,1,1,1] 0 None turn 1 stats: [1,1,1,1,0] 0 [1,1,1,1] - 20 1 [0,0,0,0] [1,1,1,1] [1,1,1,1] [1,1,1,1] 1 [0,1,2,3] turn 2 stats: [1,1,1,1,0] 0 [1,1,1,1] - 24 1 [0,0,0,0] [1,1,1,1] [1,1,1,1] [1,1,1,1] 1 [0,1,2,3] turn 3 stats: [1,1,1,1,0] 0 [1,1,1,1] - 24 1 [0,0,0,0] [1,1,1,1] [1,1,1,1] [1,1,1,1] 1 [0,1,2,3] turn 4 stats: [1,1,1,1,0] 0 [1,1,1,1] - 22 1 [0,0,0,0] [1,1,1,1] [1,1,1,1] [1,1,1,1] 1 [0,1,2,3] turn 5 stats: [2,1,2,1,0] 0 [1,1,1,1] - 22 1 [0,0,0,0] [1,1,1,1] [1,1,1,1] [1,1,1,1] 1 [0,1,2,3] ...
If you saw the preceding output, then everything should be working.
Create Test Script
Now, let's create our own script for this tutorial that uses a new map and our own bot.
Create a file called "tutorial.cmd".
C:\aichallenge>notepad tutorial.cmd
Create a file called "tutorial.sh".
user@localhost:~$ gedit tutorial.sh
After editing the file, make it executable:
user@localhost:~$ chmod u+x tutorial.sh
The contents of the text file will be:
python tools/playgame.py "python MyBot.py" "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
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 java bot needs to be compiled into a jar file for us to use. You can run the following command to create the file:
make
- The first 2 options are the names of the bots to run. We'll be using HunterBot as our opponent.
- The
--map_file
options specifies the map to use. This is a simple map with 2 ant hills. - The
--log_dir
options specifies a location to store the replay file, and optionally the logs of bot input and output and errors. - The
--turns
options specifies when to stop the game if it goes too long. We don't want a lot of extra output, so will keep it to 60 turns. - The
--scenario
option allows us to use the food specified on the map as the starting food. It has been specially placed for this tutorial. (remove this for real games) - The
--food none
option allows us to turn off the food spawning during the game. Again, it will be off just for this tutorial. (remove this for real games) - The
--player_seed
option ensures that you can get the same game results as in the tutorial. HunterBot will use this value to initialize the random number generator, so it will always do the same thing. (Note: if you want your bot to be able to replay games for debugging, you'll want to implement this as well.) - The
--verbose
option will print a running total of game stats so we can watch the progress as the game is played. - The
-e
option will output any bot errors to the console, so if you make a mistake during the tutorial you can see what the error message is. (Note: remove the--scenario
and--food none
options when you want to play games on different maps.) (Note: the tutorial was made with view radius 55, which is not the official view radius. You can add--viewradius 55
to the engine to override the default and make the tutorial bot behave the same as the same replays.) (Both HunterBot and the tutorial bot we will be making are deterministic, meaning if you give them the same input, they will produce the same set of moves. This means if you follow along exactly, you should see the tutorial games play out exactly the same on your machine. The python code that sorts lists will sort every element, the java code wasn't implemented to sort correctly, so results may differ.)
Let's run the command and see how the starter bot does.
C:\aichallenge>tutorial
user@localhost:~$ ./tutorial.sh
You should see the following 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 only ran for 8 turns, which is very fast. It looks like player 0 (that's us) got eliminated. A browser should have launched to show you the game in the visualizer.
# [ { "embedded": true }, 600, 600, { "speedFactor": 0, "speedFastest": 2, "speedSlowest": 2, "zoom": 1 }, "example_games/tutorial.0.replay" ]
You can see the starter bot's strategy is so horrible, it kills itself by colliding 2 ants. That will be our first improvement.
Next
Here's the list of improvements we'll be implementing in this tutorial: