Я сделал функцию, которая меняет игрока после каждого хода в моей игре ti c ta c toe, но, похоже, она не работает - PullRequest
1 голос
/ 25 апреля 2020

Таким образом, я сделал игру ti c ta c toe и сделал функцию marker_change(), чтобы менять игрока после каждого хода, но когда я запускаю игру, маркер не меняется, он остается прежним после каждого хода. Вот мой код, извините, если мой код беспорядок, ребята, но я буквально много чего пытался решить:

board = [" "]*10
marker = None
def display_board(board):

    print("""
▀█▀ █ █▀▀   ▀█▀ ▄▀█ █▀▀   ▀█▀ █▀█ █▀▀
░█░ █ █▄▄   ░█░ █▀█ █▄▄   ░█░ █▄█ ██▄

        █▀▀ ▄▀█ █▀▄▀█ █▀▀
        █▄█ █▀█ █░▀░█ ██▄""")
    print("\n"*1)
    print(board[7]+"|"+board[8]+"|"+board[9])
    print("-----")
    print(board[4]+"|"+board[5]+"|"+board[6])
    print("-----")
    print(board[1]+"|"+board[2]+"|"+board[3])

def place_marker(board):
    global marker
    marker = " "

    while marker != "x" and marker != "o":
        marker = input("Player 1, choose x or o: ")

    player1 = marker    

    if player1 == "x":
        player2 = "o"       
        print("""   
                |------------------------------------|
                | Player 1 is now X and player 2 is O|   
                |------------------------------------|  
                                                     """)       
    else:
        player2 = "x"  
        print("""   
                |------------------------------------|
                | Player 1 is now O and player 2 is X| 
                |------------------------------------|
                                                      """)      

#Input of the player
    count = 0   
    for a in range(10):
        count += 1

        display_board(board)

        position = int(input("Choose a number between 1-9: "))

        board[position] = marker


        if board[1] == board[2] == board[3] != ' ':

            win_check = str(board[1] + " HAS WON !!!")
            print(win_check)
            break

        elif board[1] == board[5] == board[9] != ' ':

            variabila1 = str(board[1] + " HAS WON !!!")
            print(variabila1)
            break

        elif board[7] == board[8] == board[9] != ' ':

            variabila2 = str(board[7] + " HAS WON !!!")
            print(variabila2)
            break

        elif board[7] == board[5] == board[3] != ' ':

            variabila3 = str(board[7] + " HAS WON !!!")
            print(variabila3)
            break
        elif board[1] == board[4] == board[7] != ' ':

            variabila4 = str(board[1] + " HAS WON !!!")
            print(variabila4)
            break

        elif board[3] == board[6] == board[9] != ' ':

            variabila5 = str(board[3] + " HAS WON !!!")
            print(variabila5)
            break
        elif count == 9:
            print("THIS IS A TIE")





    return(a)
    return (player1,player2)
place_marker(board)

def marker_change():
    global marker
    if marker == "x":
        marker = "o"
        print("Now it's o's turn")
    else:
        print("Now it's x's turn")
marker_change()






#restart function
def clear_board(board):


    restart = input("Would you like to play again?:\nyes/no: ")
    if restart == "yes":
        board = [" "]*10
        place_marker(board)
        clear_board(board)
    else:
        restart == "no"
        print("\n"*100)
        print("""
▀▀█▀▀ █──█ █▀▀█ █▀▀▄ █─█ █▀▀   ░█▀▀▀ █▀▀█ █▀▀█   ░█▀▀█ █── █▀▀█ █──█ ─▀─ █▀▀▄ █▀▀▀ 
─░█── █▀▀█ █▄▄█ █──█ █▀▄ ▀▀█   ░█▀▀▀ █──█ █▄▄▀   ░█▄▄█ █── █▄▄█ █▄▄█ ▀█▀ █──█ █─▀█ 
─░█── ▀──▀ ▀──▀ ▀──▀ ▀─▀ ▀▀▀   ░█─── ▀▀▀▀ ▀─▀▀   ░█─── ▀▀▀ ▀──▀ ▄▄▄█ ▀▀▀ ▀──▀ ▀▀▀▀""")

if __name__ == "__main__":
    clear_board(board)

Ответы [ 2 ]

0 голосов
/ 25 апреля 2020

Сохранение большей части вашего исходного кода

Следующие моды

  1. Исправлено marker_change, чтобы исправить предложение else
  2. Поместил place_marker в для l oop place_market
  3. Place_market show_board для выигрышей и ничьих

