2

I am trying to create an engine but my evaluation function is terrible so I decided to use stockfish to evaluate it for me.

import chess
import chess.engine

def stockfish_evaluation(board, time_limit = 0.01):
    engine = chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64")
    result = engine.play(board, chess.engine.Limit(time=time_limit))
    return result.info["score"]

board = chess.Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
result = stockfish_evaluation(board)
print(result)

but it prints out:

KeyError: 'score'
TheLizzard
  • 7,248
  • 2
  • 11
  • 31

1 Answers1

6

I think you are using the wrong function, it should be engine.analyse not engine.play

import chess
import chess.engine

def stockfish_evaluation(board, time_limit = 0.01):
    engine = chess.engine.SimpleEngine.popen_uci("stockfish_10_x64")
    result = engine.analyse(board, chess.engine.Limit(time=time_limit))
    return result['score']

board = chess.Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
result = stockfish_evaluation(board)
print(result)

#+58
ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14