2

So I am implementing an intelligent chess agent but I can't get the checkmate working.

I was wondering if there is a program that takes the chessboard notation and returns a result of yes or no to whether it is a checkmate or not.

4

I imagine any chess engine must have such function. Find an open source one in your programming language of choice and draw inspiration from there.

I often use the python-chess package for simple chess programming and data mining tasks, as it offers a good API with many useful functions (available here: https://pypi.python.org/pypi/python-chess). For your purposes, it has a function:

is_checkmate() Checks if the current position is a checkmate.

The package is open source, so you can see its code, too.

You could also try implementing the function yourself! Stockfish, for example, has a simple, efficient implementation - it produces a list of legal moves, and if the list is empty and the king is in check, then it clearly is a checkmate. See the code: https://github.com/mcostalba/Stockfish/blob/master/src/search.cpp

|improve this answer|||||
  • First of all thank you for your answer.I've tried implementing it myself(using the same principle as Stockfish) but since i use a pre-existing gui and game mechanics(my focus is to construct an AI) some of its functions are wrong and the list is empty when it shouldn't be.So i wanted to find some other way of checkmate evaluation. – A.Vasil Mar 8 '16 at 13:32
0

It's actually quite easy to check for a checkmate:

  • Can I move out of mate?
  • Can I block mate?
  • Can I take the attacker?

The king only has 8 spots to move. You should already have a list of squares under attack from enemy pieces, just check if that square is in that list.

You should have a list of squares that are movable. Check if one of those blocks every attacker.

You can easily tell who is attacking the king by checking each enemy piece's attacked squares for the king's square. If there's only one attacker, and that piece's squares is in one of your piece's attacked squares, you're all set.

|improve this answer|||||
  • I actually tried to implement the method you suggest above but the pre-existing gui as well as the game mechanics seem to have a peculiar bug that doesn't compute all the possible moves correctly.That's why i ask for a program that takes the chessboard notation as input and decides if it is checkmate or not. – A.Vasil Mar 8 '16 at 18:43
  • And temper "can I attack the attacker" and "can I block mate" with "... with an unpinned piece." – Tony Ennis Mar 10 '16 at 12:20
0

Most engines will read a pgn. To find if it is checkmate you have to play through the game. From your comments I think you want the program to do that automatically. None of the engines that I have looked at have this capability.

My first suggestion is to rewrite your code to remove your bug(s).

The other is to search for the pgn for (#)the mate symbol.

https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/help.html pgn extract claims to have a match criteria for checkmate and stalemate and many others.

|improve this answer|||||

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.