Chess Engine in Python – Part 9 – Castling

In this video, we will add castling to our chess engine.

NOTE: There’s a bug with queen side castling, should be:

def getQueensideCastleMoves(self, r, c, moves):
if self.board[r][c-1] == ‘–‘ and self.board[r][c-2] == ‘–‘ and self.board[r][c-3] == ‘–‘:
if not self.squareUnderAttack(r, c-1) and not self.squareUnderAttack(r, c-2):
moves.append(Move((r, c), (r, c-2), self.board, isCastleMove=True))

I forgot the == ‘–‘ after board[r][c-3].


  1. There is a problem that you could theoretically castle with rook that isn't there anymore because it got captured

  2. hey, love from India, i just want to say that please increase the font size or zoom in while recording because it is very difficult for us to code while watching the video. So far this is the best chess playlist on YouTube.

  3. Hi. I have a problem with my code. If i undo a castle move and then redo it, my castleRightsLog updates like my currentCastlingRights and then adding the same values and I really don t know how to debug this. Please help me someone

  4. I really enjoy this series, I'm following along and trying to learn as much from this tutorial as I can. Thank you for making it.

  5. Hello. Thanks for this and all the other films about Python Chess. I think there is a little bug in rule 316, the first rule of getQueensideCastleMoves. This rule must end with self.board[r][c – 3] == '–':

  6. i followed the code, debugged it too, but somehow i can't seem to perform the castling operation, it meets all the checks, nothing is under attack and still when i do the castling, nothing happens. any possible explanations or suggestions?

  7. in the advanced version, there is still the problem that you cannot move your king out of a pin. if for example you have a pawn between your king and thier queen. theoretically you should be allowed to move your king away to release the pin but the anvanced version does not allow it

  8. hello can someone help me with bug ???, when i make castling my king move right but my rook disappear. how can i fix it ?

  9. Thanks Eddie, amazing series of videos!

    I had the problem where if you castle > undo > castle > undo, on the third time you attempt to castle you lose your right to castle.
    When the UpdateCastlingRights function is called it immediately changes the latest entry in the log to false before adding an additional entry.

    I got around this using import copy & copy.deepcopy. Just putting this here in case anyone else is concerned about this very niche problem!

  10. I've been trying to implement castling with the advanced move algorithm, but when I try to castle, nothing happens. Any tips?

  11. Can someone share the final code? I have spent more than 2 days and still canot find my bug.

  12. really loving the videos, they're giving me something productive to do this mid term. is there a way you could make a video explaining properly how to do all the code with the more complicated version? i've used the more complicated move functions and i find myself having to constantly check if my code matches yours 🙁

  13. Since I prefer the advanced algorith, I already did castling, but I still went throughout the whole video without fast-forwarding to understand the logics and implementations. Best chess playlist ever, Thank you so so much !!!

  14. I have a problem with both castling and en passant. When I castle the rook remains the same and when I eat a pawn with en passant, my pawn moves but the enemy pawn remains the same. help

  15. the updating_castle_rights function is changing the last logged value of castle_rights_log for me. Anyone know how to handle that ?

  16. My rook is dissapearing with the advanced variation

  17. Hello,
    I have pretty much the same bug here at 53:35, but it is not the "update castling rights",
    I have checked it million times, and also checked the append to the "castleRightsLog" list, found nothing.

  18. Another way to keep track of castling rights: keep a move counter on all pieces. Check if rook and king both have 0.

  19. I have an issue with Undoing Castling. I can CASTLE, UNDO, CASTLE, UNDO, and then I cant castle anymore. it doesn't change back from false on the second undo

  20. u called the function incheck() and that calls squareunderattack() and that calls getpossiblemove() and then it keeps looping forever

  21. I've been following along with the advanced algorithm, and I've matched it as best as I can to what you've created, but when I run my program I cannot castle at all. I've done so many hours of debugging but I can't figure out what's wrong.

  22. Hi Eddie, thanks for this wonderful tutorial, in updateCastleRights, I think we don't need to check the start row of the rooks anymore if they have moved, we only need to check the start column because the black and white rooks could never really switch rows at the beginning of the game, the fact that we are checking if its a wR or bR, is enough parameter already to know which rook belongs to which row have moved. Thanks

  23. Hey thanks for all the help Eddie, I appreciate the help with everything. I was just curious if you could give me pointers on what could be going wrong with my code, I have the more advanced algorithm and the castling doesn't seem to work. I've been trying to figure out what went wrong, I tried to add a print statement to the end of the get.QueensideCastleMoves and get.KingsideCastleMoves and they both show up, so I'm assuming the moves are being appended, but when I try to make the move, nothing happens.

  24. I have a bug when I push my white pawn 2 squares at the beginning of my game and respond with the same file 2 square pawn push for black and then redo the move. Somehow my white pawn replaces the black pawn that was pushed on its starting square. I assume this has something to do with my undo move function, any help?

  25. Where can one find castling for the advanced algorithm please?

  26. I really like this way of doing castling, but I would like for someone to come up with a generalised way of doing this, so my code could work with Fischer Random…

  27. I am still getting the same maxrecursion error 😔😔

Leave a Reply

Your email address will not be published. Required fields are marked *