diff --git a/blokus.py b/blokus.py index 30c20ed..d55d872 100755 --- a/blokus.py +++ b/blokus.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from typing import Any import numpy as np import random @@ -37,8 +38,8 @@ tiles = [ ] -def get_permutations(which_tiles: list[int]): - permutations = [] +def get_permutations(which_tiles: list[int]) -> list[tuple[int, np.ndarray]]: + permutations: list[tuple[int, np.ndarray]] = [] for i, tile in enumerate(tiles): if i not in which_tiles: @@ -48,7 +49,7 @@ def get_permutations(which_tiles: list[int]): flips = [np.flip(r, axis=1) for r in rots] # flip horizontally all_orients = rots + flips # 8 orientations - seen = set() + seen: set[tuple[Any, bytes]] = set() for t in all_orients: key = (t.shape, t.tobytes()) if key not in seen: @@ -58,8 +59,10 @@ def get_permutations(which_tiles: list[int]): return permutations -def can_place(board, tile, player): - placements = [] +def can_place( + board: np.ndarray, tile: np.ndarray, player: int +) -> list[tuple[int, int]]: + placements: list[tuple[int, int]] = [] has_minus_one = False for x in range(BOARD_SIZE): for y in range(BOARD_SIZE): @@ -85,7 +88,7 @@ def can_place(board, tile, player): break else: placements.append((x, y)) - final = [] + final: list[tuple[int, int]] = [] if has_minus_one: for x, y in placements: with np.nditer(tile, flags=["multi_index"]) as it: @@ -130,7 +133,14 @@ def can_place(board, tile, player): return final -def do_placement(tidx, tile, placement, game_state, player): +def do_placement( + tidx: int, + tile: np.ndarray, + placement: tuple[int, int], + game_state: tuple[np.ndarray, list[int], list[int]], + player: int, +): + assert player > 0 (x, y) = placement with np.nditer(tile, flags=["multi_index"]) as it: for v in it: @@ -140,7 +150,7 @@ def do_placement(tidx, tile, placement, game_state, player): game_state[player].remove(tidx) -def print_game_state(game_state): +def print_game_state(game_state: tuple[np.ndarray, list[int], list[int]]): (board, p1tiles, p2tiles) = game_state for row in board: @@ -169,6 +179,7 @@ playing = True player = 1 while playing: moves = [] + assert player > 0 for tidx, tile in get_permutations(game_state[player]): for placement in can_place(game_state[0], tile, player): moves.append((tidx, tile, placement))