# zahidesad/yahtzeegame architecture

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

- Interactive diagram: https://gitdiagram.com/zahidesad/yahtzeegame
- Repository: https://github.com/zahidesad/yahtzeegame

## Overview

##### Project Type & Purpose
- This is a Java-based networked two-player Yahtzee game with a headless AWS-hosted server and a Swing desktop client.
- The server manages match-making, game sessions and enforces Yahtzee rules; each client presents an MVC-style GUI, communicates over TCP, and renders game state.

##### File Structure Analysis
- Top-level modules:
- /src/main/java/server → server entrypoint (YahtzeeServer), session manager (GameSession), per-player socket handler (PlayerHandler)
- /src/main/java/yahtzee → client application:
– controller/GameController.java: orchestrates UI ↔ model ↔ network
– model/: game logic (Dice, Player, ScoreCard, Categories, Game)
– network/: Message, MessageType, NetworkClient (JSON over TCP via Gson)
– view/: Swing panels (LobbyPanel, GamePanel, ScoreBoard, YahtzeeDice)
- Resources: images for GUI.
- Build/deploy: pom.xml (Maven), fat-jar packaging, Systemd unit on AWS.

##### Key Architecture Insights from README
- Both clients open persistent TCP sockets to the public AWS server.
- Server uses a fixed thread-pool: each pair of connections spawns a GameSession.
- MVC on the client side; message-based protocol for all state updates.

a. Main Components
- Client Application
- View Layer (Swing panels)
- Controller (GameController)
- Model (Game, Player, ScoreCard, Dice, Categories)
- NetworkClient (socket connection, JSON↔Message)
- Server Application
- YahtzeeServer (listener, thread-pool)
- PlayerHandler (one per socket)
- GameSession (pairs two PlayerHandlers, runs game loop)
- External Environment
- AWS EC2 (public IP)
- TCP/IP network
b. Relationships / Data Flows
- Clients establish two TCP connections to the server’s listen port (12345).
- NetworkClient sends/receives Message objects (JSON) to/from PlayerHandler.
- PlayerHandler hands messages into GameSession.
- GameSession manipulates server-side Game model and broadcasts state updates back through PlayerHandlers.
- On the client, GameController translates incoming messages into model updates and view rendering calls.
c. Architectural Patterns
- Client: MVC (Model–View–Controller separation)
- Server: thread-pooled, session-based concurrency
- Communication: message-based protocol over raw TCP, JSON serialization via Gson
d. Technologies & Frameworks
- Java 21, Maven
- Swing + FlatLaf (UI)
- AWS EC2 + Systemd (hosting)
- Gson (JSON)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Infrastructure
    TCP["TCP/IP Network"]:::external
    AWS["AWS EC2"]:::external

    subgraph "Client Application"
        direction TB
        subgraph "View Layer"
            GP["GamePanel"]:::client
            LP["LobbyPanel"]:::client
            SB["ScoreBoard"]:::client
            SG["ScoreGroup"]:::client
            SSG["StaticScoreGroup"]:::client
            YD["YahtzeeDice"]:::client
        end
        YC["YahtzeeFrame"]:::client
        CC["GameController"]:::client
        subgraph "Model"
            GM["Game"]:::client
            PL["Player"]:::client
            SC["ScoreCard"]:::client
            SE["ScoreEntry"]:::client
            DI["Dice"]:::client
            CA["Categories"]:::client
        end
        subgraph "Network & Messaging"
            NC["NetworkClient"]:::client
            M["Message"]:::client
            MT["MessageType"]:::client
        end
    end

    subgraph "Server Application"
        direction TB
        SS["YahtzeeServer"]:::server
        PH["PlayerHandler"]:::server
        GS["GameSession"]:::server
    end

    %% Data Flows
    GP -->|"UIActions"| CC
    LP -->|"UIActions"| CC
    SB -->|"UIActions"| CC
    SG -->|"UIActions"| CC
    SSG -->|"UIActions"| CC
    YD -->|"UIInteractions"| CC
    YC -->|"Initializes"| CC

    CC -->|"SendJSONMessage"| NC
    NC -->|"JSONoverTCP"| TCP
    TCP -->|"IncomingConnection"| PH
    PH -->|"PairingHandlers"| GS
    GS -->|"GameStateUpdates"| PH
    PH -->|"JSONMessage"| NC
    NC -->|"DeliverMessage"| CC

    CC -->|"UpdateModel"| GM
    CC -->|"UpdateModel"| PL
    CC -->|"UpdateModel"| SC
    CC -->|"UpdateModel"| SE
    CC -->|"UpdateModel"| DI
    CC -->|"UpdateModel"| CA
    CC -->|"InvokeViewUpdates"| GP
    CC -->|"InvokeViewUpdates"| LP
    CC -->|"InvokeViewUpdates"| SB

    TCP -->|"HostedOn"| AWS
    SS -->|"ManagesThreadPool"| PH
    PH -->|"DelegatedBy"| SS
    PH -->|"SpawnsInPairs"| GS

    %% Click Events
    click GP "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/GamePanel.java"
    click LP "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/LobbyPanel.java"
    click SB "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/ScoreBoard.java"
    click SG "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/ScoreGroup.java"
    click SSG "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/StaticScoreGroup.java"
    click YD "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/view/YahtzeeDice.java"
    click CC "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/controller/GameController.java"
    click GM "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/model/Game.java"
    click PL "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/model/Player.java"
    click SC "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/model/ScoreCard.java"
    click SE "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/model/ScoreEntry.java"
    click DI "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/model/Dice.java"
    click CA "https://github.com/zahidesad/yahtzeegame/tree/main/src/main/java/yahtzee/model/Categories"
    click NC "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/network/NetworkClient.java"
    click M "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/network/Message.java"
    click MT "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/network/MessageType.java"
    click YC "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/yahtzee/YahtzeeFrame.java"
    click SS "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/server/YahtzeeServer.java"
    click PH "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/server/PlayerHandler.java"
    click GS "https://github.com/zahidesad/yahtzeegame/blob/main/src/main/java/server/GameSession.java"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#000,color:#000
    classDef server fill:#90EE90,stroke:#000,color:#000
    classDef external fill:#D3D3D3,stroke:#666,color:#000
```
