Programming a Sudoku generator in Python

Logic puzzles like sudokus not only train the brain, but also make a great programming exercise. The rules are simple: every row, every column and every box of a sudoku must contain the numbers 1 to 9, but none of them can appear more than once. Creating a Soduko, on the other hand, is more difficult: The naive programming approach of opening up an array of size 9 × 9 and filling it with a few random numbers that do not violate the rules of the game rarely leads to success. Because one cannot rule out that several solutions exist.

There are plenty of Sudoku generators, we describe one variant in this article: First, our program generates a completely solved Sudoku. After that it removes a number and checks if the sudoku has only one solution. It repeats this step until enough fields are empty. Our program exports the puzzle as a vector graphic so that you can also solve the finished Sudoku using pen and paper.

If your fingers are on fire and you want to solve your first sudokus, you can find the entire code in the GitHub repository. Also a pure solver that finds all possible solutions for a given Sudoku.

To home page

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *