# joseph-v-a/tic-tac-toe-game architecture

> Architecture diagram of the GitHub repository joseph-v-a/tic-tac-toe-game, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/joseph-v-a/tic-tac-toe-game
- Repository: https://github.com/joseph-v-a/tic-tac-toe-game

## Overview

##### Project type + purpose (what you’re diagramming)

Given the tiny file tree:
- `index.c` = entire implementation (all modules/functions likely live here)
- `README.md` = usage/build notes
- `.gitignore` = not architecturally relevant

##### What to extract from the code (since everything is in index.c)
- **State**: how the board is represented (array of 9 chars? 3x3 grid?), current player, move count
- **Core functions** (names may vary, but roles are consistent):
- `main()` (entry point)
- `drawBoard()` / `printBoard()` (rendering)
- `readMove()` / `playerInput()` (input)
- `isValidMove()` (validation / cell occupancy check)
- `applyMove()` (write into board state)
- `checkWin()` / `checkWinner()` (winning conditions)
- `checkDraw()` (move count == 9)
- `resetGame()` / `initBoard()`
- `playAgain()` / replay prompt
- **I/O dependencies**:
- `stdio.h` for `printf/scanf` etc.
- `conio.h` for `getch` (not portable on all OSes). This is an important “external dependency” to show because it affects portability.

##### Main components
Program as a set of internal components plus a couple of external actors:

##### External actors
- **Player 1 / Player 2** (human users)
- **Console/Terminal (stdin/stdout)**

##### Game Controller / Main Loop
- Orchestrates turns, calls render/input/check logic, manages replay.
##### Board State (Data Model)
- The board representation + current player + move counter.
##### Input Handler
- Reads user selection (1–9), optionally uses `getch` for immediate keypress.
##### Move Validator
- Ensures input is in range and cell is not already occupied.
##### Renderer (UI)
- Prints the board, prompts, error messages, and results.

##### Platform/Library dependencies
- **C Runtime + Standard I/O (`stdio`)**
- **Console I/O extension (`conio` / `getch`)** if used

##### Core turn sequence (control flow)
- `Player -> Input Handler -> Move Validator -> Game Controller`
- If valid:
- `Game Controller -> Board State (apply move)`
- `Game Controller -> Renderer (print updated board + status)`
- If invalid:
- `Game Controller -> Renderer (error message)`
- Loop back to `Input Handler`

##### Game lifecycle (state transitions)
- Start:
- `main()` initializes `Board State` and `currentPlayer`
- Each move increments move count
- Terminal conditions:
- Win: announce winner -> replay prompt -> reset or exit

