mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): add _move_and_merge
This commit is contained in:
parent
84b12f11bb
commit
fe1630df68
@ -45,57 +45,62 @@ class Game2048:
|
|||||||
tile_loc = np.random.randint(0, len(tile_row_options))
|
tile_loc = np.random.randint(0, len(tile_row_options))
|
||||||
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) -> tuple[bool, int]:
|
||||||
match direction:
|
match direction:
|
||||||
case Direction.LEFT:
|
case Direction.LEFT:
|
||||||
self.move_left()
|
return self.move_left()
|
||||||
case Direction.RIGHT:
|
case Direction.RIGHT:
|
||||||
self.move_right()
|
return self.move_right()
|
||||||
case Direction.UP:
|
case Direction.UP:
|
||||||
self.move_up()
|
return self.move_up()
|
||||||
case Direction.DOWN:
|
case Direction.DOWN:
|
||||||
self.move_down()
|
return self.move_down()
|
||||||
|
|
||||||
def move_right(self) -> tuple[bool, int]:
|
def move_right(self) -> tuple[bool, int]:
|
||||||
self.board, has_pushed = self._push_board_right()
|
self.board, moved = self._move_and_merge(self.board)
|
||||||
has_merged = self.merge()
|
|
||||||
self.board, _ = self._push_board_right()
|
if moved:
|
||||||
move_made = has_pushed or has_merged
|
|
||||||
if move_made:
|
|
||||||
self.add_random_tile()
|
self.add_random_tile()
|
||||||
return move_made, self.score
|
return moved, self.score
|
||||||
|
|
||||||
def move_left(self) -> tuple[bool, int]:
|
def move_left(self) -> tuple[bool, int]:
|
||||||
self.board = np.rot90(self.board, 2)
|
board = np.rot90(self.board, 2)
|
||||||
self.board, has_pushed = self._push_board_right()
|
board, moved = self._move_and_merge(board)
|
||||||
has_merged = self.merge()
|
self.board = np.rot90(board, 2)
|
||||||
self.board, _ = self._push_board_right()
|
|
||||||
self.board = np.rot90(self.board, 2)
|
if moved:
|
||||||
move_made = has_pushed or has_merged
|
|
||||||
if move_made:
|
|
||||||
self.add_random_tile()
|
self.add_random_tile()
|
||||||
return move_made, self.score
|
return moved, self.score
|
||||||
|
|
||||||
def move_up(self) -> None:
|
def move_up(self) -> tuple[bool, int]:
|
||||||
|
board = np.rot90(self.board, -1)
|
||||||
|
board, moved = self._move_and_merge(board)
|
||||||
|
self.board = np.rot90(board, 1)
|
||||||
|
|
||||||
|
if moved:
|
||||||
|
self.add_random_tile()
|
||||||
|
return moved, self.score
|
||||||
|
|
||||||
|
def move_down(self) -> tuple[bool, int]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def move_down(self) -> None:
|
def _move_and_merge(self, board: np.ndarray) -> tuple[np.ndarray, bool]:
|
||||||
pass
|
board, has_pushed = self._push_board_right(board)
|
||||||
|
board, has_merged = self._merge(board)
|
||||||
|
board, _ = self._push_board_right(board)
|
||||||
|
return board, has_pushed or has_merged
|
||||||
|
|
||||||
def merge(self) -> bool:
|
def _merge(self, board: np.ndarray) -> tuple[np.ndarray, bool]:
|
||||||
done = False
|
done = False
|
||||||
for row in range(self.size):
|
for row in range(self.size):
|
||||||
for col in range(self.size - 1, 0, -1):
|
for col in range(self.size - 1, 0, -1):
|
||||||
if (
|
if board[row, col] == board[row, col - 1] and board[row, col] != 0:
|
||||||
self.board[row, col] == self.board[row, col - 1]
|
board[row, col] *= 2
|
||||||
and self.board[row, col] != 0
|
self.score += board[row, col]
|
||||||
):
|
board[row, col - 1] = 0
|
||||||
self.board[row, col] *= 2
|
|
||||||
self.score += self.board[row, col]
|
|
||||||
self.board[row, col - 1] = 0
|
|
||||||
done = True
|
done = True
|
||||||
|
|
||||||
return done
|
return board, done
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
print(f"Score: {self.score}")
|
print(f"Score: {self.score}")
|
||||||
@ -104,15 +109,15 @@ class Game2048:
|
|||||||
print(f"{val:^3}", end="")
|
print(f"{val:^3}", end="")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
def _push_board_right(self) -> tuple[np.ndarray, bool]:
|
def _push_board_right(self, board: np.ndarray) -> tuple[np.ndarray, bool]:
|
||||||
new = np.zeros((self.size, self.size), dtype=int)
|
new_board = np.zeros((self.size, self.size), dtype=int)
|
||||||
done = False
|
done = False
|
||||||
for row in range(self.size):
|
for row in range(self.size):
|
||||||
count = self.size - 1
|
count = self.size - 1
|
||||||
for col in range(self.size - 1, -1, -1):
|
for col in range(self.size - 1, -1, -1):
|
||||||
if self.board[row, col] != 0:
|
if board[row, col] != 0:
|
||||||
new[row, count] = self.board[row, col]
|
new_board[row, count] = board[row, col]
|
||||||
if col != count:
|
if col != count:
|
||||||
done = True
|
done = True
|
||||||
count -= 1
|
count -= 1
|
||||||
return new, done
|
return new_board, done
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user