Untitled
unknown
plain_text
2 years ago
1.1 kB
14
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 check_win(direction, r, c, symbol):
count = 0
dr, dc = direction
r_pos, c_pos = r, c
while in_bound(r_pos, c_pos) and board[r_pos][c_pos] == symbol:
count += 1
if count == 4:
return True
r_pos, c_pos = r_pos + dr, c_pos + dc
r_neg, c_neg = r - dr, c - dc
while in_bound(r_neg, c_neg) and board[r_neg][c_neg] == symbol:
count += 1
if count == 4:
return True
r_neg, c_neg = r_neg - dr, c_neg - dc
return False
row_top = 0
while row_top < len(board) and board[row_top][column] == '.':
row_top += 1
row_top -= 1
if row_top < 0:
return False
for direction in directions:
if check_win(direction, row_top, column, board[row_top][column]):
return True
return FalseEditor is loading...