Search code examples
pythonpython-3.xchessstockfish

Find out if and how a chess game was finished


I'm using Stockfish 15 and Python 3.10 to build a browser chessgame. Now I'm struggling to figure out if a game is over (win, lose, draw).

I have now searched for hours in forums etc. for an answer and found none. Can anyone help me further?


Solution

  • The python-chess package has a board.is_checkmate() option:

    python-chess.readthedocs.io/en/latest

    Any game that is not a checkmate can be a draw by repetition, so having the history of the moves in the game is necessary. Knowing the history of the game, python-chess can also look for draws, stalemates, general outcome, etc.

    import chess
    board = chess.Board()
    
    board.is_checkmate()
    board.is_stalemate()
    board.outcome()
    board.can_claim_draw()
    board.can_claim_threefold_repetition()
    board.can_claim_fifty_moves()
    board.is_insufficient_material()
    board.is_fivefold_repetition()
    board.is_seventyfive_moves()