【问题标题】:Converting If statement to a loop将 If 语句转换为循环
【发布时间】:2026-01-20 05:55:02
【问题描述】:

我正在处理一个练习题,我们要在函数参数中输入一个列表,这将代表一个井字棋盘,并返回棋盘的结果。即 X 胜、O 胜、Draw 或 None(空字符串)。

我已经解决了,但我想知道是否有一种方法可以将我的算法操纵成一个循环,因为建议使用循环来比较主对角线的每个元素与所有 元素与其相交的行和列,然后检查两条对角线。我是 python 新手,所以我的解决方案可能比它需要的长一点。如何实现一个循环来检查井字游戏的结果?

def gameState (List):
    xcounter=0
    ocounter=0
    if List[0][0]==List[0][1]   and List[0][0]==List[0][2]:
        return List[0][0]
    elif List[0][0]==List[1][0] and List[0][0]==List[2][0]:
        return List[0][0]
    elif List[0][0]==List[1][1] and List[0][0]==List[2][2]:
        return List[0][0]
    elif List[1][1]==List[1][2] and List[1][1]==List[1][0] :
        return List[1][1]
    elif List[1][1]==List[0][1] and List[1][1]==List[2][1]:
        return List[1][1]
    elif List[1][1]==List[0][0] and List[1][1]==List[2][2]:
        return List[1][1]
    elif List[2][2]==List[2][0] and List[2][2]==List[2][1]:
        return List[2][2]
    elif List[2][2]==List[1][2] and List[2][2]==List[0][2]:
        return List[2][2]
    elif List[2][2]==List[1][1] and List[2][2]==List[0][0]:
        return List[2][2]
    for listt in List:
        for elm in listt:
            if elm=="X" or elm=="x":
                xcounter+=1
            elif elm=="O" or elm=="o":
                ocounter+=1
    if xcounter==5 or ocounter==5:
        return "D"
    else:
        return ''

【问题讨论】:

  • 您是否尝试过使用循环?你有什么问题?此外,List 是一个错误的变量名,因为它也是 Python 内置函数,在某些时候使用这样的内置函数会给您带来问题。
  • 这是“工作”代码吗?如果是这样,请在Code Review 询问
  • 看起来您在使用嵌套的for 语句时已经实现了一个循环。
  • @sudo_coffee 是的!但是建议使用循环将主对角线的每个元素与其相交的行和列的所有元素进行比较,然后检查两个对角线。坦率地说,这些指示让我迷失了方向。我不明白我们如何使用循环,因为我们每次总是处于非常不同的状态

标签: python loops for-loop while-loop


【解决方案1】:

首先,在井字游戏中获胜的方法只有 八种。你有九个比较和返回语句,所以一个是多余的。事实上,在进一步检查中,您检查00, 11, 22 三次 次(案例 3、6 和 9)并且完全错过 02, 11, 20 案例。

就循环检查而言,您可以将行/列检查从对角线中拆分出来,如下所示:

# Check all three rows and columns.

for idx in range(3):
    if List[0][idx] != ' ':
        if List[0][idx] == List[1][idx] and List[0][idx] == List[2][idx]:
            return List[0][idx]
    if List[idx][0] != ' ':
        if List[idx][0] == List[idx][1] and List[idx][0] == List[idx][2]:
            return List[idx][0]

# Check two diagonals.

if List[1][1] != ' ':
    if List[1][1] == List[0][0] and List[1][1] == List[2][2]:
        return List[1][1]
    if List[1][1] == List[0][2] and List[1][1] == List[2][0]:
        return List[1][1]

# No winner yet.

return ' '

请注意,这可确保一排空单元格不会立即被无人认领。您只需要检查“真实”玩家是否获胜。我的意思是,您不想检测第一行中的三个空单元格并根据第二行是否有 实际 获胜者返回指示。


当然,有很多方法可以重构此类代码,使其更易于阅读和理解。一种方法是分离出检查单个行的逻辑,然后为每一行调用它:

# Detect a winning line. First cell must be filled in
#   and other cells must be equal to first.

def isWinLine(brd, x1, y1, x2, y2, x3, y3):
    if brd[x1][y1] == ' ': return False
    return brd[x1][y1] == brd[x2][y2] and brd[x1][y1] == brd[x3][y3]

# Get winner of game by checking each possible line for a winner,
#   return contents of one of the cells if so. Otherwise return
#   empty value.

def getWinner(brd):
    # Rows and columns first.

    for idx in range(3):
        if isWinLine(brd, idx, 0, idx, 1, idx, 2): return brd[idx][0]
        if isWinLine(brd, 0, idx, 1, idx, 2, idx): return brd[0][idx]

    # Then diagonals.

    if isWinLine(brd, 0, 0, 1, 1, 2, 2): return brd[1][1]
    if isWinLine(brd, 2, 0, 1, 1, 0, 2): return brd[1][1]

    # No winner yet.

    return ' '

然后你可以使用:

winner = getWinner(List)

在您的代码中,您将获得获胜者,如果没有,您将获得一个空的指示。

【讨论】: