Search⌘ K
AI Features

Moving the Whole Board

Explore how to implement the move_board function for a 4x4 2048 game in Python. Learn to reuse left move logic with transpose and reverse helpers for all directions. Understand how to keep functions pure, integrate AI support for coding and testing, and wire interactive moves to enable game play.

By the end of this lesson, you will:

  • Implement move_board(board, direction) -> (new_board, gained_score, moved_flag).

  • Reuse compress_row and merge_row to implement a left move.

  • Use reverse and transpose transformations to reuse left-move logic for right/up/down.

  • Keep move_board pure (returning a new board instead of mutating the original).

  • Wire move_board into main() so the user can actually move tiles with w/a/s/d.

  • Use AI to:

    • Help describe the transformation strategy

    • Generate helper functions (transpose, reverse_rows)

    • Refine their move_board implementation.

We have implemented the complete board movement thus far, but only assuming the user presses the left arrow key (or key ‘a’). Now it’s time to use that to implement the other three directions as well.

Step 0: Starter code

We start from the end-of-lesson file from the previous lesson (where compress_row and merge_row were added).

Python 3.10.4
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()

Step 1: Move left and transformations

We want you to understand the pattern:

  1. Implement “move left” on each row using compress_row + merge_row + compress_row. ...