Python Tic Toc Toe | GAME无法RESTAR

时间:2017-04-22 18:35:10

标签: python-3.x tic-tac-toe

一切都在顺利进行,直到while循环中断并且else语句运行并询问玩家是否想要再次播放。

else:
        if input("Do you want to play again? Type Yes or No: ").lower() != 'no':
             game_tic()

当我再次致电game_tic()时,主板不会刷新。旧的举动仍然在显现。我想如果我打电话给game_tic()游戏就想重新开始。

请查看我的代码:

import os

x_moves = [None, None, None, None, None, None, None, None, None]
o_moves = [None, None, None, None, None, None, None, None, None]
numeros= [1,2,3,
       4,5,6,
       7,8,9]

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

def draw_map_choices():
    print("#" * 7)
    tile = "|{}"
    both_set = set(x_moves) | set(o_moves)
    for num in numeros:
        if num in both_set:
            if num == 3:
                if num in x_moves:
                    print(tile.format("X") + '|', end='\n')
                else:
                    print(tile.format("O") + '|', end='\n')

            elif num == 6:
                if num in x_moves:
                    print(tile.format("X") + '|', end='\n')
                else:
                    print(tile.format("O") + '|', end='\n')

            elif num == 9:
                if num in x_moves:
                    print(tile.format("X") + '|', end='\n')
                else:
                    print(tile.format("O") + '|', end='\n')
                print("#" * 7)
            else:
                if num in x_moves:
                    print(tile.format("X"), end='')
                else:
                    print(tile.format("O"), end='')

        else:
            if num == 3:
                print(tile.format(num)+'|',end='\n')

            elif num == 6:
                print(tile.format(num) + '|', end='\n')

            elif num == 9:
                print(tile.format(num) + '|')
                print("#" * 7)
            else:
                print(tile.format(num),end="")

def return_open():
    return sorted(set(numeros) - (set(x_moves) | set(o_moves)))

def player_X():
    clear_screen()
    draw_map_choices()
    print("You can pick from the following number positions: {}.\n".format(", ".join(str(x) for x in return_open())))
    x = int(input("Player X's turn: "))
    if x not in x_moves and x not in o_moves:
        x_moves[x-1] = x
    else:
        print("Pick a location that is open")

def player_O():
    clear_screen()
    draw_map_choices()
    print("You can pick from the following number positions: {}.\n".format(", ".join(str(x) for x in return_open())))
    o = int(input("Player O's turn: "))
    if o not in o_moves and o not in x_moves:
        o_moves[o-1] = o
    else:
        print("Pick a location that is open")


def check_if_won(xo_list):

    #Horizontal MAtch
    for n in range(0, 9, 3):
        if (numeros[n], numeros[n+1], numeros[n+2]) == (xo_list[n], xo_list[n+1], xo_list[n+2]):
            return True

    # Vertical MAtch
    for n in range(0, 3):
        if(numeros[n],numeros[n+3], numeros[n+6]) == (xo_list[n], xo_list[n+3], xo_list[n+6]):
            return True
    #Diagonal Check
    if (numeros[0], numeros[4], numeros[8]) == (xo_list[0], xo_list[4], xo_list[8]):
        return True
    if (numeros[2], numeros[4], numeros[6]) == (xo_list[2], xo_list[4], xo_list[6]):
        return True
    else:
        return False



def game_tic():
    juego = True
    num_turn = 1

    while juego:

        player_X()
        if check_if_won(x_moves):
            clear_screen()
            draw_map_choices()
            print("Player X Won! Congratulations!\n")
            juego = False

        elif num_turn == 5:
            clear_screen()
            draw_map_choices()
            print("Is a DRAW!\n")
            juego = False

        if juego:
            player_O()
            if check_if_won(o_moves):
                clear_screen()
                draw_map_choices()
                print("Player O Won! Congratulations!\n")
                juego = False

        num_turn += 1

    else:
        if input("Do you want to play again? Type Yes or No: ").lower() != 'no':
            game_tic()



clear_screen()
print("Let's Start Our Game of Tic Toc Toe!")
input("Press ENTER when you are ready!")
game_tic()

1 个答案:

答案 0 :(得分:1)

快速解决方案是在x_moves中重置o_movesgame_tic(),如下所示:

def game_tic():
    juego = True
    num_turn = 1
    global x_moves, o_moves
    x_moves = [None for i in range(0, 9)]
    o_moves = [None for i in range(0, 9)]
    ...