Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Create a Sudoku Puzzle Generator using VB....
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Create a Sudoku Puzzle Generator using VB.NET
By: Pete Rodriguez
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2008-07-29

    Table of Contents:
  • Create a Sudoku Puzzle Generator using VB.NET
  • The User Interface
  • Creating Puzzles
  • Saving and Loading Puzzles
  • Solving Puzzles
  • Printing Puzzles

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic.NET Articles
    More By Pete Rodriguez


     

    VISUAL BASIC.NET ARTICLES

    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT