TerjeKir / weiss

Weiss - a UCI chess engine
GNU General Public License v3.0
103 stars 18 forks source link

Illegal moves #17

Closed TerjeKir closed 5 years ago

TerjeKir commented 5 years ago

Very rarely the engine plays an illegal move, searching and moving for the wrong side, despite at least seemingly having read in the correct position (and side) to search.

The bug was discovered during #16 , but was introduced before that as it was confirmed to also happen after #15 . It is assumed to have been introduced after #10 and testing so far seems to confirm this.

TerjeKir commented 5 years ago

200 games at #10 without an illegal move, concluding that the bug was likely introduced later. Starting testing at #12 .

TerjeKir commented 5 years ago

500 games at #12 without an illegal move.

Got an illegal move at #13 after 37 games so I will assume it was introduced here.

TerjeKir commented 5 years ago

Fixed in #19

Turned out to be 2 separate bugs:

This happened because with the old way of using a bitmask and shifting it around, the bitmask was shifted completely out for pawns on their 8. rank, so their attack bitboards would be empty. I failed to remember this particularity when I changed to using SETBIT, and didn't add the required rank 8 check.

I didn't realize that ParsePosition would do this and removed the memset that zeroed out input before reading again. #19 fixes ParsePosition so it won't go past a null terminator, and also reintroduces the zeroing out before reading just in case (both separately fix the problem according to testing). This part isn't speed critical as it's done during preparations before the go command, so we may as well.

Finding the errors took a long time as I didn't expect there to be two separate causes of what seemed like one error; thinking it was the opponents turn again. Testing possible solutions, one of the two would always be present, showing illegal moves and making me think the proposed fix had had no effect even in the cases where I had actually fixed one or the other. This confused me as I was unable to pinpoint the cause.

The first one going undetected was unfortunate, but understandable; after print debugging, locating the input bug and fixing it, it took 190 games to get an illegal move. When not looking for them, it's easy to miss that one or two out of a couple hundred games ended in an illegal move. Going forward I will be looking for this more attentively.