Мой код не проверяет, есть ли X и Y и Z в списке, но проходит, если только один покрыт. Что я делаю неправильно? - PullRequest
1 голос
/ 17 февраля 2020

Я делаю игру 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)

Самая простая игра, правда? Я сбит с толку, потому что и не делают то, для чего я думал, что это было предназначено. Любая помощь?

1 Ответ

3 голосов
/ 17 февраля 2020

Вам необходимо изменить условия так, чтобы они выглядели следующим образом:

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!")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...