Answers.Body: <p>Generally speaking, the most "expensive" part of chess programs is generating all legal moves from a certain position.  Anything that can be done to avoid that is usually done because it speeds everything up significantly.  </p>
  
  <p>In order to find out whether a move is check or not, you can do several relatively simple checks (all of these are simple addition in most board coordinate systems).</p>
  
  <ol>
  <li>See if the king is a rook's move away from enemy rooks or queens</li>
  <li>See if the king is a bishop's move away from enemy bishops or queens</li>
  <li>See if the king is a knight's move away from enemy knights</li>
  <li>See if the king is diagonally adjacent to enemy pawns</li>
  </ol>
  
  <p>By changing the thinking to start with the king's position, it makes some of these determinations far more simple.  Another note is that most programs have an explicit variable set for the king's position because it's used quite frequently and there is always a single king for each side.  </p>
  
  <p>As a note, this same routine is called to see if a piece is pinned when generating legal moves.  After the move is "made", if the friendly king is attacked then the move was illegal.  </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-03T07:20:59.750Z
Answers.LastActivityDate: 2016-01-03T07:20:59.750Z
Answers.UserDisplayName: Andrew
Answers.Body: <p>You say that your definition seems to be wrong because it is circular. You are right.</p>
  <p>The part that makes it circular is step 2.3, which is required to evaluate “legal” in step 1.1. This is where you went wrong. The moves that would capture the king must be legal <em>except for possibly putting the opponent in check</em>. In other words, a piece pinned to the opponent’s king can still give check to the first player.</p>
  <p>See the links in <a href="https://chess.stackexchange.com/a/13274/25131">limits’s answer</a>.</p>
  <hr />
  <p>You worded step 1.2 as (emphasis added):</p>
  <blockquote>
  <p>determine whether such a move <strong>threatens</strong> our king (i.e. could 'take' the king)</p>
  </blockquote>
  <p>This is an incorrect use of the word “threatens”. For example, in the game below, 1...e5 <em>threatens</em> 2...Qh4#. In this sense, it <em>threatens</em> the king, but is not itself a capture of the king.</p>
  <pre><code>[FEN &quot;&quot;]
  1.f3 e5 2.g4 Qh4
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-06T11:04:12.413Z
Answers.LastActivityDate: 2020-12-06T11:21:37.107Z
Answers.LastEditDate: 2020-12-06T11:21:37.107Z
Answers.UserDisplayName: Brian Drake
Answers.Body: <p>&quot;If you want to determine whether a player is in check&quot;, then consider each opposing unit in turn. If it observes the turn player's king, the turn player is in check. <a href="https://chess.stackexchange.com/a/13277">Andrew's answer</a> goes some way to explaining how one might do this, but missed the subtlety that an enemy line-piece's line of attack on the turn player's king might be obstructed by another unit, in which case the said enemy line-piece does not give check.</p>
  <p>This task is similar to, but not the same as, generating all legal moves for the turn player's opponent. But unlike the latter task, the former does not entail deciding whether any king is in check. This algorithm is therefore not &quot;circular&quot; (i.e. does not entail an infinte recursion), and implements the definition of check, which is therefore not circular either.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-15T17:45:01.473Z
Answers.LastActivityDate: 2021-07-15T17:45:01.473Z
Answers.UserDisplayName: Rosie F
Answers.Body: <p>I think you are taking it too hard.</p>
  
  <p>Why not simply think like this:</p>
  
  <ol>
  <li>Simulate all possible next moves by same side</li>
  <li>If, in any resulting position in simulation, king is missing(captured) go to 4</li>
  <li>King is not in check, go to 5</li>
  <li>King is in check</li>
  <li>End</li>
  </ol>
  
  <p>Why this works? Because if king is in check, it means if the same side plays again(plays double times) it can capture the king. So if it can capture the king by playing double moves, then it means opponent was in check.</p>
  
  <p>For example, assume it's Black to move again(plays double move):</p>
  
  <pre><code>rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3
  </code></pre>
  
  <p>Enumerate all possible moves. Now check all resulting positions, is White king missing in any position? Then it means White king was in check.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-03T00:45:31.970Z
Answers.LastActivityDate: 2016-01-03T04:43:01.393Z
Answers.LastEditDate: 2016-01-03T04:43:01.393Z
Answers.UserDisplayName: ferit
Answers.Body: <p>A move can be illegal because the opponent could "take" the king. This "taking the king" move does not need to be "legal". See <a href="https://chess.stackexchange.com/questions/2348/can-a-piece-pinned-to-my-king-put-the-opponents-king-in-check?rq=1">Can a piece pinned to my king put the opponent&#39;s king in check?</a> and <a href="https://chess.stackexchange.com/questions/3966/is-the-king-actually-in-check-if-the-attacking-piece-cannot-be-moved?rq=1">Is the king actually in check if the attacking piece cannot be moved?</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-02T23:59:42.137Z
Answers.LastActivityDate: 2016-01-02T23:59:42.137Z
Answers.LastEditDate: 2017-04-13T12:48:53.193Z
Answers.UserDisplayName: limits
Body: <p>If you want to determine whether a player is in check I think you would:</p>
  
  <p>1.1) determine every legal move of the opposition;</p>
  
  <p>1.2.) determine whether such a move threatens our king (i.e. could 'take' the king)</p>
  
  <p>To determine 1.1) above you need to:</p>
  
  <p>2.1.) consider every active piece of the opposition and its possible moves;</p>
  
  <p>2.2.) see whether that move conforms to a valid type of move (i.e. moves likes a castle,knight etc)</p>
  
  <p>2.3.) confirm that such a move doesn't place the opposition in check (otherwise it's not a valid move)</p>
  
  <p>Yet 2.3 requires us to go back to 1.1-in order to determine whether the opposition's move is not check (and thus valid) we need to consider all the possible moves of our side (the side which we wanted to determine if it was in check originally).</p>
  
  <p>It seems that this leads to some horrible circular dependency. Obviously, it doesn't as otherwise chess wouldn't be a game!</p>
  
  <p>Where am I going wrong?</p>
  
