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. 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!

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

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

  5. 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 🙁

  6. 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 !!!

  7. 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

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

  9. My rook is dissapearing with the advanced variation

  10. 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.

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

  12. 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

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

  14. 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.

  15. 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

  16. 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.

  17. 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?

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

  19. 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…

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

  21. that recap in the end was helpful 😉

  22. hello, i have some bug that in the Castle move, my if move.isCastleMoves: seem not working, i really need some help

  23. RecursionError: maximum recursion depth exceeded in comparison help me

    def getAllPossibleMoves(self):

    moves = []

    for r in range(len(self.board)):

    for c in range(len(self.board[r])):

    turn = self.board[r][c][0]

    if (turn == 'w' and self.whiteToMove) or (turn == 'b' and not self.whiteToMove):

    piece = self.board[r][c][1]

    self.moveFunctions[piece](r, c, moves)

    return moves

Leave a Reply

Your email address will not be published.