autoformatter go brr
This commit is contained in:
parent
bc71e758cf
commit
74ab281f75
1 changed files with 90 additions and 64 deletions
154
blokus.py
154
blokus.py
|
|
@ -4,50 +4,52 @@ import random
|
||||||
|
|
||||||
BOARD_SIZE = 14
|
BOARD_SIZE = 14
|
||||||
|
|
||||||
|
|
||||||
def make_board():
|
def make_board():
|
||||||
a = np.array([[0 for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)])
|
a = np.array([[0 for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)])
|
||||||
a[4,4] = -1
|
a[4, 4] = -1
|
||||||
a[9,9] = -1
|
a[9, 9] = -1
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
tiles = [
|
tiles = [
|
||||||
np.array([[1]]),
|
np.array([[1]]),
|
||||||
np.array([[1],[1]]),
|
np.array([[1], [1]]),
|
||||||
np.array([[1],[1],[1]]),
|
np.array([[1], [1], [1]]),
|
||||||
np.array([[1,0],[1,1]]),
|
np.array([[1, 0], [1, 1]]),
|
||||||
np.array([[1],[1],[1],[1]]),
|
np.array([[1], [1], [1], [1]]),
|
||||||
np.array([[1,0],[1,0],[1,1]]),
|
np.array([[1, 0], [1, 0], [1, 1]]),
|
||||||
np.array([[1,0],[1,1],[1,0]]),
|
np.array([[1, 0], [1, 1], [1, 0]]),
|
||||||
np.array([[1,1],[1,1]]),
|
np.array([[1, 1], [1, 1]]),
|
||||||
np.array([[1,1,0],[0,1,1]]),
|
np.array([[1, 1, 0], [0, 1, 1]]),
|
||||||
np.array([[1],[1],[1],[1],[1]]),
|
np.array([[1], [1], [1], [1], [1]]),
|
||||||
np.array([[1,0],[1,0],[1,0],[1,1]]),
|
np.array([[1, 0], [1, 0], [1, 0], [1, 1]]),
|
||||||
np.array([[1,0],[1,0],[1,1],[0,1]]),
|
np.array([[1, 0], [1, 0], [1, 1], [0, 1]]),
|
||||||
np.array([[1,0],[1,1],[1,1]]),
|
np.array([[1, 0], [1, 1], [1, 1]]),
|
||||||
np.array([[1,1],[1,0],[1,1]]),
|
np.array([[1, 1], [1, 0], [1, 1]]),
|
||||||
np.array([[1,0],[1,1],[1,0],[1,0]]),
|
np.array([[1, 0], [1, 1], [1, 0], [1, 0]]),
|
||||||
np.array([[0,1,0],[0,1,0],[1,1,1]]),
|
np.array([[0, 1, 0], [0, 1, 0], [1, 1, 1]]),
|
||||||
np.array([[1,0,0],[1,0,0],[1,1,1]]),
|
np.array([[1, 0, 0], [1, 0, 0], [1, 1, 1]]),
|
||||||
np.array([[1,1,0],[0,1,1],[0,0,1]]),
|
np.array([[1, 1, 0], [0, 1, 1], [0, 0, 1]]),
|
||||||
np.array([[1,0,0],[1,1,1],[0,0,1]]),
|
np.array([[1, 0, 0], [1, 1, 1], [0, 0, 1]]),
|
||||||
np.array([[1,0,0],[1,1,1],[0,1,0]]),
|
np.array([[1, 0, 0], [1, 1, 1], [0, 1, 0]]),
|
||||||
np.array([[0,1,0],[1,1,1],[0,1,0]]),
|
np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_permutations(which_tiles: list[tuple[int,int]]):
|
def get_permutations(which_tiles: list[tuple[int, int]]):
|
||||||
permutations = []
|
permutations = []
|
||||||
for i,tile in enumerate(tiles):
|
for i, tile in enumerate(tiles):
|
||||||
if i not in which_tiles:
|
if i not in which_tiles:
|
||||||
continue
|
continue
|
||||||
permutations.append((i,tile))
|
permutations.append((i, tile))
|
||||||
permutations.append((i,np.rot90(tile)))
|
permutations.append((i, np.rot90(tile)))
|
||||||
permutations.append((i,np.rot90(np.rot90(tile))))
|
permutations.append((i, np.rot90(np.rot90(tile))))
|
||||||
permutations.append((i,np.rot90(np.rot90(np.rot90(tile)))))
|
permutations.append((i, np.rot90(np.rot90(np.rot90(tile)))))
|
||||||
permutations.append((i,np.flip(tile)))
|
permutations.append((i, np.flip(tile)))
|
||||||
permutations.append((i,np.flip(np.rot90(tile))))
|
permutations.append((i, np.flip(np.rot90(tile))))
|
||||||
permutations.append((i,np.flip(np.rot90(np.rot90(tile)))))
|
permutations.append((i, np.flip(np.rot90(np.rot90(tile)))))
|
||||||
permutations.append((i,np.flip(np.rot90(np.rot90(np.rot90(tile))))))
|
permutations.append((i, np.flip(np.rot90(np.rot90(np.rot90(tile))))))
|
||||||
|
|
||||||
unique_arrays = []
|
unique_arrays = []
|
||||||
for arr in permutations:
|
for arr in permutations:
|
||||||
|
|
@ -56,80 +58,107 @@ def get_permutations(which_tiles: list[tuple[int,int]]):
|
||||||
|
|
||||||
return unique_arrays
|
return unique_arrays
|
||||||
|
|
||||||
|
|
||||||
def can_place(board, tile, player):
|
def can_place(board, tile, player):
|
||||||
placements = []
|
placements = []
|
||||||
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):
|
||||||
if board[x,y] == -1:
|
if board[x, y] == -1:
|
||||||
has_minus_one = True
|
has_minus_one = True
|
||||||
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:
|
||||||
if v == 1:
|
if v == 1:
|
||||||
(i,j) = it.multi_index
|
(i, j) = it.multi_index
|
||||||
if x + i >= BOARD_SIZE:
|
if x + i >= BOARD_SIZE:
|
||||||
break
|
break
|
||||||
if y + j >= BOARD_SIZE:
|
if y + j >= BOARD_SIZE:
|
||||||
break
|
break
|
||||||
if board[x + i][y + j] > 0:
|
if board[x + i][y + j] > 0:
|
||||||
break
|
break
|
||||||
if x + i - 1 >= 0 and board[x + i - 1][y+j] == player:
|
if x + i - 1 >= 0 and board[x + i - 1][y + j] == player:
|
||||||
break
|
break
|
||||||
if y + j - 1 >= 0 and board[x + i][y + j - 1] == player:
|
if y + j - 1 >= 0 and board[x + i][y + j - 1] == player:
|
||||||
break
|
break
|
||||||
if x + i + 1 < BOARD_SIZE and board[x + i + 1][y+j] == player:
|
if x + i + 1 < BOARD_SIZE and board[x + i + 1][y + j] == player:
|
||||||
break
|
break
|
||||||
if y + j + 1 < BOARD_SIZE and board[x + i][y+j+1] == player:
|
if y + j + 1 < BOARD_SIZE and board[x + i][y + j + 1] == player:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
placements.append((x,y))
|
placements.append((x, y))
|
||||||
final = []
|
final = []
|
||||||
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:
|
||||||
for v in it:
|
for v in it:
|
||||||
(i,j) = it.multi_index
|
(i, j) = it.multi_index
|
||||||
if v == 1 and board[x+i,y+j] == -1:
|
if v == 1 and board[x + i, y + j] == -1:
|
||||||
final.append((x,y))
|
final.append((x, y))
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
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:
|
||||||
for v in it:
|
for v in it:
|
||||||
(i,j) = it.multi_index
|
(i, j) = it.multi_index
|
||||||
if x+i+1 < BOARD_SIZE and y+j+1 < BOARD_SIZE and board[x+i+1][y+j+1] == player:
|
if (
|
||||||
final.append((x,y))
|
x + i + 1 < BOARD_SIZE
|
||||||
|
and y + j + 1 < BOARD_SIZE
|
||||||
|
and board[x + i + 1][y + j + 1] == player
|
||||||
|
):
|
||||||
|
final.append((x, y))
|
||||||
break
|
break
|
||||||
if x+i+1 < BOARD_SIZE and y+j-1 >= 0 and board[x+i+1][y+j-1] == player:
|
if (
|
||||||
final.append((x,y))
|
x + i + 1 < BOARD_SIZE
|
||||||
|
and y + j - 1 >= 0
|
||||||
|
and board[x + i + 1][y + j - 1] == player
|
||||||
|
):
|
||||||
|
final.append((x, y))
|
||||||
break
|
break
|
||||||
if x+i-1 >= 0 and y+j+1 < BOARD_SIZE and board[x+i-1][y+j+1] == player:
|
if (
|
||||||
final.append((x,y))
|
x + i - 1 >= 0
|
||||||
|
and y + j + 1 < BOARD_SIZE
|
||||||
|
and board[x + i - 1][y + j + 1] == player
|
||||||
|
):
|
||||||
|
final.append((x, y))
|
||||||
break
|
break
|
||||||
if x+i-1 >= 0 and y+j-1 >= 0 and board[x+i-1][y+j-1] == player:
|
if (
|
||||||
final.append((x,y))
|
x + i - 1 >= 0
|
||||||
|
and y + j - 1 >= 0
|
||||||
|
and board[x + i - 1][y + j - 1] == player
|
||||||
|
):
|
||||||
|
final.append((x, y))
|
||||||
break
|
break
|
||||||
return final
|
return final
|
||||||
|
|
||||||
|
|
||||||
def do_placement(tidx, tile, placement, game_state, player):
|
def do_placement(tidx, tile, placement, game_state, player):
|
||||||
(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:
|
||||||
(i,j) = it.multi_index
|
(i, j) = it.multi_index
|
||||||
if v == 1:
|
if v == 1:
|
||||||
game_state[0][x+i,y+j] = player
|
game_state[0][x + i, y + j] = player
|
||||||
game_state[player].remove(tidx)
|
game_state[player].remove(tidx)
|
||||||
|
|
||||||
|
|
||||||
def print_game_state(game_state):
|
def print_game_state(game_state):
|
||||||
(board, p1tiles, p2tiles) = game_state
|
(board, p1tiles, p2tiles) = game_state
|
||||||
|
|
||||||
for row in board:
|
for row in board:
|
||||||
print("".join([" " if x == 0 else "X" if x == 1 else "O" if x == 2 else "S" for x in row]))
|
print(
|
||||||
|
"".join(
|
||||||
|
[
|
||||||
|
" " if x == 0 else "X" if x == 1 else "O" if x == 2 else "S"
|
||||||
|
for x in row
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
print(f"Player 1 tiles left: {p1tiles}")
|
print(f"Player 1 tiles left: {p1tiles}")
|
||||||
print(f"Player 2 tiles left: {p2tiles}")
|
print(f"Player 2 tiles left: {p2tiles}")
|
||||||
|
|
||||||
|
|
||||||
game_state = (
|
game_state = (
|
||||||
make_board(),
|
make_board(),
|
||||||
[i for i in range(21)],
|
[i for i in range(21)],
|
||||||
|
|
@ -141,7 +170,7 @@ playing = True
|
||||||
player = 1
|
player = 1
|
||||||
while playing:
|
while playing:
|
||||||
moves = []
|
moves = []
|
||||||
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))
|
||||||
|
|
||||||
|
|
@ -156,10 +185,7 @@ while playing:
|
||||||
(tidx, tile, placement) = random.choice(moves)
|
(tidx, tile, placement) = random.choice(moves)
|
||||||
do_placement(tidx, tile, placement, game_state, player)
|
do_placement(tidx, tile, placement, game_state, player)
|
||||||
|
|
||||||
|
|
||||||
if player == 1:
|
if player == 1:
|
||||||
player = 2
|
player = 2
|
||||||
elif player == 2:
|
elif player == 2:
|
||||||
player = 1
|
player = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue