add type hints in various spots
This commit is contained in:
parent
d73dba80cd
commit
98f161c620
1 changed files with 19 additions and 8 deletions
27
blokus.py
27
blokus.py
|
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
from typing import Any
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
@ -37,8 +38,8 @@ tiles = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_permutations(which_tiles: list[int]):
|
def get_permutations(which_tiles: list[int]) -> list[tuple[int, np.ndarray]]:
|
||||||
permutations = []
|
permutations: list[tuple[int, np.ndarray]] = []
|
||||||
|
|
||||||
for i, tile in enumerate(tiles):
|
for i, tile in enumerate(tiles):
|
||||||
if i not in which_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
|
flips = [np.flip(r, axis=1) for r in rots] # flip horizontally
|
||||||
all_orients = rots + flips # 8 orientations
|
all_orients = rots + flips # 8 orientations
|
||||||
|
|
||||||
seen = set()
|
seen: set[tuple[Any, bytes]] = set()
|
||||||
for t in all_orients:
|
for t in all_orients:
|
||||||
key = (t.shape, t.tobytes())
|
key = (t.shape, t.tobytes())
|
||||||
if key not in seen:
|
if key not in seen:
|
||||||
|
|
@ -58,8 +59,10 @@ def get_permutations(which_tiles: list[int]):
|
||||||
return permutations
|
return permutations
|
||||||
|
|
||||||
|
|
||||||
def can_place(board, tile, player):
|
def can_place(
|
||||||
placements = []
|
board: np.ndarray, tile: np.ndarray, player: int
|
||||||
|
) -> list[tuple[int, int]]:
|
||||||
|
placements: list[tuple[int, int]] = []
|
||||||
has_minus_one = False
|
has_minus_one = False
|
||||||
for x in range(BOARD_SIZE):
|
for x in range(BOARD_SIZE):
|
||||||
for y in range(BOARD_SIZE):
|
for y in range(BOARD_SIZE):
|
||||||
|
|
@ -85,7 +88,7 @@ def can_place(board, tile, player):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
placements.append((x, y))
|
placements.append((x, y))
|
||||||
final = []
|
final: list[tuple[int, int]] = []
|
||||||
if has_minus_one:
|
if has_minus_one:
|
||||||
for x, y in placements:
|
for x, y in placements:
|
||||||
with np.nditer(tile, flags=["multi_index"]) as it:
|
with np.nditer(tile, flags=["multi_index"]) as it:
|
||||||
|
|
@ -130,7 +133,14 @@ def can_place(board, tile, player):
|
||||||
return final
|
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
|
(x, y) = placement
|
||||||
with np.nditer(tile, flags=["multi_index"]) as it:
|
with np.nditer(tile, flags=["multi_index"]) as it:
|
||||||
for v in it:
|
for v in it:
|
||||||
|
|
@ -140,7 +150,7 @@ def do_placement(tidx, tile, placement, game_state, player):
|
||||||
game_state[player].remove(tidx)
|
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
|
(board, p1tiles, p2tiles) = game_state
|
||||||
|
|
||||||
for row in board:
|
for row in board:
|
||||||
|
|
@ -169,6 +179,7 @@ playing = True
|
||||||
player = 1
|
player = 1
|
||||||
while playing:
|
while playing:
|
||||||
moves = []
|
moves = []
|
||||||
|
assert player > 0
|
||||||
for tidx, tile in get_permutations(game_state[player]):
|
for tidx, tile in get_permutations(game_state[player]):
|
||||||
for placement in can_place(game_state[0], tile, player):
|
for placement in can_place(game_state[0], tile, player):
|
||||||
moves.append((tidx, tile, placement))
|
moves.append((tidx, tile, placement))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue