# ormai/snake architecture

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

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

## Overview

##### Project type and purpose
- This is a small, single-process C terminal game (Snake).
- Purpose: render a game board in the terminal, process user input to move the snake, detect collisions, and update display each frame.

##### File structure
- Top level:
- main.c      → application entry point and game loop
- .{c,h}   → game-board data structure and operations
- snake.{c,h} → snake body representation and movement logic
- term.{c,h}  → raw terminal I/O and input-reading
- window.{c,h}→ drawing routines (rendering to terminal)
- makefile, LICENSE, README.md

##### Insights from README
- No external dependencies beyond standard C and a terminal.
- Build with make + gcc/clang.

##### Main components
a. GameLoop (main.c)
b. Data Model
- Module (map.c/h)
- Snake module (snake.c/h)
c. I/O
- Terminal I/O module (term.c/h)
- Renderer/Window module (window.c/h)

##### Relationships & interactions
- GameLoop orchestrates everything each frame:
1) term.read_input() → user key
2) snake.update(direction) → new snake position
3) .check_collision(snake) → game-over logic
5) term.flush() → update terminal screen
- Data flows from user ⇒ GameLoop ⇒ Data Model ⇒ Renderer ⇒ Terminal

##### Architectural patterns & design principles
- Modular separation by responsibility (I/O, rendering, game logic, data).
- Single process, synchronous game loop.
- Minimal external coupling: modules communicate via well-defined interfaces (headers).

##### Technologies & libraries
- Standard C (no external libs).
- makefile for build orchestration.
- Terminal control via term module (likely using ANSI escapes).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "I/O Modules"
        TermIO["Terminal I/O\n(term.c/.h)"]:::io
        Window["Window/Renderer\n(window.c/.h)"]:::io
    end
    subgraph "Logic Module"
        GameLoop["GameLoop\n(main.c)"]:::logic
    end
    subgraph "Data Modules"
        Snake["Snake Module\n(snake.c/.h)"]:::data
        Map["Map Module\n(map.c/.h)"]:::data
    end

    TermIO -->|"read_input()"| GameLoop
    GameLoop -->|"update(direction)"| Snake
    GameLoop -->|"check_collision()"| Map
    GameLoop -->|"render(map,snake)"| Window
    Window -->|"flush()"| TermIO

    click GameLoop "https://github.com/ormai/snake/blob/main/main.c"
    click Snake "https://github.com/ormai/snake/blob/main/snake.c"
    click Map "https://github.com/ormai/snake/blob/main/map.c"
    click TermIO "https://github.com/ormai/snake/blob/main/term.c"
    click Window "https://github.com/ormai/snake/blob/main/window.c"

    classDef data fill:#f9f,stroke:#333,stroke-width:1px
    classDef logic fill:#ff9,stroke:#333,stroke-width:1px
    classDef io fill:#9ff,stroke:#333,stroke-width:1px
```