Tags: <rules><terminology><check>
Title: Why is the definition of check not circular?
ThreadText: # Why is the definition of check not circular?
  
  (Asked by: username_0 on 2016-01-02)
  
  If you want to determine whether a player is in check I think you would:
  
  1.1) determine every legal move of the opposition;
  
  1.2.) determine whether such a move threatens our king (i.e. could 'take' the king)
  
  To determine 1.1) above you need to:
  
  2.1.) consider every active piece of the opposition and its possible moves;
  
  2.2.) see whether that move conforms to a valid type of move (i.e. moves likes a castle,knight etc)
  
  2.3.) confirm that such a move doesn't place the opposition in check (otherwise it's not a valid move)
  
  Yet 2.3 requires us to go back to 1.1-in order to determine whether the opposition's move is not check (and thus valid) we need to consider all the possible moves of our side (the side which we wanted to determine if it was in check originally).
  
  It seems that this leads to some horrible circular dependency. Obviously, it doesn't as otherwise chess wouldn't be a game!
  
  Where am I going wrong?
  
  **Tags:** rules, terminology, check
  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_5 on 2016-01-02. Score: 11 | Accepted answer
  
  A move can be illegal because the opponent could "take" the king. This "taking the king" move does not need to be "legal". See [Can a piece pinned to my king put the opponent's king in check?](https://chess.stackexchange.com/questions/2348/can-a-piece-pinned-to-my-king-put-the-opponents-king-in-check?rq=1) and [Is the king actually in check if the attacking piece cannot be moved?](https://chess.stackexchange.com/questions/3966/is-the-king-actually-in-check-if-the-attacking-piece-cannot-be-moved?rq=1)
  
  
  ---
  
  ### Answer by username_1 on 2016-01-03. Score: 5
  
  Generally speaking, the most "expensive" part of chess programs is generating all legal moves from a certain position. Anything that can be done to avoid that is usually done because it speeds everything up significantly.
  
  In order to find out whether a move is check or not, you can do several relatively simple checks (all of these are simple addition in most board coordinate systems).
  
  1. See if the king is a rook's move away from enemy rooks or queens
  2. See if the king is a bishop's move away from enemy bishops or queens
  3. See if the king is a knight's move away from enemy knights
  4. See if the king is diagonally adjacent to enemy pawns
  
  By changing the thinking to start with the king's position, it makes some of these determinations far more simple. Another note is that most programs have an explicit variable set for the king's position because it's used quite frequently and there is always a single king for each side.
  
  As a note, this same routine is called to see if a piece is pinned when generating legal moves. After the move is "made", if the friendly king is attacked then the move was illegal.
  
  
  ---
  
  ### Answer by username_2 on 2020-12-06. Score: 4
  
  You say that your definition seems to be wrong because it is circular. You are right.
  The part that makes it circular is step 2.3, which is required to evaluate “legal” in step 1.1. This is where you went wrong. The moves that would capture the king must be legal except for possibly putting the opponent in check. In other words, a piece pinned to the opponent’s king can still give check to the first player.
  See the links in username_5’s answer.
  
  You worded step 1.2 as (emphasis added):
  
  determine whether such a move threatens our king (i.e. could 'take' the king)
  
  This is an incorrect use of the word “threatens”. For example, in the game below, 1...e5 threatens 2...Qh4#. In this sense, it threatens the king, but is not itself a capture of the king.
  [FEN ""]
  1.f3 e5 2.g4 Qh4
  
  
  ---
  
  ### Answer by username_3 on 2021-07-15. Score: 1
  
  "If you want to determine whether a player is in check", then consider each opposing unit in turn. If it observes the turn player's king, the turn player is in check. [username_1's answer](https://chess.stackexchange.com/a/13277) goes some way to explaining how one might do this, but missed the subtlety that an enemy line-piece's line of attack on the turn player's king might be obstructed by another unit, in which case the said enemy line-piece does not give check.
  
  This task is similar to, but not the same as, generating all legal moves for the turn player's opponent. But unlike the latter task, the former does not entail deciding whether any king is in check. This algorithm is therefore not "circular" (i.e. does not entail an infinte recursion), and implements the definition of check, which is therefore not circular either.
  
  
  ---
  
  ### Answer by username_4 on 2016-01-03. Score: 0
  
  I think you are taking it too hard.
  Why not simply think like this:
  
  Simulate all possible next moves by same side
  If, in any resulting position in simulation, king is missing(captured) go to 4
  King is not in check, go to 5
  King is in check
  End
  
  Why this works? Because if king is in check, it means if the same side plays again(plays double times) it can capture the king. So if it can capture the king by playing double moves, then it means opponent was in check.
  For example, assume it's Black to move again(plays double move):
  rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3
  
  Enumerate all possible moves. Now check all resulting positions, is White king missing in any position? Then it means White king was in check.
  
  
  ---
  
  