refactor(game): add_random_tile

This commit is contained in:
Kristofers Solo 2024-01-03 17:59:49 +02:00
parent 7372b25479
commit 7dce54a209

View File

@ -40,12 +40,10 @@ class Game2048:
def add_random_tile(self) -> None: def add_random_tile(self) -> None:
"""Add a random tile to the board.""" """Add a random tile to the board."""
empty_cells: np.ndarray = np.argwhere(self.board == 0) tile_value = random.choices([2, 4], weights=Config.tile.probability)[0]
if empty_cells.shape[0] > 0: tile_row_options, tile_col_options = np.nonzero(np.logical_not(self.board))
row, col = random.choice(empty_cells) tile_loc = np.random.randint(0, len(tile_row_options))
self.board[row, col] = random.choices( self.board[tile_row_options[tile_loc], tile_col_options[tile_loc]] = tile_value
[2, 4], weights=Config.tile.probability
)[0]
def move(self, direction: Direction) -> None: def move(self, direction: Direction) -> None:
tmp_board = np.copy(self.board) tmp_board = np.copy(self.board)