# teahana/tic-tac-toe architecture

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

- Interactive diagram: https://gitdiagram.com/teahana/tic-tac-toe
- Repository: https://github.com/teahana/tic-tac-toe

## Overview

##### Project type and purpose
- This is a small full-stack Java “game” server that serves static HTML to a browser client and manages Single-player, Multi-player, Chat, and Authentication over TCP/IP sockets.
- It uses Maven (pom.xml), plain Java (no heavyweight framework), and a handful of HTML files as its “view.”

##### File structure
- Top-level:
- pom.xml – Maven build + JDK settings
- src/main/java/org/example/Server.java – TCP server bootstrap & routing
- src/main/java/org/example/Client.java – abstraction of a connected client
- src/main/java/org/example/Game.java – core game‐state & logic
- src/main/java/org/example/*.html – view templates (auth.html, chat.html, game.html, menu.html)
- No separate “frontend” project: HTML lives alongside Java code, so server both hosts static pages and manages socket endpoints.
- No database/config directory: all state likely in-memory, routing by endpoints in Server.java.

##### README for additional insights
- The server listens on a TCP port (default 8080).
- Endpoints: /auth, /signout, /menu, /singleplayer, /multiplayer, /chat.
- All endpoints are guarded: unauthenticated users get redirected to /auth; unknown endpoints → /menu.

a. Main components:
- Browser Client (User’s Chrome/Firefox) – loads HTML + opens socket.
- Java TCP Server (Server.java) – single container listening on port 8080.
- Static View Layer – auth.html, menu.html, chat.html, game.html.
- Controller Layer inside the server – routing logic for Auth, Menu, SinglePlayer, Multiplayer, Chat.
- Game Engine (Game.java) – encapsulates game rules, single-player AI, multi-player session management.
- Client Handler (Client.java) – represents each connected browser, manages I/O, session state.
b. Relationships & Interactions:
1. Browser Client → (HTTP/TCP) → Server: initial request for HTML + socket handshake.
2. Server’s Router → dispatch to AuthController or MenuController based on endpoint & session.
3. AuthController → verifies credentials (in-memory), sets session in Client instance.
4. Client Handler ↔ Game Engine for game state updates:
– Single-player: Game.java invoked directly
– Multi-player: Game.java coordinates multiple Client instances
5. ChatController ↔ Client instances to broadcast chat messages.
6. Server → serves HTML files from “view” folder to the browser.
c. Architectural patterns & design principles:
- MVC-style separation: HTML “views,” routing “controllers” in Server.java, “model” in Game.java & Client.java.
- Single container (no microservices), in-memory session store.
- Socket‐based message passing instead of REST.
d. Key technologies/frameworks:
- Java SE (java.net.ServerSocket / Socket)
- Maven for build (pom.xml)
- Plain HTML for front-end
- In-memory session management (Client.java instances)

##### Final reminder
- Be very detailed: break the server into Controller, Game Engine, Client Handler, Static View.
- Every endpoint and show how session guards route requests.
- Lifecycle of a single player connecting → authenticating → playing/chating → signing out.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User and Browser
    subgraph "User Side"
        BC["Browser Client"]:::frontend
    end

    %% Network Boundary
    Port8080["Port 8080 (HTTP/TCP)"]:::boundary

    %% Server Container
    subgraph "Java Server"
        direction TB
        S["Java TCP Server"]:::backend
        SV["Static View Layer"]:::frontend
        subgraph "Controller Layer"
            AuthC["Auth Controller"]:::backend
            MenuC["Menu Controller"]:::backend
            SPG["Single-Player Controller"]:::backend
            MP["Multi-Player Controller"]:::backend
            ChatC["Chat Controller"]:::backend
        end
        CH["Client Handler"]:::backend
        GE["Game Engine"]:::backend
    end

    %% Views
    AV["auth.html"]:::frontend
    MV["menu.html"]:::frontend
    CV["chat.html"]:::frontend
    GV["game.html"]:::frontend

    %% Build Config
    POM["pom.xml"]:::config

    %% Network Flows
    BC -->|HTTP GET /auth| Port8080
    Port8080 -->|routes to| AuthC
    AuthC -->|serves HTML| BC
    AuthC -->|sets session| CH

    BC -->|HTTP GET /menu| Port8080
    Port8080 -->|routes to| MenuC
    MenuC -->|serves HTML| BC

    BC -->|HTTP GET /singleplayer| Port8080
    Port8080 -->|routes to| SPG
    SPG -->|serves HTML| BC

    BC -->|HTTP GET /multiplayer| Port8080
    Port8080 -->|routes to| MP
    MP -->|serves HTML| BC

    BC -->|HTTP GET /chat| Port8080
    Port8080 -->|routes to| ChatC
    ChatC -->|serves HTML| BC

    BC -->|TCP Socket| CH

    %% In-memory interactions
    CH -->|message events| SPG
    CH -->|message events| MP
    CH -->|message events| ChatC

    SPG -->|invokes| GE
    MP -->|coordinates| GE
    ChatC -->|broadcast via| CH

    %% Static View Layer
    SV --> AV
    SV --> MV
    SV --> CV
    SV --> GV

    %% Build Config Usage
    S --> POM

    %% Click Events
    click S "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/Server.java"
    click CH "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/Client.java"
    click GE "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/Game.java"
    click AV "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/auth.html"
    click MV "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/menu.html"
    click CV "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/chat.html"
    click GV "https://github.com/teahana/tic-tac-toe/blob/master/src/main/java/org/example/game.html"
    click POM "https://github.com/teahana/tic-tac-toe/blob/master/pom.xml"

    %% Styles
    classDef frontend fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef backend fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef boundary fill:#ffcc99,stroke:#333,stroke-width:1px
    classDef config fill:#e5e5e5,stroke:#333,stroke-width:1px
```
