尝试重新启动井字游戏时,为什么会出现此错误?

时间:2019-05-20 02:40:45

标签: python python-3.x

我是第一学期的学生,我正在尝试重新开始比赛。问题是即使游戏运行正常,我也不知道该如何做。我以为解决方案是再次调用函数play(),但终生难倒!

试图创建一个while循环以尝试在仍然玩游戏的同时循环游戏

while playing == True:

    play()
    continue_play = input("Continue playing? (y/n)")
    if continue_play.lower() == "n":
        playing = False
    else:
        new_game = play()
        show_board()
        new_game()

这是while循环。...新游戏返回不可调用的“ nonetype”。 下面是我整个游戏的实际代码:

playing = True


#Global variables to be used
game_active = True
champion = None
active_player = " X "

#Displays board
def show_board():
    print("     1     2     3")
    print("1   " + board[0] + " | " + board[1] + " | " + board[2])
    print("    ----+-----+----")
    print("2   " + board[3] + " | " + board[4] + " | " + board[5])
    print("    ----+-----+----")
    print("3   " + board[6] + " | " + board[7] + " | " + board[8])

#play game of TIC TAC TOE
def play():
    global board
    board = ["   ", "   ", "   ",
             "   ", "   ", "   ",
             "   ", "   ", "   "]
#display the game board to users
    show_board()
#while game is still active
    while game_active:
#Whos turn is it?
        turns(active_player)
#check if game has met finishing requirements
        check_game_done()
#Change player turn
        change_player()
#game has ended
    if champion == " X " or champion == " O ":
        print(champion + " Is the winner!")
    elif champion == None:
        print(" Draw ")


#function for handling player turns
def turns(player):
    print(player + "'s Turn.")
    player_position = input("Please choose a number for your move between 1-9: ")

    ok = False
    while not ok:

        while player_position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
            player_position = input(" Please choose a number for your move between 1-9: ")

        player_position = int(player_position) - 1

        if board[player_position] == "   ":
            ok = True
        else:
            print("Sorry this spot is taken.")

    board[player_position] = player

    show_board()

#function to check over game to see if game completed
def check_game_done():
    check_win()
    check_draw()

def check_win():
#using global variable from above code
    global champion
#rows
    winning_row = check_rows()
#columns
    winning_column = check_columns()
#diagonals
    winning_diagonal = check_diagonals()
    if winning_row:
#winner found
        champion = winning_row
    elif winning_column:
#winner found
        champion = winning_column
    elif winning_diagonal:
#winner found
        champion = winning_diagonal
    else:
#no winner found
        champion = None

def check_rows():
#call global variable to check if game still active
    global game_active
#check rows for winning condition
    row1 = board[0] == board[1] == board[2] != "   "
    row2 = board[3] == board[4] == board[5] != "   "
    row3 = board[6] == board[7] == board[8] != "   "
#winning conditions met?
    if row1 or row2 or row3:
        game_active = False
#who won?
    if row1:
        return board[0]
    elif row2:
        return board[3]
    elif row3:
        return board[6]
    else:
        return None


def check_columns():
#call global variable to check if game still active
    global game_active
#check columns for winning condition
    column1 = board[0] == board[3] == board[6] != "   "
    column2 = board[1] == board[4] == board[7] != "   "
    column3 = board[2] == board[5] == board[8] != "   "
#Win conditions met
    if column1 or column2 or column3:
        game_active = False
#who won
    if column1:
        return board[0]
    elif column2:
        return board[1]
    elif column3:
        return board[3]
    else:
        return None


def check_diagonals():
#call global variable to check if game still active
    global game_active
#check diagonals for winning condition
    diagonal_1 = board[0] == board[4] == board[8] != "   "
    diagonal_2 = board[2] == board[4] == board[6] != "   "
#win conditon met
    if diagonal_1 or diagonal_2:
        game_active = False
#who won
    if diagonal_1:
        return board[0]
    elif diagonal_2:
        return board[2]
    else:
        return None

        # Checking to see if conditions for a draw have been met

def check_draw():

    global game_active
    if "   " not in board:
        game_active = False
        return True
    else:
        return False

        # Using function to decide whos turn it is
def change_player():

    global active_player

    if active_player == " X ":
        active_player = " O "
    elif active_player == " O ":
        active_player = " X "

    return None

        # Restarting game again
while playing == True:

    play()
    continue_play = input("Continue playing? (y/n)")
    if continue_play.lower() == "n":
        playing = False
    else:
        new_game = play()
        show_board()
        new_game()


play()

1 个答案:

答案 0 :(得分:0)

将while循环更改为:

...

while playing == True:

    play()
    continue_play = input("Continue playing? (y/n)")
    if continue_play.lower() == "n":
        playing = False
    else:
        game_active = True
        champion = None
        play()
        show_board()

...

现在它可以工作了。

相关问题