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()