Architecture & Data Flow
Chess Analyzer Pro follows a Model-View-Controller (MVC) oriented architecture to ensure separation of concerns between the chess logic, the user interface, and the data management.
High-Level Pattern
- Model (Backend): Handles the core logic and data state.
- State:
GameAnalysisobjects hold moves, evaluation scores, and metadata. - Computation: The
Analyzerclass manages the heavy lifting of communicating with Stockfish.
- State:
- View (GUI): Displays the data to the user.
- PyQt6: The entire UI is built using the Qt framework.
- Components:
BoardWidget(Visual Board),AnalysisPanel(Stats/Graphs),MoveListPanel,MetricsWidget(Dashboard).
- Controller (Logic): Orchestrates the application.
- Signals & Slots: The robust event system in Qt connects UI actions (like clicking "Next Move") to backend functions (like "Load Board State").
Directory Structure
Chess_analyzer/
├── main.py # Application entry point
├── config.json # User configuration (saved settings)
├── analysis_cache.db # SQLite database for game history & cache
├── requirements.txt # Python dependencies
├── build.spec # PyInstaller configuration
│
├── assets/ # Static resources
│ ├── icons/ # SVG/PNG icons for classifications
│ ├── images/ # Logo, backgrounds
│ ├── pieces/ # Piece theme directories (cburnett, merida, etc.)
│ └── sounds/ # Audio files for moves, captures, etc.
│
├── src/ # Source code
│ ├── backend/ # Core logic, analysis, and data handling
│ │ ├── analyzer.py # Main analysis engine & move classification
│ │ ├── base_api.py # Shared API utilities
│ │ ├── book.py # Opening book lookups
│ │ ├── cache.py # Position analysis caching
│ │ ├── chess_com_api.py # Chess.com API integration
│ │ ├── engine.py # Stockfish UCI wrapper
│ │ ├── game_history.py # SQLite game persistence
│ │ ├── gemini_service.py # Google Gemini AI integration
│ │ ├── lichess_api.py # Lichess API integration
│ │ ├── models.py # Data structures (GameAnalysis, MoveAnalysis)
│ │ └── pgn_parser.py # PGN file parsing
│ │
│ ├── gui/ # User Interface components (PyQt6)
│ │ ├── main_window.py # Root application window
│ │ ├── analysis_view.py # Analysis dashboard layout
│ │ ├── analysis_worker.py # Background analysis thread
│ │ ├── graph_widget.py # Evaluation curve visualization
│ │ ├── game_list.py # Game selection dialogs
│ │ ├── sidebar.py # Navigation sidebar
│ │ ├── styles.py # QSS theming & color constants
│ │ ├── gui_utils.py # Shared UI helper functions
│ │ ├── live_analysis.py # Real-time position analysis
│ │ ├── loading_widget.py # Loading overlay animations
│ │ ├── metrics_widget.py # Stats dashboard with charts
│ │ │
│ │ ├── analysis/ # Analysis-specific widgets
│ │ │ ├── captured.py # Captured pieces display
│ │ │ └── controls.py # Game navigation buttons
│ │ │
│ │ ├── board/ # Chessboard components
│ │ │ ├── board_widget.py # Main board renderer
│ │ │ ├── eval_bar.py # Evaluation bar widget
│ │ │ └── piece_themes.py # Piece set management
│ │ │
│ │ ├── components/ # Reusable UI components
│ │ │ └── stat_card.py # Stat display card widget
│ │ │
│ │ ├── dialogs/ # Modal dialogs
│ │ │ ├── game_selection_dialog.py # Game picker dialog
│ │ │ └── splash_screen.py # Startup splash screen
│ │ │
│ │ ├── metrics/ # Dashboard charts & workers
│ │ │ ├── charts.py # Chart generation (donut, line)
│ │ │ └── workers.py # Background stat calculation
│ │ │
│ │ └── views/ # Main view pages
│ │ ├── history_view.py # Game history browser
│ │ └── settings_view.py # Settings panel
│ │
│ └── utils/ # Helper utilities
│ ├── config.py # Configuration file management
│ ├── logger.py # Logging setup
│ ├── path_utils.py # Resource path resolution
│ └── resources.py # Asset loading helpers
│
├── tests/ # pytest suite
Key Components
Backend (src/backend/)
| Component | Responsibility |
|---|---|
analyzer.py | The Brain. Manages the analysis queue, calculates Win Probability Loss (WPL), determines move classifications (Brilliant, Blunder, etc.), computes accuracy using Lichess-style algorithm, and updates the game state. |
engine.py | Stockfish Bridge. Wraps the Stockfish process using python-chess. Handles UCI protocol commands (position, go, stop) and parses engine output. |
models.py | Data Types. Single source of truth for data structures: GameAnalysis, MoveAnalysis, GameMetadata. |
pgn_parser.py | Data Ingestion. Robust parsing of .pgn files to extract moves, headers, and comments. |
game_history.py | Persistence. SQLite database manager for saving/loading analyzed games with full metadata and move data. |
cache.py | Performance. Caches engine analysis results by FEN to avoid re-analyzing known positions. |
book.py | Opening Detection. Interfaces with opening book to identify known opening positions. |
chess_com_api.py | Chess.com Integration. Fetches games from Chess.com usernames or game URLs. |
lichess_api.py | Lichess Integration. Fetches games from Lichess usernames or game URLs with token auth. |
gemini_service.py | AI Summaries. Connects to Google's Gemini API to generate natural language game summaries and coaching insights. |
base_api.py | API Utilities. Shared request handling, error logging, and JSON parsing for API classes. |
GUI (src/gui/)
| Component | Responsibility |
|---|---|
main_window.py | Root Container. Initializes the application window, sidebar, page stack, and routes global events. Manages game loading and analysis orchestration. |
analysis_view.py | Dashboard. The central hub displaying the board, stats, graphs, and move list side-by-side. Contains AnalysisPanel and MoveListPanel. |
board/board_widget.py | Visuals. Renders the chess board with customizable themes, pieces, arrows (for best moves), and highlights (last move, check). |
board/eval_bar.py | Evaluation Bar. Vertical bar showing white/black advantage percentage. |
board/piece_themes.py | Piece Sets. Manages loading and applying different piece styles (cburnett, merida, etc.). |
graph_widget.py | Visualization. Uses matplotlib to draw the evaluation curve with hover tooltips and classification markers. |
metrics_widget.py | Statistics Dashboard. Displays aggregate performance metrics, charts (donut, line), openings analysis, and AI coaching insights. |
views/settings_view.py | Settings Panel. UI for configuring engine path, API keys, themes, and usernames. |
views/history_view.py | Game History. Browse, reload, and manage previously analyzed games. |
dialogs/splash_screen.py | Startup Screen. Displays branded splash with progress during app initialization. |
dialogs/game_selection_dialog.py | Game Picker. Modal dialog for selecting from multiple loaded games. |
styles.py | Theming. Contains CSS-like stylesheets (QSS) for the application. Defines colors, fonts, and widget styles. |
gui_utils.py | Helpers. Reusable utility functions for creating buttons, labels, and other common UI patterns. |
Utils (src/utils/)
| Component | Responsibility |
|---|---|
config.py | Manages config.json for saving user settings (theme, engine path, API keys, board/piece themes, usernames). |
logger.py | Sets up the application logging (writes to chess_analyzer.log). |
path_utils.py | Resolves resource paths correctly for both development and PyInstaller builds. |
resources.py | Helper functions for loading images, icons, and sounds from the assets directory. |
Data Flow Example: Analysis Loop
- User Action: User clicks "Analyze Game".
- Controller:
MainWindowcreates anAnalysisWorkerthread and starts it. - Backend Processing:
AnalysisWorkercallsAnalyzer.analyze_game()with theGameAnalysisobject.Analyzeriterates through every move in the game.- For each position, it checks
cache.pyfor existing analysis. - If not cached, it sends the FEN to
engine.py. engine.pyqueries Stockfish and returns multi-PV analysis.Analyzercalculates win probability, move accuracy, and classifies the move.- Results are cached for future use.
- UI Update:
AnalysisWorkeremitsprogress_updatesignal with current/total move count.MainWindowupdates the progress display.- When complete,
finishedsignal triggers UI refresh.
- Persistence:
GameHistoryManager.save_game()stores the analyzed game to SQLite.
Data Flow: Stats Dashboard
- User Action: User navigates to Stats page.
- Background Worker:
StatsWorkerruns in a separate thread to calculate aggregate statistics. - Data Collection: Worker queries
GameHistoryManagerfor all games matching configured usernames. - Calculation: Aggregates win/loss/draw rates, accuracy trends, opening frequency, termination types, and color performance.
- UI Update: Worker emits signals with stats;
MetricsWidgetdisplays charts and metrics. - AI Insights (optional): If Gemini API is configured, generates coaching insights from the stats.
