-1

As I just said, I'm writing a Tic-Tac-Toe game and an error ocurred that I'm still not able to fix. The Error itself involves the check of if the player has won. It doesn't show an explicit error message, but it just doesn't work how I wanted it to.

I first made a list of all the different winning possibillities. They look something like this:

gridwintest = [[True, True, True,
                None, None, None,
                None, None, None],

               [None, None, None,
                True, True, True,
                None, None, None],
               
               [None, None, None,
                None, None, None,
                True, True, True],
               
               
               
               [True, None, None,
                True, None, None,
                True, None, None],
               
               [None, True, None,
                None, True, None,
                None, True, None],
               
               [None, None, True,
                None, None, True,
                None, None, True],
               
               
               
               [True, None, None,
                None, True, None,
                None, None, True],
               
               [None, None, True,
                None, True, None,
                True, None, None],
               ]

The function that handles them looks like this:

def check_grid(grid,check):
    global gridwintest
    
    gridvl = []
    for item in grid:
        if item == check:
            gridvl.append(True)
        else:
            gridvl.append(None)
    correct = True
    for chkgrid in gridwintest:
        for i in range(len(chkgrid)-1):
            if not (chkgrid[i] == gridvl[i] or chkgrid[i] == None):
                correct = False
    return correct

The code that uses that function is:

if check_grid(gridvl, invcurrmode):
        print("You won, "+currmode+"!")
        quit()
    elif check_grid(gridvl, currmode):
        print("Try again, "+currmode+".")
        quit()

But the return of the "check_grid" function is always false, even though if I logged the outputs of each line inside that function, it seems like it should actually be true.

Guillaume G
  • 313
  • 1
  • 2
  • 15
Byer_20
  • 9
  • 3

1 Answers1

1

You can simplify a bit by reducing

    correct = True
    for chkgrid in gridwintest:
        for i in range(len(chkgrid)-1):
            if not (chkgrid[i] == gridvl[i] or chkgrid[i] == None):
                correct = False

to

    for chkgrid in gridwintest:
        if chkgrid == gridvl:
                return True
    return False

Because Python allows ordered list comparison with ==.

And that should solve your bug too. not (chkgrid[i] == None) will evaluate to True on any of the three True values in each winning grid.

EDIT: A moderator posted an excellent link in a comment above. I would highlight its bottom line: "write the specification, test cases, preconditions, postconditions and assertions for a method before you write the body of the method".

tehCheat
  • 333
  • 2
  • 8
  • The issue with that is if I don't EXCACTLY have 3 in a row (so [True,True,True,None,None,None,None,None,None]) it will count as nothing – Byer_20 Dec 23 '22 at 17:02
  • Sorry, not sure what you mean. Could you clarify, or provide an example? Or, even better, verify the behavior by rolling that hypothesis into a test? – tehCheat Dec 23 '22 at 17:09