# adrianovaladar/snake architecture

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

- Interactive diagram: https://gitdiagram.com/adrianovaladar/snake
- Repository: https://github.com/adrianovaladar/snake

## Overview

##### Main Components
- Game Engine/Core Loop: Represented by the Game class (Game.cpp/Game.h) and the entry point main.cpp.
- Input Handling: The Input module (Input.cpp/Input.h) that captures user inputs.
- Rendering/Graphics: The Graphics module (Graphics.cpp/Graphics.h) for drawing the game state to the terminal.
- Game Entities:
- Snake: Encapsulated in Snake.cpp/Snake.h.
- Regular Food: Represented by Food.cpp/Food.h.
- SuperFood: Represented by SuperFood.cpp/SuperFood.h.
- Player and Best Scores: Managed by Player.cpp/Player.h and BestScores.cpp/BestScores.h to maintain score tracking and high score listings.
- Utility Functions: Provided by Utils.cpp/Utils.h for any helper or common functions.
- Testing Components: Under the tests directory, individual test files (e.g., GameTests, SnakeTests, FoodTests) ensure each component works correctly.

##### Relationships and Interactions
- The Main entry point (main.cpp) initializes the Game engine.
- The Game component coordinates the overall game loop, managing updates, rendering, and input reading.
- During the game loop, the Input module collects user commands which are processed by the Game module to update the Snake's state.
- The Graphics component renders the current game state (snake position, food items, borders, etc.) to the terminal.
- The various entity classes (Snake, Food, SuperFood) interact with game dynamics such as collision detection, score updating, and level adjustments.
- BestScores and Player modules handle the persistence of game scores and ranking based on the configured map size and border settings.
- The Utils module likely provides helper functions used across various parts of the game (e.g., random number generation, formatting output, etc.).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each module (input, graphics, game logic, entities) is isolated in its own file(s), fostering maintainability.
- Modular Design: Because of well-defined components, changes in one part (like graphics) have minimal impact on others (e.g., game logic).
- Test-Driven Approach: The tests directory indicates unit tests for core functionalities, which helps with robustness and reliability.
- Use of CMake: Emphasizes portability and build automation for the C++ project.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Game Core Layer
    subgraph "Game Core"
        main["main.cpp"]:::engine
        game["Game Engine"]:::engine
        main -->|"initializes"| game
    end

    %% I/O Modules Layer
    subgraph "I/O Modules"
        input["Input Handling"]:::io
        graphics["Graphics Rendering"]:::io
    end

    %% Game Entities Layer
    subgraph "Game Entities"
        snake["Snake Entity"]:::entity
        food["Regular Food Entity"]:::entity
        superfood["SuperFood Entity"]:::entity
        player["Player Module"]:::entity
        bestscores["Best Scores Module"]:::entity
    end

    %% Utilities Layer
    subgraph "Utilities"
        utils["Utility Functions"]:::utils
    end

    %% Testing Suite Layer
    subgraph "Testing Suite"
        tests["Testing Suite"]:::test
    end

    %% Connections between layers
    game -->|"processes_input"| input
    game -->|"triggers_render"| graphics
    game -->|"updates"| snake
    game -->|"updates"| food
    game -->|"updates"| superfood
    game -->|"manages"| player
    game -->|"manages"| bestscores
    game -->|"utilizes"| utils
    tests -->|"validates"| game

    %% Click Events
    click main "https://github.com/adrianovaladar/snake/blob/main/src/main.cpp"
    click game "https://github.com/adrianovaladar/snake/blob/main/src/Game.cpp"
    click input "https://github.com/adrianovaladar/snake/blob/main/src/Input.cpp"
    click graphics "https://github.com/adrianovaladar/snake/blob/main/src/Graphics.cpp"
    click snake "https://github.com/adrianovaladar/snake/blob/main/src/Snake.cpp"
    click food "https://github.com/adrianovaladar/snake/blob/main/src/Food.cpp"
    click superfood "https://github.com/adrianovaladar/snake/blob/main/src/SuperFood.cpp"
    click player "https://github.com/adrianovaladar/snake/blob/main/src/Player.cpp"
    click bestscores "https://github.com/adrianovaladar/snake/blob/main/src/BestScores.cpp"
    click utils "https://github.com/adrianovaladar/snake/blob/main/src/Utils.cpp"
    click tests "https://github.com/adrianovaladar/snake/tree/main/tests"

    %% Styling Classes
    classDef engine fill:#f9c,stroke:#333,stroke-width:2px;
    classDef io fill:#9cf,stroke:#333,stroke-width:2px;
    classDef entity fill:#cfc,stroke:#333,stroke-width:2px;
    classDef utils fill:#fc9,stroke:#333,stroke-width:2px;
    classDef test fill:#ccf,stroke:#333,stroke-width:2px;
```
