首页 > 解决方案 > 制作井字游戏,代码完整,没有错误消息但不会运行?

问题描述

这是我的第一个项目,我在同一个项目中使用了其他人的大量资源,这就是我想出的。我正在使用 Jupyter 笔记本。我的代码中没有收到更多错误消息,但由于某种原因我无法让它运行?此外,我的代码中的任何建议或改进也将不胜感激。

我试图只调用 tic_tac_toe() 命令,但没有任何反应,我不知道为什么。

def tic_tac_toe():
    brd = [None] + list(range(1,10)) 
    end = False
    winner = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),    (3,5,7))
    from IPython.display import clear_output
    def show_board():
        print(brd[1]+'|'+brd[2]+'|'+brd[3])
        print(brd[4]+'|'+brd[5]+'|'+brd[6])
        print(brd[7]+'|'+brd[8]+'|'+brd[9])
        print()
    def player_input():
        marker = ''
        while marker != 'x' and marker != 'o':
            marker = input('Do you want to be x or o?: ')
        player1 = marker
        if player1 == 'x':
            player2 ='o'
        else:
            player2 = 'x'
        player_markers = [player1,player2]
    def choose_number():
         while True:
                try:
                    val = int(input())
                    if val in brd:
                        return val
                   else:
                        print('\n Please choose another number')
                except ValueError:
                    print('\n Please choose another number')
    def game_over():
        for a, b, c in winner:
            if brd[a] == brd[b] == brd[c]:
                print("{0} wins!\n".format(board[a]))
                print("Congrats\n")
                return True
        if 9 == sum((pos == 'x' or pos == 'o') for pos in board):
            print("The game ends in a tie\n")
            return True
        for player in 'x' or 'o' * 9:
            draw()
            if is_game_over():
                break
            print("{0} pick your move".format(player))
            brd[choose_number()] = player
            print()
    while True:
        tac_tac_toe()
        if input("Play again (y/n)\n") != "y":
            break

我不确定为什么它不能正常运行。

标签: pythontic-tac-toe

解决方案


您的代码在这里有一些问题。你的缩进。还想知道为什么你的函数都在另一个函数中。您还创建了一堆函数,但从不调用其中的大部分。并且有一些似乎不存在的功能。这里和那里也有很多逻辑错误。

试试这个:

# numpy is a package that has a lot of helpful functions and lets you manipulate
# numbers and arrays in many more useful ways than the standard Python list allows you to
import numpy as np

def show_board(brd):
    print(brd[0]+'|'+brd[1]+'|'+brd[2])
    print(brd[3]+'|'+brd[4]+'|'+brd[5])
    print(brd[6]+'|'+brd[7]+'|'+brd[8])
    print()

def player_input():
    marker = ''
    while marker != 'x' and marker != 'o':
        marker = input('Do you want to be x or o?: ')
    player1 = marker
    if player1 == 'x':
        player2 ='o'
    else:
        player2 = 'x'
    player_markers = [player1,player2]
    return player_markers

def choose_number(brd):
    while True:
        try:
            val = int(input())
            if brd[val-1] == "_":
                return val
            else:
                print('\nNumber already taken. Please choose another number:')
        except ValueError:
            print('\nYou did not enter a number. Please enter a valid number:')

def is_game_over(winner, brd):
    for a, b, c in winner:
        if brd[a] != "_" and (brd[a] == brd[b] == brd[c]):
            print("{} wins!\n".format(brd[a]))
            print("Congrats\n")
            return True
    if 9 == sum((pos == 'x' or pos == 'o') for pos in brd):
        print("The game ends in a tie\n")
        return True

# I split this function in two because the "is_game_over" code was included here
# instead of being by itself.         
def game_over(winner, brd, player_markers):
    last = 0
    # The first player is the one stored in "player_markers[0]"
    player = player_markers[0]
    # There are nine turns so that is what this is for. It has nothing to do with
    # 'x' or 'o'. And one more turn is added for the "is_game_over" to work in 
    # case of a tie.
    for i in range(10):
        if is_game_over(winner, brd):
            break
        print()
        print("{0} pick your move [1-9]:".format(player))
        brd[choose_number(brd)-1] = player
        show_board(brd)
        # This is added to change from one player to another 
        # by checking who was the last one (look up ternary operators)
        player = player_markers[1] if last==0 else player_markers[0]
        last = 1 if last==0 else 0

def tic_tac_toe():
    brd = ["_"] * 9 
    end = False
    winner = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))
    winner = np.array([list(elem) for elem in winner]) - 1
    player_markers = player_input()
    show_board(brd)
    game_over(winner, brd, player_markers)


while True:
    tic_tac_toe()
    if input("Play again (y/n)\n") != "y":
        break

推荐阅读