# dhruvicious/snakegame architecture

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

- Interactive diagram: https://gitdiagram.com/dhruvicious/snakegame
- Repository: https://github.com/dhruvicious/snakegame

## Overview

##### Project Type and Purpose
- This is a desktop game application (“Snake Game”) written in C++ with Raylib for rendering, organized with a layered, clean-architecture approach.
- It implements classic game-loop and command patterns, with separation between core logic, rendering, and application orchestration.

##### High-Level File Structure & Architectural Layers
- Assets/            → Game textures used at runtime
- external/          → Third-party library (Raylib)
- source/
- core/           → Pure game logic (Board, Snake, Food, Commands, Utilities)
- render/         → Rendering/UI code wrapping Raylib (RenderBoard, GameUI, etc.)
- app/            → Entry point, game loop, state management (Game, GameState, main)
- tests/             → Unit tests for core logic (Catch2)

##### Key Components
A. Application Layer (“app”)
- Game Loop
- GameState Manager
- InputHandler (invokes Command objects)
B. Core Logic Layer (“core”)
- Entities: Snake, Board, Food (via IFood interface)
- Command Pattern (Command, InputHandler)
- Utilities & GameConstants
C. Rendering Layer (“render”)
- Renderer classes: RenderBoard, RenderSnake, RenderFood, GameUI, Button
- Wraps Raylib calls
D. External Services & Assets
- Raylib library (in external/)
E. Test Harness
- Catch2 tests targeting core/ classes

##### Relationships & Data Flow
1. User presses a key → InputHandler (app)
2. InputHandler creates/invokes a Command (core)
3. Game Loop (app) updates GameState: calls core logic (Board.update(), Snake.move(), Food.spawn())
5. Raylib renders textures from Assets to the screen
6. Tests directly invoke core classes (Board, Snake, IFood) to verify game logic

##### Architectural Patterns & Principles
- Layered Design: app → core → render → external
- Dependency Direction: app depends on core & render; render depends on core & Raylib; core has no external deps
- Command Pattern for input abstraction
- Game Loop for frame-by-frame updates
- SOLID principles: interfaces (IFood, IObject), separation of concerns

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External & Assets
    subgraph "External Services"
        RaylibLib["Raylib Library"]:::cloud
    end
    Assets["Game Textures & Icons"]:::external
    RaylibLib -->|"provides rendering API"| Assets

    %% Rendering Layer
    subgraph "Rendering Layer"
        direction TB
        RB["RenderBoard"]:::render
        RS["RenderSnake"]:::render
        RF["RenderFood"]:::render
        GUI["GameUI"]:::render
        BTN["Button"]:::render
    end

    %% Core Logic Layer
    subgraph "Core Logic Layer"
        direction TB
        Board["Board"]:::core
        Snake["Snake"]:::core
        IFood["IFood Interface"]:::core
        Cmd["Command Pattern"]:::core
        IH["InputHandler"]:::core
        Utils["Utilities & Constants"]:::core
        IObj["IObject Interface"]:::core
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        UI["User Input"]:::external
        IHApp["InputHandler"]:::app
        CmdApp["Command"]:::app
        GL["Game Loop"]:::app
        GS["GameState Manager"]:::app
        Main["Entry Point (main)"]:::app
    end

    %% Test Harness
    subgraph "Test Harness"
        direction TB
        class TestBoard,TestSnake,TestIFood tests
        TestBoard["TestBoard"] 
        TestSnake["TestSnake"] 
        TestIFood["TestIFood"] 
    end

    %% Application flow
    UI -->|"handleInput"| IHApp
    IHApp -->|"invokes"| CmdApp
    Main -->|"starts"| GL
    GL -->|"updateState"| GS
    GS -->|"calls"| Board
    GS -->|"calls"| Snake
    GL -->|"updateState"| IFood
    GL -->|"renderFrame"| RB
    GL -->|"renderFrame"| RS
    GL -->|"renderFrame"| RF
    RB -->|"drawBoard"| RaylibLib
    RS -->|"drawSnake"| RaylibLib
    RF -->|"drawFood"| RaylibLib
    GUI -->|"drawUI"| RaylibLib
    BTN -->|"drawButton"| RaylibLib

    %% Core-Render connections
    RB -->|reads| Board
    RS -->|reads| Snake
    RF -->|reads| IFood

    %% Tests
    TestBoard -->|tests| Board
    TestSnake -->|tests| Snake
    TestIFood -->|tests| IFood

    %% Click Events
    click GL "https://github.com/dhruvicious/snakegame/blob/main/source/app/Game/Game.cpp"
    click GS "https://github.com/dhruvicious/snakegame/blob/main/source/app/Game/GameState.cpp"
    click Main "https://github.com/dhruvicious/snakegame/blob/main/source/app/main.cpp"
    click Board "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/Board.cpp"
    click Snake "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/Snake.cpp"
    click IFood "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/IFood.cpp"
    click Cmd "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/Command.cpp"
    click IH "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/InputHandler.cpp"
    click Utils "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/Utilities.cpp"
    click IObj "https://github.com/dhruvicious/snakegame/blob/main/source/core/src/IObject.cpp"
    click RB "https://github.com/dhruvicious/snakegame/blob/main/source/render/src/RenderBoard.cpp"
    click RS "https://github.com/dhruvicious/snakegame/blob/main/source/render/src/RenderSnake.cpp"
    click RF "https://github.com/dhruvicious/snakegame/blob/main/source/render/src/RenderFood.cpp"
    click GUI "https://github.com/dhruvicious/snakegame/blob/main/source/render/src/GameUI.cpp"
    click BTN "https://github.com/dhruvicious/snakegame/blob/main/source/render/src/Button.cpp"
    click RaylibLib "https://github.com/dhruvicious/snakegame/tree/main/external/"
    click Assets "https://github.com/dhruvicious/snakegame/tree/main/Assets/"
    click TestBoard "https://github.com/dhruvicious/snakegame/blob/main/tests/TestBoard.cpp"
    click TestSnake "https://github.com/dhruvicious/snakegame/blob/main/tests/TestSnake.cpp"
    click TestIFood "https://github.com/dhruvicious/snakegame/blob/main/tests/TestIFood.cpp"

    %% Styles
    classDef app fill:#ffa500,stroke:#333
    classDef core fill:#add8e6,stroke:#333
    classDef render fill:#90ee90,stroke:#333
    classDef external fill:#d3d3d3,stroke:#333
    classDef cloud fill:#ccc,stroke:#333,stroke-dasharray:5 5
    classDef tests stroke:#333,stroke-dasharray:5 5,fill:none
```
