Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 5 - 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 - 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.

    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