2

I am working on a simple cheat detection script to find if a user used one of the top engine moves in a game, so I found this great python-chess library Additionally, I found this example to getting the moves score. But that topic is an old version of the lib so I tried to update the script. However, I keep getting errors, and I don't know how to get the scores in the right way.

import asyncio
import chess
#import chess.uci
import chess.engine
import chess.pgn
import io
#Let's try our code with the starting position of chess:

fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
board = chess.Board(fen)


#Now make sure you give the correct location for your stockfish engine file
#...in the line follows: e.g., /home/.../stockfish_6_x64


engine = chess.engine.SimpleEngine.popen_uci('/home/shar/Desktop/cheat detect python/stockfish-10-linux/Linux/stockfish_10_x64')

if board.turn: print( 'White to move');
else: print( 'black to move');

for el in board.legal_moves:



    pgn = io.StringIO(board.san(el))

    thnwpgntofen = chess.pgn.read_game(pgn);
    nwinfo = engine.analysis(thnwpgntofen.board());



    print(nwinfo["score"])


    print("-------------------------");

I get this error after running my code:

Traceback (most recent call last): File "prntengmv.py", line 37, in print(nwinfo["score"]) TypeError: 'SimpleAnalysisResult' object is not subscriptable

3

As per: https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis

engine.analysis(board) now returns an AnalysisResult object, which has the property info that you want.

So instead of:

nwinfo = engine.analysis(...)
print(nwinfo["score"])

You would loop over info in the AnalysisResult, and print the score property from each element:

with engine.analysis(...) as analysis:
    for info in analysis:
        print(info.get("score"))

However, your use case looks like it may benefit from engine.analyse more than engine.analysis.

  • thanks now the error gones but i getting a hundreds of outputs – shar Aug 7 at 14:25
  • This is because engine.analysis gives an indefinite analysis - i.e. the engine will keep analyzing forever. You can resolve this by setting some termination condition and exiting the loop, or you could use engine.analyse instead, which allows you to set a limit beforehand. Check the documentation link in my answer. – konsolas Aug 7 at 14:32
  • every time i run my script i getting a different score are for the same moves are that normal ? – shar Aug 7 at 14:46
  • Engines aren't wholly deterministic. As long as the scores are reasonable each time, it's probably correct. – konsolas Aug 7 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.