Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 6 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 15
    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 - Printing Puzzles


    (Page 6 of 6 )

    While developing the generator, I couldn’t think of an easy and succinct way to print the puzzle. I toyed with simulating a printable grid using ASCII line characters, Excel and various third-party tools, but each potential solution fell short of what I envisioned, even introducing needless complexity. This feature almost remained on the enhancement list until I thought about an old program I wrote back in the VB3 days that facilitated screen captures.

    The idea of presenting a printable, WYSIWYG rendition of the puzzle to the user was appealing, but I didn’t have the old code and writing it from scratch was not an option, especially for an optional feature. So it was as good a time as any to “borrow” some code. My web search led me to the Developerfusion UK website and an article written by James Crowley entitled "Capture a Screen Shot". You can find the article and code at http://www.developerfusion.co.uk/show/4630/. I ported a portion of the original C# code to VB and you’ll find it in the cScreenCapture class. Due to constraints on this article’s length, I’ll defer study of its logic to you.

    The CaptureWindow()method is tasked with getting a snap shot of any window given its handle. As you might expect, the call to it is encapsulated in the cSudokuBoard class’ Print()method. The File > Print menu’s Click Event handler passes the handle of the panel control (containing the numeric labels that comprise the 9x9 cell puzzle grid) to the method and it returns a bitmap rendition of the puzzle. To scale and print the returned screen shot, the PrintDocument object and PrintPreviewDialog dialog box are employed. The PrintDocument defines “a reusable object that sends output to a printer” while the PrintPreviewDialog dialog box allows us to preview the output before sending it to the printer. By hooking a handler into the PrintDocument’s PrintPage event and assigning the document to the Document property of the dialog, we can actually render our bitmap onto the PrintDocument. The call to the ShowDialog method sets this all in motion, raising the PrintPage event handler, PrintPuzzleBoard() shown in Listing 5. The routine literally owner-draws the puzzle bitmap and some text onto the canvas of the PrintDocument and displays the document for preview (See Figure 4) and eventual printing.

    Listing 5 Render the puzzle onto the print preview document


    Private Sub PrintPuzzleBoard(ByVal sender As Object, _

    ByVal e As System.Drawing.Printing.PrintPageEventArgs) e System.Drawing.Printing.PrintPageEventArgs)


    'This subroutine sinks the PrintPage event raised by the PrintDialog.

    'It draws header and footer information and the captured puzzle bitmap unto the

    'graphics canvas of the PrintDocument.

    '

    'Use GDI+ to scale the image for printing

    With e.Graphics


    'Make sure to draw the puzzle image within the bounds of the printed page's margins

    'Image is scaled and centered automatically

    .DrawImage(mbmp_PuzzleBoardImage, e.MarginBounds)


    .DrawString(msb_PuzzlePrintHeader.ToString & me_SelectedComplexity.ToString, _

    New Font("MS Trebuchet", 20, FontStyle.Bold), Brushes.Blue, _

    e.MarginBounds.X, e.PageBounds.Y + 50)


    .DrawString(msb_PuzzlePrintFooter.ToString, New Font("MS Trebuchet", 10, _

    FontStyle.Bold), Brushes.Blue, e.MarginBounds.X, _

    e.MarginBounds.Bottom + 5)


    End With


    e.HasMorePages = False


    End Sub


    Figure 4. The Puzzle Print Preview

    Enhancing the Puzzle Generator

    During the design phase, I came up with various ideas for improving the generator, but alas there are only so many hours in a day and I could not implement them all. Following are some of the better enhancement ideas you may wish to incorporate. Drop me an email if you do get around to implementing any or if you come up with a great one yourself!

    1. Include some AI to generate challenging puzzles.

    2. Create a user-drawn Graphical User Interface (GUI).

    3. Support multiple solutions for puzzles of greater complexity.

    4. Create a timed version of the puzzle.

    5. Substitute colors, shapes, or text for the numbers.

    In general, I find that nothing exercises a computer’s capabilities better than the development of a great puzzle, strategy, or arcade game. It’s not only an entertaining endeavor, but a rich learning experience. Hopefully you have come away with a better understanding of Sudoku and the different techniques used to provide its features. Maybe the next time you won’t be in such a hurry to buy or download the latest must-have game and instead you’ll challenge yourself to program it. Hope you enjoyed this as much as I did. Happy Programming and Gaming!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek