dandman3019 dandman3019 - 6 months ago
302 0

Have a guess

Python

Tic Tac Toe AI (Python)

Embed
"""
TIC-TAC-TOE AI thingy

"""

global a_list
a_list = [" ", " ", " "]
global b_list
b_list = [" ", " ", " "]
global c_list 
c_list = [" ", " ", " "]

global fa_list
fa_list = list()
global fb_list
fb_list = list()
global fc_list
fc_list = list()

def person_try():
    position_moved = False
    while position_moved == False:
        position_decided = input("Select your position to move: ")
        if position_decided[0] in "ABCabc" and position_decided[1] in "123":
            position_moved = True
            if check_position_ok(position_decided,"X") == True: 
                #Now call the print_tic_tac_toe() function
                print_tic_tac_toe()
                position_moved = True
                #ALL GOODS BRO
            else:
                position_moved = False
        else:
            print("Error. pls try agian.")

def print_tic_tac_toe():
	print("| " + str(a_list[0]) + " | " + str(a_list[1]) + " | " + str(a_list[2]) + " | ")
	print("| " + str(b_list[0]) + " | " + str(b_list[1]) + " | " + str(b_list[2]) + " | ")
	print("| " + str(c_list[0]) + " | " + str(c_list[1]) + " | " + str(c_list[2]) + " | ")
	print("-" * 20)

def check_position_ok(position_decided, mark):
    #This checks to see if the player can go there, and also puts it there. Returns if position has been made.
    if position_decided[0] == "A" or position_decided[0] == "a":
        index = (int(position_decided[1]) - 1)
        if a_list[index] == " ":
            if mark != "Null":
                a_list[index] = mark
            return_value = True
        else:
            return_value = False
            
    if position_decided[0] == "B" or position_decided[0] == "b":
        index = (int(position_decided[1]) - 1)
        if b_list[index] == " ":
            if mark != "Null":
                b_list[index] = mark
            return_value = True
        else:
            return_value = False
            
    if position_decided[0] == "C" or position_decided[0] == "c":
        index = (int(position_decided[1]) - 1)
        if c_list[index] == " ":
            if mark != "Null":
                c_list[index] = mark
            return_value = True
        else:
            return_value = False
    return return_value

def check_win():
    game_status = (False, "No one")
    if (a_list[0]=="X" and a_list[1]=="X" and a_list[2]=="X") or a_list[0]=="O" and a_list[1]=="O" and a_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (b_list[0]=="X" and b_list[1]=="X" and b_list[2]=="X") or b_list[0]=="O" and b_list[1]=="O" and b_list[2]=="O":
        if b_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (c_list[0]=="X" and c_list[1]=="X" and c_list[2]=="X") or c_list[0]=="O" and c_list[1]=="O" and c_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)
            
    if (a_list[0]=="X" and b_list[0]=="X" and c_list[0]=="X") or a_list[0]=="O" and b_list[0]=="O" and c_list[0]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (a_list[1]=="X" and b_list[1]=="X" and c_list[1]=="X") or a_list[1]=="O" and b_list[1]=="O" and c_list[1]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (a_list[2]=="X" and b_list[2]=="X" and c_list[2]=="X") or a_list[2]=="O" and b_list[2]=="O" and c_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)



    if (a_list[0]=="X" and b_list[1]=="X" and c_list[2]=="X") or a_list[0]=="O" and b_list[1]=="O" and c_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

        if (a_list[2]=="X" and b_list[1]=="X" and c_list[0]=="X") or a_list[2]=="O" and b_list[1]=="O" and c_list[0]=="O":
            if a_list[0]=="X":
                person_won = "Player"
            else:
                person_won = "Computer_rek"
                game_status = (True,person_won)


    return game_status

def check_position_ok():
     if position_decided[0] == "A" or position_decided[0] == "a":
        index = (int(position_decided[1]) - 1)
        if a_list[index] == " ":
            a_list[index] = mark
            return_value = True
        else:
            return_value = False

    if position_decided[0] == "B" or position_decided[0] == "b":
        index = (int(position_decided[1]) - 1)
        if b_list[index] == " ":
            b_list[index] = mark
            return_value = True
        else:
            return_value = False
            
    if position_decided[0] == "C" or position_decided[0] == "c":
        index = (int(position_decided[1]) - 1)
        if c_list[index] == " ":
            c_list[index] = mark
            return_value = True
        else:
            return_value = False
    return return_value

def computer_check_win():
 
    game_status = (False, "No one")
    if (a_list[0]=="X" and fa_list[1]=="X" and fa_list[2]=="X") or fa_list[0]=="O" and fa_list[1]=="O" and fa_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (b_list[0]=="X" and fb_list[1]=="X" and fb_list[2]=="X") or fb_list[0]=="O" and fb_list[1]=="O" and fb_list[2]=="O":
        if b_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (c_list[0]=="X" and fc_list[1]=="X" and fc_list[2]=="X") or fc_list[0]=="O" and fc_list[1]=="O" and fc_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)
            
    if (a_list[0]=="X" and fb_list[0]=="X" and fc_list[0]=="X") or fa_list[0]=="O" and fb_list[0]=="O" and fc_list[0]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (a_list[1]=="X" and fb_list[1]=="X" and fc_list[1]=="X") or fa_list[1]=="O" and fb_list[1]=="O" and fc_list[1]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

    if (a_list[2]=="X" and fb_list[2]=="X" and fc_list[2]=="X") or fa_list[2]=="O" and fb_list[2]=="O" and fc_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)


    if (a_list[0]=="X" and fb_list[1]=="X" and fc_list[2]=="X") or fa_list[0]=="O" and fb_list[1]=="O" and fc_list[2]=="O":
        if a_list[0]=="X":
            person_won = "Player"
        else:
            person_won = "Computer_rek"
            game_status = (True,person_won)

        if (a_list[2]=="X" and fb_list[1]=="X" and fc_list[0]=="X") or fa_list[2]=="O" and fb_list[1]=="O" and fc_list[0]=="O":
            if a_list[0]=="X":
                person_won = "Player"
            else:
                person_won = "Computer_rek"
                game_status = (True,person_won)
    return game_status
    

def computer_rek():
        
        
    ## See if the player can win the game.
    ## H11
    fa_list = [a_list[0]] + [a_list[1]] + [a_list[2]]
    fb_list = [b_list[0]] + [b_list[1]] + [b_list[2]]
    fc_list = [c_list[0]] + [c_list[1]] + [c_list[2]]
    
    letter_position = "A" #This is the position horrizontally... Assigned by letters.. A_ B_ C_
    number_posiiion = 1 #This is the vertial position of the tic / tac assigned by numbers A1 B2 C3.... 
    letter_position_list = ["A", "B", "C"]
    number_position_list = [0, 1, 2]
    opposition = "X"
    for letter in letter_position_list:
        for number in number_position_list:
            if letter == "A":
                if fa_list[number] ==" ":
                    #make move
                    game_status = computer_check_win()
                    if game_status[0] == True:
                        #GAME HAS BEEN WON
                        
                                



    #MAKE MOVE HERE


        
        if position_decided(, "Null")
    
def main():
    print("Working")
    for value in range(0,10):
        person_try()
        print("Yolo")
        win = check_win()
        computer_rek()
main()
Comments