import random
# ===== Working functions from previous lessons =====
def add_random_tile(board: list[list[int]]) -> None:
empty_positions = [
(r, c)
for r in range(4)
for c in range(4)
if board[r][c] == 0
]
if not empty_positions:
return
r, c = random.choice(empty_positions)
board[r][c] = 4 if random.random() < 0.1 else 2
def create_initial_board() -> list[list[int]]:
board = [[0] * 4 for _ in range(4)]
add_random_tile(board)
add_random_tile(board)
return board
def render_board(board: list[list[int]]) -> str:
size = len(board)
cell_width = 5
line = "+" + ("-" * cell_width + "+") * size
lines = [line]
for r in range(size):
row_text = "|"
for c in range(size):
val = board[r][c]
text = "" if val == 0 else str(val)
row_text += text.rjust(cell_width) + "|"
lines.append(row_text)
lines.append(line)
return "\n".join(lines)
def compress_row(row: list[int]) -> list[int]:
non_zero = [x for x in row if x != 0]
zeros_needed = len(row) - len(non_zero)
return non_zero + [0] * zeros_needed
def merge_row(row: list[int]) -> tuple[list[int], int]:
result = []
score = 0
i = 0
n = len(row)
while i < n:
if row[i] == 0:
break
if i + 1 < n and row[i] == row[i + 1] and row[i] != 0:
merged = 2 * row[i]
result.append(merged)
score += merged
i += 2
else:
result.append(row[i])
i += 1
while len(result) < n:
result.append(0)
return result, score
def move_left(board: list[list[int]]) -> tuple[list[list[int]], int, bool]:
total_score = 0
changed = False
new_board = []
for row in board:
new_row, gained = compress_merge_row(row)
total_score += gained
if new_row != row:
changed = True
new_board.append(new_row)
return new_board, total_score, changed
# ===== Lesson 6: helper + target functions =====
def transpose(board: list[list[int]]) -> list[list[int]]:
"""
Return the transpose of the 4x4 board (rows <-> columns).
We'll implement this in this lesson.
"""
# TODO
raise NotImplementedError
def reverse_rows(board: list[list[int]]) -> list[list[int]]:
"""
Return a new board where each row is reversed (for left/right moves).
We'll implement this in this lesson.
"""
# TODO
raise NotImplementedError
def move_board(board: list[list[int]], direction: str) -> tuple[list[list[int]], int, bool]:
"""
Apply a move to the board in one of the four directions:
'left', 'right', 'up', 'down'.
Returns:
new_board: the updated 4x4 board
gained_score: total score from merged tiles
moved: True if the board actually changed, else False
"""
# TODO: implement in this lesson
raise NotImplementedError
# ===== Still stubbed; will be done in Lesson 7+ =====
def check_game_state(board: list[list[int]], target: int = 2048) -> str:
raise NotImplementedError
def main():
board = create_initial_board()
score = 0
print("Initial board:")
print("Score:", score)
print(render_board(board))
# We'll add interactive moves here later in the lesson.
if __name__ == "__main__":
main()