Progress in 2023

I started this blog in 2017. I made a couple posts. I have worked on some projects this year, some are simple, some are more involved. I made a Sudoku solver on a Saturday morning. Here is a screenshot of how to use it:

Here is how to use it: enter the puzzle number, up to and including the max number that it gives you. It spits out the solved puzzle. It asks you if you want to go again, enter yes or no. Capitalization doesn’t matter. Run as many times as you want, enter no to quit.

To add a new puzzle, here is how you do it:

The 2 at the very top corresponds to the number of puzzles in the file. The next line down, 0, refers to puzzle 0. The next nine lines are the contents of puzzle 0. After that, we have 1, which stands for puzzle 1. Then, we have the nine lines for puzzle 1. To add a puzzle, add the puzzle number, which is 1 above the closest one above. Also, increment the very top number of the file. Then, after the puzzle number, add the nine lines of content. Save. The program is ready to run.

How the program works:

The way the program works is as follows:

  1. Get the maximum number of puzzles from puzzles.txt, put into max.
  2. Prompt the user for the number of the puzzle, from 0 to max. If the user’s response is out of range, try again until the response is in range.
  3. Load the proper puzzle from the file, into a list of integers.
  4. Solve the puzzle, put the solved puzzle into another list of integers.
  5. Display the puzzle in the console.
  6. prompt the user for a yes or no whether they want to go again. Convert the response to all upper case so that they can enter lower or upper case.
  7. repeat.

Next is how the solver works.

How the solver works:

The solver uses a backtracking algorithm, basically guessing each cell, and testing if it is safe via row, col, and box. Here is the code, based off of the code on GeeksforGeeks:

As you can see, it basically runs thru 1..9 for each cell, when it finds one that is safe, it places it into the cell, then moves onto the next cell. When it advances past the end of the puzzle, it returns back to the main loop. Cool. There is some junk dealing with converting between lists and arrays, for a better look here is the code on my Github: https://github.com/smc897/sudoku

Leave a comment