Skip to main content

Automatic detection of chess tactical themes in PGN games

Project description

chess_detect

Automatic detection of chess tactical themes in PGN games using python-chess.

Analyzes PGN files and annotates moves with tactical motifs: forks, pins, skewers, discovered checks, and more. Supports Russian and English output with visual PGN arrow annotations.

Документация на русском

Installation

pip install chess-detect

For development:

git clone https://github.com/aslyamov/chess_detect.git
cd chess_detect
pip install -e ".[dev]"

Quick Start

from chess_detect import ChessDetector

detector = ChessDetector(lang="en")  # or lang="ru"

pgn = """
1. e4 e5 2. Nf3 Nc6 3. Bb5 a6
"""

result = detector.analyze(pgn)
print(result)
# PGN with tactical annotations in comments

Supported Tactics

Theme Russian English
Double Check Двойной шах Double Check
Fork Вилка Fork
Discovered Check Вскрытый шах Discovered Check
Pin Связка Pin
Skewer Копьё Skewer
Trapped Piece Пойманная фигура Trapped Piece
Hanging Capture Взятие висящей фигуры Hanging Capture
Removing Defender (material) Уничтожение защитника (выигрыш фигуры) Removing Defender (winning material)
Removing Defender (mate) Уничтожение защитника (угроза мата) Removing Defender (mate threat)
Exploiting Pin Использование связки Exploiting Pin

Planned

  • Deflection
  • Decoy
  • Zwischenzug
  • Discovered Attack
  • Back Rank Mate
  • Smothered Mate

PGN Arrow Annotations

Detected tactics are visualized with green arrows ([%cal]) and square highlights ([%csl]) in the PGN output. These annotations are displayed by chess viewers like Lichess and chess.com.

Example output:

1. Nc7+ { [%csl Ga8,Ge8][%cal Gc7a8,Gc7e8] Fork (Nc7 → Qa8, Ke8) }

Architecture

chess_detect/
├── __init__.py          # Exports ChessDetector
├── detector.py          # Main class
├── context.py           # MoveContext — lazy evaluation
├── utils.py             # Utilities (piece values, vulnerability checks)
├── i18n/                # Translations
│   ├── ru.py
│   └── en.py
└── detectors/
    ├── base.py          # BaseDetector
    └── tactics/
        ├── double_check.py
        ├── fork.py
        ├── discovered_check.py
        ├── pin.py
        ├── skewer.py
        ├── trapped.py
        ├── hanging_capture.py
        ├── removing_defender.py
        └── exploiting_pin.py

Adding a New Detector

from ..base import BaseDetector
from ...context import MoveContext

class MyTacticDetector(BaseDetector):
    key = "my_tactic"  # i18n key

    def detect(self, ctx: MoveContext) -> bool:
        # ctx.board_before — position before the move
        # ctx.board_after — position after the move
        # ctx.move — the move itself
        # ctx.is_check — is it check?
        # ctx.is_capture — is it a capture?
        # ctx.captured_piece — captured piece
        return False
  1. Create a file in detectors/tactics/
  2. Add translations to i18n/ru.py and i18n/en.py
  3. Register in detector.py

Tests

python -m pytest tests/ -v

89 tests — all passing.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chess_detect-0.1.0.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chess_detect-0.1.0-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file chess_detect-0.1.0.tar.gz.

File metadata

  • Download URL: chess_detect-0.1.0.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for chess_detect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eea9015a221fb4b0250953135431ecc94f0789644aa9109934dcdfafae5fa869
MD5 6bc46557526a0bfe889d137f3f9a84eb
BLAKE2b-256 50f098e9dc24d36fe71ea61feb0c9821e39bc36913b6a9c26e3b31e50e031c2a

See more details on using hashes here.

File details

Details for the file chess_detect-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: chess_detect-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for chess_detect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e2d6df652775fd8c9de544cc611d9e3e492f3d5877d48b325bf2160047ed3db
MD5 093c75142b4e0734c3ec274313e9e006
BLAKE2b-256 371234d4f6f8f8e67e9ee8ee353ae3dad0dc90b997e36cd6b22988ca67290a97

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page