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_rowandmerge_rowto implement a left move.Use reverse and transpose transformations to reuse left-move logic for right/up/down.
Keep
move_boardpure (returning a new board instead of mutating the original).Wire
move_boardintomain()so the user can actually move tiles withw/a/s/d.Use AI to:
Help describe the transformation strategy
Generate helper functions (
transpose,reverse_rows)Refine their
move_boardimplementation.
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 ...