jeudi 1 décembre 2016

Why is no correct Sudoku being generated?

I made a program that:

  • Generates (possibly) wrong sudoku solutions.
  • Checks if a solution is correct, and prints it if it is.

I tested it and it detects valid and invalid solutions correctly.

My problem is with generating a correct solution.
When telling the program to generate a new, random solution, until it finds a valid one, it just keeps running.

The way I generate the attempted 'solutions' is by making a list of 9 lists of 9 numbers each, each list being the numbers 1-9 mixed up, like so:

   SUDOKU_ATTEMPT = [[8, 9, 6, 3, 7, 4, 2, 5, 1],
                     [6, 3, 4, 8, 1, 9, 7, 2, 5],
                     [1, 2, 5, 6, 8, 4, 7, 3, 9],
                     [1, 2, 9, 4, 3, 7, 8, 5, 6],
                     [6, 4, 9, 2, 3, 1, 7, 8, 5],
                     [3, 1, 8, 2, 7, 4, 9, 5, 6],
                     [3, 7, 2, 8, 4, 9, 5, 6, 1],
                     [7, 2, 1, 6, 4, 5, 3, 8, 9],
                     [2, 7, 8, 9, 4, 5, 1, 3, 6]]

As you can see, it's a given that rows will be correct. But not columns, nor 3x3 boxes.

I can provide the full code (should I?).
As mentioned, when checking a valid solution it detects it, and same for invalid ones.
I get no errors.

Posts I checked:




Aucun commentaire:

Enregistrer un commentaire