Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - 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 - Saving and Loading Puzzles


    (Page 4 of 6 )

    Sometimes you might not be able to solve a puzzle at one sitting. That’s where the application’s save and load features come in handy. The Save()(see Listing 3) and Load()methods assist in this endeavor.

    Listing 3 Save a puzzle to disk


    Public Sub Save(ByVal szSaveFilePath As String)


    'This method accepts the full path and name of a file to save the contents

    'of the 9x9 2-dimensional byte arrays holding the puzzle's current state and

    'solution. In addition, the selected complexity is saved in the 1st element (0,0)

    'of the puzzle's current state array. Note that a vanilla binaryformatter is used

    'to serialize the array contents to a file.

    '

    Dim oFS As FileStream

    Dim oFormatter As IFormatter

    Dim szBaseFileName As String

    Dim szBaseFilePath As String

    Dim szBaseFileExt As String


    Try


    szBaseFileName = Path.GetFileNameWithoutExtension(szSaveFilePath)

    szBaseFileExt = Path.GetExtension(szSaveFilePath)


    szBaseFilePath = Path.GetDirectoryName(szSaveFilePath)

    If Not szBaseFilePath.EndsWith("") Then szBaseFilePath &= ""


    oFormatter = New Formatters.Binary.BinaryFormatter


    'Save original SudokuBoard

    oFS = New FileStream(szBaseFilePath & szBaseFileName & szBaseFileExt, _

    FileMode.OpenOrCreate, FileAccess.Write)

    oFormatter.Serialize(oFS, mbyary_OrgSudokuBoard)


    oFS.Close()


    'Save current SudokuBoard

    oFS = New FileStream(szBaseFilePath & szBaseFileName & _

    szBaseFileExt.Substring(0, 3) & "1", FileMode.OpenOrCreate, FileAccess.Write)

    oFormatter.Serialize(oFS, mbyary_SudokuBoard)


    Catch ex As Exception

    If File.Exists(szBaseFilePath & szBaseFileName & ".*") Then

    File.Delete(szBaseFilePath & szBaseFileName & ".*")

    End If


    Finally

    If Not (oFS Is Nothing) Then oFS.Close()

    oFS = Nothing


    oFormatter = Nothing


    End Try


    End Sub


    Both methods incorporate BinaryFormatters and FileStreams to de/serialize both puzzle arrays from and to disk. BinaryFormatters provide a way to take an object and break it up into a linear sequence of bytes, thereby making it easy to store and retrieve. The contents of the arrays are saved to and consequently loaded from two separate files. The UI supplies the name of the files to save/load by calling upon the easy-to-use SaveFileDialog and OpenFileDialog dialog boxes. As mentioned previously, the puzzle’s complexity is always saved within the mbyary_SudokuBoard array. During a load request, the UI code retrieves this value from the appropriate array element and uses it to properly set the loaded puzzle’s complexity in the File > New menu.

    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