From 7dce54a20929e97695d75811022bc8ceded9c78c Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 3 Jan 2024 17:59:49 +0200 Subject: [PATCH] refactor(game): `add_random_tile` --- src/game/game.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/game/game.py b/src/game/game.py index 4c3a2fb..501671d 100644 --- a/src/game/game.py +++ b/src/game/game.py @@ -40,12 +40,10 @@ class Game2048: def add_random_tile(self) -> None: """Add a random tile to the board.""" - empty_cells: np.ndarray = np.argwhere(self.board == 0) - if empty_cells.shape[0] > 0: - row, col = random.choice(empty_cells) - self.board[row, col] = random.choices( - [2, 4], weights=Config.tile.probability - )[0] + tile_value = random.choices([2, 4], weights=Config.tile.probability)[0] + tile_row_options, tile_col_options = np.nonzero(np.logical_not(self.board)) + tile_loc = np.random.randint(0, len(tile_row_options)) + self.board[tile_row_options[tile_loc], tile_col_options[tile_loc]] = tile_value def move(self, direction: Direction) -> None: tmp_board = np.copy(self.board)