Jan 7, 2007

[Bug] En passant bug

I was fairly sure my move generation was flawless. But of course it was not. :)

Mediocre ran through most perft tests without trouble, but one caused trouble, the start position.

At depth 5 the number started to differ. 36 moves were not found correctly.

So I did a divide-search like I mentioned in my previous post and I noticed moves were missing from the branches starting with pawn to 'a4', 'a3', 'b4', 'g4', 'h3' and 'h4'.

I started tracking the 'h4' move and ran into the following position:

Black has just played g7g4 and my perft method reported finding 21 moves. The correct number is of course 22 moves, 21 ordinary moves and one en passant. It does not take a rocket scientist to understand where the problem was. :)

So I went and looked in the Board-class and looked up the en-passant generation routine. There I found this line:
if((index-17 & 0x88) == 0 && (index-15 & 0x88) == 0
&& (((index - 17) == enPassant)
|| ((index - 15) == enPassant)))
It is supposed to check if the pawn we are generating moves for can reach the en-passant square on the board.

But the logic is obviously flawed. In words it says "if both the square diagonally to the right and diagonally to the left are on the board, and one of them is the en-passant square, add the en-passant move".

I have no idea what I was thinking when I wrote this, but you might remember another bug I had with this. The en-passant square is -1 if no en passant is available and if we have a black pawn on 'a2' it would think an en passant was available outside the board.

Obviously I messed up the bugfix back then. :) So now I simply changed it to:
if(enPassant != -1 &&
(index+17 == enPassant || index+15 == enPassant))
Since the board should never produce an en passant outside the board, we never have to check for the en passant being on the board. As long as it is not -1.

I have to pick my default values more careful in the future.

All better now

After this fix all perft scores are working like a charm. I have not found a single error in any of the tests I have run. So hopefully the move generation is bugfree for now.

3 comments:

genorb said...

Dear Jonatan,

OK Mediocre works with Arena (I have tried now the last version 1.1b only). I don't let Arena use its book but I let Mediocre chooses its moves from the beginning and it works fine.

Of course takeback doesn't work for example (yes I was in a bad position), but I suppose that this is because this is not yet implemented in the code.

I can now start to play with it :).

In few days, I should get the domain www.ordichec.net (yes I speak french and this name is the contraction of "ordinateur" [computer] ane "echecs" [chess]) where I test a lot of chess engine (a lot of things are already done, I am waiting that the link between this domain and my actual website is done). If you need help to test your engine, let me know.

Your blog is very useful, let it online long enough (I plan to develop a chess engine also). I think that I will read in detail your blog to know where to start.

And sorry for my english, I try to do my best.

Jonatan Pettersson said...

As you expected Mediocre does not support take back yet. Or atleast it is not designed to do it yet. Although you could insert a fen-string from the position one move back and play from there.

I am currently working on basic UCI support, and after that I will start to finetune the two protocols to support as much of the features as possible.

As for testing I am always interested in test-results from Mediocre. It is hard to cover every area by myself. So if you want to run some test I'd be very happy.

Glad you're enjoying the blog. I intend to keep it up for a very long time, even if I should stop developing Mediocre sometime in the future.

And your english looks just fine to me. :)

Genorb said...

Thanks for your quick answer.

Please feel free to visit my website

http://users.skynet.be/fab_et_ed/

this should becomes www.ordichec.net in few days.

This is in French but it is easy to understand what I am doing there. In the CONTACT section you will find the email address to contact me. Feel free to propose any test you would like I do for you. I will then try to do my best with the free time I have.

In the DIVERS section, you will find some tests I did for other engines (this is not yet finished because it takes time to do those tests for all engines). I could try for Mediocre even if this is perhaps a bit too soon.

But you have perhaps some specific tests I can do for you.

Of course I can also let Medicore play against other engines, this is the easier test, because I have then almost nothing to do :).