Dec 22, 2006

[Plan] Pawns and castling

Finished up the unmake-move algorithm and it is working great. A whole bunch faster than the previous setup.

Also the generation of the pseudo-legal moves is coming along nicely. I got the generation for common moves up and running. I.e. king, queen, rook, bishop and knight moves are working.

Now we have the special moves.
  • Castling - Here we need a special check to see if the squares between the king and rook are empty. Also it does not follow any delta and moves two pieces (the only move that does that), so we can not use the same generation as ordinary moves.

    So what I will do is; when a king is found both the ordinary generation method is called (for ordinary king moves), as well as a special castling generation method. We have to make sure the castling generation is cut off quickly if no castling is available so we do not slow down the end game with slow checks for castling at every move.

  • Pawn moves - The pawns do follow a delta, but there are many differences compared to ordinary pieces. The differences we have to look out for are:

    • Captures diagonally and can not capture forwards. This is a major difference since the generation method for other pieces check the arrival square and turn the move into a capture if it holds an opposite side piece.
    • On the original square a pawn has the option of moving one or two squares. So we need an extra check to catch that.
    • If the pawn reaches the last rank, it gets promoted. Basically for one pawn move we then have four options (queen, rook, bishop, knight), resulting in four moves generated.

    It is hard to catch all these differences in the ordinary move generation method.
So I will be to making separate methods for each of these two (castling and pawn moves). It takes quite some extra code compared to tweaking the ordinary move generation to accept castling and pawn moves. But it will speed up the move generation a bit since we will not have to do extra checks for the ordinary moves (which there are much more of).

2 comments:

Paulo Santos said...

Sorry for commenting in a very old post, but as I have seen the code for movement generation hasn't changed much from this day.

I'm wondering how you handle the rule that no castling can be made if the king passes through an attacked square while castling.

Paulo Santos said...

Never mind... I just found it! :-P