# kongquan98/snakegame architecture

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

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

## Overview

##### Project type and purpose
- This is an Android mobile application implementing the classic Snake game in Kotlin using Jetpack Compose.
- It follows MVVM with DI (Hilt), a local persistence layer (Room + DataStore), and a clean separation between presentation, view-model, and service layers.

##### File structure
- Top-level “app” module with Gradle/Kotlin DSL build scripts
- src/main/java/com/example/snakegame/
– presentation/
· ui/ (Compose screen components, NavigationController)
· viewmodel/ (HighScoreViewModel, SettingsViewModel)
· datamodel/ (enums and settings data classes)
– service/
· repo/ (HighScoreRepository, SettingsRepository)
· roomDB/ (Room entities, DAO, Database)
· datastore/ (SettingsDataStore)
· hilt/ (DatabaseModule for DI)
- res/ (drawables, fonts, raw audio, XML rules, values)

##### Key insights from README
- Five primary screens: Main Menu, Game Screen, Settings, High Score, Special Modes
- Uses MVVM + Jetpack Compose + Material3
- Persists settings in DataStore, high-scores in Room DB
- Hilt injects repositories and DB into ViewModels

a. Main components
1) Android Framework Layer
- Activities (MainMenuActivity, MyApplication)
- NavigationController
2) Presentation Layer (UI)
- Compose Screens (MainMenuScreen, SnakeGameScreen, HighScoreScreen, SettingsScreen, SpecialModeScreen, etc.)
3) ViewModel Layer
- HighScoreViewModel
- SettingsViewModel
4) Service Layer
- HighScoreRepository
- SettingsRepository
5) Persistence Layer
- Room Database (HighScoreDatabase with HighScoreDao)
- DataStore (SettingsDataStore)
6) Dependency Injection
- Hilt modules (DatabaseModule)
7) Resources
- drawables, raw audio, fonts, XML configurations

b. Relationships & data flow
- User taps UI (Compose screen) → UI calls methods on its ViewModel
- ViewModel → invokes Repository interfaces
- Repository → queries/persists via Room DAO or DataStore
- DataStore/Room → returns data → Repository → ViewModel → UI
- Hilt manages injection of Repositories, Database, DataStore into ViewModels

c. Architectural patterns & principles
- MVVM: clear separation between UI (Compose) and business logic (ViewModel)
- Clean layering: Presentation → Domain-like Repositories → Data sources
- DI with Hilt: decoupled component instantiation
- Single-Activity with Compose and NavigationController

