So 4 ‘sessions’ and I hit a snag. I then spend the next week working on code which solves most (non extremely difficult) puzzles.
And then I put the code aside.
A few things I learn’t along the way..
- It’s great to have a goal, but instead of stepping up to the plate and going for broke, plan a little.
- Remember to break it down in to multiple steps and then break those down even more.
- By following 2, it’s easier to go back and fix small parts of the code as opposed to rewriting the entirety.
In saying that, I still didn’t fully apply these lessons learned when completing version 1. I just realised them more after I had completed it.
What I’ve got now is code which will allow you to enter a puzzle, then using a few ‘solving’ selections, will start to fill out the form.
If you have read any content from the last 4 posts, especially the ending of the last one, you will see I was questioning the naming scheme of the cells.
I decided to simplify it to [row][column].
The only reason I was thinking of using a ‘subgrid’ in the naming scheme was to generate arrays which had the cells located in a respective ‘subgrid’. In the final version of the code, I hard coded the values in to the array. I applied the thought method of WORM (Write Once, Read Many). I had issues with where the arrays were being generated because that information was read from more than one function.
The solving method.
Stepping through the puzzle:
- Work out all possible values for each cell. Mark submitted values as solved.
- Start at 11 (first cell) and step through each cell, using the check conditions below. Repeat from 11 until either puzzle is solved or no cells after solved in a pass.
What I’m checking in each cell
- If only 1 possible value, set as solved value.
- If any value is unique in row.
- If any value is unique in column.
- If any value is unique in subgrid.
After each step I perform a clean up of possible values but removing the value from all cells in the row, column and subgrid.
Applying that logic, most puzzles can be solved.
Solving harder puzzles is where I’m at at the moment. Another step needs to be added when the puzzle isn’t solved and after passing the puzzle, 0 values are solved.
I’ve played a number of puzzles in a Sudoku app I have on my phone and I’ve documented, the best way I can describe it is ‘Worded Logic’ for what I need to check. So far I have this:
- Applying to each Row and Column – If 1-3 possible values are ONLY in one row/column of a subgrid, remove values from respective row/column in other subgrids.
- Where multiple cells in a row/column/subgrid have same possible numbers, and the count of those possible numbers = the number cells they appear in, remove possible numbers from other cells in that row/column/subgrid
There are additional checks which I need to work out how to word up, but I’ve been able to solve all but one (manually), using the steps above.
Currently, as I mentioned above, the solver is just generating a grid to display values/current state of the puzzle, and a basic output ‘Solving [cell] – [how it was solved]’.
Going forward, I’m going to have a ‘solve this’ button, and then an ‘analyse’ which will only solve one number at a time, which a check box to do one full pass.
After starting my solver, I did find one online which does the same thing, but to me, it’s more about working through to the solution.. even if there is already one available..