# shaiderm16/pac-man-game architecture

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

- Interactive diagram: https://gitdiagram.com/shaiderm16/pac-man-game
- Repository: https://github.com/shaiderm16/pac-man-game

## Overview

##### Main Components
- Game Engine/Core Loop: At the heart of the program, the engine manages the game cycle, which includes processing user input, updating game states (e.g., moving Pac-Man, moving ghosts, collision detection), and rendering changes to the map.
- Input Handling: Represents key presses (W, A, S, D, and Q) that trigger movement or game-state commands.
- Entity Management:
- Pac-Man: Tracked via its coordinates (pacmanX, pacmanY) and associated flags for tracking visited positions.
- Ghosts: Handled as structures (the Ghost struct) with their own positions and movement logic.
- Pellets & Walls: Represented on the map by designated characters and managed via functions like initialisePellets.
- Scoring and Lifespan Mechanics: Global variables like score and lives are changed via functions like incrementScore and decrementLive.
- Persistence: Save and Load capabilities (implemented with saveGame and loadGame) to store and retrieve the game state.

##### Component Relationships and Interactions
- Input (keyboard events) feeds into the Game Engine, which directs how the state is updated.
- The Game Engine uses its Internal Logic module to call functions that move entities (both Pac-Man and ghosts) and update the game map.
- Updated game states interact with the Rendering module, which displays the current board to the player.
- Entity interactions (like collisions) affect game state changes (e.g., decrementing lives when Pac-Man meets a ghost, or incrementing score when collecting pellets).
- The Persistence module (Save/Load) interacts with the game state, allowing the game to be paused and restored.

##### Architectural Patterns/Design Principles
- Procedural Programming: The project is implemented in a procedural C programming style with clearly separated functions for different behaviors.
- Modularity (within a single source file): Although all code resides in one C file (pacman.c), logically it can be separated into discrete responsibilities (initialization, input processing, state updates, rendering, and persistence).
- State Management: Uses global state variables for score, lives, and entity positions to control game flow.

##### What
- Main Components:
- Input Handling (keyboard controls)
- Game Engine / Core Loop
- Game State Management (global variables such as score, lives, pacmanX & pacmanY, ghost positions)
- Rendering/Display Module (displayMap and setTextColor usage)
- Persistence/State Storage (saveGame and loadGame)
- For example, input triggers actions in the Game Engine, which then updates the map, adjusts entity positions, and invokes rendering, as well as optionally saving state.

## Diagram (Mermaid)

```mermaid
flowchart TD
    InputHandling["Input Handling Module\n(Keyboard Controls W, A, S, D, Q)"]:::input
    GameEngine["Game Engine / Core Loop\n(movePacman, ghostMovement, collision detection)"]:::engine
    EntityManagement["Entity Management\n(Pac-Man, Ghosts, Pellets, Walls)"]:::entity
    Scoring["Scoring & Lifespan Mechanics\n(incrementScore, decrementLive)"]:::scoring
    Rendering["Rendering Module\n(displayMap, setTextColor)"]:::rendering
    Persistence["Persistence Module\n(saveGame, loadGame)"]:::persistence

    InputHandling -->|"triggers"| GameEngine
    GameEngine -->|"updates"| EntityManagement
    GameEngine -->|"updates"| Scoring
    GameEngine -->|"renders"| Rendering
    GameEngine -->|"saves/loads"| Persistence

    click InputHandling "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"
    click GameEngine "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"
    click EntityManagement "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"
    click Scoring "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"
    click Rendering "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"
    click Persistence "https://github.com/shaiderm16/pac-man-game/blob/main/src/pacman.c"

    classDef input fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef engine fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef entity fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef scoring fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef rendering fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef persistence fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
