Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2008, 1:17 AM   #1
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Smile Coder's Block Arena - The Game AI Platform

Three days ago I had an idea, and decided to put it into action as an experiment.

It's essentially a website where you build AIs for two player games. They are then automatically contested and ranked against other users' bots through the Coder's Block API.

The first game is Tic-Tac-Toe (Noughts and Crosses), because I thought that would be a nice starting point to test the platform's stability.

You can never lose Tic-Tac-Toe if you play it perfectly, so achieving an "Intelligence Rating" of 100% is fairly easy.
http://codersblock.net/arena/viewuser.php?user_id=6

The site is currently under development. It is a bare template, and it's lacking any ranking pages. However, the database is complete, and the platform is easily extendable. So it should be fully functional, and will sky rocket in appearance during any spare moments I have.

http://codersblock.net/arena/lobby.php
http://codersblock.net/arena/tutorial.php
http://codersblock.net/arena/register.php

I would like to see as many of you guys as possible writing Tic-Tac-Toe AIs to test this out over the next week. I'm confident that many of you can write a perfect AI in an hour or two. It's even brute-forceable.

The platform uses PHP and MySQL. It can be extended to handle any number of games (not just Tic-Tac-Toe), so more complicated games will appear in the future. The database has a ranking system (although there's no page to show it yet). MySQL injections are protected against. Passwords are securely double-salted and hashed. Invalid API protocol and any attempted protocol hacking (either accidental or on purpose) is all controlled server-side, and thus strongly protected.

Once you're done writing an AI, to see how to link it to the API, read below. If you're not using C and Windows, please help me by rewriting the API for your language. All the API does is make GET requests to a PHP page.

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 C in Windows. Rewriting it for other languages and OS's is necessary. If you'd like to help me, please do so.

How Does It Work?
You simply write an AI that can solve a particular problem, or make an appropriate move for one of the games in the framework. Then by calling the functions in the CB API, it will automatically be contested against other AIs.

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:

cb_init(do_debug)

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

cb_login(name, password, game)

Establishes and urlencodes the configuration. Does not verify. Use cb_cmd("CHECK") to verify.
Returns void.

The "game" parameter is "TIC-TAC-TOE" for Tic-Tac-Toe.

cb_cmd("CHECK")

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

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

cb_cmd("GETGAME")

Enters the lobby and either looks for a game to join, or hosts a new game.
This must be called every 15 seconds to reserve your spot in the lobby.
Returns either "WAIT" or the name of the bot you are up against. NULL pointer if client or server failed.

Note: As soon as you call the GETGAME command, you are dedicating yourself to a game. If someone responds within 15 seconds, 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")

Attempts to leave the lobby.
Returns either "OKAY" or "NO". NULL pointer if client or server failed.

If "NO" is returned, then you are currently inside a game, and must proceed, or face disqualified from the match.

cb_cmd("GETBOARD")

Gets the state of the current game.
Returns either "NOT IN GAME", "WAIT", "WIN", "LOSE", "TIE", or the state of the board. NULL pointer if client or server failed.

If "WAIT" is returned, your opponent is still making his/her move. It is suggested to wait 3-5 seconds until calling "GETBOARD" again. Taking more than 15 seconds will count as a timeout and you will be disqualified from the match.

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.

Replying To Move

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 in, through cb_cmd().
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.
Returns either "DQ" or "OKAY", depending on whether or not your move was valid.

"DQ" means your command was unrecognizable, your move was illegal, or you timed out. This resulted in disqualification from the match.

cb_cleanup()

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

cbapi.h

API In C For Windows (Note: Must Add "-lwsock32" To Your Linker)

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), "DQ") == 0 ) {
            printf("Disqualified For Illegal Move\n");
            return cb_cleanup();
        }
        else printf("Made Move\n");
    }
    
    return cb_cleanup();
}


And if anyone wants to translate the API to Unix and/or other languages. It should not be difficult at all.

