Skip to main content

Wraps the open-source Stockfish chess engine for easy integration into python.

Project description

Stockfish

Implements an easy-to-use Stockfish class to integrates the Stockfish chess engine with Python.

Install

$ pip install stockfish

Ubuntu

$ sudo apt install stockfish

Mac OS

$ brew install stockfish

Features and usage examples

Initialize Stockfish class

You should install the stockfish engine in your operating system globally or specify path to binary file in class constructor

from stockfish import Stockfish

stockfish = Stockfish(path="/Users/zhelyabuzhsky/Work/stockfish/stockfish-9-64")

There are some default engine's settings:

{
    "Write Debug Log": "false",
    "Contempt": 0,
    "Min Split Depth": 0,
    "Threads": 1,
    "Ponder": "false",
    "Hash": 16,
    "MultiPV": 1,
    "Skill Level": 20,
    "Move Overhead": 30,
    "Minimum Thinking Time": 20,
    "Slow Mover": 80,
    "UCI_Chess960": "false",
}

You can change them, as well as the default search depth, during your Stockfish class initialization:

stockfish = Stockfish(path="/Users/zhelyabuzhsky/Work/stockfish/stockfish-9-64", depth=18, parameters={"Threads": 2, "Minimum Thinking Time": 30})

Set position by sequence of moves

stockfish.set_position(["e2e4", "e7e6"])

Update position by making a sequence of moves from the current position

stockfish.make_moves_from_current_position(["g4d7", "a8b8", "f1d1"])

Set position by Forsyth–Edwards Notation (FEN)

stockfish.set_fen_position("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")

Get best move

stockfish.get_best_move()
d2d4

Get best move based on a time constraint

stockfish.get_best_move_time(1000)

Time constraint is in milliseconds

e2e4

Check is move correct with current position

stockfish.is_move_correct('a2a3')
True

Get info on the top n moves

stockfish.get_top_moves(3)
[
    {'Move': 'f5h7', 'Centipawn': None, 'Mate': 1},
    {'Move': 'f5d7', 'Centipawn': 713, 'Mate': None},
    {'Move': 'f5h5', 'Centipawn': -31, 'Mate': None}
]

Get Stockfish's win/draw/loss stats for the side to move in the current position

stockfish.get_wdl_stats()
[87, 894, 19]

Find if your version of Stockfish is recent enough to display WDL stats

stockfish.does_current_engine_version_have_wdl_option()
True

Tell Stockfish whether or not to display WDL stats after analyzing a position

stockfish.set_show_wdl_option(True)

Set current engine's skill level (ignoring ELO rating)

stockfish.set_skill_level(15)

Set current engine's ELO rating (ignoring skill level)

stockfish.set_elo_rating(1350)

Set current engine's depth

stockfish.set_depth(15)

Get current engine's parameters

stockfish.get_parameters()
{
    'Write Debug Log': 'false',
    'Contempt': 0,
    'Min Split Depth': 0,
    'Threads': 1,
    'Ponder': 'false',
    'Hash': 16,
    'MultiPV': 1,
    'Skill Level': 20,
    'Move Overhead': 30,
    'Minimum Thinking Time': 20,
    'Slow Mover': 80,
    'UCI_Chess960': 'false'
}

Get current board position in Forsyth–Edwards notation (FEN)

stockfish.get_fen_position()
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

Get current board visual

stockfish.get_board_visual()
+---+---+---+---+---+---+---+---+
| r | n | b | q | k | b | n | r | 8
+---+---+---+---+---+---+---+---+
| p | p | p | p | p | p | p | p | 7
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 6
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 5
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 4
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 3
+---+---+---+---+---+---+---+---+
| P | P | P | P | P | P | P | P | 2
+---+---+---+---+---+---+---+---+
| R | N | B | Q | K | B | N | R | 1
+---+---+---+---+---+---+---+---+
  a   b   c   d   e   f   g   h

Get current board evaluation in centipawns or mate in x

stockfish.get_evaluation()

Positive is advantage white, negative is advantage black

{"type":"cp", "value":12}
{"type":"mate", "value":-3}

Run benchmark

BenchmarkParameters

params = BenchmarkParameters(**kwargs)

parameters required to run the benchmark function. kwargs can be used to set custom values.

ttSize: range(1,128001)
threads: range(1,513)
limit: range(1,10001)
fenFile: "path/to/file.fen"
limitType: "depth", "perft", "nodes", "movetime"
evalType: "mixed", "classical", "NNUE"
stockfish.benchmark(params)

This will run the bench command with BenchmarkParameters. It is an additional custom non-UCI command, mainly for debugging. Do not use this command during a search!

Get current major version of stockfish engine

stockfish.get_stockfish_major_version()
11

Find if the version of Stockfish being used is a development build

stockfish.is_development_build_of_engine()
False

Testing

$ python setup.py test

Security

If you discover any security related issues, please email zhelyabuzhsky@icloud.com instead of using the issue tracker.

Credits

License

MIT License. Please see License File for more information.

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

stockfish-3.22.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

stockfish-3.22.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file stockfish-3.22.1.tar.gz.

File metadata

  • Download URL: stockfish-3.22.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for stockfish-3.22.1.tar.gz
Algorithm Hash digest
SHA256 2910b9c9ad1a7b8bf6d126f4ec2b66615bffa32151441a8753d5c48c16e21287
MD5 45d7f1c81fcbd0f31aade6462eec8027
BLAKE2b-256 eb7fec70835463d60c4ca9c495d25ea4daa3b779ab9301d62e7dba0986b13b81

See more details on using hashes here.

File details

Details for the file stockfish-3.22.1-py3-none-any.whl.

File metadata

  • Download URL: stockfish-3.22.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for stockfish-3.22.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f229ae31fa97720cabef689675f6c9fe5a41261117b77cd2ae182528e2b20370
MD5 3b798bbf31a0a664cd360c683d31e249
BLAKE2b-256 71dad5a261d14d295d462346b9c2f034f7a80a34ea3f139f2fe158564b578d81

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