Я делаю игру Ti c -Ta c -Toe, и я пытаюсь сделать функцию, чтобы проверить, выиграл игрок или нет.
Это моя функция:
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!")
На доску можно сослаться с помощью num pad.
Вот поток моей игры, где выигрыш используется проверка:
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")
Я знаю, что, возможно, я слишком усложняю свой код, и поэтому он работает неправильно. Но прошло уже пару месяцев с тех пор, как я написал строку кода, и я пытаюсь обновить sh мой мозг.
Вот результат, который я получаю:
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)
Самая простая игра, правда? Я сбит с толку, потому что и не делают то, для чего я думал, что это было предназначено. Любая помощь?