# angelazqian/2048-ai architecture

> Architecture diagram of the GitHub repository angelazqian/2048-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/angelazqian/2048-ai
- Repository: https://github.com/angelazqian/2048-ai

## Overview

2048 AI is a browser-playable 2048 game with optional neural-network autoplay modes. Players control moves by keyboard or touch; the game manager updates the board, applies its tile-spawn logic, persists progress and renders each state. Selecting an AI mode loads a packaged TensorFlow.js model, ranks moves from the board, and sends candidate moves through the same game manager. The README confirms the playable game; model training assets are present, but no training workflow is documented there. Edges reflect shown calls or explicit README details; unsampled HTML wiring is not inferred.

Main entry points: [`js/game_manager.js`](https://github.com/angelazqian/2048-ai/blob/main/js/game_manager.js) (Game manager), [`js/keyboard_input_manager.js`](https://github.com/angelazqian/2048-ai/blob/main/js/keyboard_input_manager.js) (Keyboard and touch), [`js/application.js`](https://github.com/angelazqian/2048-ai/blob/main/js/application.js) (AI mode selection), [`js/grid.js`](https://github.com/angelazqian/2048-ai/blob/main/js/grid.js) (Grid and tiles)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_play["Game interaction"]
  node_controls["Keyboard and touch"]
  node_app_boot["Application bootstrap<br/>[application.js]"]
end

subgraph group_engine["Game engine"]
  node_manager["Game manager<br/>[game_manager.js]"]
  node_board["Grid and tiles<br/>[grid.js]"]
  node_tile["Tile state<br/>[tile.js]"]
  node_spawn["Spawn strategy<br/>[game_manager.js]"]
end

subgraph group_ai["AI autoplay"]
  node_ai_mode["AI mode selection<br/>[application.js]"]
  node_predict["Move prediction<br/>[application.js]"]
  node_models["Packaged models"]
end

subgraph group_state["Persistence and display"]
  node_storage_manager["Storage manager"]
  node_browser_storage[("Browser storage")]
  node_actuator["HTML actuator<br/>[html_actuator.js]"]
end

node_player(("Player"))
node_game_screen["Game screen"]

node_player -->|"sends moves"| node_controls
node_controls -->|"emits events"| node_manager
node_app_boot -->|"creates"| node_manager
node_manager -->|"registers handlers"| node_controls
node_manager -->|"updates board"| node_board
node_manager -->|"spawns tiles"| node_spawn
node_board -->|"restores tiles"| node_tile
node_manager -->|"loads and saves"| node_storage_manager
node_storage_manager -->|"reads and writes"| node_browser_storage
node_manager -->|"renders state"| node_actuator
node_actuator -->|"updates display"| node_game_screen
node_player -.->|"selects mode"| node_ai_mode
node_ai_mode -->|"loads model"| node_models
node_ai_mode -->|"starts autoplay"| node_predict
node_predict -->|"predicts moves"| node_models
node_predict -->|"tries ranked moves"| node_manager
node_app_boot -->|"configures input"| node_controls

click node_controls "https://github.com/angelazqian/2048-ai/blob/main/js/keyboard_input_manager.js"
click node_manager "https://github.com/angelazqian/2048-ai/blob/main/js/game_manager.js"
click node_board "https://github.com/angelazqian/2048-ai/blob/main/js/grid.js"
click node_tile "https://github.com/angelazqian/2048-ai/blob/main/js/tile.js"
click node_spawn "https://github.com/angelazqian/2048-ai/blob/main/js/game_manager.js"
click node_ai_mode "https://github.com/angelazqian/2048-ai/blob/main/js/application.js"
click node_predict "https://github.com/angelazqian/2048-ai/blob/main/js/application.js"
click node_models "https://github.com/angelazqian/2048-ai/tree/main/model"
click node_storage_manager "https://github.com/angelazqian/2048-ai/blob/main/js/local_storage_manager.js"
click node_actuator "https://github.com/angelazqian/2048-ai/blob/main/js/html_actuator.js"
click node_app_boot "https://github.com/angelazqian/2048-ai/blob/main/js/application.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_controls,node_app_boot,node_game_screen toneBlue
class node_manager,node_board,node_tile,node_spawn toneAmber
class node_ai_mode,node_predict,node_models toneMint
class node_storage_manager,node_browser_storage,node_actuator toneRose
class node_player toneIndigo
```

## Components

### Game interaction

- [Keyboard and touch](https://github.com/angelazqian/2048-ai/blob/main/js/keyboard_input_manager.js): `js/keyboard_input_manager.js`
- [Application bootstrap](https://github.com/angelazqian/2048-ai/blob/main/js/application.js): `js/application.js`

### Game engine

- [Game manager](https://github.com/angelazqian/2048-ai/blob/main/js/game_manager.js): `js/game_manager.js`
- [Grid and tiles](https://github.com/angelazqian/2048-ai/blob/main/js/grid.js): `js/grid.js`
- [Tile state](https://github.com/angelazqian/2048-ai/blob/main/js/tile.js): `js/tile.js`
- [Spawn strategy](https://github.com/angelazqian/2048-ai/blob/main/js/game_manager.js): `js/game_manager.js`

### AI autoplay

- [AI mode selection](https://github.com/angelazqian/2048-ai/blob/main/js/application.js): `js/application.js`
- [Move prediction](https://github.com/angelazqian/2048-ai/blob/main/js/application.js): `js/application.js`
- [Packaged models](https://github.com/angelazqian/2048-ai/tree/main/model): `model`

### Persistence and display

- [Storage manager](https://github.com/angelazqian/2048-ai/blob/main/js/local_storage_manager.js): `js/local_storage_manager.js`
- **Browser storage**
- [HTML actuator](https://github.com/angelazqian/2048-ai/blob/main/js/html_actuator.js): `js/html_actuator.js`

### Other components

- **Player**
- **Game screen**

## Connections

- Player → Keyboard and touch: sends moves (evidence: `js/keyboard_input_manager.js`)
- Keyboard and touch → Game manager: emits events (evidence: `js/game_manager.js`)
- Application bootstrap → Game manager: creates (evidence: `js/application.js`)
- Game manager → Keyboard and touch: registers handlers (evidence: `js/game_manager.js`)
- Game manager → Grid and tiles: updates board (evidence: `js/game_manager.js`)
- Game manager → Spawn strategy: spawns tiles (evidence: `js/game_manager.js`)
- Grid and tiles → Tile state: restores tiles (evidence: `js/grid.js`)
- Game manager → Storage manager: loads and saves (evidence: `js/game_manager.js`)
- Storage manager → Browser storage: reads and writes (evidence: `js/local_storage_manager.js`)
- Game manager → HTML actuator: renders state (evidence: `js/game_manager.js`)
- HTML actuator → Game screen: updates display (evidence: `js/html_actuator.js`)
- Player → AI mode selection: selects mode
- AI mode selection → Packaged models: loads model (evidence: `js/application.js`)
- AI mode selection → Move prediction: starts autoplay (evidence: `js/application.js`)
- Move prediction → Packaged models: predicts moves (evidence: `js/application.js`)
- Move prediction → Game manager: tries ranked moves (evidence: `js/application.js`)
- Application bootstrap → Keyboard and touch: configures input (evidence: `js/application.js`)
