I am writing a chess engine. I need to know how to tell if the king is in check. I was thinking that maybe I can use the approach that Stockfish uses. Where and how in Stockfish's code is check detected?


3 Answers 3


If you're curious about the Stockfish code, it can be found here:

 si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
  1. We call the attackers_to method to figure out if any pieces are attacking the king's position, including pieces on the king's side.
 Bitboard Position::attackers_to(Square s, Bitboard occupied) const {

    return  (pawn_attacks_bb(BLACK, s)       & pieces(WHITE, PAWN))
         | (pawn_attacks_bb(WHITE, s)       & pieces(BLACK, PAWN))
         | (attacks_bb<KNIGHT>(s)           & pieces(KNIGHT))
         | (attacks_bb<  ROOK>(s, occupied) & pieces(  ROOK, QUEEN))
         | (attacks_bb<BISHOP>(s, occupied) & pieces(BISHOP, QUEEN))
         | (attacks_bb<KING>(s)             & pieces(KING));
  1. We then call the pieces function, which returns all the pieces owned by the opposite side

  2. Finally, we run an AND operator to confirm if any of the opposite side's pieces are threatening the king.

The code is very well written and relatively straightforward to follow, in case you'd like to explore it further.


Where and how in Stockfish's code is check detected?

This is simple engine programming. You will need to build a bitboard of the attacking squares and two bitboards of your king, one for each color. You will then run AND on the two bitboards. If the result is non-zero, you know the king is in check.

I can show you the code in Stockfish, but does my answer make sense to you? If not, please add a comment so I will try to grab some code.

  • How is the operation (Bitboard AND Bitboard) defined? This is ambiguous. Bitboard is a matrix, while the operands of AND are bits.
    – Klangen
    Commented Oct 8, 2020 at 20:40
  • 3
    You operate over the matrix, ANDing the corresponding positions one by one. So 64 "single ANDs" in total.
    – IronEagle
    Commented Oct 8, 2020 at 22:25
  • 3
    @IronEagle You just need two 64 bits integers. And do like a & b.
    – SmallChess
    Commented Oct 9, 2020 at 17:23
  • 1
    @SmallChess That’s another way to think of it (and probably closer to reality), I just wanted to emphasize that the AND operation is bit by bit, even with a matrix.
    – IronEagle
    Commented Oct 9, 2020 at 17:25
  • I think this is sometimes called a “pointwise” operation, to distinguish from row- or column-wise operations (at least, this is true in the numpy and matlab parlance). Commented Oct 11, 2020 at 15:12

Your question has nothing to do with Stockfish. Though it may be interesting to check the original source code.

This is purely algorithmic.

One dumb solution would be implementing a boolean function (is_check) that takes an array as input representing the board. Only white pieces can check the black king, except the white king and vice versa.

If one of the following conditions is verified then there is a check on the board(return True) :

  • If a knight is placed at (-+)2(-+)1 or (-+)1(-+)2 squares from your king
  • If a pawn is placed at (-+)1+1 squares from your king (depends on the side you make the verification for as pawns move and attack only in one way)
  • If your king is in diagonal of a bishop or a queen and that no other pieces interferes
  • If your king is aligned (column/row) with a rook or a queen and that no other pieces interferes

Otherwise return False.

Note that such a function could be used in another method to check if a planned move is legal (if your king is in check after you played, your move is illegal)

  • 3
    If you're going to use it to see if a move is legal (i.e. you are not assuming legality of the current position, only the preceding one), you need to allow for checks from the opposing king. Commented Oct 8, 2020 at 12:47
  • 1
    Checkmate is slightly more complicated of course - because you'd have to then check every legal move that could be done in response and see if the king is still checked in all of them. Stalemate would be the same only the king is not currently in check. Draw by repetition or by X moves with no change in material, or insufficient material on both sides to complete, slightly more complicated, but not impossible. Commented Oct 8, 2020 at 18:06
  • If you use it to decide whether the king is in check after the move, beware of pinned pieces. Thus, it must be re-calculated for non-king-moves. Commented Oct 9, 2020 at 1:22
  • @Deduplicator Why is it an issue whether something is pinned? A unit may check whether or not it is pinned.
    – Rosie F
    Commented Oct 9, 2020 at 15:35
  • @DarrelHoffman: I would tend to assume that both checkmate and stalemate naturally fall out of the "find all of the legal moves" logic (which you obviously need to write anyway). If you can't find any legal moves, then it must be one or the other, and you need only look for check to determine which it is.
    – Kevin
    Commented Oct 10, 2020 at 0:49

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.