如何在python中检查列或行(或对角线)是否都相等

2024-05-18 23:26:07 发布

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

我有一本字典:

squares = {
          'r1c1':{'location':[0,0,150,150],'status':'o'},
          'r1c2':{'location':[150,0,300,150],'status':None},
          'r1c3':{'location':[300,0,450,150],'status':None},
          'r2c1':{'location':[0,150,150,300],'status':'x'},
          'r2c2':{'location':[150,150,300,300],'status':'x'},
          'r2c3':{'location':[300,150,450,300],'status':'x'},
          'r3c1':{'location':[0,300,150,450],'status':None},
          'r3c2':{'location':[150,300,300,450],'status':None},
          'r3c3':{'location':[300,300,450,450],'status':'o'}
          }

忽略位置。R代表行;C代表列。我想知道是否有一个紧凑的函数,我可以用来检查是否所有的行,列,或对角线有相同的值。。。想想提克托。。。到目前为止,我有一个很大的假设声明:

def TicTacToe(self):
    #rows
    if self.squares['r1c1']['status'] == 'x' and self.squares['r1c2']['status'] == 'x' and self.squares['r1c3']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r2c1']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r2c3']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r3c1']['status'] == 'x' and self.squares['r3c2']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
        self.gameOver('x')

    elif self.squares['r1c1']['status'] == 'o' and self.squares['r1c2']['status'] == 'o' and self.squares['r1c3']['status'] == 'o':
        self.gameOver('o')
    elif self.squares['r2c1']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r2c3']['status'] == 'o':
        self.gameOver('o')
    elif self.squares['r3c1']['status'] == 'o' and self.squares['r3c2']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
        self.gameOver('o')
    #columns
    elif self.squares['r1c1']['status'] == 'x' and self.squares['r2c1']['status'] == 'x' and self.squares['r3c1']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r1c2']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c2']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r1c3']['status'] == 'x' and self.squares['r2c3']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
        self.gameOver('x')

    elif self.squares['r1c1']['status'] == 'o' and self.squares['r2c1']['status'] == 'o' and self.squares['r3c1']['status'] == 'o':
        self.gameOver('o')
    elif self.squares['r1c2']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c2']['status'] == 'o':
        self.gameOver('o')
    elif self.squares['r1c3']['status'] == 'o' and self.squares['r2c3']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
        self.gameOver('o')
    #diagonal
    elif self.squares['r1c1']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r1c3']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c1']['status'] == 'x':
        self.gameOver('x')
    elif self.squares['r1c1']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
        self.gameOver('o')
    elif self.squares['r1c3']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c1']['status'] == 'o':
        self.gameOver('o')

但这很难看,我想做得更好。有什么想法吗?。。。你知道吗


Tags: andselfstatuslocationsquareselifgameoverr1c2
3条回答

首先,把squares转换成更好的,比如

board = [[squares[f"r{row}c{col}"]["status"] for row in range(1, 4)] for col in range(1, 4)]

现在board是一个列表列表:

[[ 'o', 'x', None], 
 [None, 'x', None], 
 [None, 'x',  'o']]

您现在可以使用此问题的数千个代码示例中的任意一个:

def check_columns(board):
    for column in board:
        if len(set(column)) == 1 and column[0] is not None:
            return column[0]

def check_rows(board):
    return check_columns(zip(*reversed(board)))  # rotate the board 90 degrees

def check_diagonals(board):
    if ((board[0][0] == board[1][1] == board[2][2]) or
        (board[2][0] == board[1][1] == board[0][2])):
        if board[1][1] is not None:
            return board[1][1]

def who_won(board):
    for check in [check_columns, check_rows, check_diagonals]:
        result = check(board)
        if result is not None:
            return result

像这样使用:

>>> who_won(board)
'x'
>>> board[1][1] = 'o'
>>> who_won(board)
'o'
>>> board[1][1] = None
>>> who_won(board)
>>> 
>>> # nothing printed means it returned `None` (because there's no winner)

