为啥我的井字游戏代码无法检测到有人中奖了?蟒蛇 3

Posted

技术标签:

【中文标题】为啥我的井字游戏代码无法检测到有人中奖了?蟒蛇 3【英文标题】:Why does my TicTacToe code fail to detect is someone has won? Python 3为什么我的井字游戏代码无法检测到有人中奖了?蟒蛇 3 【发布时间】:2022-01-04 00:15:24 【问题描述】:

我最近开始尝试编写一个井字游戏板,以测试我对在线学习的概念的理解。但是,我在代码中遇到了一些问题,不明白为什么它不能按预期工作。

    在尝试获取代码以打印获胜者时,我的代码无法按预期工作,并且没有根据我输入的值选择获胜者。有时代码运行并在某人已经赢了两回合后检测到获胜者,有时它甚至没有下过第 5 步就退出了代码。 下面是我处理的代码。它包括您不能在已经占据的位置输入 X 或 O 并打印玩家选择的 X 和 Os 的保险。我相信错误发生在代码的行()、列()和对角线()部分的某个地方。
 player = 0
 player_add = 1
 winner = None
 already_taken = []
 turns_taken = 0
 board = [
     "-", "-", "-",
     "-", "-", "-",
     "-", "-", "-"
]


def handle_turn(turn, turn_add):
    while turn % 2 == 0:
        position = int(input("Choose a position from 1-9 to add an \"X\": ")) - 1
        already_taken.append(position)
        board[position] = "X"
        turn += turn_add
        display_board()
        while already_taken.count(position) > 1:
            print("Position already taken.")
            board[position] = "O"
            display_board()
            already_taken.remove(position)
        turn += turn_add

    while turn % 2 == 1:
        position = int(input("Choose a position from 1-9 to add an \"O\": ")) - 1
        already_taken.append(position)
        board[position] = "O"
        turn += turn_add
        display_board()
        while already_taken.count(position) > 1:
            print("Position already taken.")
            board[position] = "X"
            display_board()
            already_taken.remove(position)
            turn += turn_add


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


def play_game():
    display_board()
    handle_turn(player, player_add)


def rows():
     global running
    if board[0] and board[1] and board[2] == board[0] and board[0] != "-":
        running = False
        return board[0]
    if board[3] and board[4] and board[5] == board[3] and board[3] != "-":
        running = False
        return board[3]
    if board[6] and board[7] and board[8] == board[6] and board[6] != "-":
        running = False
        return board[6]
    return


def columns():
    global running
    if board[0] and board[3] and board[6] == board[0] and board[0] != "-":
        running = False
        return board[0]
    if board[1] and board[4] and board[7] == board[1] and board[1] != "-":
        running = False
        return board[3]
    if board[2] and board[5] and board[8] == board[2] and board[2] != "-":
        running = False
        return board[6]
    return


def diagonals():
    global running
    if board[0] and board[4] and board[8] == board[0] and board[0] != "-":
        running = False
        return board[0]
    if board[2] and board[4] and board[6] == board[2] and board[2] != "-":
        running = False
        return board[3]
    return


def check_if_win():
    global winner
    # Rows
    row_winner = rows()
    # Columns
    column_winner = columns()
    # Diagonals
    diagonal_winner = diagonals()
    if row_winner:
        winner = row_winner
    elif column_winner:
        winner = column_winner
    elif diagonal_winner:
        winner = diagonal_winner
    else:
        winner = None


def check_if_game_over():
    check_if_win()


running = True
while running:
    play_game()
    check_if_game_over()
if winner == "X":
    print("Player " + str(winner) + " has won!")
elif winner == "O":
    print("Player " + str(winner) + " has won!")
elif winner is None:
    print("Whoops! Looks like you both loose.")

'''

【问题讨论】:

请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。 【参考方案1】:

你几乎是对的,但你打错了:

if board[0] and board[3] and board[6] == board[0] and board[0] != "-":

这应该是:

if board[0] == board[3] and board[6] == board[0] and board[0] != "-":

我会说,如果你解决所有这些问题。它可能会奏效——至少会好一点。 希望这教会了你不要过多地复制粘贴代码,因为你很容易一遍又一遍地复制相同的错误。

那么,发生了什么?好吧,如果你有if(board[0] and board[3]),它会检查board[0] 的计算结果是否为真,board[3] 的计算结果是否相同。 truefalse的实现方式是false = 0true正好是false的反面:true = !false。所以,您检查的内容几乎是“board[0] 为零吗?”不,那是true。所以,难怪你会感到困惑。

【讨论】:

以上是关于为啥我的井字游戏代码无法检测到有人中奖了?蟒蛇 3的主要内容,如果未能解决你的问题,请参考以下文章

从示例井字游戏开始的 Chromecast 应用程序 - 无法启动应用程序:请求失败

为啥按钮在点击时改变位置?

带有链表 C++ 的井字游戏

带有井字游戏的 Minimax 算法(但每个玩家只能有 3 个 tacs)

井字游戏中计算机的随机位置选择无法正常工作

我做了一个功能,在我的井字游戏中的每一个动作之后都会改变玩家,但似乎不起作用