All the API does is open up a webpage, "api.php", with a bunch of GET commands.

#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <string.h>
#include <ctype.h>

#define ERR -1
#define MAX 1024

SOCKET cb_sock;
WSADATA cb_ws;
hostent *cb_he;
struct sockaddr_in cb_a;

const char cb_hostname[] = "codersblock.net";
char cb_buff[MAX], cb_reply[MAX];
char cb_user[MAX] = {0}, cb_pass[MAX] = {0}, cb_game[MAX] = {0};
int cb_debug = 0;

char *cb_error(const char *msg) {
    fprintf(stderr, "%s\n", msg);
    return NULL;
}

int cb_urlencode(char *dest, const char *src) {
    /* urlencode all non-alphanumeric characters in the C-string 'src'
       store result in the C-string 'dest'
       return the length of the url encoded C-string
    */
    char *d;
    int i;
    for(i=0, d=dest; src[i]; i++) {
        if(isalnum((unsigned char)src[i])) *(d++) = src[i];
        else {
            sprintf(d, "%%%02X", src[i]);
            d += 3;
        }
    }   
    *d = 0;
    return d-dest;
}

int cb_init(int do_debug) {
    cb_debug = do_debug;
    if (WSAStartup(0x101, &cb_ws) == ERR) {
        cb_error("Could Not Create Socket");
        return 1;
    }
    return 0;
}

void cb_login(const char *tuser, const char *tpass, const char *tgame) {
    cb_urlencode(cb_user, tuser);
    cb_urlencode(cb_pass, tpass);
    cb_urlencode(cb_game, tgame);
}

char *cb_cmd(const char *cmd) {
    char *size, *msg;
    int status, len;
    char cpycmd[MAX];
    
    cb_sock = socket(AF_INET, SOCK_STREAM, 0);
    cb_he = gethostbyname(cb_hostname);
    if (!cb_he) return cb_error("Could Not Resolve Host Name");
    
    cb_a.sin_family = AF_INET;
    cb_a.sin_port = htons(80);
    cb_a.sin_addr.s_addr = *((unsigned long *)cb_he->h_addr);
    
    status = connect(cb_sock, (struct sockaddr *)&cb_a , sizeof(cb_a));
    if (status == ERR) return cb_error("Could Not Connect To Server");
    
    cb_urlencode(cpycmd, cmd);
    sprintf(cb_buff, "GET /arena/api.php?cmd=%s&name=%s&pass=%s&game=%s HTTP/1.1\r\nHost: %s\r\n\r\n", cmd, cb_user, cb_pass, cb_game, cb_hostname);
    
    if (cb_debug) printf("<<< %s\n-------------\n\n", cb_buff);
    
    status = send(cb_sock, cb_buff, strlen(cb_buff), 0);
    if (status == ERR) return cb_error("Could Not Request Web Page");
    
    status = recv(cb_sock, cb_buff, sizeof(cb_buff), 0);
    if (status == ERR) return cb_error("Could Not Receive Web Page");
  
    if (cb_debug) printf(">>> %s\n-------------\n\n", cb_buff);
      
    size = strstr(cb_buff, "MSG: ");
    if (!size) return cb_error("Malformed Server Response ID: 1");
    size += 5;
    
    msg = strchr(size, ' ');
    if (!msg) return cb_error("Malformed Server Response ID: 2");
    *msg = 0;
    
    len = atoi(size);
    memcpy(cb_reply, msg+1, len);
    cb_reply[len] = 0;
    
    return cb_reply;
}

int cb_cleanup() {
    return WSACleanup() == ERR;
}
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 22nd, 2008, 12:44 PM   #2
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Coder's Block Arena - The Game AI Platform

Python is now supported.

I made it more clear that the AI is being run on your own computer. This is not an experiment to find different AIs for games. It is an experiment to test your own AI against others.

I shouldn't have copied the FAQ here, since I've been making changes... The most up-to-date information can be found here:
http://codersblock.net/arena/tutorial.php

