mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): add move direction method templates
This commit is contained in:
parent
7dce54a209
commit
9e073c5bea
@ -12,10 +12,10 @@ def play() -> None:
|
|||||||
game.display()
|
game.display()
|
||||||
move = input("Enter direction: ")
|
move = input("Enter direction: ")
|
||||||
moves = {
|
moves = {
|
||||||
"w": Direction.UP,
|
"w": game.move_up,
|
||||||
"a": Direction.LEFT,
|
"a": game.move_left,
|
||||||
"s": Direction.DOWN,
|
"s": game.move_down,
|
||||||
"d": Direction.RIGHT,
|
"d": game.move_right,
|
||||||
}
|
}
|
||||||
|
|
||||||
if move == "q":
|
if move == "q":
|
||||||
@ -24,7 +24,7 @@ def play() -> None:
|
|||||||
direction = moves.get(move, None)
|
direction = moves.get(move, None)
|
||||||
|
|
||||||
if direction:
|
if direction:
|
||||||
game.move(direction)
|
direction()
|
||||||
|
|
||||||
game.display()
|
game.display()
|
||||||
|
|
||||||
@ -46,22 +46,27 @@ class Game2048:
|
|||||||
self.board[tile_row_options[tile_loc], tile_col_options[tile_loc]] = tile_value
|
self.board[tile_row_options[tile_loc], tile_col_options[tile_loc]] = tile_value
|
||||||
|
|
||||||
def move(self, direction: Direction) -> None:
|
def move(self, direction: Direction) -> None:
|
||||||
tmp_board = np.copy(self.board)
|
|
||||||
|
|
||||||
match direction:
|
match direction:
|
||||||
case Direction.LEFT:
|
case Direction.LEFT:
|
||||||
self.board = np.apply_along_axis(self.merge, 1, self.board)
|
self.move_left()
|
||||||
case Direction.RIGHT:
|
case Direction.RIGHT:
|
||||||
self.board = np.apply_along_axis(self.merge, 1, self.board)
|
self.move_right()
|
||||||
self.board = np.flip(self.board, axis=1)
|
|
||||||
case Direction.UP:
|
case Direction.UP:
|
||||||
self.board = np.apply_along_axis(self.merge, 0, self.board)
|
self.move_up()
|
||||||
case Direction.DOWN:
|
case Direction.DOWN:
|
||||||
self.board = np.apply_along_axis(self.merge, 0, self.board)
|
self.move_down()
|
||||||
self.board = np.flip(self.board, axis=0)
|
|
||||||
|
|
||||||
if not np.array_equal(self.board, tmp_board):
|
def move_left(self) -> None:
|
||||||
self.add_random_tile()
|
pass
|
||||||
|
|
||||||
|
def move_right(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def move_up(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def move_down(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
def merge(self, row: np.ndarray) -> bool:
|
def merge(self, row: np.ndarray) -> bool:
|
||||||
done = False
|
done = False
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user