-3

I'm trying to write a function in my tic-tac-toe program that checks to see if all three spaces in a line on the grid have been marked as either 'x's or 'o's. lst_1 is a list of all the possible combinations of lines:

def conc():
    global lst
    lst_1 = [[lst[0],lst[1],lst[2]],[lst[3],lst[4],lst[5]],[lst[6],lst[7],lst[8]],[lst[0],lst[3],lst[6]],[lst[1],lst[4],lst[7]],    [lst[2],lst[5],lst[8]],[lst[0],lst[4],lst[8]],[lst[2],lst[4],lst[6]],    [lst[0],lst[1],lst[2]],[lst[3],lst[4],lst[5]],[lst[0],lst[3],lst[6]],     [lst[1],lst[4],lst[7]],[lst[2],lst[5],lst[8]],[lst[0],lst[4],lst[8]],    [lst[2],lst[4],lst[6]]]
    triplet = [s for s in lst_1]
     if all('o' == item for item in triplet):
        print('Player 2 wins!')
        reply()
    elif all('x' == item for item in triplet):
        print('Player 1 wins!')
        reply()
    else:
        pass 

I thought this would stop the game when someone managed to get 3 in a row, but it keeps on going. Any ideas?

Boris
  • 716
  • 1
  • 4
  • 25
  • I think you have some extra data in `lst_1` that's messing things up. – TigerhawkT3 Nov 30 '16 at 22:47
  • And your `all()` calls are missing some key parts as well. – TigerhawkT3 Nov 30 '16 at 22:51
  • Your indentation is off, which is critical in Python. Your variable `triplet` seems to be the same as `lst_1`. You do not tell us what `reply()` is or does. Please see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton Nov 30 '16 at 22:52

1 Answers1

1

To answer your question simply, you're not iterating through lst_1. I imagine you think you are by the statement you're using for triplet, but that actually just copies lst_1 to triplet.

I assume you're actually trying to make a generator out of lst_1 and assigning it to triplet, but you're accidentally using list comprehension instead and don't fully understand how generators work. Read more about generators here.

While generators are undeniably useful for certain situations, I don't think they'd actually simplify your code here. You're going to need a loop anyway, and you have to check for a StopIteration exception to find out when the generator ends.

So, my suggestion is just to use a for loop.

def conc():
    global lst
    lst_1 = [[lst[0],lst[1],lst[2]],[lst[3],lst[4],lst[5]],[lst[6],lst[7],lst[8]],[lst[0],lst[3],lst[6]],[lst[1],lst[4],lst[7]],    [lst[2],lst[5],lst[8]],[lst[0],lst[4],lst[8]],[lst[2],lst[4],lst[6]],    [lst[0],lst[1],lst[2]],[lst[3],lst[4],lst[5]],[lst[0],lst[3],lst[6]],     [lst[1],lst[4],lst[7]],[lst[2],lst[5],lst[8]],[lst[0],lst[4],lst[8]],    [lst[2],lst[4],lst[6]]]
    for triplet in lst_1:
        if all('o' == item for item in triplet):
            print('Player 2 wins!')
            reply()
            break
        elif all('x' == item for item in triplet):
            print('Player 1 wins!')
            reply()
            break

Some other nits: Adding else: pass to the end of your if-elif-else loop is completely extraneous. It doesn't have to be there.

Also, try to avoid global variables. Read up a bit about scope in Python. It might work for simple programs, but if you rely on them when you get to anything remotely complicated it'll hurt you.

Dodosaur
  • 26
  • 3
  • Thanks @Dodosaur. Why are global functions undesirable? – Boris Nov 30 '16 at 23:31
  • [This](http://stackoverflow.com/questions/19158339/why-are-global-variables-evil) answer explains it more eloquently than I can. – Dodosaur Nov 30 '16 at 23:45
  • Sorry, I hit enter and it submitted. The basic gist is that it makes code hard to understand, because if a function uses a global variable then there's something outside the scope of the function affecting it. – Dodosaur Nov 30 '16 at 23:46