how do I debug my move generation/make unmake functions?

Discussion of chess software programming and technical issues.

Moderator: Ras

pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: how do I debug my move generation/make unmake functions?

Post by pedrojdm2021 »

The way i debug is:
Open stockfish terminal

Load a sample position in the engine and in stockfish

then start the perft:
go perft 4 for example

if it returns an invalid number in any move, select that move and start a ( depth - 1) perft there:

for example for start position if it prints different number in your engine than in stockfish for a2a4 for depth 4 then you need to:

Code: Select all

position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1  moves a2a4 
go perft 3
for example at depth 3 you find another difference for e7e5
then is:

Code: Select all

position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1  moves a2a4 e7e5
go perft 3
compare results again, and repeat the same process until you reach depth 1 and you will end up with that position that generates the problem.

this can be a very tedious task. So sometimes is better to write some kind of function in your engine that reads a .txt file with the stockfish results and compares the numbers for you. It will save you hours of debugging. ( i did this, saved me a lot of time )
User avatar
RedBedHed
Posts: 84
Joined: Wed Aug 04, 2021 12:42 am
Full name: Ellie Moore

Re: how do I debug my move generation/make unmake functions?

Post by RedBedHed »

When debugging silent semantic errors, there aren't many options.

I struggled for weeks with bugs in my move generator, but after printing move stats and comparing them with known perft numbers, I did eventually catch all of them (At least I think I did lol). So the best advice I can offer is to be patient and don't give up. Print, print, print. Use known perft numbers and the process of elimination to locate the problem areas in your code.
>> Move Generator: Charon
>> Engine: Homura
void life() { playCapitalism(); return; live(); }
Daniel Anulliero
Posts: 770
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: how do I debug my move generation/make unmake functions?

Post by Daniel Anulliero »

am5083 wrote: Wed Jul 27, 2022 8:51 pm This is a better demonstration of the issue:

FEN: 5r2/1K2P3/8/8/8/8/8/3k4 b - - 1 1

Code: Select all

DEPTH 0: 19
D1C2: 214
D1D2: 362
D1E2: 307
D1C1: 351
D1E1: 236
F8A8: 344
F8B8: 71
F8C8: 336
F8D8: 357
F8E8: 324
F8G8: 363
F8H8: 364
F8F7: 129
F8F6: 330
F8F5: 426
F8F4: 429
F8F3: 428
F8F2: 434
F8F1: 334
DEPTH 3: 6139
stockfish:

Code: Select all

position fen 5r2/1K2P3/8/8/8/8/8/3k4 b - - 1 1
go perft 3
f8f1: 176
f8f2: 224
f8f3: 224
f8f4: 224
f8f5: 224
f8f6: 167
f8f7: 120
f8a8: 122
f8b8: 81
f8c8: 122
f8d8: 164
f8e8: 65
f8g8: 151
f8h8: 151
d1c1: 175
d1e1: 151
d1c2: 214
d1d2: 208
d1e2: 186
Then make(...F8F1...) (new FEN: 8/1K2P3/8/8/8/8/8/3k1r2 w - - 2 2)

Code: Select all

DEPTH 0: 12
B7A8: 15
B7B8: 15
B7C8: 15
B7A7: 15
B7C7: 15
B7A6: 15
B7B6: 15
B7C6: 15
E7E8n: 15
E7E8b: 15
E7E8r: 13
E7E8q: 13
DEPTH 2: 176
which matches up with the stockfish output for f8f1
Use a divide function .
In your exemple , f8f1 is false , play it , run divide 2 , look at a false move count , play it , run a divide 1 , you will see what happened
Sure sf have a divide function , too
I always have debuged my gen , make , unmake with divide

Edit : sorry , I mean , you must start divide from depth 4 or 5