Tic Tac Toe AI with PyProcessing

207 views Asked by At

I didn't know what sort of title to give this post. I'm a new to python, and I'm 4 weeks into my first class. I wrote a Tic Tac Toe program with an AI, but the AI is weird. It behaves differently at my home PC than at my school PC. At school, the program just crashes once it become's the computers turn for a move. It doesn't even draw the boxes. At home the computer just doesn't move. My home computer is significantly better than my school computer by the way. Also it's running Windows 10 TP vs. Windows 7 at school. I showed my teacher and he couldn't tell me the problem. This is my code. Sorry it's all one file and it's all the code I have because I have no idea where the problem is. Also I'm using PyProcessing which is basically a python IDE that provides some simple ways for graphics. The setup() function runs automatically at the beginning and so does draw() but I'm not sure when or what causes the draw() function to run.

board = [[None, None, None],
         [None, None, None],
         [None, None, None]]
turn = 0
player_letter = ""
computer_letter = ""
game_ended = False

def setup():
    global game_ended
    global player_letter
    global turn
    global board
    global computer_letter
    size(300,300)
    drawBoard(board)
    drawLetters(board)
    turn = 0
    if turn == 0:
        player_letter = "X"
        computer_letter = "O"
    else:
        player_letter = "O"
        computer_letter = "X"

def check_win(board, letter):
    if board[0][0] == letter and board[0][1] == letter and board[0][2] == letter:
        return True
    elif board[1][0] == letter and board[1][1] == letter and board[1][2] == letter:
        return True
    elif board[2][0] == letter and board[2][1] == letter and board[2][2] == letter:
        return True
    elif board[0][0] == letter and board[1][0] == letter and board[2][0] == letter:
        return True
    elif board[0][1] == letter and board[1][1] == letter and board[2][1] == letter:
        return True
    elif board[0][2] == letter and board[1][2] == letter and board[2][2] == letter:
        return True
    elif board[0][0] == letter and board[1][1] == letter and board[2][2] == letter:
        return True
    elif board[2][0] == letter and board[1][1] == letter and board[0][2] == letter:
        return True
    else:
        return False
def run_player_turn(player_letter):
    global board
    if mousePressed and board[mouseY//100][mouseX//100] == None:
        board[mouseY//100][mouseX//100] = player_letter
        return True
    else:
        return False

def getPotentialWin(computer_letter, player_letter):
    global board
    check1 = [board[0][0], board[0][1], board[0][2]]
    check2 = [board[1][0], board[1][1], board[1][2]]
    check3 = [board[2][0], board[2][1], board[2][2]]
    check4 = [board[0][0], board[1][0], board[2][0]]
    check5 = [board[0][1], board[1][1], board[2][1]]
    check6 = [board[0][2], board[1][2], board[2][2]]
    check7 = [board[0][0], board[1][1], board[2][2]]
    check8 = [board[2][0], board[1][1], board[0][2]]
    checks = [check1, check2, check3, check4, check5, check6, check7, check8]

    cletters = 0
    pletters = 0
    letters = 0

    for check in checks:
        for letter in check:
            if letter != None:
                letters += 1
                if letter == computer_letter:
                    cletters += 1
                else:
                    pletters += 1
        if cletters == 2 and letters == 2:
            for letter in check:
                if letter == None:
                    return letter
    return None

def getPotentialLoss(computer_letter, player_letter):
    global board
    check1 = [board[0][0], board[0][1], board[0][2]]
    check2 = [board[1][0], board[1][1], board[1][2]]
    check3 = [board[2][0], board[2][1], board[2][2]]
    check4 = [board[0][0], board[1][0], board[2][0]]
    check5 = [board[0][1], board[1][1], board[2][1]]
    check6 = [board[0][2], board[1][2], board[2][2]]
    check7 = [board[0][0], board[1][1], board[2][2]]
    check8 = [board[2][0], board[1][1], board[0][2]]
    checks = [check1, check2, check3, check4, check5, check6, check7, check8]

    cletters = 0
    pletters = 0
    letters = 0

    for check in checks:
        for letter in check:
            if letter != None:
                letters += 1
                if letter == player_letter:
                    pletters += 1
                else:
                    cletters += 1
        if pletters == 2 and letters == 2:
            for letter in check:
                if letter == None:
                    return letter
    return None

def draw():
    global board
    global turn
    global player_letter
    global computer_letter
    if (not check_win(board, player_letter)) and (not check_win(board, computer_letter)):
        if turn == 0:
            if run_player_turn(player_letter):
                turn = 1
        else:
            if run_computer_turn(computer_letter):
                turn = 0
        drawLetters(board)


def get_starting_player():
    random_num = int(random(2))
    return random_num

def drawLetters(board):
    for row in range(len(board)):
        for col in range(len(board[0])):
            if board[row][col] != None:
                textSize(32)
                fill(0,0,0)
                text(board[row][col], col*100+25, row*100+75)

def drawBoard(board):
    for y in range(len(board)):
        for x in range(len(board[0])):
            rect(x * 100, y * 100, 100, 100)

def run_computer_turn(computer_letter):
    global board
    global turn
    global cHasGone
    global player_letter
    potentialLoss = getPotentialLoss(computer_letter, player_letter)
    potentialWin = getPotentialWin(computer_letter, player_letter)

    if potentialLoss != None:
        potentialLoss = computer_letter
        return True
    elif potentialWin != None:
        potentialWin = computer_letter
        return True
    else:
        found = False
        while not found:
            x = int(random(0, 3))
            y = int(random(0, 3))
            if board[x][y] == None:
                board[x][y] == computer_letter
                found = True
                return True

    return True

def checkAvailibleWin(row, col):
    print("Hi")

def getAdjacents(row, col):
    adjs = [[None, None, None, None, None],
            [None, None, None, None, None],
            [None, None, None, None, None],
            [None, None, None, None, None],
            [None, None, None, None, None]]
    row += 1
    col += 1
    adjs[row - 1][col - 1] = True;
    adjs[row - 1][col] = True;
    adjs[row - 1][col + 1] = True;
    adjs[row][col - 1] = True;
    adjs[row][col] = True;
    adjs[row][col + 1] = True;
    adjs[row + 1][col - 1] = True;
    adjs[row + 1][col] = True;
    adjs[row + 1][col + 1] = True;

    trueAdjs = [[None, None, None],
                [None, None, None],
                [None, None, None]]

    for y in adjs:
        for x in adjs:
            if((x > 0 and x < 3) and (y > 0 and y < 1)):
                trueAdjs[y-1][x-1] = True

    return trueAdjs
0

There are 0 answers