if first == 'computer'://If its the computer's turn first in the game
move = chooseRandomMoveFromList(board, [1, 3, 7, 9])\\Choose a random corner move.
if move!= None://If you can move there
return move//Do it!
if isSpaceFree(board, 5)://If you can go in the middle
return 5//Do it!
return chooseRandomMoveFromList(board, [2, 4, 6, 8])//Otherwise go a random spot not in the middle or on the corners
else://If player starts
if isSpaceFree(board, 5)://If you can go in the middle
return 5//Do it!
if board[5] == computerLetter://If computer has gone in the middle
move = chooseRandomMoveFromList(board, [2, 4, 6, 8])
if move != None:
return move
return chooseRandomMoveFromList(board, [1, 3, 7, 9])
else:
move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
if move!= None:
return move
return chooseRandomMoveFromList(board, [2, 4, 6, 8])
This is some code I made in python for an unbeatable tic tac toe ai. Hope you can work it out . Doesn't always win but it never loses.