1f
unknown
python
2 years ago
818 B
15
Indexable
#Re-written version of 1f:
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]
for dr, dc in directions:
if count_consecutive(row_top, column, dr, dc, symbol) + count_consecutive(row_top, column, -dr, -dc, symbol) - 1 >= 4:
return True
return False
print(has_won(board, 3))Editor is loading...