Search code examples
chess

How to code the chess stalemate rule ?


I'm trying to write a chess game and find that I cannot find solutions to find a stalemate situation. I'm trying to google, but can't find anything. Is there a well-known algorithm or something?


Solution

  • Your move generator will be one of two different designs;

    • either it checks for legality while generating the moves
    • or you generate all possible moves and remove those that are illegal afterwards.

    The former is better as it doesn't need post-processing.

    A stalemate condition is simply one where there are no legal moves and the moving-side's king is not in check. A checkmate condition is one where there are no legal moves but the moving-side's king is in check.

    In other words if you've figured out how to detect check and checkmate, you've already got everything necessary to detect stalemate.