My tic tac toe evaluation function dosen't seem to work, can't find the issue

36 views Asked by At
import random

grid = [1, 2, 3, 4, 5, 6, 7, 8, 9]
lines = ['|']
end = False
win_commbinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
def draw():
    print(grid[0], lines[0], grid[1], lines[0], grid[2])
    print('----------')
    print(grid[3], lines[0] , grid[4], lines[0], grid[5])
    print('----------')
    print(grid[6], lines[0], grid[7],lines[0], grid[8])
    print()

def player1():
    n = choose_number()
    if grid[n] == "X" or grid[n] == "O":
        print("You can't go there. Try again")
        player1()
    else:
        grid[n] = "X"

def computer():
    x = random.randint(0,8)
    if(grid[x] == "X" or grid[x] == "O"):
        computer()
    else:
        if(grid[0] == 'X' or 'O' and grid[1] == 'X' or 'O'):
            grid[2] = "O"

        elif(grid[3] == 'X' or 'O' and grid[4] == 'X' or 'O'):
            grid[5] = "O"

        elif(grid[6] == 'X' or 'O' and grid[7] == 'X' or 'O'):
            grid[8] = "O"

        elif(grid[0] == 'X' or 'O' and grid[3] == 'X' or 'O'):
            grid[6] = "O"

        elif(grid[1] == 'X' or 'O' and grid[4] == 'X' or 'O'):
            grid[7] = "O"

        elif(grid[2] == 'X' or 'O' and grid[5] == 'X' or 'O'):
            grid[8] = "O"

        elif(grid[0] == 'X' or 'O' and grid[4] == 'X' or 'O'):
            grid[8] = "O"

        elif(grid[2] == 'X' or 'O' and grid[4] == 'X' or 'O'):
            grid[6] = "O"

        else:
            grid[x] = "O"


def choose_number():
    while True:
        while True:
            a = input()
            try:
                a  = int(a)
                a -= 1
                if a in range(0, 9):
                    return a
                else:
                    print("That's not on the grid. Try again")
                    continue
            except ValueError:
                print("That's not a number. Try again")
                continue

def check_grid():
    count = 0
    for a in win_commbinations:
        if grid[a[0]] == grid[a[1]] == grid[a[2]] == "X":
            print("Player 1 Wins")
            print("Congrats")
            return True

        if grid[a[0]] == grid[a[1]] == grid[a[2]] == "O":
            print("computer Wins!")
            print("Better luck next time.")
            return True
    for a in range(9):
        if grid[a] == "X" or grid[a] == "O":
            count += 1
        if count == 9:
            print("The game ends in a Tie")
            return True

while not end:
    draw()
    end = check_grid()
    if end == True:
        break
    print("Player 1 choose where to place an x")
    player1()
    print()
    end = check_grid()
    if end == True:
        break
    computer()
    print()

The spacing might be a little off. I cant seem to get an algorithm that works, any suggestions aside from what I wrote? I also cant get my code to work it puts an O in the exact same spot each time.

0

There are 0 answers