Untitled

 avatar
unknown
python
2 years ago
1.2 kB
6
Indexable
def has_won(board, column):
    directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
    def in_bound(row, col):
        return 0 <= row < len(board) and 0 <= col < len(board[0])

    def count_consecutive(r, c, dr, dc, symbol):
        count = 0
        while in_bound(r, c) and board[r][c] == symbol:
            count += 1
            r, c = r + dr, c + dc
        return count

    row_top = len(board) - 1
    while row_top >= 0 and board[row_top][column] != '.':
        row_top -= 1

    if row_top < 0:
        return False

    symbol = board[row_top][column]

    if count_consecutive(row_top + 1, column, 1, 0, symbol) >= 4:
        return True

    if count_consecutive(row_top, column - 1, 0, -1, symbol) + count_consecutive(row_top, column + 1, 0, 1, symbol) + 1 >= 4:
        return True

    if count_consecutive(row_top - 1, column - 1, -1, -1, symbol) + count_consecutive(row_top + 1, column + 1, 1, 1, symbol) + 1 >= 4:
        return True

    if count_consecutive(row_top - 1, column + 1, -1, 1, symbol) + count_consecutive(row_top + 1, column - 1, 1, -1, symbol) + 1 >= 4:
        return True

    return False
Editor is loading...