Create a Sudoku Puzzle Generator using VB.NET - The User Interface
(Page 2 of 6 )
Good application architecture usually calls for separation between the presentation and business (or logic) tier, and the generator incorporates this design approach. The design lends itself well to maintainability, scalability and reusability, which is important when we talk about porting the application to the mobile platform. For the most part, the user interface or UI handles player input and puzzle plotting. Available features and functions are exposed through the UI’s menu and any intensive processing is delegated to the cSudokuBoard class. Figure 2 displays the application’s File menu.

Figure 2. The Applications Main Menu
The File > New submenu is actually built dynamically. Listing 1 illustrates how this is done in the UI’s Load Event handler.
Listing 1 Build the New Submenu
'Loop through puzzle complexity enumerated type and create dynamic menus and add
'menu click event handler to each
Dim szAryNames As String() = System.Enum.GetNames(GetType(cSudokuBoard.enumComplexity))
Dim mnu As MenuItem
For Each sz As String In szAryNames
If Not String.Equals(sz, cSudokuBoard.enumComplexity.Uninitialized.ToString) Then
mnu = Me.mniNew.MenuItems.Add(sz)
AddHandler mnu.Click, AddressOf NewGameSelected
End If
Next 'sz
Each element of the enumComplexity enumerated type exposed by the cSudokuBoard class is actually mapped to each MenuItem using the System.Enum.GetNames()method. Each MenuItem’s Click event is then hooked into the handler, NewGameSelected().NewGameSelected()pulls double duty because it prepares the UI for both new and previously saved puzzles and discerns between them by interrogating its sender parameter. If a new puzzle is requested, this parameter holds the selected complexity MenuItem otherwise it is set to the value Nothing. New puzzles get directed to the cSudokuBoard class’ Generate()method that accepts the complexity parameter. Previously saved puzzles bypass the Generate()method, and instead get loaded from disk. In this case, the complexity is retrieved from the puzzle itself (more on this later).
The cSudokuBoard Class
The cSudokuBoard class encapsulates all of the logic for generating, checking, saving, loading, and printing puzzles. Puzzles are created or loaded and stored in arrays, which are virtual representations of the 9x9 cell grid. Actually, five private module-level byte arrays are defined to store data required for generation. Table 1 lists the arrays.
Array | Description |
mbyary_OrgSudokuBoard | 9x9 2-dimensional array holding puzzle solution |
mbyary_SudokuBoard | 9x9 2-dimensional array holding current puzzle state |
mbyary_SudokuNumbers | 1-dimensional array holding number samples for puzzle |
mbyary_Boxes | 1-dimensional array holding 9 3x3 puzzle regions |
mbyary_IdentityMatrix | 1-dimensional array holding identity matrix used to discover number collisions |
Table 1. Key data arrays
The first two arrays are repopulated by the Generate()or Load()methods each time a player requests a new or previously saved puzzle from the UI. For new puzzles, the SelectNumbers() private method is called to populate mbyary_SudokuNumbers with random numbers from 1 to 9. The latter two arrays provide processing support and are initialized once in the class’s New()constructor. mbyary_Boxes identifies the rectangular coordinates (left, right, top and bottom array indexes) of the 9 regions within the 9x9 cell grid while mbyary_IdentityMatrix allows traversal of a cell’s region, the 3x3 cell grid encompassing the cell. Used exclusively by the isCollision() method, both arrays help to identify valid number placement during initial puzzle load and player input.
Next: Creating Puzzles >>
More Visual Basic.NET Articles
More By Pete Rodriguez