From ab9e13325fb1bae04d3677b0d2ab2a8e505806aa Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 3 Jan 2024 17:41:03 +0200 Subject: [PATCH] feat(game): add movement --- src/game/game.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/game/game.py b/src/game/game.py index ee81da1..a6b8ad3 100644 --- a/src/game/game.py +++ b/src/game/game.py @@ -24,7 +24,22 @@ class Game2048: )[0] def move(self, direction: Direction) -> None: - pass + tmp_board = np.copy(self.board) + + match direction: + case Direction.LEFT: + self.board = np.apply_along_axis(self.merge, 1, self.board) + case Direction.RIGHT: + self.board = np.apply_along_axis(self.merge, 1, self.board) + self.board = np.flip(self.board, axis=1) + case Direction.UP: + self.board = np.apply_along_axis(self.merge, 0, self.board) + case Direction.DOWN: + self.board = np.apply_along_axis(self.merge, 0, self.board) + self.board = np.flip(self.board, axis=0) + + if not np.array_equal(self.board, tmp_board): + self.add_random_tile() def merge(self, row: np.ndarray) -> np.ndarray: pass