Python 将If语句转换为循环

Python 将If语句转换为循环,python,loops,for-loop,while-loop,Python,Loops,For Loop,While Loop,我正在处理一个实践问题,在这个问题上,我们将把一个列表输入到一个函数参数中,它将表示一个tic-tac趾板,并返回该板的结果。也就是说,X赢、O赢、平局或无(空字符串) 我已经解决了这个问题,但我想知道是否有一种方法可以将我的算法操作成一个循环,因为有人建议使用一个循环来比较主对角线的每个元素和所有元素 元素,然后检查两条对角线。我是python新手,所以我的解决方案可能比需要的要长一点。如何实现循环以检查tic tac趾板的结果 def gameState (List): xcount

我正在处理一个实践问题,在这个问题上,我们将把一个列表输入到一个函数参数中,它将表示一个tic-tac趾板,并返回该板的结果。也就是说,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 ''
首先,在蒂克塔克托只有八种获胜方式。您有九个比较和返回语句,因此一个是多余的。事实上,在进一步检查时,您检查了三次
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)
在您的代码中,您将获得赢家,或者如果没有赢家,您将获得一个空的指示。

首先,在Tictaoe只有八种赢家方式。您有九个比较和返回语句,因此一个是多余的。事实上,在进一步检查时,您检查了三次
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)

在您的代码中,您将获得赢家或空指示(如果没有)。您是否尝试过使用循环?你有什么问题?另外,
List
是一个不好的变量名,因为它也是Python内置的,在某些情况下使用这样的内置会给您带来问题。这是“工作”代码吗?如果是这样的话,请在上面询问。看起来您在使用嵌套的
for
语句时已经实现了一个循环。@sudo\u coffee Yes!但是,建议使用循环将主对角线的每个元素与其相交行和列的所有元素进行比较,然后检查两条对角线。坦率地说,这些指示让我不知所措。我不明白我们怎么能使用循环,因为我们每次都处于非常不同的索引中。你试过使用循环吗?你有什么问题?另外,
List
是一个不好的变量名,因为它也是Python内置的,在某些情况下使用这样的内置会给您带来问题。这是“工作”代码吗?如果是这样的话,请在上面询问。看起来您在使用嵌套的
for
语句时已经实现了一个循环。@sudo\u coffee Yes!但是,建议使用循环将主对角线的每个元素与其相交行和列的所有元素进行比较,然后检查两条对角线。坦率地说,这些指示让我不知所措。我不知道如何使用循环,因为我们每次都处于非常不同的索引中