Programming A Chess Engine in C Part 35 – Move Generation #8 – Castling

We chatter on this Discord server:

Video series programming a chess engine in C


Follow me on twitter:

But really, it’s for fun and for free.


  1. These tutorials have helped me a lot with a fear that I've had since I started programming, I didn't really know how to put ideas into my work, like I could think the way to do castling or move generation but not how to actually write code that could do it, how to use the tools that I have and use them, I've struggled a lot with it, but watching your videos is making me overcome that fear and actually see that it's not that hard, thanks again man!

  2. I love this series. Great explanation of a very complex subject (chess engine) in simple code. Can't wait to go on making my own engine.

  3. Great! It's meant to be that – a simple example – non optimised! 😉

  4. Super series. Just a question: why don't you include the final square where the king goes to? For example: (!SqAttacked(G1,BLACK,pos)) in case of the WKCA? Thanks in advance.

  5. Another bug in here, I double checked with later videos to varify. For knight and king attacks in SqAttacked function in attack.c, you need to add pce != OFFBOARD as well when you check whether or not to return true. Correct code is:
    if (pce != OFFBOARD && IsKn(pce) && PieceCol[pce] == side) // For knight
    if (pce != OFFBOARD && IsKi(pce) && PieceCol[pce] == side) // For king

    Learning some fun stuff with this, accessing out of bounds in arrays does some wacky stuff.

  6. Why don't you check whether the final square where king goes is attacked?

Leave a Reply

Your email address will not be published.