Implements an easy-to-use Stockfish class to integrates the Stockfish chess engine with Python.
$ pip install stockfish
# apt install stockfish
$ brew install stockfish
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("/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 during your Stockfish class initialization:
stockfish = Stockfish(parameters={"Threads": 2, "Minimum Thinking Time": 30})
stockfish.set_position(["e2e4", "e7e6"])
stockfish.set_fen_position("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
stockfish.get_best_move()
d2d4
stockfish.get_best_move_time(1000)
Time constraint is in milliseconds
e2e4
stockfish.is_move_correct('a2a3')
True
stockfish.set_skill_level(15)
stockfish.set_depth(15)
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'}
stockfish.get_fen_position()
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
stockfish.get_board_visual()
+---+---+---+---+---+---+---+---+
| r | n | b | q | k | b | n | r |
+---+---+---+---+---+---+---+---+
| p | p | p | p | p | p | p | p |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| P | P | P | P | P | P | P | P |
+---+---+---+---+---+---+---+---+
| R | N | B | Q | K | B | N | R |
+---+---+---+---+---+---+---+---+
stockfish.get_evaluation()
Positive is advantage white, negative is advantage black
{"type":"cp", "value":12}
{"type":"mate", "value":-3}
stockfish.get_stockfish_major_version()
11
$ python setup.py test
If you discover any security related issues, please email zhelyabuzhsky@icloud.com instead of using the issue tracker.
MIT License. Please see License File for more information.