Implement 2048 Game Logic
Problem Description
Implement the core logic for the classic 2048 game. You will be given an n x n integer board, where 0 represents an empty cell and any non-zero value represents a tile.
Core Functionality
You need to implement a method, move(direction), that updates the board after a single move in one of the four cardinal directions: up, down, left, or right.
Game Rules
The move operation must adhere to the following rules:
- All tiles slide as far as possible in the chosen direction until they hit another tile or the edge of the board.
- When two adjacent tiles with the same value collide during a slide, they merge into a single tile with double the value.
- A tile can participate in at most one merge operation per single
moveaction. For example, in a row[2, 2, 4], a left move results in[4, 4, 0], not[8, 0, 0]. - After all sliding and merging operations are complete, any newly empty cells on the board should be filled with
0.
Constraints & Assumptions
- You are not required to implement the user interface (UI).
- If random new-tile generation is considered, it should be kept separate from the deterministic movement logic you implement.
Examples (for a left move on a single row)
[2, 0, 2, 0]becomes[4, 0, 0, 0][2, 2, 2, 0]becomes[4, 2, 0, 0][2, 2, 2, 2]becomes[4, 4, 0, 0]
Follow-up: Game Over Condition
Implement an additional method, isGameOver(), which returns true if no legal moves remain on the board. A game is considered over when two conditions are met:
- The board contains no empty cells (
0s). - There are no horizontally or vertically adjacent tiles with equal values that could merge.
AI interview prep
Core dilemma
The central engineering conflict is designing a robust and generalized `move` function that correctly applies the sliding and merging rules, especially the 'one merge per tile per move' constraint, across all four directions without excessive code duplication or state management errors.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement 2048 Game Logic
Problem Description
Implement the core logic for the classic 2048 game. You will be given an n x n integer board, where 0 represents an empty cell and any non-zero value represents a tile.
Core Functionality
You need to implement a method, move(direction), that updates the board after a single move in one of the four cardinal directions: up, down, left, or right.
Game Rules
The move operation must adhere to the following rules:
- All tiles slide as far as possible in the chosen direction until they hit another tile or the edge of the board.
- When two adjacent tiles with the same value collide during a slide, they merge into a single tile with double the value.
- A tile can participate in at most one merge operation per single
moveaction. For example, in a row[2, 2, 4], a left move results in[4, 4, 0], not[8, 0, 0]. - After all sliding and merging operations are complete, any newly empty cells on the board should be filled with
0.
Constraints & Assumptions
- You are not required to implement the user interface (UI).
- If random new-tile generation is considered, it should be kept separate from the deterministic movement logic you implement.
Examples (for a left move on a single row)
[2, 0, 2, 0]becomes[4, 0, 0, 0][2, 2, 2, 0]becomes[4, 2, 0, 0][2, 2, 2, 2]becomes[4, 4, 0, 0]
Follow-up: Game Over Condition
Implement an additional method, isGameOver(), which returns true if no legal moves remain on the board. A game is considered over when two conditions are met:
- The board contains no empty cells (
0s). - There are no horizontally or vertically adjacent tiles with equal values that could merge.