这里有一个NxN维正方形板的通用解决方案。你知道吗

squares = {
    'r1c1':{'location':[0,0,150,150],'status':'o'},
    'r1c2':{'location':[150,0,300,150],'status':None},
    'r1c3':{'location':[300,0,450,150],'status':None},
    'r2c1':{'location':[0,150,150,300],'status':'x'},
    'r2c2':{'location':[150,150,300,300],'status':'x'},
    'r2c3':{'location':[300,150,450,300],'status':'x'},
    'r3c1':{'location':[0,300,150,450],'status':None},
    'r3c2':{'location':[150,300,300,450],'status':None},
    'r3c3':{'location':[300,300,450,450],'status':'o'}
}
N = 3 # Assuming you have NxN dimensional board
def check_rows():
    # Check for all rows
    for i in range(N):
        row = 'r'+str(i+1)
        flag = True
        # Check if all value in current row are NOT 'x' or 'o'
        for j in range(N-1):
            cell1 = row+'c'+str(j+1)
            cell2 = row+'c'+str(j+2)
            if squares[cell1]['status'] != squares[cell2]['status']:
                flag = False
                break
        if flag: # All values are same
            print('Found match in',row)
            return True
    return False # No match
def check_cols():
    # Check for all cols
    for i in range(N):
        col = 'c'+str(i+1)
        flag = True
        # Check if all value in current col are NOT 'x' or 'o'
        for j in range(N-1):
            cell1 = 'r'+str(j+1)+col
            cell2 = 'r'+str(j+2)+col
            if squares[cell1]['status'] != squares[cell2]['status']:
                flag = False
                break
        if flag: # All values are same
            print('Found match in',col)
            return True
    return False # No match
def check_left_diag():
    flag = True
    # Check if all value in left diagonal are NOT 'x' or 'o'
    for i in range(N-1):
        cell1 = 'r'+str(i+1)+'c'+str(i+1)
        cell2 = 'r'+str(i+2)+'c'+str(i+2)
        if squares[cell1]['status'] != squares[cell2]['status']:
            flag = False
            break
    if flag: # All values are same
        print('Found match in left diagonal')
        return True
    return False # No match
def check_right_diag():
    flag = True
    # Check if all value in right diagonal are NOT 'x' or 'o'
    for i in range(N-1):
        cell1 = 'r'+str(i+1)+'c'+str(N-i)
        cell2 = 'r'+str(i+2)+'c'+str(N-i-1)
        if squares[cell1]['status'] != squares[cell2]['status']:
            flag = False
            break
    if flag: # All values are same
        print('Found match in right diagonal')
        return True
    return False # No match

print(check_rows())
print(check_cols())
print(check_left_diag())
print(check_right_diag())

输出

Found match in r2
True
False
False
False

这里有一个检查tic-tac-toe游戏结果的更短的方法,创建一个generatorlines()产生所有8行(3行、3列和2条对角线),然后检查其中是否有一个元素只由一个元素组成,而那个元素不是None

一旦你有了

board = [
    [ 'o', 'x', None], 
    [None, 'x', None], 
    [None, 'x',  'o']
]

请执行以下操作:

def _rows_and_diagonal(board):
    yield from board  # the rows
    yield [board[i][i] for i in range(len(board))]  # one of the diagonals

def lines(board):
    yield from _rows_and_diagonal(board)
    # rotate the board 90 degrees to get the columns and the other diagonal
    yield from _rows_and_diagonal(list(zip(*reversed(board))))

def who_won(board):
    for line in lines(board):
        if len(set(line)) == 1 and line[0] is not None:
            return line[0]
    return None  # if we got this far, there's no winner

这将适用于任何尺寸的tic-tac趾板,而不仅仅是3×3。你知道吗

相关问题 更多 >

    热门问题