Tutorial | Lobby | Users | Ranking | Register

Coder's Block Arena - The Game AI Platform


What Is This?

It is a platform for bots to compete against each other in various games, both contrived and common, to test each programmer's ability to write algorithmic and logical code.

What Languages?

You can use absolutely any language. However, the API has currently only been written for Java, Python and C/Windows. Rewriting it for other languages and OS's is necessary. If you'd like to help me, please do so.

What Games?

Currently, the only supported game is:

How Does It Work?

You simply write an AI that can play as one of the players in any of the games on the platform. You run the AI on your computer, and have the code call the functions in the CB API. Calling these functions will automatically make it play against other AIs in real-time.

What For?

The purpose of this is to challenge yourself, practice general and specific coding skills, learn more about algorithms and AIs, and have fun.

Anything Else?

After you bind your AI to the API, you will see the results of all your matches updated real-time on this website. You will also be ranked against every other bot who's played the same game in a variety of ways. This can help you track your progress as a programmer on Coders Block, set personal goals, and compare yourself to others.


The API

The API is limited to only the necessary commands. They are all described as follows for the C/Windows API. There will be slight variations for each language. See the examples at the bottom.

cb_init(do_debug)
Returns 0 for success. True for failure.
Initiates the CB API. Accepts an integer. If the integer is true, you will see all HTTP packets printed out in the console.

cb_login(name, password, game)
Returns void.
Establishes and urlencodes the configuration. Does not verify. Use cb_cmd("CHECK") to verify. The game parameter is TIC-TAC-TOE for Tic-Tac-Toe.

cb_cmd("CHECK")
Returns either "NOT LOGGED IN", "INVALID GAME NAME", or "OKAY". NULL pointer if client or server failed.
Checks whether or not your login configuration is correct.

Note: The login configuration is set like so: cb_login(name, password, game)

cb_cmd("GETGAME")
Returns either "WAIT" or the name of the bot you are up against. NULL pointer if client or server failed.
Enters the lobby and either looks for a game to join, or hosts a new game.

This command lasts for 15 seconds. That means after you call GETGAME, anyone can join your game for the next 15 seconds. If no one replies, it assumes you have left the lobby. Therfore, you should call this command every 15 seconds to reserve your spot in the lobby.

Note: As soon as you call the GETGAME command, you are dedicating yourself to a game for the next 15 seconds. If someone responds within that time period, or you have entered someone's game, then failing to continue the game will result in disqualification from the match. If you want to try to leave the lobby, you may call LEAVELOBBY.

cb_cmd("LEAVELOBBY")
Returns either "OKAY" or "NO". NULL pointer if client or server failed.
Attempts to leave the lobby. If NO is returned, then you are not in the lobby. Or you are currently inside a game, and must proceed, or face disqualified from the match.

cb_cmd("GETBOARD")
Returns either "INVALID PROTOCOL", "WAIT", "WIN", "LOSE", "TIE", or the state of the board. NULL pointer if client or server failed.
Gets the state of the current game. If WAIT is returned, your opponent is still making his/her move. It is suggested to wait 3-5 seconds until calling GETBOARD again. From the time that your opponent finishes his/her move, to the time you post yours, if more than 15 seconds have passed, you will be disqualified for a time out (see the command below).

Even if your game is over, GETBOARD will always return the result of your most recent game, until a new game is started. However, if you have never played a game before, you will get an INVALID PROTOCOL reply.

For Tic-Tac-Toe, the state of the board is xxxxxxxxx where x the character "0", "1", or "2".
These characters represent the pieces from top-left to bottom-right of the 3x3 board. 0 is a blank piece. 1 is your piece. 2 is your opponent's piece.

cb_cmd(BOARDSTATE)
Returns either "DQ", "INVALID PROTOCOL" or "OKAY", depending on whether or not your move was valid.
Replying with your move is as straightforward as replying with the new state of the board, in the same format that it was given to you, through cb_cmd().

While you are replying, if your opponent checks the board, and it has been 15 seconds since he/she made his/her move, then he/she will assume you have timed out, and you will be disqualified from the match.

DQ means your command was unrecognizable or your move was illegal. INVALID PROTOCOL usually means you timed out. The result was disqualification from the match.

cb_cleanup()
Returns 0 for success. True for failure.
Cleans up the socket connection.


Download The CB API

API In C For Windows (Note: Must Add "-lwsock32" To Your Linker)
API In Python For All OS's (Note: Must Rename File To "cbapi.py")
API In Java For All OS's * (Note: Must Add The Line "import net.codersblock.api.CodersBlock;")

Credit

* Many thanks to andro for the Java Implementation of the CB API.


Example In C For Windows

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "cbapi.h"

void delay(int sec) { 
    Sleep(sec*1000);
    printf(".");
}

int main() {
    
    char *reply;
    cb_init(0);

    cb_login("Sane", "************", "TIC-TAC-TOE");
    if( strcmp(reply = cb_cmd("CHECK"), "OKAY") ) {
        printf(reply);
        return cb_cleanup();
    }
    
    printf("Looking For Game ...");
    while( strcmp(reply = cb_cmd("GETGAME"), "WAIT") == 0 ) delay(5);
    if( !reply ) return cb_cleanup()
    
    printf("\nPlaying Against \"%s\"\n", reply);
    while(1) {
        printf("Waiting For Turn ...");
        while( strcmp(reply = cb_cmd("GETBOARD"), "WAIT") == 0 ) delay(0);
        if( !reply ) return cb_cleanup()

        if( strcmp(reply, "WIN") == 0 || strcmp(reply, "LOSE") == 0 || strcmp(reply, "TIE") == 0 ) {
            printf("Game Over: %s\n", reply);
            return cb_cleanup();
        }

        /* Make Move By Updating One Of The Pieces In 'reply' */
    
        if( strcmp(reply = cb_cmd(reply), "OKAY") ) {
            printf("Disqualified For Illegal Move Or Timed Out\n");
            return cb_cleanup();
        }
        else printf("Made Move\n");
    }
    
    return cb_cleanup();
}

Example In Python

import cbapi

cb = cbapi.init(0)
cb.login("Sane", "************", "TIC-TAC-TOE")
if (cb.cmd("CHECK") != "OKAY"):
    print "Invalid Login"

cb.cleanup()

Example In Java

import net.codersblock.api.CodersBlock;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		new Main();
	}
	
	public Main() {
		CodersBlock cb = new CodersBlock();
		
		cb.login("java_api_test", "apitest", "TIC-TAC-TOE");
		
		if (!cb.cmd("CHECK").equals("OKAY"))
			System.out.println("Invalid login");
			
		Scanner s = new Scanner(System.in);
		
		while (true) {
			System.out.print("Enter command: ");
			String cmd = s.nextLine();
			String response = cb.cmd(cmd);
			System.out.println("Server: " + response);
		}
	}
}