Search⌘ K
AI Features

Moving the Whole Board

Explore how to build the board movement logic for the 2048 game. This lesson guides you to implement move_board using compress and merge operations, applying transformations like reverse and transpose to handle all directional moves. You'll learn to maintain functional purity and integrate user inputs for interactive gameplay, while leveraging AI assistance to write clear helper functions and refine your code.

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. ...