wk

When it comes to king moves, the most interesting is castling, for which I did manage to find a few statistics on the web, based on official games:

  1. White castles kings side 80.2% of the time.
  2. White castles queens side 8.8% of the time.
  3. White doesn’t castle at all…11% of the time.
  4. White castles queens side about twice as much as black.
  5. Castling kings side gives a higher statistic of draws.

From a tactical standpoint, they say that to castle opposite of your opponent creates a more tactical game, while to castle the same side as your opponent creates a positional game. Now I just need to go look up what that means….

In any event, we need to get a move on! Well, at least our king does. So, I created two commits: the king moves, and the king castle moves.

The king moves are really straight forward. I keep thinking that there must be an easier way to do this, but again, it’s just pure math. king position +/- 8 to go up/down a rank, +/- 1 to go side to side, and +/- 7 or +/- 9 to jog diagonally. The easy thing about the king is that he can only move one space… unless he castles.

Castling is rather tricky from a programming standpoint. There are several conditions which must be met:

  • The king cannot be in check.
  • No square the king moves to can be in check.
  • No square the king moves across can be in check.
  • There must be empty spaces between the king and the rook.
  • The king must never have moved.
  • The rook must never have moved.

As you can see, that maneuver alone takes 6 factors into account! Later, I ended up changing this a little bit, rather than booleans of true/false for king moved or rook moved, I went with numbers, but I’ll touch on that later. For now, I went with booleans.

The “king is safe” method has actually not been written yet, so it was interesting to check if the king is safe for this move, before having a way to check that, but in theory it works just fine, once I write the king is safe method. For now, I assign a number to the king’s position, and have it check if the king is safe by moving the number, rather than the actual king, on the logical board, and check if the king would be safe there.

It’s a little complicated, but I think it will do the trick. Be sure to check out the code!

Linux – keep it simple.

Leave a Reply

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