Home > Software design >  How to find illegal moves in move generator using Stockfish perft funtion?
How to find illegal moves in move generator using Stockfish perft funtion?

Time:06-02

I'm making a chess program in Unity and I've made a perft function to find the bugs in my move generator. I've downloaded Stockfish to compare the results.

From the starting chess position, with a depth of 3, my results match with Stockfish's for the most part. The only difference is after black's knight moves.

Here's Stockfish's result:

a2a3: 380
b2b3: 420
c2c3: 420
d2d3: 539
e2e3: 599
f2f3: 380
g2g3: 420
h2h3: 380
a2a4: 420
b2b4: 421
c2c4: 441
d2d4: 560
e2e4: 600
f2f4: 401
g2g4: 421
h2h4: 420
b1a3: 400
b1c3: 440
g1f3: 440
g1h3: 400

Nodes searched: 8902

And here are my results:

a2a3: 380
b2b3: 420
c2c3: 420
d2d3: 539
e2e3: 599
f2f3: 380
g2g3: 420
h2h3: 380
a2a4: 420
b2b4: 421
c2c4: 441
d2d4: 560
e2e4: 600
f2f4: 401
g2g4: 421
h2h4: 420
b1a3: 420
b1c3: 460
g1f3: 460
g1h3: 420

Nodes searched: 8982

My algorithm is generating a total of exactly 20 more moves than Stockfish after each of black's knight move.

My question is, how can I use these results to find what illegal moves my program is considering legal? What's the fastest way to debug my algorithm given these or any future test results?

I couldn't find anything else online, that's why I'm asking here.

CodePudding user response:

Follow the following procedure.

  1. We start at b1a3 because there is difference, yours is 420 and sf is 400. Push the move in the position, meaning startpos b1a3 or
    position startpos moves b1a3, then send a perft 2 command. Notice the depth is reduced from depth 3 to depth 2. We reduce it because we push the move. For sf and your engine:
position startpos moves b1a3
perft 2

Then record the moves the way you did in the post,

  1. Find the move where they differ and push that move again similar to step 1 but now use depth 1, we did depth 2 last time. Record the perft and compare. Since that is only depth 1 you will be able to see what is going on with your engine. It is better to setup the position on the real board or on software board.

These are legal move generation issues, so you have to review your move generations, makemove and unmakemove functions. You have knight move issues so probably your knight move offsets are incorrect. Also check your attack function.

Also in your code add some asserts everywhere like a white piece cannot capture another white piece, the to square of the move is within the 64-board squares and others.

  • Related