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?


5 Answers 5


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? and Is the king actually in check if the attacking piece cannot be moved?


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.


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 limits’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

"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. Andrew's answer 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.


I think you are taking it too hard.

Why not simply think like this:

  1. Simulate all possible next moves by same side
  2. If, in any resulting position in simulation, king is missing(captured) go to 4
  3. King is not in check, go to 5
  4. King is in check
  5. 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.