If you have any questions, please ask.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 23rd, 2008, 4:57 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Coder's Block Arena - The Game AI Platform

I'm afraid I created two accounts due to idiocy - forgot how to spell "Trogdor". Could you please delete one of them? Sorry 'bout that.

Also, I'd like to see exactly how the board is represented - was going to just play a game and forfeit but no one's on. Could you show me how?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 23rd, 2008, 9:06 AM   #4
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Coder's Block Arena - The Game AI Platform

Sure, no problem.

And it's mentioned very briefly in the tutorial. It's a string of 9 characters (Yes, I know there are more efficient ways to represent it. But if I gave it as an integer that would ruin half the fun ).

000010221

Which, from top-left to bottom-right is:

000
010
221

Which is finally:

---
-X-
OOX

You always play as X for simplicity (my server flips the board for the second player).

Out of curiosity, which API are you using?
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 4:05 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Coder's Block Arena - The Game AI Platform

I'm using the Python API... no real reason why really. Perhaps I've used C too much lately.

I've rewritten your tutorial code in Python - when I'm satisfied it works, I'll post it up here.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 24th, 2008, 11:37 AM   #6
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Coder's Block Arena - The Game AI Platform

Awesome. Thank-ya. Also looking forward to seeing that AI soon.

I've added a rankings page. Next up will be an individual page just on Tic-Tac-Toe, to include the game id name and the board representation. Then I will make individual pages for each API.

Andro is currently rewriting the API for Java. And we've got infinate and a few others currently writing their AIs.

I've already started making more games, including fun assignments from AI courses, and will remake the website into something that looks better after everything is fully functioning.



And there's the current logo. I'm going to make the site colourful and simple. I will also be asking some of my artistic friends to help me out with graphics. As you can tell, I'm not the most experienced graphic artist out there.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 4:15 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Re: Coder's Block Arena - The Game AI Platform

Here's a Java version of the API. It's packaged as a .jar file so just add it as a library resource to your project to use it.

You'll need the line:
import net.codersblock.api.CodersBlock;
at the top of the class you're trying to use it in if your IDE doesn't add it for you.

To use it just instantiate a new CodersBlock object and then it can be used as the tutorial specifies.
Attached Files
File Type: zip cbapi.zip (3.1 KB, 1 views)
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Mar 24th, 2008, 7:54 PM   #8
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Coder's Block Arena - The Game AI Platform

Andro's Java API has been added to the tutorial. The tutorial has also been updated to clear up confusion over some of the commands.

I'm curious. If there any lurkers about, I'd like to know your thoughts.

For what reasons don't you want to take part?
- Lack of time?
- Tic-Tac-Toe is too difficult?
- No API for your language?
- Don't understand how to use the API or it looks too difficult?
- Not interesting enough?
- Doesn't appear to be stable enough?
- You want a harder game?
- Needs to look like a "real" website?
- Waiting for more users?

I'd like to hear from you, because I'd like to make this a fun activity for programmers of all skill-levels. I'll take your suggestions for games to add in the future. I've already got some awesome ideas lined up, and will be adding more user rewards, pages, and more.

So any complaints? Comments?
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 9:05 PM   #9
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Re: Coder's Block Arena - The Game AI Platform

This is pretty damn cool and I'd really like to see some more AI's in the lobby
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Mar 24th, 2008, 9:12 PM   #10
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Coder's Block Arena - The Game AI Platform

Ooble! How could you make such a silly!

http://codersblock.net/arena/viewgame.php?match_id=226

__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visualizing How Game Development Would Take Place Josef_Stalin Coder's Corner Lounge 4 Mar 14th, 2008 11:52 AM
Text based game jayme Existing Project Development 25 Dec 15th, 2005 8:26 AM
Text based game jayme C++ 12 Nov 22nd, 2005 12:03 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 8:25 PM
Programmers Needed! Online Game troy_eisert C++ 2 Jan 29th, 2005 1:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC