Answers.Body: <p>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.</p>
  
  <p>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: <a href="https://pypi.python.org/pypi/python-chess" rel="noreferrer">https://pypi.python.org/pypi/python-chess</a>). For your purposes, it has a function:</p>
  
  <blockquote>
    <p>is_checkmate()
    Checks if the current position is a checkmate.</p>
  </blockquote>
  
  <p>The package is open source, so you can see its code, too. </p>
  
  <p>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: <a href="https://github.com/mcostalba/Stockfish/blob/master/src/search.cpp" rel="noreferrer">https://github.com/mcostalba/Stockfish/blob/master/src/search.cpp</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-08T13:32:46.577Z
Answers.Text: 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.
Answers.UserDisplayName: Agisilaos Vasileiadis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-11T00:51:51.183Z
Answers.Text: Note that python-chess is licensed under GPL-3.0, so if you use the library, you are required to make your project open source, too. (At least that is how I interpret GPL-3.0.)
Answers.UserDisplayName: HelloGoodbye
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-08T13:16:43.580Z
Answers.LastActivityDate: 2016-03-08T13:40:55.160Z
Answers.LastEditDate: 2016-03-08T13:40:55.160Z
Answers.UserDisplayName: GloriaVictis
Answers.Body: <p>It's actually quite easy to check for a checkmate: </p>
  
  <ul>
  <li>Can I move out of mate?</li>
  <li>Can I block mate?</li>
  <li>Can I take the attacker?</li>
  </ul>
  
  <p>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.</p>
  
  <p>You should have a list of squares that are movable. Check if one of those blocks every attacker.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-08T18:43:28.390Z
Answers.Text: 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.
Answers.UserDisplayName: Agisilaos Vasileiadis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-10T12:20:35.170Z
Answers.Text: And temper "can I attack the attacker" and "can I block mate" with "... with an unpinned piece."
Answers.UserDisplayName: Tony Ennis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-11T00:46:04.110Z
Answers.Text: What you say is easy seems to me like it can easily become quite difficult once you start to implement it. Can I move out of mate? Sure, but you need to know which squares are attacked. Can I block mate? Sure, you can block the attack with another piece, but you may open up check from another opposite color piece, so you need to know which pieces are pinned to the king and not use them to block. Can I take the attacker? Sure, but you need to know which pieces are pinned to the king for this one too, and if you take with the king, you need to know whether the captured piece is protected.
Answers.UserDisplayName: HelloGoodbye
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-21T19:06:48.487Z
Answers.Text: @HelloGoodbye You're overthinking it - you make your list of available moves (which is simple) and test the move to see if you are still in check (which is also simple).
Answers.UserDisplayName: corsiKa
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-08T18:17:21.543Z
Answers.LastActivityDate: 2016-03-08T18:17:21.543Z
Answers.UserDisplayName: corsiKa
Answers.Body: <p>What I did in my program was to only look for checkmates if the king was in check. I only generate pseudo-legal moves in my move generation function so I simply play out all the moves generated and check if the king still is in check. If this is the case I undo that move and delete it from my list of moves. This is repeated until I find a legal move or the list is exhausted. When the list is exhausted I know it must be checkmate.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-19T10:27:24.163Z
Answers.LastActivityDate: 2020-09-19T10:27:24.163Z
Answers.UserDisplayName: Grim
Answers.Body: <p>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.  </p>
  
  <p>My first suggestion is to rewrite your code to remove your bug(s).</p>
  
  <p>The other is to search for the pgn for (#)the mate symbol.</p>
  
  <p><a href="https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/help.html" rel="nofollow">https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/help.html</a> pgn extract claims to have a match criteria for checkmate and stalemate and many others.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-23T11:39:49.610Z
Answers.Text: What is a pgn? :)
Answers.UserDisplayName: HelloGoodbye
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-10T10:10:40.243Z
Answers.LastActivityDate: 2016-03-20T07:23:00.700Z
Answers.LastEditDate: 2016-03-20T07:23:00.700Z
Answers.UserDisplayName: Mike Jones
Answers.Body: <p>From Chess Programming Wiki, this article (<a href="https://www.chessprogramming.org/Checkmate" rel="nofollow noreferrer">https://www.chessprogramming.org/Checkmate</a>) explains some rules that you can use to implement checkmate detection.</p>
  
  <p>1) double check - only the king can move on this situation. So, if the king has no valid moves, you have a checkmate. </p>
  
  <p>2) single check - in this situation, the king may move out of check position, or if the attacker piece is a knight or a pawn, you should detect if they may be captured. For the attacking from queen, rook and bishop, they could be captured or some opposite piece should be placed between the attacked king and the attacker.</p>
  
  <p>-</p>
  
  <p>To start the above, you may create a function that lists the pieces that are attacking the king. If the list is empty, no check. If the list has a single pice, a single check. And, finnaly, if the list has two pieces, you have a double check. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-25T11:40:49.300Z
Answers.LastActivityDate: 2020-04-25T11:40:49.300Z
Answers.UserDisplayName: Welyab Paula
Answers.Body: <p>If you are implementing either min-max search or alpha–beta search for a chess engine, one very simple (but rather inefficient) way of checking for checkmate is to consider the king a normal but very valuable piece, search two plys ahead from the current position and see whether the opponent can capture your king (which they will if they can if the king is valuable enough). If they can capture the king, the current position is a terminal position (the game is over).</p>
  <p>Besides, if you in the current position switch the active color to your opponent’s color and search one ply ahead and the opponent can capture your king, you are in check in the current position.</p>
  <p>If you are not in check and the position is terminal (because of a king capture in two plys), the position is a stalemate. If you are in check and the position is terminal, the position is a checkmate.</p>
  <p>When doing these one and two ply searches, you need to ignore the threefold and fivefold repetition rules, as well as the fifty-move and seventyfive-move rules.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-23T11:59:31.890Z
Answers.LastActivityDate: 2024-01-23T12:04:45.793Z
Answers.LastEditDate: 2024-01-23T12:04:45.793Z
Answers.UserDisplayName: HelloGoodbye
Body: <p>So I am implementing an intelligent chess agent but I can't get the checkmate working.</p>
  
  <p>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.</p>
  
Tags: <checkmate><programming>
Title: Check for checkmate
ThreadText: # Check for checkmate
  
  (Asked by: username_0 on 2016-03-08)
  
  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.
  
  **Tags:** checkmate, programming
  
  
  ---
  
  ## 6 answers
  
  ### Answer by username_1 on 2016-03-08. Score: 9
  
  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>
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: Note that python-chess is licensed under GPL-3.0, so if you use the library, you are required to make your project open source, too. (At least that is how I interpret GPL-3.0.)  
  
  ---
  
  ### Answer by username_3 on 2016-03-08. Score: 2
  
  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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_4**: And temper "can I attack the attacker" and "can I block mate" with "... with an unpinned piece."  
  > **username_2**: What you say is easy seems to me like it can easily become quite difficult once you start to implement it. Can I move out of mate? Sure, but you need to know which squares are attacked. Can I block mate? Sure, you can block the attack with another piece, but you may open up check from another opposite color piece, so you need to know which pieces are pinned to the king and not use them to block. Can I take the attacker? Sure, but you need to know which pieces are pinned to the king for this one too, and if you take with the king, you need to know whether the captured piece is protected.  
  > **username_3**: @username_2 You're overthinking it - you make your list of available moves (which is simple) and test the move to see if you are still in check (which is also simple).  
  
  ---
  
  ### Answer by username_5 on 2020-09-19. Score: 2
  
  What I did in my program was to only look for checkmates if the king was in check. I only generate pseudo-legal moves in my move generation function so I simply play out all the moves generated and check if the king still is in check. If this is the case I undo that move and delete it from my list of moves. This is repeated until I find a legal move or the list is exhausted. When the list is exhausted I know it must be checkmate.
  
  
  ---
  
  ### Answer by username_6 on 2016-03-10. Score: 1
  
  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.
  
  > **Comments:**
  > **username_2**: What is a pgn? :)  
  
  ---
  
  ### Answer by username_7 on 2020-04-25. Score: 1
  
  From Chess Programming Wiki, this article (<https://www.chessprogramming.org/Checkmate>) explains some rules that you can use to implement checkmate detection.
  
  1) double check - only the king can move on this situation. So, if the king has no valid moves, you have a checkmate.
  
  2) single check - in this situation, the king may move out of check position, or if the attacker piece is a knight or a pawn, you should detect if they may be captured. For the attacking from queen, rook and bishop, they could be captured or some opposite piece should be placed between the attacked king and the attacker.
  
  -
  
  To start the above, you may create a function that lists the pieces that are attacking the king. If the list is empty, no check. If the list has a single pice, a single check. And, finnaly, if the list has two pieces, you have a double check.
  
  
  ---
  
  ### Answer by username_2 on 2024-01-23. Score: 1
  
  If you are implementing either min-max search or alpha–beta search for a chess engine, one very simple (but rather inefficient) way of checking for checkmate is to consider the king a normal but very valuable piece, search two plys ahead from the current position and see whether the opponent can capture your king (which they will if they can if the king is valuable enough). If they can capture the king, the current position is a terminal position (the game is over).
  
  Besides, if you in the current position switch the active color to your opponent’s color and search one ply ahead and the opponent can capture your king, you are in check in the current position.
  
  If you are not in check and the position is terminal (because of a king capture in two plys), the position is a stalemate. If you are in check and the position is terminal, the position is a checkmate.
  
  When doing these one and two ply searches, you need to ignore the threefold and fivefold repetition rules, as well as the fifty-move and seventyfive-move rules.
  
  
  ---
  
  