Four In A Row

Make your own bot

You have two options when you want to participate in this competition. The first is to download one of the starterbots from the right. All the basic stuff like communication with the engine is already implemented, but making the bot smart is up to you. The other option is to create your own bot from scratch. Look at the Languages page for a list of all available languages to code your bot in.

All communication between your bot and the engine works through the standard input and output channels. In the section "Communicating with the game engine" you can find a detailed explanation regarding the protocol all bots should implement to communicate with the game server.

The engine source code is now available on our Github page

Starter bots

Java
PascalBy skalogryz
RustBy Travv0
C#By suwik

A simple example

import java.util.Scanner;

public class MyBot {

    private Scanner scan = new Scanner(System.in);

    public void run()
    {
        while(scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.length() == 0) { continue; }
            String[] parts = line.split(" ");

            switch(parts[0]) {
                case "settings":
                    // store game settings
                    break;
                case "update":
                    // store game updates
                    break;
                case "action":
                    System.out.println("place_disc 0");
                    System.out.flush();
                    break;
                default:
                    // error
            }
        }
    }

    public static void main(String[] args) {
        (new MyBot()).run();
    }
}

This is a very simple example of a working bot in Java. It reads the engine's output and only does something when an action is requested by the engine: it will place a disc in column 0. This bot obviously won't win most of the games, but technically it is doing the correct stuff.

Communicating with the game engine

The communication between your bot and the engine goes via the standard input and output channels. Every single line the engine gives is a specific piece of information or a request. The response from your bot should also be just one line. We follow a standardized API. There are three types of lines, in the following format:

  • settings [type] [value] given only at the start of the game. General settings of the game are given here.
  • action [type] [time] indicates a request for an action.
  • update [player] [type] [value] this is an update of the game state. [player] indicates what bot the update is about, but could also be 'game' to indicate a general update.

In the table below you can find all specific commands your bot can expect for this competition. The format for an action response is also given.

Output from engineDescription
settings timebank t Maximum time in milliseconds that your bot can have in its time bank
settings time_per_move t Time in milliseconds that is added to your bot's time bank each move
settings player_names [b,...] A list of all player names in this match, including your bot's name
settings your_bot b The name of your bot for this match
settings your_botid i The number used in a field update as your bot's chips
settings field_columns i The number of columns of the playing field
settings field_rows i The number of rows of the playing field
update game round i The number of the current round
update game field [[c,...];...]The complete playing field in the current game state
action move tRequest for a move
Output from botDescription
place_disc iThe move your bot wants to make

Each "i" can be any integer. Each "b" is represents the name of a bot (as far as the engine is concerned) and can be either player1 or player2 for this competition. "t" represents a time in milliseconds. A "c" is a cell in the playing field. This can be a 0 for empty, or 1 or 2, for one of the players (given by your_botid). An update of the playing field separates field rows by a semicolon and cells in each row by a comma. Finally, your bot's output should always start with place_disc, followed by a number (0-6, when field_columns is 7).

The engine will always try to perform the action given by the output of your bot. In case the action is not valid in the current game situation, or when the output can't be parsed at all, the engine will output a warning and ask for an action again. If your bot fails to give an acceptable response 3 times, it automatically loses the game.

Input and output between the bot and the engine go via the standard input and output channels, but you can use the error output channel to print stuff in the error dump of your game. This will not be read by the engine and can be used for debugging.

By example

The game itself is not very difficult to understand, but to make it a bit more clear what the inputs and outputs look like we're providing an example. You can find this in the dump of any of your own games as well.

settings timebank 10000
settings time_per_move 500
settings player_names player1,player2
settings your_bot player1
settings your_botid 1
settings field_columns 7
settings field_rows 6

These are the settings of the game. This information is sent at the start of a match only. Your bot now knows the timebank settings, the player names, it's own id number, and the field dimensions. Continuing with the rest of the input/output:

update game round 1
update game field 0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0
action move 10000

Your bot gets the round number and what the field looks like. This is given each time before an action is requested. In the last line the engine requests your bot's move

place_disc 0 // places a disc in the first column from the left

This is your bot's move, namely putting a disc is the left-most column. Now there should be a disc in the bottom-left corner of the field, which is given with another field update directly after your move.

update game field 0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;1,0,0,0,0,0,0
update game round 2
update game field 0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,0,0;1,0,0,0,0,0,2

Here we see the field update after our own move, another round update, and the field update after the opponent's move. If you look closely, you can see the opponent has placed a disc is the right-most column. After this another action would be requested and your bot gets another field update, then your opponent will do a move and your bot gets a field update again, and so on. Now you should know everything you need to start coding your own bot!