d. Key technologies & frameworks
- Kotlin, Jetpack Compose, Material3
- Android ViewModel, LiveData/StateFlow
- Room (SQLite abstraction)
- DataStore (key-value settings)
- Hilt for DI
- Gradle Kotlin DSL

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Android Framework Layer
    subgraph "Android Framework Layer"
        AMA["MainMenuActivity"]:::android
        MA["MyApplication"]:::android
        NC["NavigationController"]:::android
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        subgraph "Compose Screens"
            MMS["MainMenuScreen"]:::presentation
            SGS["SnakeGameScreen"]:::presentation
            HSS["HighScoreScreen"]:::presentation
            SMS["SpecialModeScreen"]:::presentation
            WSS["WallsSelectionScreen"]:::presentation
            GL["GameLogic"]:::presentation
        end
        subgraph "Control Options"
            AB["ArrowButtons"]:::presentation
            JO["Joystick"]:::presentation
        end
        subgraph "Settings Screens"
            SeS["SettingScreen"]:::presentation
            BTS["ButtonTypeScreen"]:::presentation
            LS["LanguageScreen"]:::presentation
            MVCS["MusicVibrationControlScreen"]:::presentation
            SSS["SnakeSpeedScreen"]:::presentation
        end
        subgraph "UI Theme"
            C["Color.kt"]:::presentation
            Sh["Shapes.kt"]:::presentation
            Th["Theme.kt"]:::presentation
            Ty["Type.kt"]:::presentation
        end
        subgraph "UI Utilities"
            SM["SoundManager"]:::presentation
            WG["WallsGenerator"]:::presentation
            U["Utility"]:::presentation
            GOD["GameOverDialog"]:::presentation
            SHD["SaveHighScoreDialog"]:::presentation
        end
    end

    %% ViewModel Layer
    subgraph "ViewModel Layer"
        HSV["HighScoreViewModel"]:::viewmodel
        SeV["SettingsViewModel"]:::viewmodel
    end

    %% Service Layer
    subgraph "Service Layer"
        HSR["HighScoreRepository"]:::service
        SeR["SettingsRepository"]:::service
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        subgraph "Room Database"
            HSEnt["HighScore entity"]:::persistence
            HSDao["HighScoreDao"]:::persistence
            HSDb["HighScoreDatabase"]:::persistence
        end
        subgraph "DataStore"
            SDS["SettingsDataStore"]:::persistence
        end
    end

    %% Dependency Injection
    subgraph "Dependency Injection"
        DM["DatabaseModule"]:::di
    end

    %% Resources
    subgraph "Resources"
        DR["Drawables"]:::resources
        FT["Fonts"]:::resources
        RA["Raw audio"]:::resources
        XR["XML rules"]:::resources
        VA["Values"]:::resources
    end

    %% Relationships
    MA -->|init Hilt| DM
    DM -->|provides| HSDb
    DM -->|provides| SDS
    DM -->|injects| HSR
    DM -->|injects| SeR
    DM -->|injects| HSV
    DM -->|injects| SeV

    AMA -->|launches| NC
    NC -->|navigates| MMS
    NC -->|navigates| SGS
    NC -->|navigates| HSS
    NC -->|navigates| SMS
    NC -->|navigates| WSS

    MMS -->|"user events"| HSV
    HSS -->|"user events"| HSV
    SGS -->|"user events"| HSV
    SMS -->|"user events"| HSV
    WSS -->|"user events"| HSV

    SeS -->|"user events"| SeV
    BTS -->|"user events"| SeV
    LS -->|"user events"| SeV
    MVCS -->|"user events"| SeV
    SSS -->|"user events"| SeV

    HSV -->|"data requests"| HSR
    SeV -->|"data requests"| SeR

    HSR -->|"DB queries"| HSDao
    HSDao -->|"reads/writes"| HSDb

    SeR -->|"settings IO"| SDS

    %% Click Events
    click AMA "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/MainMenuActivity.kt"
    click MA "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/MyApplication.kt"
    click NC "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/NavigationController.kt"
    click MMS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/MainMenuScreen.kt"
    click SGS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/SnakeGameScreen.kt"
    click HSS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/HighScoreScreen.kt"
    click SMS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/SpecialModeScreen.kt"
    click WSS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/WallsSelectionScreen.kt"
    click GL "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/GameLogic.kt"
    click AB "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/controloption/ArrowButtons.kt"
    click JO "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/controloption/Joystick.kt"
    click SeS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/settingscreen/SettingScreen.kt"
    click BTS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/settingscreen/ButtonTypeScreen.kt"
    click LS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/settingscreen/LanguageScreen.kt"
    click MVCS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/settingscreen/MusicVibrationControlScreen.kt"
    click SSS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/screen/settingscreen/SnakeSpeedScreen.kt"
    click C "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/theme/Color.kt"
    click Sh "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/theme/Shapes.kt"
    click Th "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/theme/Theme.kt"
    click Ty "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/theme/Type.kt"
    click SM "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/utility/SoundManager.kt"
    click WG "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/utility/WallsGenerator.kt"
    click U "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/utility/Utility.kt"
    click GOD "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/utility/dialogBox/GameOverDialog.kt"
    click SHD "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/ui/utility/dialogBox/SaveHighScoreDialog.kt"
    click HSV "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/viewmodel/HighScoreViewModel.kt"
    click SeV "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/presentation/viewmodel/SettingsViewModel.kt"
    click HSR "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/repo/HighScoreRepository.kt"
    click SeR "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/repo/SettingsRepository.kt"
    click HSEnt "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/roomDB/HighScore.kt"
    click HSDao "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/roomDB/HighScoreDao.kt"
    click HSDb "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/roomDB/HighScoreDatabase.kt"
    click SDS "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/datastore/SettingsDataStore.kt"
    click DM "https://github.com/kongquan98/snakegame/blob/main/app/src/main/java/com/example/snakegame/service/hilt/DatabaseModule.kt"
    click DR "https://github.com/kongquan98/snakegame/tree/main/app/src/main/res/drawable/"
    click FT "https://github.com/kongquan98/snakegame/blob/main/app/src/main/res/font/nokia_font.xml"
    click RA "https://github.com/kongquan98/snakegame/tree/main/app/src/main/res/raw/"
    click XR "https://github.com/kongquan98/snakegame/blob/main/app/src/main/res/xml/backup_rules.xml"
    click VA "https://github.com/kongquan98/snakegame/tree/main/app/src/main/res/values/"

    %% Styles
    classDef android fill:#AED6F1,stroke:#21618C
    classDef presentation fill:#85C1E9,stroke:#1B4F72
    classDef viewmodel fill:#82E0AA,stroke:#196F3D
    classDef service fill:#F5B041,stroke:#B9770E
    classDef persistence fill:#F7DC6F,stroke:#B7950B
    classDef di fill:#D2B4DE,stroke:#7D3C98
    classDef resources fill:#ABB2B9,stroke:#566573
```
