# vincentzyx/douzero_for_hlddz_fullauto architecture

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

- Interactive diagram: https://gitdiagram.com/vincentzyx/douzero_for_hlddz_fullauto
- Repository: https://github.com/vincentzyx/douzero_for_hlddz_fullauto

## Overview

##### Project’s Core Purpose and Type
- It is an automation solution for a popular card game (Happy Dou Di Zhu), integrating AI decision-making through reinforcement learning and supervised learning models.
- It builds upon the original DouZero and its Happy Dou Di Zhu variant, so the architecture reflects components for game screen capture, environment simulation, AI inference (using deep learning models like ResNet), and the user interface.

##### Main Components
a. User Interface (UI) Layer:
– Files such as MainWindow.py and MainWindow.ui define the visual controls.
– The UI allows manual/automatic toggling; it also outputs key instructions (e.g., auto-start, auto-continue) after capturing the game screen.

b. Game Environment and Screen Capture:
– The project requires capturing game screens for decision making (see debug_screenshot.py).
– Components in the douzero/env folder (e.g., game.py, move_detector.py, move_generator.py) encapsulate logic to detect game state, moves, and interactions with the game’s window.

c. AI Models and Decision Logic:
– Core AI components include BidModel.py, FarmerModel.py, LandlordModel.py. These encapsulate decision-making logic for bidding, playing cards, and doubling.
– The project leverages pretrained deep neural network models (stored in the baselines/resnet and weights directories) with ResNet-based checkpoints.

d. Data and Configuration:
– Configuration files like requirements.txt and data.json, as well as weight pickle files under the weights folder, provide necessary dependencies and pretrained parameters.
– Also include the README and usage documents (使用说明.docx) as supporting documentation.

##### Relationships and Flow
- The screen capture mechanism (using debug_screenshot.py and integrated window handling) collects game state information.
- This information is sent to the Game Environment modules to interpret the current game setting (cards, moves, etc.).
- The interpreted state then drives the AI Models (bid, farmer, landlord) which load the appropriate weights from the baselines/resnet and weights directories.
- Results (e.g., the decision to bid, call, or double) are fed back and displayed on the UI, and commands are dispatched to interact with the game.
- In parallel, evaluation modules (found under douzero/evaluation) may simulate multiple game scenarios, enhancing and validating the AI’s decisions.

##### Architectural Patterns and Design Principles
- The design is modular – separating UI, environment detection, and AI decision-making logic for maintainability and ease of testing.
- It follows a loosely coupled pipeline architecture: input (game screen) → processing (environment analysis) → decision (model inference) → output (command issuance).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface Layer"
        UI_py["MainWindow.py"]:::ui
        UI_ui["MainWindow.ui"]:::ui
    end

    subgraph "Game Environment & Screen Capture" 
        DSC["debug_screenshot.py"]:::env
        subgraph "Game Environment"
            GAME["game.py"]:::env
            MOVE_DET["move_detector.py"]:::env
            MOVE_GEN["move_generator.py"]:::env
        end
    end

    subgraph "AI Decision Engine – Models & Inference"
        BID["BidModel.py"]:::ai
        FARMER["FarmerModel.py"]:::ai
        LANDLORD["LandlordModel.py"]:::ai
        RESNET["baselines/resnet"]:::ai
        WEIGHTS["weights"]:::ai
        RLUTIL["douzero/dmc"]:::ai
    end

    subgraph "Evaluation & Simulation Module"
        EVAL["douzero/evaluation"]:::eval
    end

    subgraph "Data & Configuration"
        REQ["requirements.txt"]:::data
        DATA["data.json"]:::data
        README["README.md"]:::data
        USAGE["使用说明.docx"]:::data
    end

    %% Data Flow
    UI_py -->|"initiate"| DSC
    UI_ui -->|"initiate"| DSC

    DSC -->|"capture screen"| GAME
    DSC -->|"capture screen"| MOVE_DET
    DSC -->|"capture screen"| MOVE_GEN

    GAME -->|"game state"| BID
    MOVE_DET -->|"state detection"| BID
    MOVE_GEN -->|"move generation"| BID

    BID -->|"decision"| UI_py
    FARMER -->|"decision"| UI_ui
    LANDLORD -->|"decision"| UI_py
    
    RESNET --> BID
    RESNET --> FARMER
    RESNET --> LANDLORD

    WEIGHTS --> BID
    WEIGHTS --> FARMER
    WEIGHTS --> LANDLORD

    RLUTIL --> BID
    RLUTIL --> FARMER
    RLUTIL --> LANDLORD

    BID --- FARMER
    FARMER --- LANDLORD

    BID -->|"simulation feedback"| EVAL
    FARMER --> EVAL
    LANDLORD --> EVAL

    %% Data and Configurations influence UI and AI
    REQ --> UI_py
    DATA --> BID
    README --> UI_ui
    USAGE --> UI_ui

    %% Click Events
    click UI_py "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/MainWindow.py"
    click UI_ui "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/MainWindow.ui"
    click DSC "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/debug_screenshot.py"
    click GAME "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/douzero/env/game.py"
    click MOVE_DET "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/douzero/env/move_detector.py"
    click MOVE_GEN "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/douzero/env/move_generator.py"
    click BID "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/BidModel.py"
    click FARMER "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/FarmerModel.py"
    click LANDLORD "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/LandlordModel.py"
    click RESNET "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/tree/main/baselines/resnet"
    click WEIGHTS "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/tree/main/weights"
    click EVAL "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/tree/main/douzero/evaluation"
    click RLUTIL "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/tree/main/douzero/dmc"
    click REQ "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/requirements.txt"
    click DATA "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/data.json"
    click README "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/README.md"
    click USAGE "https://github.com/vincentzyx/douzero_for_hlddz_fullauto/blob/main/使用说明.docx"

    %% Classes for styling
    classDef ui fill:#a6cee3,stroke:#1f78b4,stroke-width:2px;
    classDef env fill:#b2df8a,stroke:#33a02c,stroke-width:2px;
    classDef ai fill:#fdbf6f,stroke:#ff7f00,stroke-width:2px;
    classDef eval fill:#cab2d6,stroke:#6a3d9a,stroke-width:2px;
    classDef data fill:#dddddd,stroke:#888888,stroke-width:2px;
```
