View Single Post
Old Mar 24th, 2008, 9:13 PM   #11
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

Well, my algorithm works... kinda. Initially I implemented it using "X", "O" and "-" and was surprised when it broke, but it's fine now. Still loses games sometimes, so I need to work out the bugs.

It uses trees: calculates every single path (though I can specify a maximum depth), algorithmically works out which is best by working out each win and lose path and giving each one a rank based on how soon it'll happen, and goes down the best one.

Here's the tutorial code in Python:

import cbapi
from time import sleep

def playCBArena():
	cb = cbapi.cb_arena(0)
	cb.login("Trogdor the Burninator", "********", "TIC-TAC-TOE");

	if cb.cmd("CHECK") != "OKAY":
		return cb.cleanup()

	print "Looking For Game ..."
	reply = cb.cmd("GETGAME")
	while reply == "WAIT":
		sleep(5)
		reply = cb.cmd("GETGAME")
	if reply == "":
		return cb.cleanup()

	print "Playing against", reply
	while True:
		print "Waiting for turn ..."
		reply = cb.cmd("GETBOARD")
		while reply == "WAIT":
			sleep(3)
			reply = cb.cmd("GETBOARD")

		if reply == "":
			return cb.cleanup()
		elif reply == "WIN" or reply == "LOSE" or reply == "TIE":
			print "Game over:", reply
			return cb.cleanup()

		# Make Move By Updating One Of The Pieces In 'reply'

		reply = cb.cmd(reply)
		if reply != "OKAY":
			print "Disqualified For Illegal Move Or Timed Out"
			return cb.cleanup()
		else:
			print "Made Move"

	return cb.cleanup()

if __name__ == "__main__":
	playCBArena()
__________________
Me :: You :: Them

Last edited by Ooble; Mar 24th, 2008 at 9:33 PM. Reason: Removed a couple things that are part of my code only.
Ooble is offline   Reply With Quote