Skip to main content

Moonfish is a didactic Python chess engine showcasing parallel search algorithms and modern chess programming techniques.

Project description

moonfish

Moonfish Engine (~2000 Elo Rating Lichess.org)

Moonfish is a didactic Python chess engine designed to showcase parallel search algorithms and modern chess programming techniques. Built with code readability as a priority, Moonfish makes advanced concepts easily accessible providing a more approachable alternative to cpp engines.

The engine achieves approximately ~2000 Elo when playing against Lichess Stockfish bots (beats level 5 and loses to level 6) and includes comprehensive test suites including the Bratko-Kopec tactical test positions.

Quickstart

Requirements

  • Python 3.10

Installation and usage

Install the python library:

pip install moonfish

From python:

$ python
>>> import chess
>>> import moonfish
>>> board = chess.Board()
>>> moonfish.search_move(board)
Move.from_uci('g1f3')

You can also call the CLI, the CLI works as an UCI Compatible Engine:

$ moonfish --mode=uci
uci # <- user input
id name Moonfish
id author luccabb
uciok

You can also run it as an API:

moonfish --mode=api

Then send a request:

$ curl "http://localhost:5000/?fen=rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR%20w%20KQkq%20-%200%201&depth=4&quiescence_search_depth=3&null_move=True&null_move_r=2&algorithm=alpha_beta"
{
  "body": {
    "move": "e2e4"
  },
  "headers": {
    "Access-Control-Allow-Headers": "Content-Type",
    "Access-Control-Allow-Methods": "OPTIONS,GET",
    "Access-Control-Allow-Origin": "*"
  },
  "statusCode": 200
}

Features

Search Algorithms

  • Alpha-Beta Pruning - Negamax with α-β cutoffs
  • Lazy SMP - Shared memory parallel search utilizing all CPU cores
  • Layer-based Parallelization - Distributing work at specific search depths
  • Null Move Pruning - Skip moves to detect zugzwang positions
  • Quiescence Search - Extended search for tactical positions

Evaluation & Optimization

  • PeSTO Evaluation - Piece-square tables (PST) with tapered evaluation. Using Rofchade's PST.
  • Transposition Tables - Caching to avoid redundant calculations
  • Move Ordering - MVV-LVA (Most Valuable Victim - Least Valuable Attacker)
  • Syzygy Tablebase support for perfect endgame play
  • Opening Book integration (Cerebellum format)

Engine Interfaces

  • UCI Protocol - Compatible with popular chess GUIs
  • Web API - RESTful interface for online integration
  • Lichess Bot - Ready for deployment on Lichess.org

Configuration Options

Parameter Description Default Options
--mode Engine Mode uci uci, api
--algorithm Search algorithm alpha_beta alpha_beta, lazy_smp, parallel_alpha_beta_layer_1
--depth Search depth 3 1-N
--null-move Whether to use null move pruning False True, False
--null-mov-r Null move reduction factor 2 1-N
--quiescence-search-depth Max depth of quiescence search 3 1-N
--syzygy-path Tablebase directory None Valid path

Contributing

We welcome contributions, feel free to open PRs/Issues! Areas of interest:

  • New search algorithms
  • Improved evaluation functions
  • Time constrained search (e.g. find the best move in 40s)
  • Additional test positions
  • Github CI testing
  • Different evaluation functions
  • Neural Net integration
  • Performance benchmarking on different hardware
  • Improving caching

References

License

MIT License - see LICENSE file for details.

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

moonfish-0.0.1.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

moonfish-0.0.1-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file moonfish-0.0.1.tar.gz.

File metadata

  • Download URL: moonfish-0.0.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for moonfish-0.0.1.tar.gz
Algorithm Hash digest
SHA256 389ac3e6e1dfd3347f96623d107b1b94e95c3567df53aa0809abd1b2c9d5135e
MD5 c3df72ccbc8f06eca44185bdf3193e1d
BLAKE2b-256 e371290630908fee748ab0d050f44ac2e6a61d16ae584e7c58e668d7db54fb37

See more details on using hashes here.

File details

Details for the file moonfish-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: moonfish-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for moonfish-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba9af9d9a143e2875a2dcb9b70c4ee9dfc6e73e6aae57d69d13932d92445fd98
MD5 9b0a53d2a89d207cf7ffc0e6ff394848
BLAKE2b-256 24ffc8f21babaa3acb9d8f77cbefb9a058ace69423d300c88427c3700af376a8

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