将If语句转换为循环

2024-09-23 22:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个实践问题,在这个问题中,我们将把一个列表输入到一个函数参数中,它将表示一个tic-tac-toe板,并返回该板的结果。也就是说,X胜、O胜、平局或无(空字符串)。你知道吗

我已经解决了这个问题,但是我想知道是否有一种方法可以将我的算法操作成一个循环,因为有人建议使用一个循环来比较主对角线的每个元素和所有元素 元素,然后检查两条对角线。我是python新手,所以我的解决方案可能会比它需要的时间长一些。如何实现一个循环来检查tic-tac趾板的结果?你知道吗

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 ''

Tags: orand元素forreturnifticlist
1条回答
网友
1楼 · 发布于 2024-09-23 22:27:08

首先,在蒂克塔乔只有八种获胜的方法。你有九个比较和返回语句,所以一个是多余的。事实上,在进一步的检查中,你检查了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)

在你的代码中,你要么得到赢家,要么得到一个空的指示,如果没有一个。你知道吗

相关问题 更多 >