Код

board = [" "]*10
marker = None
def display_board(board):

    print("""
▀█▀ █ █▀▀   ▀█▀ ▄▀█ █▀▀   ▀█▀ █▀█ █▀▀
░█░ █ █▄▄   ░█░ █▀█ █▄▄   ░█░ █▄█ ██▄

        █▀▀ ▄▀█ █▀▄▀█ █▀▀
        █▄█ █▀█ █░▀░█ ██▄""")
    print("\n"*1)
    print(board[7]+"|"+board[8]+"|"+board[9])
    print("-----")
    print(board[4]+"|"+board[5]+"|"+board[6])
    print("-----")
    print(board[1]+"|"+board[2]+"|"+board[3])

def place_marker(board):
    global marker
    marker = " "

    while marker != "x" and marker != "o":
        marker = input("Player 1, choose x or o: ")

    player1 = marker    

    if player1 == "x":
        player2 = "o"       
        print("""   
                |------------------------------------|
                | Player 1 is now X and player 2 is O|   
                |------------------------------------|  
                                                     """)       
    else:
        player2 = "x"  
        print("""   
                |------------------------------------|
                | Player 1 is now O and player 2 is X| 
                |------------------------------------|
                                                      """)      

#Input of the player
    count = 0   
    for a in range(10):
        count += 1

        display_board(board)

        position = int(input("Choose a number between 1-9: "))

        board[position] = marker
        print('Hello  world-----', marker, a, count)

        if board[1] == board[2] == board[3] != ' ':

            win_check = str(board[1] + " HAS WON !!!")
            display_board(board)
            print(win_check)
            break

        elif board[1] == board[5] == board[9] != ' ':

            variabila1 = str(board[1] + " HAS WON !!!")
            display_board(board)
            print(variabila1)
            break

        elif board[7] == board[8] == board[9] != ' ':

            variabila2 = str(board[7] + " HAS WON !!!")
            display_board(board)
            print(variabila2)
            break

        elif board[7] == board[5] == board[3] != ' ':

            variabila3 = str(board[7] + " HAS WON !!!")
            display_board(board)
            print(variabila3)
            break
        elif board[1] == board[4] == board[7] != ' ':

            variabila4 = str(board[1] + " HAS WON !!!")
            display_board(board)
            print(variabila4)
            break

        elif board[3] == board[6] == board[9] != ' ':

            variabila5 = str(board[3] + " HAS WON !!!")
            display_board(board)
            print(variabila5)
            break
        elif count == 9:
            display_board(board)
            print("THIS IS A TIE")

        marker_change()

def marker_change():
    global marker
    print('inside marker_change', marker)
    if marker == "x":
        marker = "o"
        print("Now it's o's turn")
    else:
        marker = "x"
        print("Now it's x's turn")
    print('leaving market_change', marker)

#restart function
def clear_board(board, first_game = None):
    if first_game is None:
      first_game = True

    if first_game:
        restart = 'yes'
        first_ganme = False
    else:
      restart = input("Would you like to play again?:\nyes/no: ")

    if restart == "yes":
        board = [" "]*10
        place_marker(board)
        clear_board(board, first_game)#--bad practice for games in Python since it does not have tail recursion
                          # can easily make this into a while loop

    else:
        restart == "no"
        print("\n"*100)
        print("""
▀▀█▀▀ █──█ █▀▀█ █▀▀▄ █─█ █▀▀   ░█▀▀▀ █▀▀█ █▀▀█   ░█▀▀█ █── █▀▀█ █──█ ─▀─ █▀▀▄ █▀▀▀ 
─░█── █▀▀█ █▄▄█ █──█ █▀▄ ▀▀█   ░█▀▀▀ █──█ █▄▄▀   ░█▄▄█ █── █▄▄█ █▄▄█ ▀█▀ █──█ █─▀█ 
─░█── ▀──▀ ▀──▀ ▀──▀ ▀─▀ ▀▀▀   ░█─── ▀▀▀▀ ▀─▀▀   ░█─── ▀▀▀ ▀──▀ ▄▄▄█ ▀▀▀ ▀──▀ ▀▀▀▀""")

if __name__ == "__main__":
    clear_board(board)
0 голосов
/ 25 апреля 2020

Вы звоните marker_change() в конце игры, а не после каждого хода. Вы должны позвонить после if, где вы проверите, выиграл ли кто-то.

...