# gabrielecirulli/2048 architecture

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

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

## Overview

This project is a browser-based HTML5 game—a clone of the popular 2048 game—written entirely in JavaScript and styled with CSS/SCSS. Given its nature, the project’s architecture is relatively simple and front-end–focused, but it’s still beneficial to clearly illustrate its components and how they interact.

##### Main Components
- User Interface (index.html): The entry point that loads the game and presents the UI.
- JavaScript Modules (located in the js/ directory):
– game_manager.js: The central coordinator managing game state, moves, and overall game flow.
– grid.js & tile.js: Represent the game board structure, responsible for grid operations, tile management, and game logic.
– keyboard_input_manager.js: Captures user inputs (keyboard events and, as noted in the README, mobile swipes) and relays them to the game manager.
– html_actuator.js: Handles DOM updates and renders the game state onto the page.
– local_storage_manager.js: Manages saving and retrieving game state and best scores to/from the browser's local storage.
– Additional polyfill files (animframe_polyfill.js, bind_polyfill.js, classlist_polyfill.js): Ensure compatibility across browsers.
- Styling Components (style/ directory):
– CSS/SCSS files (main.css, main.scss, helpers.scss): Define the look and feel of the game.
– Fonts (in style/fonts/): Provide custom fonts to enhance design aesthetics.
- Meta/Build/Contribution Files:
– Files like .gitignore, CONTRIBUTING.md, Rakefile, LICENSE.txt provide project governance and build/maintenance support.

##### Relationships and Interactions
- This input is sent to the game_manager, which updates the game state by manipulating grid.js and tile.js (i.e., moving tiles, merging, generating new tiles).
- After processing the game state, game_manager invokes html_actuator to update the DOM (using index.html as the base layout).
- The local_storage_manager is called to persist the game state and best score so that progress can be maintained across sessions.
- Polyfills ensure that any potential browser-specific issues are prevented, guaranteeing consistency in animations and function bindings.

##### Important Architectural Patterns and Design Principles
- Separation of Concerns: Different modules handle specific tasks (input handling, game state, rendering, persistence).
- Modular JavaScript: Each file in the js/ directory represents a modular piece of functionality.
- Progressive Enhancement and Compatibility: Use of polyfills ensures the game works across a broad range of browsers.
- MVC-like Flow (even if not formally an MVC framework):
– Model: The game state and board structure represented by grid.js and tile.js.
– Controller: Game_manager.js as the orchestrator.
– View: html_actuator.js, which updates the presentation layer in the browser.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction Layer
    subgraph "User Interaction"
        UI["User Interface (index.html)"]:::ui
        KEYINPUT["Keyboard Input Manager (js/keyboard_input_manager.js)"]:::module
        MANAGER["Game Manager (js/game_manager.js)"]:::manager
    end

    %% Game Modules Layer
    subgraph "Game Modules"
        GRID["Grid Module (js/grid.js)"]:::logic
        TILE["Tile Module (js/tile.js)"]:::logic
        RENDER["HTML Actuator (js/html_actuator.js)"]:::render
        STORAGE["Local Storage Manager (js/local_storage_manager.js)"]:::storage
    end

    %% Polyfills Group
    subgraph "Polyfills"
        POLY_A["animframe_polyfill.js"]:::polyfill
        POLY_B["bind_polyfill.js"]:::polyfill
        POLY_C["classlist_polyfill.js"]:::polyfill
    end

    %% Styling Component
    STYLING["Styling Components (style/)"]:::styling

    %% Connections
    UI -->|"captures"| KEYINPUT
    KEYINPUT -->|"sendsEventsTo"| MANAGER
    MANAGER -->|"updatesState"| GRID
    MANAGER -->|"updatesState"| TILE
    MANAGER -->|"triggersViewUpdate"| RENDER
    MANAGER -->|"persistsState"| STORAGE
    UI -->|"appliesStyles"| STYLING
    POLY_A -.->|"compatibility"| MANAGER
    POLY_B -.->|"compatibility"| MANAGER
    POLY_C -.->|"compatibility"| MANAGER

    %% Click Events
    click UI "https://github.com/gabrielecirulli/2048/blob/master/index.html"
    click KEYINPUT "https://github.com/gabrielecirulli/2048/blob/master/js/keyboard_input_manager.js"
    click MANAGER "https://github.com/gabrielecirulli/2048/blob/master/js/game_manager.js"
    click GRID "https://github.com/gabrielecirulli/2048/blob/master/js/grid.js"
    click TILE "https://github.com/gabrielecirulli/2048/blob/master/js/tile.js"
    click RENDER "https://github.com/gabrielecirulli/2048/blob/master/js/html_actuator.js"
    click STORAGE "https://github.com/gabrielecirulli/2048/blob/master/js/local_storage_manager.js"
    click POLY_A "https://github.com/gabrielecirulli/2048/blob/master/js/animframe_polyfill.js"
    click POLY_B "https://github.com/gabrielecirulli/2048/blob/master/js/bind_polyfill.js"
    click POLY_C "https://github.com/gabrielecirulli/2048/blob/master/js/classlist_polyfill.js"
    click STYLING "https://github.com/gabrielecirulli/2048/tree/master/style/"

    %% Styles
    classDef ui fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef module fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
    classDef manager fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef logic fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef render fill:#F5B041,stroke:#D35400,stroke-width:2px;
    classDef storage fill:#FAD7A0,stroke:#CA6F1E,stroke-width:2px;
    classDef polyfill fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef styling fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
```
