Step 3: Not Blocking Hills

The Plan

We need to make sure if our ant spawns we move it off the hill right away so more ants in the hive can spawn. This will only need to be done if the ant hasn't been given any other order, so we'll put it after the food gathering code.

Also, if for some reason food spawned such that an ant right next to the hill wanted to move back onto the hill to go get it, we are going to prevent that.

The Implementation

Let's first prevent stepping on our own hill. We are already tracking information about all moves. Let's just add a dummy order so that the move helper functions think it is always occupied.

Next, at the end of our do_turn function, we'll check if an ant is still on the hill and have it move one of the four directions.

Let's first prevent stepping on our own hill. We are already tracking information about reserved tiles. Let's just add some dummy entries so that doMoveDirection will think it is always occupied.

Next, at the end of our doTurn function, we'll check if an ant is still on a hill and have it move any of the four directions.

The Code

Add this code just before the food gathering section:

    # prevent stepping on own hill
    for hill_loc in ants.my_hills():
        orders[hill_loc] = None

The dummy entry doesn't need a from location, so we just set the value to None.

Add this code after the food gathering section:

    # unblock own hill
    for hill_loc in ants.my_hills():
        if hill_loc in ants.my_ants() and hill_loc not in orders.values():
            for direction in ('s','e','w','n'):
                if do_move_direction(hill_loc, direction):
                    break

Here we check if an ant is on our hill, and if so, we loop through all four directions trying to get it off. Once we find a direction that works, we stop trying the other ones by using the break statement. It's a good thing our helper function returns some useful information!

  • ants.my_hills returns us a list of locations where our hills are located. Remember that a location is a tuple of (row, col).

Add this to the top of the doTurn function (just after the foodTargets declaration):

        // prevent stepping on own hill
        for (Tile myHill : ants.getMyHills()) {
            orders.put(myHill, null);
        }

This will loop through all our hills and add them to the set of reserved tiles. No ant will try to move onto the our hill now.

Add this after the loops to find close food:

        // unblock hills
        for (Tile myHill : ants.getMyHills()) {
            if (ants.getMyAnts().contains(myHill) && !orders.containsValue(myHill)) {
                for (Aim direction : Aim.values()) {
                    if (doMoveDirection(myHill, direction)) {
                        break;
                    }
                }
            }
        }

This will loop through all our hill locations. If there is an ant there that does not have an order, we will try and send it one of the four directions.

  • method ants.getMyHills() returns a Set of Tile objects containing our hill locations

The Results

Let's run the bot again and see how we do.

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:   [7,6,0]     0    [1,1]    -     7     1       [0,0]      [1,1]   [1,1]  [1,1]   1     [0,1]
score 1 1
status survived survived
playerturns 60 60

Here is the replay:

# [ { "embedded": true, "game": "3 - Not Blocking Hills" }, 600, 600, { "speedFactor": 0, "speedFastest": 2, "speedSlowest": 2, "zoom": 1 }, "example_games/tutorial.3.replay" ]

Good! All of our ants got out of the hive. But they just stop doing stuff after they can't see any more food. If you click on the vision button on the left side of the map, you can see that the remaining food is outside the ants' vision. Let's fix that next.

Next

On to Step 4: Exploring the Map