DavisHenckel / PS-Sudoku

A PowerShell recursive Sudoku Solver & Sudoku puzzle Generator
6 stars 1 forks source link

Solved Sudoku incorrect on custom grid #17

Closed LiquidDeath911 closed 2 years ago

LiquidDeath911 commented 2 years ago

I created an empty grid and added all the numbers where they needed to be. The solved grid was incorrect.

Here is the code I ran:

Clear-Host

Import-Module -Name PS-Sudoku

$grid = GenerateGrid -Difficulty Empty $grid.Item(0) = ('-','3','9','-','-','4','-','-','-') $grid.Item(1) = ('1','-','-','-','-','-','5','-','-') $grid.Item(2) = ('-','-','-','2','7','-','-','-','-') $grid.Item(3) = ('8','-','4','-','-','-','-','-','1') $grid.Item(4) = ('-','-','5','-','-','-','-','9','8') $grid.Item(5) = ('9','-','-','-','6','2','7','4','5') $grid.Item(6) = ('-','-','2','6','-','9','8','5','-') $grid.Item(7) = ('6','-','-','-','-','-','-','-','-') $grid.Item(8) = ('-','9','-','-','2','-','-','1','3')

SolveSudoku -SudokuGrid $grid PrintGrid -SudokuGrid $grid

Here is the solved grid that was printed out:

+++++++++++++++++++++++++ | 2 3 9 | 1 3 4 | 4 8 6 | | 1 4 6 | 5 9 8 | 5 2 7 | | 5 7 8 | 2 7 6 | 1 3 9 | |-------+-------+------ | | 8 1 4 | 4 5 7 | 3 6 1 | | 3 6 5 | 9 8 1 | 2 9 8 | | 9 2 7 | 3 6 2 | 7 4 5 | |-------+-------+------ | | 7 8 2 | 6 1 9 | 8 5 4 | | 6 5 1 | 8 4 3 | 6 7 2 | | 4 9 3 | 7 2 5 | 9 1 3 | +++++++++++++++++++++++++

You can see in row 8 that there are two 6s. One of those 6s was already present in the original grid. Let me know if you would like any more information on this issue.

DavisHenckel commented 2 years ago

Hey there! Thanks for reporting this. I'll take a look at it and see if I can figure out where that went wrong.

DavisHenckel commented 2 years ago

@LiquidDeath911 Thanks again for reporting this. I verified that the code you displayed does produce an error in the output.

The main thing I noticed was in your code you are initializing the numbers with ' '. I believe this is causing a problem because they are interpreted as strings instead of integers. Removing the quotes around the numbers appears to be the fix. I verified that if the code is changed to

$grid = GenerateGrid -Difficulty Empty
$grid.Item(0) = ('-',3,9,'-','-',4,'-','-','-')
$grid.Item(1) = (1,'-','-','-','-','-',5,'-','-')
$grid.Item(2) = ('-','-','-',2,7,'-','-','-','-')
$grid.Item(3) = (8,'-',4,'-','-','-','-','-',1)
$grid.Item(4) = ('-','-',5,'-','-','-','-',9,8)
$grid.Item(5) = (9,'-','-','-',6,2,7,4,5)
$grid.Item(6) = ('-','-',2,6,'-',9,8,5,'-')
$grid.Item(7) = (6,'-','-','-','-','-','-','-','-')
$grid.Item(8) = ('-',9,'-','-',2,'-','-',1,3)
PrintGrid -SudokuGrid $Grid
+++++++++++++++++++++++++
| - 3 9 | - - 4 | - - - |
| 1 - - | - - - | 5 - - |
| - - - | 2 7 - | - - - |
|-------+-------+------ |
| 8 - 4 | - - - | - - 1 |
| - - 5 | - - - | - 9 8 |
| 9 - - | - 6 2 | 7 4 5 |
|-------+-------+------ |
| - - 2 | 6 - 9 | 8 5 - |
| 6 - - | - - - | - - - |
| - 9 - | - 2 - | - 1 3 |
+++++++++++++++++++++++++

The same grid is output as shown above, then when running

SolveSudoku -SudokuGrid $Grid
PrintGrid -SudokuGrid $Grid
+++++++++++++++++++++++++
| 2 3 9 | 5 8 4 | 1 7 6 |
| 1 4 7 | 3 9 6 | 5 8 2 |
| 5 8 6 | 2 7 1 | 4 3 9 |
|-------+-------+------ |
| 8 2 4 | 9 5 7 | 3 6 1 |
| 7 6 5 | 1 4 3 | 2 9 8 |
| 9 1 3 | 8 6 2 | 7 4 5 |
|-------+-------+------ |
| 3 7 2 | 6 1 9 | 8 5 4 |
| 6 5 1 | 4 3 8 | 9 2 7 |
| 4 9 8 | 7 2 5 | 6 1 3 |
+++++++++++++++++++++++++

The correct output is then displayed. In my module I have only used numbers and have not tested for when numbers are initialized with ' '.

However, your issue brings up a good point. There isn't a very intuitive way for someone to initialize a custom puzzle. I will make a branch to add a function to allow a user to easily generate and solve a custom puzzle.

This product started as a school project so I can't guarantee I'll get it done quickly, but that would be a cool feature to implement. If you would like to help with it, feel free to fork a copy and submit a PR.

Let me know if anything is unclear or if you have any questions.

LiquidDeath911 commented 2 years ago

I’m still pretty new to GitHub, so not sure the best place to ask this. Have you thought about doing any kind of GUI? I know PS is not the best language for creating GUIs.

DavisHenckel commented 2 years ago

I’m still pretty new to GitHub, so not sure the best place to ask this. Have you thought about doing any kind of GUI? I know PS is not the best language for creating GUIs.

I did think of building a GUI for this but I don't really see the value considering PowerShell is so much slower at solving Sudoku puzzles compared to other languages. I think if a GUI were to be built, C# and WPF could be useful, but then at that point, it seems like you may as well write the whole solver in C# instead of PowerShell.

DavisHenckel commented 2 years ago

Solved on PR #20