Friday, January 8, 2016

Basic Tic-Tac-Toe

The first step in teaching a computer to play TTT is to program a basic TTT game. The idea is that I want to start off with randomly generated games from which the game will learn. This was a relatively simple thing to do, though I had to spend a bit of time reminding myself how to do simple things like initialize arrays and print to files, but I put all the pieces back together again and created a program that plays TTT at random and also exports a file that records the winner of the game followed by the sequence of moves. I didn't want to just output the final board because I wanted to be as robust as possible with the data. I can always process the sequences of moves to generate the final board if I wanted to.

The game board is set up with the 9 positions labeled 0 through 8, starting from the top left and working left-to-right, top-to-bottom. Player 1 is X and Player 2 is O. The game state is initialized with 0s and those values are changed as players make their moves. I have one command to display the current game state and another one to check for winners.

And that's really all there was to it. The code is below. Now I need to start thinking about how this information actually gets processed by a machine learning algorithm and see what I can make it do (or not make it do).
## Standard Tic Tac Toe game
##
## Positions
##
##  0 | 1 | 2
## ---+---+---
##  3 | 4 | 5
## ---+---+---
##  6 | 7 | 8
##
## Game state
##  - 9 element array
##  - 0 = empty
##  - 1 = X
##  - 2 = O

import random

##### Check for win
def checkWin(game):
    winner = 0
    # X win?
    if game[0]*game[1]*game[2] == 1 \
      or game[3]*game[4]*game[5] == 1 \
      or game[6]*game[7]*game[8] == 1 \
      or game[0]*game[3]*game[6] == 1 \
      or game[1]*game[4]*game[7] == 1 \
      or game[2]*game[5]*game[8] == 1 \
      or game[0]*game[4]*game[8] == 1 \
      or game[2]*game[4]*game[6] == 1:
        winner = 1
        print "X wins!"
    elif game[0]*game[1]*game[2] == 8 \
      or game[3]*game[4]*game[5] == 8 \
      or game[6]*game[7]*game[8] == 8 \
      or game[0]*game[3]*game[6] == 8 \
      or game[1]*game[4]*game[7] == 8 \
      or game[2]*game[5]*game[8] == 8 \
      or game[0]*game[4]*game[8] == 8 \
      or game[2]*game[4]*game[6] == 8:
        winner = 2
        print "O wins!"
    elif game[0]*game[1]*game[2]*game[3]*game[4]*game[5]*game[6]*game[7]*game[8] > 0:
        winner = -1
        print "Draw!"
    else:
        print "No winner yet."
    return winner

##### Display Game State
def dispGame(game):
    gameChar = []
    for i in range(0,9):
        if game[i] == 0:
            gameChar.append(" ")
        elif game[i] == 1:
            gameChar.append("X")
        elif game[i] == 2:
            gameChar.append("O")
        else:
            gameChar.append("Something bad happened!")
    print
    print " {} | {} | {}".format(gameChar[0], gameChar[1], gameChar[2])
    print "---+---+---"
    print " {} | {} | {}".format(gameChar[3], gameChar[4], gameChar[5])
    print "---+---+---"
    print " {} | {} | {}".format(gameChar[6], gameChar[7], gameChar[8])
    print

##### Main Program
game = [0]*9
move = []
turn = 1
winner = 0
dispGame(game)
while winner == 0:
    print "Player {}'s turn".format(turn)
    validMove = 0
    while validMove == 0:
        testMove = random.randint(0,8)
        if game[testMove] == 0:
            game[testMove] = turn
            validMove = turn
            move.append(testMove)
    if turn == 1:
        turn = 2
    else:
        turn = 1
    winner = checkWin(game)
    dispGame(game)
move.insert(0, winner)
f = open("TTTGames.txt", "a")
for item in move:
    f.write(str(item))
    f.write(" ")
f.write("\n")
f.close()

No comments:

Post a Comment