# ericsmoon/cardgame architecture

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

- Interactive diagram: https://gitdiagram.com/ericsmoon/cardgame
- Repository: https://github.com/ericsmoon/cardgame

## Overview

##### Project Type & Purpose
- This project is a standalone, single-player card game written in Java. Its purpose is to deliver a game experience where the player competes in a turn-based card battle against the computer. The design emphasizes simplicity in the user interface while incorporating varied types of cards (attack cards, Buff/Debuff cards, counter cards) with different functionalities.
- The README also mentions that the game is implemented based on a “VRC三层结构” (three-layer structure), which is akin to a variant of Model-View-Controller (MVC) that separates the game’s visual interface, control logic, and underlying data/models.

##### Analyzing the File Structure
- The top-level file structure shows a single project managed by IntelliJ (.idea, CardGame.iml) and a README with accompanying images.
- Within the src directory, the package hierarchy (src/cn/ericmoon/cardGame) contains several sub-packages that represent logical components:
- cards: Contains definitions for different card types (Attack, Buff, Debuff, etc.)
- controller: Contains classes that handle game logic (e.g., BattleController, RoundController, and specialized controllers for different card actions).
- enumClass: Holds enumerations that help define types and properties for cards and players.
- event: Manages various in-game events like card selection, status changes, or game round transitions.
- exception: Deals with custom exceptions (e.g., NoSuchCard).
- gameRepository: Likely supplies key configurations and initial data (e.g., different key sources for gameplay logic).
- gameUtil: Contains utilities such as RandomNumTools and other helper functionalities.
- keys: Classes related to managing game input or state identifiers.
- lib: Contains external libraries like lombok.

##### Key Components
a. View Layer (Presentation):

b. Controller/Logic Layer:
- Controller Package: Manages core game logic including round management, battle control, and card usage.
- Event Package: Handles in-game events such as card mouse actions and game state changes (e.g., RestartEvent).

c. Model/Data Layer:
- Cards Package: Defines various card types with specific behaviors (Attack, Buff, Debuff, Counter).
- Enum Classes: Provide constants and enumerations to standardize game states and card types.
- Player Package: Defines player-related data.
- GameRepository & Keys: Manage configuration data (e.g., initial key setup) and game state information.
- Utility Packages: Contain helper functions (e.g., RandomNumTools) that support the business logic.

##### Relationships & Interactions
- Controllers process these events using the game’s rules (as described in the README) and interact with the Model layer (cards, players, keys) to update game state.
- The Event classes mediate communication between the View and Controller layers to trigger UI updates (e.g., showing card details or managing round transitions).
- Utility classes and gameRepositories provide auxiliary support to both Controllers and Models.

##### Architectural Patterns & Design Principles
- The project adheres to a three-layer structure (VRC), effectively separating concerns into:
- Runtime (controller and event management)
- Core data/models (cards, players, enums, repositories)
- Loose coupling between UI and business logic, with event-driven communication to handle interactions.
- Use of enumerations, exception handling, and utility methods helps maintain code clarity and modularity.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% View Layer
    subgraph "View Layer"
        GC["GameClient (UI)"]:::view
        HL["Helper (UI Support)"]:::view
        UIA["UI Assets (Images)"]:::view
    end

    %% Controller/Logic Layer
    subgraph "Controller/Logic Layer"
        BC["BattleController"]:::controller
        RC["RoundController"]:::controller
        CCC["CardCreaterController"]:::controller
        UDC["UseDamageCard"]:::controller
        UBC["UseBuffCard"]:::controller
        UDBC["UseDeBuffCard"]:::controller
        DC["DeadController"]:::controller
        CME["CardMouseEvent"]:::controller
        RE["RestartEvent"]:::controller
    end

    %% Model/Data Layer
    subgraph "Model/Data Layer"
        CARDS["Cards (Attack, Buff, Debuff, Counter)"]:::model
        ENUM["Enumerations"]:::model
        GR["GameRepository"]:::model
        KM["Keys Management"]:::model
        PM["PlayerModel"]:::model
        GU["GameUtil"]:::model
        RNG["RandomNumTools"]:::model
    end

    %% Interactions between layers
    GC -->|"triggers"| CME
    GC -->|"triggers"| RE
    GC -->|"initiatesBattle"| BC

    CME -->|"handlesDamage"| UDC
    CME -->|"handlesBuff"| UBC
    CME -->|"handlesDebuff"| UDBC

    RE -->|"restartsRound"| RC

    BC -->|"updatesCards"| CARDS
    BC -->|"updatesPlayer"| PM
    BC -->|"managesRounds"| RC
    CCC -->|"generatesCards"| CARDS
    DC -->|"handlesEndGame"| PM

    %% Model support to Controllers (Configuration/Utilities)
    GR -.->|"config"| BC
    KM -.->|"keys"| BC
    ENUM -.->|"defines"| BC
    GU -.->|"utility"| BC
    RNG -.->|"randomness"| BC

    %% Model update triggers UI refresh
    PM -->|"stateChange"| GC

    %% UI Helper uses Assets
    HL -->|"utilizes"| UIA

    %% Styling
    classDef view fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef controller fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef model fill:#ffe0b2,stroke:#e65100,stroke-width:2px;

    %% Click Events for View Layer
    click GC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/draw/GameClient.java"
    click HL "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/draw/Helper.java"
    click UIA "https://github.com/ericsmoon/cardgame/tree/master/src/cn/ericmoon/cardGame/draw/images"

    %% Click Events for Controller/Logic Layer
    click BC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/BattleController.java"
    click RC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/RoundController.java"
    click CCC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/CardCreaterController.java"
    click UDC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/UseDamageCard.java"
    click UBC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/UseBuffCard.java"
    click UDBC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/UseDeBuffCard.java"
    click DC "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/controller/DeadController.java"
    click CME "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/event/CardMouseEvent.java"
    click RE "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/event/RestartEvent.java"

    %% Click Events for Model/Data Layer
    click CARDS "https://github.com/ericsmoon/cardgame/tree/master/src/cn/ericmoon/cardGame/cards/"
    click ENUM "https://github.com/ericsmoon/cardgame/tree/master/src/cn/ericmoon/cardGame/enumClass/"
    click GR "https://github.com/ericsmoon/cardgame/tree/master/src/cn/ericmoon/cardGame/gameRepository/"
    click KM "https://github.com/ericsmoon/cardgame/tree/master/src/cn/ericmoon/cardGame/keys/"
    click PM "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/player/Player.java"
    click GU "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/gameUtil/GameUtil.java"
    click RNG "https://github.com/ericsmoon/cardgame/blob/master/src/cn/ericmoon/cardGame/tools/RandomNumTools.java"
```