##### Data dependencies
- Renderer reads Board State (to print)
- Rules Engine reads Board State (to evaluate)
- Validator reads Board State (to check occupancy)
- Controller writes Board State (to apply move/reset)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% =========================
    %% Level 0: Context
    %% =========================
    subgraph "Level 0 — Context"
        P1["Player 1 (Human)"]:::actor
        P2["Player 2 (Human)"]:::actor

        TERM["Console/Terminal\n(stdin/stdout)"]:::io

        APPBIN["Tic-Tac-Toe Console Executable\n(single-binary C app)"]:::app

        STDIO["C Standard I/O\n(stdio.h: printf/scanf)"]:::lib
        CONIO["Console I/O Extension\n(conio.h: getch)"]:::lib
        DOCS["Build/Usage Docs\n(README)"]:::docs
    end

    P1 <-->|"interacts"| TERM
    P2 <-->|"interacts"| TERM
    TERM -->|"input/output"| APPBIN

    APPBIN -->|"uses"| STDIO
    APPBIN -.->|"optional_uses"| CONIO
    DOCS -.->|"describes_build/run"| APPBIN

    %% =========================
    %% Level 1: Internal components
    %% =========================
    subgraph "Level 1 — Internal Structure (logical modules inside index.c)"
        CTRL["Game Controller\n(main loop / turn orchestration)\ninit, turn cycle, replay/exit"]:::controller

        IN["Input Handler\n(readMove/playerInput)\n(scanf / getch)"]:::io
        OUT["Renderer / UI\n(drawBoard/printBoard, prompts,\nerror + result messages)"]:::io

        VAL["Move Validator\n(isValidMove)\n(range 1–9 + occupancy)"]:::logic
        RULES["Game Rules Engine\n(checkWin/checkDraw)\n(rows/cols/diagonals + moveCount==9)"]:::logic

        STATE[("Board State / Data Model\n(board[9] or 3x3), currentPlayer,\nmoveCount")]:::state

        subgraph "Per-Turn Flow (control + decisions)"
            T0["Start Turn"]:::step
            T1{"Valid move?"}:::decision
            T2["Apply Move\n(write to Board State,\nincrement moveCount)"]:::step
            T3{"Win or Draw?"}:::decision
            T4["Announce Winner/Draw\nand prompt replay"]:::step
            T5{"Play again?"}:::decision
            T6["Reset Game\n(initBoard/resetGame)"]:::step
            T7["Exit"]:::step
        end
    end

    %% Bridge: external terminal to internal IO handlers
    TERM -->|"stdin"| IN
    OUT -->|"stdout"| TERM

    %% Core call/control flow
    CTRL -->|"calls"| OUT
    CTRL -->|"calls"| IN
    IN -->|"returns_move"| CTRL

    CTRL -->|"calls"| VAL
    VAL -->|"valid/invalid"| CTRL

    CTRL -->|"writes"| STATE
    CTRL -->|"calls"| RULES
    CTRL -->|"calls"| OUT

    %% Data reads (dashed)
    VAL -.->|"reads"| STATE
    RULES -.->|"reads"| STATE
    OUT -.->|"reads"| STATE

    %% Activity/turn sequence (level 2-like, but kept vertical)
    CTRL -->|"start_game"| T0
    T0 -->|"render_board"| OUT
    T0 -->|"prompt_input"| IN
    IN -->|"move"| VAL
    VAL -->|"decision"| T1

    T1 -->|"no"| OUT
    OUT -->|"error_prompt"| IN

    T1 -->|"yes"| T2
    T2 -->|"update_state"| STATE
    T2 -->|"check_rules"| RULES
    RULES -->|"result"| T3

    T3 -->|"no"| T0
    T3 -->|"yes"| T4
    T4 -->|"replay_prompt"| T5
    T5 -->|"yes"| T6
    T6 -->|"reinit_state"| STATE
    T6 -->|"new_game"| T0
    T5 -->|"no"| T7

    %% =========================
    %% Click Events (from component_mapping)
    %% =========================
    click APPBIN "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click CTRL "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click STATE "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click IN "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click VAL "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click RULES "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click OUT "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/index.c"
    click DOCS "https://github.com/joseph-v-a/tic-tac-toe-game/blob/main/README.md"

    %% =========================
    %% Styles
    %% =========================
    classDef actor fill:#EAF2FF,stroke:#2B5FDB,stroke-width:1px,color:#0B1F4B
    classDef io fill:#E9FBF1,stroke:#1F7A3A,stroke-width:1px,color:#0B2A16
    classDef app fill:#FFF3D6,stroke:#B7791F,stroke-width:1px,color:#3B2A0A
    classDef lib fill:#F2F2F2,stroke:#616161,stroke-width:1px,color:#1F1F1F
    classDef docs fill:#F3E8FF,stroke:#6B21A8,stroke-width:1px,color:#2E1065

    classDef controller fill:#FFE4E6,stroke:#BE123C,stroke-width:1px,color:#4A0C1A
    classDef logic fill:#E0F2FE,stroke:#0369A1,stroke-width:1px,color:#082F49
    classDef state fill:#FFF7ED,stroke:#C2410C,stroke-width:1px,color:#431407

    classDef step fill:#FFFFFF,stroke:#6B7280,stroke-width:1px,color:#111827
    classDef decision fill:#FFF1F2,stroke:#9F1239,stroke-width:1px,color:#4A0C1A
```
