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?