2048 is a simple game featuring binary numbers and a 4x4 grid.
Originally the board was modeled as a 2 dimensional array of objects called Tiles. This was really slow when it came time to define an AI strategy, so the board was then migrated to a 64 bit bitmap where each 4 bits represents a tiles exponent. The algorithms that determine whether moves are possible and the ones that make said moves were then replaced with static maps of all the possible board and score combinations that are possible in the game for an additional speedup. This data is stored in JSON files that are loaded on creation of GameBoard objects.
Movement Sequence Diagram
This sequence diagram outlines generally how the gameboard reacts to movement attempted by an actor. It emphasizes the use of static mapping for moves and scores using databases. It also outlines how the event handler for the gameboard notifies the actor.
The AI was generalized in such a way that new strategies can be defined by subclassing TwentyFortyEightAI and overriding functions. The most important of which is the getWeightedScore function. The expectimax AI overrides this and uses a heuristic matrix to determine a weighted score for any given 2048 gameboard. The abstract base class then uses this to delegate the predicted best move based on the score.
The main activity is split into cohesive fragments.
- The score board elements
- The board of tiles
- The directional button pad
- The menu
Swipe Sequence Diagram
This sequence diagram outlines the communication between the player, gameboard, and swipe listeners.