An identical process is used for columns in cubes (vertical direction). This is a row, so the direction of influence of the exclusive values is horizontal. If the possible values of cells are exclusive to a single row of 3 cells in a cube, then no cells in remaining six cells of the puzzle row can contain those exclusive possibles. If that results in a cell having only one possible left, then that is added to a stack and the code loops processing cells until there are no more cells on the stack. If the user types a number into the red cell, then that number is removed from the possibles of all cells in the blue area. 1. Simple Cube, Row and Column Elimination The 81 cells are also arranged in to nine cubes each containing nine cells. These will probably be very familiar to fellow Sudoku solvers but being self taught, they will have different names and terminology. The grid is contained within a Viewbox which provides automatic rescaling. Turns out they are quite easy, if you’ve written a custom control before, a lot of it will be familiar. I couldn’t find much information online about writing layout panels so I reverse engineered the UniformGrid using ILSpy. The View contains a custom layout panel SudokuGrid that is used to arrange the cells and the grid lines. When the model function returns, the two cell lists are compared and the view model list updated if required which redraws the cell. It determines which action is required and calls the appropriate model function, be it Add, Edit, Delete etc. When a user changes the puzzle, a callback in the ViewModel is executed. The ViewModel also contains a list of 81 cells this time stored in an ObservableCollection. The puzzle isn't actually rotated, the code simply changes the way each cell's coordinates are converted into an array index. The other notable point is that instead of writing two functions to implement a strategy, one for rows and another very similar version for columns I simply write the rows version, rotate the puzzle then call the rows function again. The list is actually a bit field which allows the use of boolean algebra to aid the required pattern matching. To that end, each cell contains a list of possible cell values which is then updated by the strategies. The trick to solving Sudoku puzzles is not to find out where numbers go directly, but is to find out where they cannot and when there is only one possibility left that must be it. The Model contains all the solving strategies and a single dimension array of 81 cells. The program structure follows a minimal MVVM design pattern. The code will attempt to solve the puzzle on the fly each time the user changes it. It uses the MahApps library to provide a icon less window and also an easy implementation of a dark mode. I was pretty sure there was a strategy that I was missing, and this program would confirm it. I’ve converted all the strategies that I know into code which will never get bored and make mistakes. It may be obvious and I just cannot see it. Sometimes, I can get stuck on a puzzle and it can get a bit boring just going through all the strategies to try and find the next number. Being a self taught Sudoku puzzle solver, I do enjoy solving the puzzles themselves but the real challenge is working out the strategies you need to solve them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |