# anselchang/tetrisfish architecture

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

- Interactive diagram: https://gitdiagram.com/anselchang/tetrisfish
- Repository: https://github.com/anselchang/tetrisfish

## Overview

##### Main Components
- Main Application: Represented by main.py, which orchestrates the initialization and coordination of core modules.
- User Interface (UI) Module: Built on Pygame, this includes assets from the Images folder and modules like PygameButton.py to handle user interactions and rendering.
- Board Analysis Engine: Consisting of Analysis.py, AnalysisBoard.py, AnalysisConstants.py, EvalGraph.py, Evaluator.py, and TetrisUtility.py. This component is responsible for processing board states, running analysis algorithms, and determining mistakes.
- Calibration Engine: Split between Callibration.py (with its unique naming) and the calibrate directory containing modules such as autofindfield.py, autolayout.py, blockmatch.py, etc. This system adjusts for variances in board images and ensures proper extraction of board states.
- Video/Rendering Module: Involves RenderVideo.py and possibly Render from the Images folder, handling the display and post-processing of video data.
- External Communication: Although not represented by a separate file, the README emphasizes that a stable internet connection is needed for the API communication with StackRabbit. This should be highlighted as a connector or adapter that links the analysis engine with the external Tetris AI.
- Bundling and Build Scripts: The bundle directory and files such as build scripts (build_mac_os.sh, build_windows.ps1, etc.) reflect deployment and packaging concerns.

##### Relationships and Data Flow
- Calibration Engine back to the Analysis Engine to show that accurate board state extraction depends on the calibration process.
- Out the dependency of the main application (main.py) to all modules, emphasizing it as the orchestrator.
- Utility functions and supporting modules as sub-components that feed into the analysis process.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly separate the visualization (UI), processing (analysis/evaluation), and calibration (image correction) portions.
- Modularity: Note that functionalities (such as calibration and board analysis) are implemented in isolated modules, making them replaceable or upgradable. Each module is responsible for a specific set of tasks.
- External Integration: Emphasize the API connector with StackRabbit, showing how the system is designed to extend its capabilities via external services.

## Diagram (Mermaid)

```mermaid
graph TD
    MA["Main Application"]
    UI["UI Module – Pygame Interface"]
    BA["Board Analysis Engine"]
    CE["Calibration Engine"]
    VR["Video/Rendering Module"]
    EX["External API Connector (StackRabbit)"]
    UT["Utility & Support Modules"]
    BD["Build & Deployment Scripts"]

    %% Main orchestrator dependencies
    MA --> UI
    MA --> BA
    MA --> CE
    MA --> VR
    MA --> UT
    MA --> BD

    %% UI triggered actions
    UI -->|triggers| BA
    UI -->|triggers| CE

    %% Calibration refines analysis
    CE -->|supplies data to| BA

    %% Analysis communicates externally
    BA -->|communicates with| EX

    %% Click Events
    click MA "https://github.com/anselchang/tetrisfish/blob/main/main.py"
    click UI "https://github.com/anselchang/tetrisfish/blob/main/PygameButton.py"
    click BA "https://github.com/anselchang/tetrisfish/blob/main/Analysis.py"
    click CE "https://github.com/anselchang/tetrisfish/blob/main/Callibration.py"
    click VR "https://github.com/anselchang/tetrisfish/blob/main/RenderVideo.py"
    click UT "https://github.com/anselchang/tetrisfish/blob/main/Position.py"
    click BD "https://github.com/anselchang/tetrisfish/tree/main/bundle"

    %% Styles
    class MA main;
    class UI ui;
    class BA processing;
    class CE calibration;
    class VR video;
    class EX external;
    class UT utility;
    class BD build;

    classDef main fill:#B2EBF2,stroke:#333,stroke-width:2px;
    classDef ui fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef processing fill:#C8FACC,stroke:#333,stroke-width:2px;
    classDef calibration fill:#FFF2A8,stroke:#333,stroke-width:2px;
    classDef video fill:#FFD1DC,stroke:#333,stroke-width:2px;
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:2px;
    classDef utility fill:#D3D3D3,stroke:#333,stroke-width:2px;
    classDef build fill:#D1C4E9,stroke:#333,stroke-width:2px;
```
