Create a Sudoku Puzzle Generator using VB.NET - Solving Puzzles
(Page 5 of 6 )
The UI represents each numeric cell using a label control. Each label control restricts input to the numbers 1 through 9 and the spacebar, the latter used to clear out previous numeric entries. Positioning the mouse over a cell provides focus and tracking by highlighting the cell’s background (see Figure 3).

Figure 3. UI Data Entry
Data entry can be performed using the keyboard or mouse, the latter option stemming from the program’s initial design as a mobile application. I thought it best to allow input through successive taps of the screen with a mobile stylus. Naturally, I adapted this technique to the left mouse button. Clicking repeatedly on the mouse button cycles through the numbers 0 to 9, with 0 rendered as a space. UI handlers tied to the KeyPress and MouseUp label events catch all of the data entry. Ultimately all captured data is delegated to the class’ SetPuzzleCell()method. SetPuzzleCell()simply maps the number or space from the label to the underlying cell in the mbyary_SudokuBoard array and sets its value accordingly. In addition, if the move is deemed valid, the routine calls the isPuzzleSolved()method, shown in Listing 4 below. If you’re having trouble solving the puzzle and you need a hint, checking the Options > Show Solution menu on the UI will render the solution. Conversely, un-checking the menu reverts to the original puzzle’s state.
Listing 4 Check to see if a Puzzle has been solved
Public Function is PuzzleSolved(Optional ByVal bisKeystroke As Boolean = True) As Boolean
'This method keeps track of real-time puzzle statistics and checks to see if the
'puzzle has been solved. It uses the bisKeystroke parameter to discern if data entry
'was the result of a keystroke or some other UI event. If a keystroke and puzzle
'is found not to be solved, the method returns false. Otherwise, if a UI event
'other than a keystroke occurred or if the puzzle is in fact solved, the
'CompleteEventArgs module-level variable is set with the collected real-time statistics
'and the CompleteEvent is raised.
'
'Either this is a newly loaded game or board has yet to be initialized for a new game.
'Previous game has not been canceled.
If (mo_CEA Is Nothing) OrElse (mbyary_OrgSudokuBoard(1, 1) = 0) Then
mo_CEA = New CompletedEventArgs(0, False, New TimeSpan(Now.Hour, _
Now.Minute,Now.Second))
Return False
Exit Function
End If
'Loop through array and see if puzzle has been solved by comparing against
'solution array.
Dim bisSolved As Boolean = True
mo_CEA.TotalGuesses = mi_TotalGuesses
For byY As Byte = 1 To (mi_ActualBoardWidthAndHeight - 1)
For byX As Byte = 1 To (mi_ActualBoardWidthAndHeight - 1)
If (mbyary_SudokuBoard(byX, byY) <> mbyary_OrgSudokuBoard(byX, byY)) Then
bisSolved = False
Exit For
End If
Next 'byX
Next 'byY
If (Not bisSolved) Then
'Check if player started a new game, effectively canceling the existing game.
'Or the player simply was entering a cell value for the current puzzle
If bisKeystroke Then
Return False
Exit Function
End If
mo_CEA.isCanceled = True
End If
mo_CEA.ElapsedTime = New TimeSpan(Now.Hour, Now.Minute, _
Now.Second).Subtract(mo_CEA.ElapsedTime)
RaiseEvent CompleteEvent(Me, mo_CEA)
ClearBoard()
mi_TotalGuesses = 0
mo_CEA = Nothing
Return True
End Function
This method conducts two important functions. Firstly, it compares each cell of the puzzle array with each cell of its corresponding solution to determine if the puzzle is solved. Secondly, it collects interesting tidbits of information which it broadcasts back to all event subscribers should the puzzle be solved. It packages statistics such as the amount of numeric guesses made and the time taken to solve the puzzle into the CompletedEventArgsEventArgs object, which is passed as a parameter to the CompleteEventEvent. When the puzzle is solved, the event is raised and sunk by the UI and the data is used to report player results.
Next: Printing Puzzles >>
More Visual Basic.NET Articles
More By Pete Rodriguez