2

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

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?

6

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.

1

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, privacy policy and cookie policy

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