/
Mihaham
/
Table-Time
Обзор
Документация
Войти
/
Mihaham
/
Table-Time
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
services/api/app/plugins/shared/grid.py
55 строк
2 KB
MihahamYT
feat: full game catalog — 46 plugins, docs nav, web UI kit
14 июн 2026, 12:43
14 июн 2026, 12:43
2cd5f50
Код
Авторство
О чём код?
"""Grid board utilities.""" from typing import Any def empty_grid(rows: int, cols: int, fill: Any = None) -> list[list[Any]]: """Create an empty rows×cols grid.""" return [[fill for _ in range(cols)] for _ in range(rows)] def in_bounds(row: int, col: int, rows: int, cols: int) -> bool: return 0 <= row < rows and 0 <= col < cols def neighbors4(row: int, col: int, rows: int, cols: int) -> list[tuple[int, int]]: """4-connected neighbors within bounds.""" result: list[tuple[int, int]] = [] for dr, dc in ((0, 1), (0, -1), (1, 0), (-1, 0)): nr, nc = row + dr, col + dc if in_bounds(nr, nc, rows, cols): result.append((nr, nc)) return result def check_line_win( board: list[list[Any]], player_id: str, line_length: int = 3, ) -> bool: """Check if player has `line_length` in a row (4 directions).""" rows = len(board) if rows == 0: return False cols = len(board[0]) directions = ((0, 1), (1, 0), (1, 1), (1, -1)) for r in range(rows): for c in range(cols): if board[r][c] != player_id: continue for dr, dc in directions: count = 0 for i in range(line_length): nr, nc = r + dr * i, c + dc * i if in_bounds(nr, nc, rows, cols) and board[nr][nc] == player_id: count += 1 else: break if count >= line_length: return True return False def count_tokens(board: list[list[Any]], player_id: str) -> int: """Count cells owned by player.""" return sum(1 for row in board for cell in row if cell == player_id)