Oct-01-2021, 08:57 PM
(This post was last modified: Oct-01-2021, 08:57 PM by deanhystad.)
The loop in place_random() ends when it finds a number that can be placed in the cell, but what happens if there are no valid numbers?
Not that it matters much since you cannot generate sudoku puzzles using your approach. Most of the puzzles your generator makes will end up being invalid. You will need to make a backtracking algorithm similar to your solver that backs up when it encounters an invalid puzzle and tries again.
def place_random(x,y): n = random.sample(range(1,10),9) i = 0 while not possible(x,y,n[i]): # <- What is the value for i if there are no possible solutions for this cell? i += 1 grid[x][y] = n[i]This is ugly code anyway. Why are you using while?
def place_random(x, y): for n in random.sample(range(1, 10), 9) if possible(x, y, n): grid[x][y] = n return True return FalseReturns True if the cell is filled, else False.
Not that it matters much since you cannot generate sudoku puzzles using your approach. Most of the puzzles your generator makes will end up being invalid. You will need to make a backtracking algorithm similar to your solver that backs up when it encounters an invalid puzzle and tries again.