# plutov/snake architecture

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

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

## Overview

##### Project Type and Purpose
- This is a browser‐based Snake game written in Go, compiled to WebAssembly, and served locally via the wasmserve HTTP server. There is no separate “backend” in the traditional sense; the Go code becomes the frontend logic running inside the browser’s WASM VM.

##### File Structure Analysis
- All Go source files live at the project root:
- main.go – Entrypoint: sets up the game loop, ties in with JavaScript/HTML for rendering and input.
- input.go – Captures and normalizes keyboard (or pointer) events via JS syscalls.
- game.go – Core game loop, tick scheduling, state transitions (running, game over).
- board.go – Manages grid geometry, boundaries, collision detection.
- snake.go – Defines the snake’s body data structure and movement logic.
- food.go – Spawns food items, checks for consumption events.
- go.mod/go.sum – Module dependencies (standard lib + wasm), no external libraries.
- README.md – Shows that we use wasmserve to host the generated .wasm and any HTML/JS glue.

##### Key Components
a. wasmserve HTTP Server
b. Browser Environment
c. WebAssembly Module (your compiled Go binary)
d. JavaScript/HTML Glue (auto‐generated by Go’s wasm support)
e. Game Submodules inside WASM:
– Input Module
– Game Engine (game.go)
– Board Module
– Snake Module
– Food Module

##### Component Relationships and Data Flow
1) wasmserve serves index.html, wasm_exec.js, and game.wasm over HTTP to the Browser.
2) Browser loads HTML + JS glue (wasm_exec.js) which instantiates the Go WASM module.
3) User presses keys → JS event handler (in wasm_exec.js) → Input Module in Go.
4) Input Module sends events to Game Engine.
5) Game Engine calls Board, Snake, Food modules each tick to update positions, detect collisions, spawn food.

##### Architectural Patterns & Design Principles
- Modular separation of concerns: input, state management (Engine), domain entities (Board, Snake, Food).
- Event‐driven loop: centralized Game Engine controlling tick rate.
- Host communication via JS syscall boundary (bridge between Go/WASM and Browser DOM/Canvas).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Hosting Layer
    subgraph "Hosting Layer"
        Server["wasmserve HTTP Server"]:::hosting
        README["README.md"]:::hosting
    end

    %% Browser Environment
    subgraph "Browser Environment"
        Browser["Browser"]:::runtime
        JSGlue["wasm_exec.js (JS Glue)"]:::runtime
    end

    %% WASM Module with Submodules
    subgraph "WASM Module" 
        WASM["Entry Point (Initialization)"]:::wasmSub
        ModuleDef["go.mod & go.sum"]:::wasmSub
        subgraph "Go Game Modules"
            Input["Input Module"]:::wasmSub
            Engine["Game Engine"]:::wasmSub
            Board["Board Module"]:::wasmSub
            Snake["Snake Module"]:::wasmSub
            Food["Food Module"]:::wasmSub
        end
    end

    %% Connections
    Server -->|"serves index.html, wasm_exec.js, game.wasm"| Browser
    Server -->|"docs & setup"| README
    Browser -->|"loads JS glue"| JSGlue
    JSGlue -->|"instantiate WASM"| WASM
    Browser -->|"key event"| Input
    Input -->|"event data"| Engine
    Engine -->|"update tick"| Board
    Engine -->|"update tick"| Snake
    Engine -->|"update tick"| Food
    Board -->|"collision info"| Engine
    Snake -->|"position data"| Engine
    Food -->|"spawn/consume info"| Engine
    Engine -->|"draw call"| Browser

    %% Click Events
    click WASM "https://github.com/plutov/snake/blob/main/main.go"
    click Input "https://github.com/plutov/snake/blob/main/input.go"
    click Engine "https://github.com/plutov/snake/blob/main/game.go"
    click Board "https://github.com/plutov/snake/blob/main/board.go"
    click Snake "https://github.com/plutov/snake/blob/main/snake.go"
    click Food "https://github.com/plutov/snake/blob/main/food.go"
    click ModuleDef "https://github.com/plutov/snake/blob/main/go.mod"
    click README "https://github.com/plutov/snake/blob/main/README.md"

    %% Styles
    classDef hosting fill:#CCE5FF,stroke:#1F618D,stroke-width:1px;
    classDef runtime fill:#D5F5E3,stroke:#196F3D,stroke-width:1px;
    classDef wasmSub fill:#FDEBD0,stroke:#B9770E,stroke-width:1px;
```
