pher
pher

Reputation: 11

Why is this boolean not true even though the conditions are met?

print("Tic Tac Toe created by Pher")
row_a = ["□","□","□"]
row_b = ["□","□","□"]
row_c = ["□","□","□"]

def ResetGame():
    row_a = ["□","□","□"]
    row_b = ["□","□","□"]
    row_c = ["□","□","□"]

def DrawBoard():
    print()
    print("  0 1 2")
    print("A "+row_a[0]+" "+row_a[1]+" "+row_a[2])
    print("B "+row_b[0]+" "+row_b[1]+" "+row_b[2])
    print("C "+row_c[0]+" "+row_c[1]+" "+row_c[2])

def VictoryCheck():
    #checking for x wins
    #hortizontal checks
    if row_a[0] == row_a[1] == row_a[2] == "x":
        x_Victory = True
    if row_b[0] == row_b[1] == row_b[2] == "x":
        x_Victory = True
    if row_c[0] == row_c[1] == row_c[2] == "x":
        x_Victory = True
    #vertical checks
    if row_a[0] == row_b[0] == row_c[0] == "x":
        x_Victory = True
    if row_a[1] == row_b[1] == row_c[1] == "x":
        x_Victory = True
    if row_a[2] == row_b[2] == row_c[2] == "x":
        x_Victory = True
    #diagonal checks
    if row_a[0] == row_b[1] == row_c[2] == "x":
        x_Victory = True
    if row_a[2] == row_b[1] == row_c[0] == "x":
        x_Victory = True
    else:
        x_Victory = False
   
    
    #checking for o wins
    #hortizontal checks
    if row_a[0] == row_a[1] == row_a[2] == "o":
        o_Victory = True
    if row_b[0] == row_b[1] == row_b[2] == "o":
        o_Victory = True
    if row_c[0] == row_c[1] == row_c[2] == "o":
        o_Victory = True
    #vertical checks
    if row_a[0] == row_b[0] == row_c[0] == "o":
        o_Victory = True
    if row_a[1] == row_b[1] == row_c[1] == "o":
        o_Victory = True
    if row_a[2] == row_b[2] == row_c[2] == "o":
        o_Victory = True
    #diagonal checks
    if row_a[0] == row_b[1] == row_c[2] == "o":
        o_Victory = True
    if row_a[2] == row_b[1] == row_c[0] == "o":
        o_Victory = True
    else:
        o_Victory = False
    

def x_turn():
    print()
    x_input = input("X PLAYS: ")
    x_input = x_input.upper()
    x_row = str(x_input[0])
    x_col = int(x_input[1])

    if x_row == "A":
        row_a[x_col] = str("x")
        DrawBoard()
    if x_row == "B":
        row_b[x_col] = str("x")
        DrawBoard()
    if x_row == "C":
        row_c[x_col] = str("x")
        DrawBoard()

def o_turn():
    print()
    o_input = input("O PLAYS: ")
    o_input = o_input.upper()
    o_row = str(o_input[0])
    o_col = int(o_input[1])

    if o_row == "A":
        row_a[o_col] = str("o")
        DrawBoard()
    if o_row == "B":
        row_b[o_col] = str("o")
        DrawBoard()
    if o_row == "C":
        row_c[o_col] = str("o")
        DrawBoard()

ResetGame()
DrawBoard()

x_Victory = False
o_Victory = False
 

for x in range(1,10):
    VictoryCheck()
    if x_Victory == True:
        print("X HAS WON")
    if o_Victory == True:
        print("X HAS WON")
    if x % 2 == 0:
        o_turn()
        VictoryCheck()  
    if not x % 2 == 0:
        x_turn()
        VictoryCheck()

I'm very new to python so dont judge the sloppy code/terminology. The function VictoryCheck is called and the criteria is met in order for the boolean variable x_Victory to become true yet

if x_Victory == True:
        print("X HAS WON")

doesnt run the program kind of sits there Does anyone know what the problem could possibly be im willing to give any more info if needed

Upvotes: 0

Views: 58

Answers (2)

Leandro Esteban
Leandro Esteban

Reputation: 89

Two things:

1- You need to set x_Victory and o_Victory as global variables.

2- When you use conditionals and check for multiple possible instances, you need to use elif instead of multiple ifs or remove the else conditioning. When you do the diagonal checks:

    if row_a[0] == row_b[1] == row_c[2] == "x":
        x_Victory = True
    if row_a[2] == row_b[1] == row_c[0] == "x":
        x_Victory = True
    else:
        x_Victory = False

That else isn't considering what happens if none of the cases above stand, but only if the last condition (A2, B1 and C0) happen to be X, so if it doesn't count as True then it will override all other possibilities of it being True before.

The cleanest way would be to remove the else line. Same rule applies to o_Victory.

Upvotes: 0

Jan
Jan

Reputation: 43169

In your programming logic, you need to make x_Victory global:

def VictoryCheck():
    global x_Victory
    # ^^^

    #checking for x wins
    #hortizontal checks
    ...

Otherwise, x_Victory only exists within the scope of VictoryCheck() (is shadowed) and your global variable is being left untouched. Global variables tend to "pollute" your namespace though. You may better return the variables or use a class.

Upvotes: 2

Related Questions