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

如何在基于嵌套列表的井字棋(Tic-Tac-Toe)中实现平局判定功能

修复井字棋平局判定逻辑问题

我明白你的问题了——当前代码的问题在于,你把平局判断放在了每一轮玩家落子后的胜负判断else分支里。这就导致只要当前玩家没赢,不管游戏还没走完多少步,程序都会立刻输出平局提示,这显然不符合逻辑!

问题根源

你的代码在玩家每一步落子后,检查该玩家是否获胜,如果没获胜就直接走else输出平局,但此时游戏可能才进行了3、4步,还有很多格子是空的,根本没到平局的条件。正确的平局逻辑应该是:当所有9个格子都被填满(也就是循环走完9次),且没有任何一方获胜时,才判定平局。

修改后的完整代码

player1 = str(input("What is player X's name?"))
print()
player2 = str(input("What is player O's name?"))
turn = 1
location = [
    [' ', ' ', ' '],
    [' ', ' ', ' '],
    [' ', ' ', ' '],
]

# 封装胜负判断函数,减少重复代码
def check_win(symbol):
    # 检查行
    for row in location:
        if row[0] == row[1] == row[2] == symbol:
            return True
    # 检查列
    for col in range(3):
        if location[0][col] == location[1][col] == location[2][col] == symbol:
            return True
    # 检查对角线
    if location[0][0] == location[1][1] == location[2][2] == symbol:
        return True
    if location[0][2] == location[1][1] == location[2][0] == symbol:
        return True
    return False

while turn <= 9:
    if turn % 2 == 1:
        print()
        print(f"It is currently {player1}'s turn")
        row = int(input("What row would you like to mark?")) - 1
        print()
        col = int(input(" What column would you like to mark?")) - 1
        print()
        # 检查位置是否被占用
        while location[row][col] != ' ':
            print(f"{player1} that spot has already been taken")
            print("Please pick again")
            row = int(input("What row would you like to mark?")) - 1
            print()
            col = int(input(" What column would you like to mark?")) - 1
            print()
        location[row][col] = "X"
        # 打印棋盘
        print(f"{location[0][0]}|{location[0][1]}|{location[0][2]}")
        print("-----")
        print(f"{location[1][0]}|{location[1][1]}|{location[1][2]}")
        print("-----")
        print(f"{location[2][0]}|{location[2][1]}|{location[2][2]}")
        # 检查是否获胜
        if check_win("X"):
            print(f"{player1} won the game!")
            turn = 10  # 跳出循环
        else:
            turn += 1
    else:
        print(f"It is currently {player2}'s turn")
        row = int(input("What row would you like to mark?")) - 1
        print()
        col = int(input(" What column would you like to mark?")) - 1
        print()
        # 检查位置是否被占用
        while location[row][col] != ' ':
            print(f"{player2} that spot has already been taken")
            print("Please pick again")
            row = int(input("What row would you like to mark?")) - 1
            print()
            col = int(input(" What column would you like to mark?")) - 1
            print()
        location[row][col] = "O"
        # 打印棋盘
        print(f"{location[0][0]}|{location[0][1]}|{location[0][2]}")
        print("-----")
        print(f"{location[1][0]}|{location[1][1]}|{location[1][2]}")
        print("-----")
        print(f"{location[2][0]}|{location[2][1]}|{location[2][2]}")
        # 检查是否获胜
        if check_win("O"):
            print(f"{player2} won the game!")
            turn = 10  # 跳出循环
        else:
            turn += 1

# 循环结束后判断是否平局(只有当没人获胜时才会走到这里)
if turn == 10:
    print("The game ended in a tie!")

关键修改点说明

  1. 移除每轮的平局判断:不再在玩家每一步落子后就判断平局,避免提前输出错误提示。
  2. 将平局判断移到循环结束后:当while循环正常走完9次(turn从1到9),说明所有格子都被填满且没有玩家获胜,此时再输出平局提示。
  3. 封装胜负判断函数:把重复的胜负检查逻辑写成check_win函数,让代码更简洁易维护,也减少了重复代码出错的概率。
  4. 优化位置占用检查:原来的代码只检查是否是对方的棋子,现在直接检查位置是否为空(location[row][col] != ' '),逻辑更严谨,不管是X还是O占用都能正确判断。

这样修改后,游戏就会在所有格子填满且无人获胜时,才会输出平局提示,完全符合预期逻辑啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:20:24