You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python井字棋用布尔值切换回合时无报错异常退出问题求助

井字棋游戏BUG修复说明

问题根因

你代码中的两个while循环是串行执行结构:

  • 第一个while仅在player1turn == True时运行,玩家1完成落子后会将player1turn设为False,第一个循环直接退出
  • 程序接着进入第二个while循环,仅在player1turn == False时运行,玩家2完成落子后会将player1turn设为True,第二个循环条件不满足直接终止,程序就会走到最后打印Won后结束,自然无法继续交替回合。

修复方案

将两个玩家的落子逻辑整合到同一个游戏主循环内,每次循环根据player1turn的值判断当前落子方,修改后的完整可运行代码如下:

'''
Name : Tic Tac Toe
Purpose : A Simple Tic Tac Toe Game With 1 friend
Plan : Make the board with | and. and _
        Mark each box from 1-9
        Ask the user for input.
        Anything more than 9 or less than 1 will result in a message "Too Big" or "Too Small" and won't restart the game
        A tile already taken will result in a message"Already Taken" and won't restart the game.
        If possible, X or O option will be there.
'''

print("Welcome to the tic tac toe offline made by Command Blocks!")

print("Player 1 will play first as X")
print("Player 2 will play second as O")
player1turn = True
r1 = 1
r2 = 2
r3 = 3
r4 = 4
r5 = 5
r6 = 6
r7 = 7
r8 = 8
r9 = 9

def board():
    print("._______._______._______.\n|  ", r1, "  |  ", r2, "  |  ", r3, "  |\n|       |       |       |")
    print("._______._______._______.\n|  ", r4, "  |  ", r5, "  |  ", r6, "  |\n|       |       |       |")
    print("._______._______._______.\n|  ", r7, "  |  ", r8, "  |  ", r9, "  |\n|       |       |       |\n._______._______._______.")


a_p = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
board()
print("available places are:",a_p)
# 外层统一的游戏主循环,只要还有空位就一直运行
while a_p != []:
    if player1turn == True:
        # 玩家1落子逻辑
        y_m = input( "Player 1 please enter your input:\n")
        try:
            if y_m == "1":
                a_p.remove("1")
                r1 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "2":
                a_p.remove("2")
                r2 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "3":
                a_p.remove("3")
                r3 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "4":
                a_p.remove("4")
                r4 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "5":
                a_p.remove("5")
                r5 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "6":
                a_p.remove("6")
                r6 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "7":
                a_p.remove("7")
                r7 = "X"
                board()
                print("available places are:", a_p)
                player1turn = False
            elif y_m == "8":
                a_p.remove("8")
                r8 = "X"
                board()
                print("available places are", a_p)
                player1turn = False
            elif y_m == "9":
                a_p.remove("9")
                r9 = "X"
                board()
                print("available places are", a_p)
                player1turn = False
            else:
                print("Please enter a number from 1-9")
        except ValueError:
            print("That spot is already taken !")
    else:
        # 玩家2落子逻辑
        y_m = input( "Player 2 please enter your input:\n")
        try:
            if y_m == "1":
                a_p.remove("1")
                r1 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "2":
                a_p.remove("2")
                r2 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "3":
                a_p.remove("3")
                r3 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "4":
                a_p.remove("4")
                r4 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "5":
                a_p.remove("5")
                r5 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "6":
                a_p.remove("6")
                r6 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "7":
                a_p.remove("7")
                r7 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "8":
                a_p.remove("8")
                r8 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            elif y_m == "9":
                a_p.remove("9")
                r9 = "O"
                board()
                print("available places are:", a_p)
                player1turn = True
            else:
                print("Please enter a number from 1-9")
        except ValueError:
            print("That spot is already taken !")
print("Draw")

注:原代码最后直接打印Won不符合逻辑,这里修改为打印Draw(平局),你可以后续补充胜负判断逻辑,在有玩家连成三子时提前终止循环并输出获胜方。

内容的提问来源于stack exchange,提问作者Command_Blocks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 10:36:08