Fork me on GitHub

Logo

Tick 0
Sugar: 0/75 vs. 0/75
play new game help
var dir = randDir(); while (true) { // Loop forever! if (! hasFood()) { // As long as you haven't found food yet, // move in a random direction if (Math.random() < 0.05) { // chance of changing dir dir = randDir(); // to a new direction } move(dir); // Move in the random direction } else { // Go home with the sugar! moveto(home()); } }
var t = randDir(); var food = false; // list of food coordinates while (true) { // store area around ant var up = look('up'); var down = look('down'); var left = look('left'); var right = look('right'); // set food to last found sugar if (up === 'sugar') { food = Vec(location().x, location().y-1); } else if (down === 'sugar') { food = Vec(location().x, location().y+1); } else if (left === 'sugar') { food = Vec(location().x-1, location().y); } else if (right === 'sugar') { food = Vec(location().x+1, location().y-1); } var reset = false; // get out of undigable walls if (!hasFood() && !food) { // markov chain to find food if (!look(t) || look(t) === 'home') { reset = true; } if(Math.random() > 0.95 || reset) { t = randDir(); } move(t); } else if (hasFood() === false && food !== false) { moveto(food); if (look('here') !== 'sugar') { food = false; } } else { // bring food back to base moveto(home()); } }

Hive

Hive is an online programming game where you write code, dig tunnels, find sugar, and build your colony!

Game Mechanics

sugar

home

trail

Ant API and Objects

To interact with the environment, the ant must perform special operations. These are defined by the Ant API, which is documented below.

Important Objects

Vectors

A vector object consists of two properties, an x point, and a y point. It is created by calling the Vec(x, y) function.

var destination = Vec(5, 10);

There are some utility functions defined on Vec objects. These are defined below:

Directions

Directions in the API are represented as strings. They can take one of the following values: 'up', 'down', 'left', 'right', 'here'.

Useful Functions

Getting around

move('down');
moveto(Vec(10, 22));

Look around you

Spray n' Sniff

Utility

Debugging

Example Script

Using these elements, we can make some pretty interesting AIs for our ants!

Here's an annotated example of a script which wanders somewhat randomly throughout the environment, going home if it discovers food:

var dir = randDir();

while (true) { // Loop forever!
    if (! hasFood()) {
        // As long as you haven't found food yet, move in a random direction
        if (Math.random() < 0.05) { // 5% chance of changing dir to a new
            dir = randDir();        // random direction
        }

        move(dir); // Move in the direction we have previously randomly generated
    } else {
        // Go home with the sugar!
        moveto(home());
    }
}

Of course, there is a ton more interesting stuff you can do, once you start bringing in memory, pheromones, and more!

Experiment, and play with the scripts to make the best ant colony possible.