Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - 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 - Creating Puzzles


    (Page 3 of 6 )

    The Generate()method is called to create new puzzles. A brute force approach is actually employed to generate a complete, solved puzzle. The heart of this method is depicted in Listing 2.

    Listing 2 Fill a new puzzle with numbers


    'Fill Board with Numbers

    oRnd = New Random


    byNumCount = 1


    Do


    byCol = oRnd.Next(1, 10)

    byRow = oRnd.Next(1, 10)


    If (mbyary_SudokuBoard(byCol, byRow) = 0) Then


    byOrgCurrSudokuNumIndex = byCurrSudokuNumIndex

    bNoCollision = False


    Do


    If isCollision(byCol, byRow, mbyary_SudokuNumbers(byCurrSudokuNumIndex)) Then


    byCurrSudokuNumIndex = (byCurrSudokuNumIndex + 1) Mod (NUM_OF_REGIONS + 1)

    If (byCurrSudokuNumIndex = 0) Then byCurrSudokuNumIndex = 1


    Else

    mbyary_SudokuBoard(byCol, byRow) = mbyary_SudokuNumbers(byCurrSudokuNumIndex)

    bNoCollision = True


    End If


    Loop Until bNoCollision OrElse _

    (byOrgCurrSudokuNumIndex = byCurrSudokuNumIndex)


    If bNoCollision Then

    byNumCount += 1


    Else

    'Backtrack a bit. Find a box that has been filled in and clear it so that we can

    'regenerate another available solution for it.

    Do


    byCol = oRnd.Next(1, 10)

    byRow = oRnd.Next(1, 10)


    If (mbyary_SudokuBoard(byCol, byRow) <> 0) Then


    mbyary_SudokuBoard(byCol, byRow) = 0

    byNumCount -= 1


    End If


    Loop Until (mbyary_SudokuBoard(byCol, byRow) = 0)


    End If


    End If


    Loop Until (byNumCount > MAX_NUM_OF_CELLS)


    A row and column are randomly selected and used to retrieve the value of an element or cell in the mbyary_SudokuBoard array. If the cell is found to be empty (by the presence of the number 0), the next available number in the mbyary_SudokuNumbers array is selected. The isCollision() method is used to check the validity of the move and if legitimate, the cell is assigned the number and the solved cell count is increased by one. Collisions occur when the selected number is found more than once in the cell’s row, column, or immediate region. In the event of a collision, each remaining number in the mbyary_SudokuNumbers array is exhausted until a collision is undetected.

    In the event that a collision is unavoidable, some backtracking is required. In this case, a previously assigned array cell is randomly selected and cleared and the count of solved cells is reduced by one. This event effectively signals the algorithm pursue a different solution path. Processing continues until all 81 cells are filled at which point the completed puzzle array, mbyary_SudokuBoard, is copied to the mbyary_OrgSudokuBoard array. The private method RemoveNumbers()is then called to randomly remove numbers from the completed puzzle array, based on the complexity chosen by the player. The method wraps up by copying the selected complexity to an unused element in the mbyary_SudokuBoard array. This element’s value is fundamental to setting up a loaded puzzle’s complexity.

    At this point, since we are talking about generating puzzles, you should be aware that some Sudoku puzzles can have multiple solutions, based on the amount of numbers removed. The algorithm does not take this into account and simply generates one valid solution. Player input is validated against this one and only solution. Altering the algorithm to generate all possible solutions for a puzzle is left to you as an optional (and adventurous) exercise.

    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 4 hosted by Hostway
    Stay green...Green IT