I'm stuck debugging my move generation and do move function. I noticed that the castling as actually the problem in my code, but I think I have implemented everything right. I installed some java chess engine in Github that generates moves, to compare it from mine. I separated the nodes into captures, en passant, castling. Just to be sure that the castling is really the problem, I removed the generation of castle for both of my engine and from the one that I installed. I tested 3 positions and everything was fine.

This is how I generate my castling moves:

if (sideToMove can castle king side and F1|F8 is empty and G1|G8 is empty and E1|E8 is not attacked by the enemy and F1 is not attacked by the enemy) add king castle move()

if (sideToMove can castle queen side and D1|D8 is empty and C1|C8 is empty and B1|B8 is empty and E1|E8 is not attacked and D1 is not attacked) add queen castle move()

This is how I manage castling moves:

In my do move function, if a piece is captured, and It is a ROOK, then remove the castling right depending on the side of the captured rook, and the castle side of that rook (e.g. H1 captured, then remove king castling)

The same for the previous one, but if a rook move once, then remove the castling right depending of the rook side (oo | ooo)

If a king castled, or moved once then remove both castle.

This is how I represent Castling Rights:

0000 -> 0 -> no castle

0001 -> 1 -> white king castle

0010 -> 2 -> white queen castle

0100 -> 4 -> black king castle

1000 -> 8 -> black queen castle

So If I want to remove castling right, let's say starting position, has all castles which is equivalent to 1111.

// remove both castle for white

pos.castle_right &= ~(white king castle | white queen castle)

// remove both castle for black

pos.castle_right &= ~(black king castle | black queen castle)

and so on

And also, at the end of my do move function I do this:

// example side is white

... flip side

// becomes black

// simplify to, if black is attacking white king, undo move, return false

                   (attacking side)        (victim square)
if (isSquareAttacked(sideToMove, kingSquareOf(flipSide)) {


  return false


return true

Am I doing everything right? If you see anything wrong, please point it out for me.


I found the bug, It turns out that my bb attacks are making wierd results when I initialize them.

Everything is good until I mask the attacks in the last square. My enum square starts from A1 to H8.

So let's say I have loop here:

for (all squares) {
  KingAttacks[square] = maskKingAttacks(square);

Then this is the maskKingAttacks function:

long bb = sqBb(square), attacks = 0L;

// example square is H8, so the bitboard is going to be this:
a b c d e  f g h
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 3
0 0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 6
0 0 0 0 0 0 0 0 7
0 0 0 0 0 0 0 1 8

So when I shift it into any direction, exmaple, `bb >> 1`. It will fill up all the square bits. It should only move that one bit but in my case, it fills up everything, If you don't get what I mean is like `bits between from the H8 to G8`.

Note that this only happens when the square is the last square. How do I prevent this? And how will I mask attacks in the last square?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.