How to build a robot

A robot is one small JavaScript function. On your turn, it's handed a list of the moves it can make, and it answers with the number of the one it wants. That's all there is to it.

New to the game? Read the rules of Ludo first. Would rather train something than write it? See building a Ludo AI.

Your first robot

This robot always takes the first move on the list:

function(possible_moves, rules) {
    return 0;
}
  1. Open the Sandbox.
  2. Paste it into the box for a colour, and press Reload.
  3. Watch it lose. Now make it better.

What your robot sees

Each item in possible_moves is the whole board as it would look after that move. So you don't have to work out what a move does. You just look at each result and pick the one you like best.

A board looks like this:

board.players[0].pieces   your 4 pieces
board.players[1].pieces   the 4 pieces of the player who moves next
board.players[2].pieces   the player after that
board.players[3].pieces   the player after that

Each piece is just a number saying how far around it has come:

The board with every position numbered, from 1 where yellow enters, to 57 in the middle

The picture shows yellow, but every robot sees the board this way, as if it were yellow. So the same robot works from any seat. Other players' pieces on the shared track use your numbers too.

You aren't told the roll, and you don't need it: it's already in the moves you're given.

A better robot

This one gives each possible board a score, and picks the best. It likes getting home, staying safe, and sending others back to their corners:

function(possible_moves, rules) {
    var best = -Infinity;
    var choice = 0;
    for (var m = 0; m < possible_moves.length; m++) {
        var board = possible_moves[m];
        var score = 0;
        for (var i = 0; i < 4; i++) {
            var position = board.players[0].pieces[i];
            if (rules.done(position)) {
                score += 500;
            } else if (rules.started(position)) {
                score += position;
                if (rules.globe(position) || rules.outofreach(position)) score += 20;
            }
        }
        // an opponent at the start can be one we just struck
        for (var p = 1; p < 4; p++) {
            for (var i = 0; i < 4; i++) {
                if (board.players[p].pieces[i] == -1) score += 50;
            }
        }
        if (best < score) {
            best = score;
            choice = m;
        }
    }
    return choice;
}

Try changing the numbers and see what happens. The sandbox shows who's winning.

Helpers

The rules object answers questions about a position:

rules.globe(position)      is it a globe? A piece is safe there
rules.star(position)       is it a star?
rules.jump(position)       where does a star send you?
rules.started(position)    has the piece left its corner?
rules.done(position)       is the piece home?
rules.canmove(position)    is it on the board, and not home yet?
rules.outofreach(position) is it in the center row, where nobody can hit it?

To pass, return -1. If your robot crashes or returns anything else that isn't on the list, it also passes.

Our robots: the Bestiary

Fifteen robots to beat. In the Sandbox, you can pick any of them for a colour, and read its code right there. You can also use them inside your own robot: Bestiary.careful(possible_moves, rules) gives the move careful would play.

Enter it

Happy with it? Enter it in the Arena. Here's how that works.

Where your robot runs

Your robot behaves the same everywhere, as long as it only looks at the boards it's given. These are the differences:

SandboxPlayReal games
Runsin your pagein your browser, walled offon our server, walled off
Time per turnno limit, and a hang freezes the tab0.1 seconds0.1 seconds
Memoryno limitno limit we set32 MB
Network and storageyesnono
Dateyesnono
Math.randomthe browser'sseededseeded
Remembers between gamesyes, if you make itnono

A robot that runs out of time passes that turn. After three timeouts in a game, it passes for the rest of that game. Its variables last through one game, and the next game starts fresh.

Each board also has a few more things for clever robots. board.position says which colour you are (0 yellow, 1 green, 2 red, 3 blue). board.propagate(roll) lists the moves from a board for a roll, and board.rotate() turns the board so the next player is player 0, so you can look ahead.