An Android implementation of 2048 with AI features
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.idea
UML
app
docs
gradle/wrapper
images
.gitignore
.travis.yml
LICENSE
README.md
backward_moves.json
backward_scores.json
build.gradle
forward_moves.json
forward_scores.json
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

2048.ai

Description

2048 is a simple game featuring binary numbers and a 4x4 grid.

Design

Domain Model

Domain Model

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

Movement SSD

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.

AI Model

AI Model

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.

Application Model

Application Model

The main activity is split into cohesive fragments.

  1. The score board elements
  2. The board of tiles
  3. The directional button pad
  4. The menu

Swipe Sequence Diagram

Swipe SSD

This sequence diagram outlines the communication between the player, gameboard, and swipe listeners.