Skip to content
Advertisement

Sudoku backtracking solver bug

Ok, I’ve been scratching my head over this for a few hours now..

My goal was to code a sudoku solver that uses the backtracking method and to show the progress of the algorithm using pygame. For this I have to keep track of the events, I did it by appending them to a list named registre as is shown in the code:

JavaScript

I actually succeeded and everything goes fine for most of the runs. But sometimes, for some reason I couldn’t identify, this happens :

JavaScript
JavaScript

What is printed is simply the initialized grid, the solved grid and the first 20 events in self.registre. For this run the displaying on pygame didn’t work, some numbers overlap themselves and others are left blank. I am almost sure it’s not a displaying problem since the displaying function uses the list registre and it works just fine for most of the other runs. Also I don’t understand these events.

Complete script :

JavaScript

Another instance of the issue :

JavaScript

I would really appreciate it if you could help me with this.

Advertisement

Answer

After you added the code that repeats the generation of grids, it becomes clear that you don’t create a new instance of Grid, but mutate the existing one. In that process you should then take care to reset the state completely. You only do this partly, e.g. by resetting t0. But you don’t reset registre, and so when you print the first 20 items, you are actually looking at the log of the first attempt, not the successful one.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement