0

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
Elyor
  • 5,396
  • 8
  • 48
  • 76
IntrepidPig
  • 110
  • 2
  • 12
  • 1
    You'll hardly get concrete help without some sort of error. We need to know what's failing. It seems, however, that you need to check both Python versions. If the program runs well in your machine but not on another, maybe the Python versions are not matching. If using the same version works, you need to try to figure out what went wrong. But first, try to get the error message. How are you running your script? Are you using the console? An IDE? You'll probably see an error message somewhere, depending on how you're running. Copy paste that message here so we can help. – André Fratelli Jul 20 '15 at 02:31
  • Well PyProcessing is basically a simple IDE, and I'm using that. Also, I don't get an error message the program just freezes until I close it and I can't elaborate on that right now because I don't have access to my school computer. At home I'm using the latest python version as of a a week ago, but I'm not sure about my school computers. I'm working in a sort of rejected uncared for computer lab. – IntrepidPig Jul 20 '15 at 02:35
  • 1
    If it just hangs than it seems like an infinite loop. At first sight, the only possibility for an infinite loop that I see, without crashing, is the `while` in `run_computer_turn`. Try to understand if that loop ends, and if it doesn't, why not. Also try [debugging your script](https://docs.python.org/2/library/pdb.html). – André Fratelli Jul 20 '15 at 02:40

0 Answers0