A few people have asked me if it’s possible to use Microsoft Sudoku to solve puzzles other than the ones it creates, such as the ones in daily newspapers. Yes, it is! (However, a few lines of code are required to enable this feature.)
The code you can download from MSDN for Sudoku has a cheat built into it that lets you solve the current puzzle (if it’s solvable with at least one solution). So all you need to do is modify the game to clear the board so that you can enter a new collection of set cells and then run the cheat to solve it. We can do this simply by adding the following code to the OnKeyDown method in MainForm.cs:
// F5 = Clear puzzle
else if (e.KeyCode == Keys.F5 && newGameButton.Visible && newGameButton.Enabled)
thePuzzleGrid.State = null;
That’s all it takes. I’ve chosen to use the F5 key to allow the board to be cleared. If F5 is pressed and if the new game button is visible and enabled (which just tells us that we’re currently playing a game), I clear the board by setting the PuzzleGrid’s state to null (internally, it’ll allocate a blank board), I clear the undo stack, I clear the original puzzle’s cached solution, and I delete any remnants of the previous puzzle that might have been lying around on disk.
At run time, this gives me a clean slate into which I can type any puzzle I want. If I then hit alt-shift-2 (this should be familiar to any Solitaire fans out there), it’ll solve the puzzle, assuming there is a solution for the current board. As it’s currently implemented, it’ll solve it even if there’s more than one solution, but you could change that, too. All you’d need to do is modify the PuzzleGrid.SolvePuzzle() method to set options.MaximumSolutionsToFind to 2 instead of 1.
If you add this feature, I suggest you also turn off the “suggest a cell” feature, as an empty board will cause it to run decently slow with its current implementation.