1

I'm Making A Tic-Tac-Toe game, and I'm trying to make a function to check if a player has won or not.

This is my Function:

def win_check(playerpicks, player):
    #HORIZONTAL CHECK
    if 1 and 2 and 3 in playerpicks or 4 and 5 and 6 in playerpicks or 7 and 8 and 9 in playerpicks:
        print("Player " + str(player) + " Wins!")

   #Vertical Check
    elif 1 and 4 and 7 in playerpicks or 2 and 5 and 8 in playerpicks or 3 and 6 and 9 in playerpicks:
        print("Player " + str(player) +" Wins!")

    #Diagonal Check
    elif 1 and 5 and 9 in playerpicks or 3 and 5 and 7 in playerpicks:
        print("Player " + str(player) +" Wins!")

The board is going to be can be referenced by the num pad.

Here is the flow of my game where the win check is used:

def flow():

    global turn

    if turn == 1:
        position1 = input("P{} choose your position (1-9)".format(turn))

        if 9 >= int(position1) >= 1:
            if int(position1) in p1_list or int(position1) in p2_list:
                print("Spot Taken")
            else:
                p1_list.append(int(position1))
                win_check(p1_list, 1)
                turn = 2
                print(p1_list)
        else:
            print("INVALID INPUT")

    elif turn == 2:
        position2 = input("P{} choose your position (1-9)".format(turn))

        if 9 >= int(position2) >= 1:
            if int(position2) in p2_list or int(position2) in p1_list:
                print("Spot Taken")
            else:
                p2_list.append(int(position2))
                win_check(p2_list, 2)
                turn = 1
                print(p2_list)
        else:
            print("INVALID INPUT")

I know that I'm probably over complicating my code, and that's why its not working correctly. But it's been a couple months since I've written a line of code, and I'm trying to refresh my brain.

Here is the Output I'm getting:

Player 1 or player 2? (Enter 1 or 2)1
You are Player 1
P1 choose your position (1-9)2
[2]
P2 choose your position (1-9)3
Player 2 Wins!
[3]
P1 choose your position (1-9)

Easiest game ever right? I'm confused because and isn't doing what I thought it was meant for. Any help?

A.J.
  • 89
  • 6
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – AMC Feb 17 '20 at 03:53
  • As an aside, it’s probably good idea to refactor your program. I can’t see much of a reason to have functions here which print results instead of returning values. – AMC Feb 17 '20 at 03:55
  • yeah, I'm working pretty slowing trying to make this. As I'm very rusty. Going through this Udemy class again, and I'm just using the tools they've taught already. – A.J. Feb 17 '20 at 04:18

1 Answers1

3

You need to change your conditionals to be like the following:

if (1 in playerpicks and 2 in playerpicks and 3 in playerpicks) or \
   (4 in playerpicks and 5 in playerpicks and 6 in playerpicks) or \
   (7 in playerpicks and 8 in playerpicks and 9 in playerpicks):
    print("Player " + str(player) + " Wins!")
Dominic D
  • 1,778
  • 2
  • 5
  • 12
  • I get an error, "unindent does not match indentation level" when I past it in, and when i try to fix it. This is right after the print statement. Also, what are the backslashes for? – A.J. Feb 17 '20 at 04:05
  • 1
    The backslashes allow you to start a new line in your code, but python keeps reading it as if it were all one line. If you prefer you can put all the conditions on one line and remove the back slashes. – Dominic D Feb 17 '20 at 04:08
  • Oh got it, and i fixed the unrelated indent problem. Do you have time to explain why my original code was flawed? – A.J. Feb 17 '20 at 04:14
  • 1
    `and` and `or` are boolean operators, which means they work with values of `True` and `False`. ie. `True and True = True` `True and False = False` `True or False = True`. `1 in playerpicks` for example is a statement that will be either `True` or `False`, as you can imagine. `1 and 2 and 3 in playerpicks` is actually doing `(1) and (2) and (3 in playerpicks)`. 1 and 2 will convert to `True` (for numbers, 0 = `False`, anything else = `True`). So essentially, you were only checking if 3 was in playerpicks. – Dominic D Feb 17 '20 at 04:19
  • Much appreciated D – A.J. Feb 17 '20 at 04:26