Results 1 to 3 of 3

Thread: [RESOLVED] a simple error? (creating sudoku grid)

  1. #1

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Resolved [RESOLVED] a simple error? (creating sudoku grid)

    I am writing a sudoku program from scratch in vb.net (which i just switched to).
    I am having a fit of a time getting the grid to generate. Here's the code, which i don't even have all the number checks in place, and it still doesn't work.
    Code:
         Randomize()
          Dim num As Integer, x As Integer, y As Integer, z As Integer, Unique As Boolean
          ReDim SudokuGrid(9, 9) 'erases the current numbers
          For y = 1 To 9
              For x = 1 To 9
                  Do
                       Unique = True
                       num = Int(Rnd() * 9) + 1
                       'first compare to horizontal
                       For z = 1 To x
                           If z <> x Then If SudokuGrid(z, y) = num Then Unique = False : Exit For
                       Next
                       'then compare to vertical
                    If Unique = True Then
                       For z = 1 To y
                          If z <> y Then If SudokuGrid(x, z) = num Then Unique = False : Exit For
                       Next
                    End If
                    'then compare to others in this box
                  ''place compare code here''
                  If Unique = True Then SudokuGrid(x, y) = num : Exit Do
              Loop
          Next
       Next
      'debug output
       Dim debugoutput As String
       For y = 1 To 9
           For x = 1 To 9
               debugoutput = debugoutput & Str(SudokuGrid(x, y))
           Next
           debugoutput = debugoutput + vbCrLf
       Next
       MsgBox(debugoutput)
      End Sub
    I highlighted in red the trouble spot. This code is supposed to generate a 9x9 grid of numbers. The check to see if horizontal is unique works fine. Every row has 1-9 inclusive in a random order.
    But for whatever reason, it just won't work vertically. It gets stuck in a permanent loop. I've spent a few hours trying to figure out why. If no one can help, i'm going to try something different. but i sure would like to know what's wrong with this code.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: a simple error? (creating sudoku grid)

    Firstly I think choosing a number at random and then seeing if it fits is a very cpu intensive solution, secondly it's flawed because a number might fit fine but then preclude other numbers later...

    Your code fills the rows then the columns starting in the top corner. It generates a random number and then checks each row, column (and then presumably block) for a repetition of your randomly chosen number ...

    example
    1 2 6 | 5 4 7 | 8 9 3
    8 5 9 | 1 6 2 | 4 7 ?

    In this example everything works fine until you reach the last square on the second row where there are no possible answers.

    If you look at any decent Sudoku solvers and present it with a blank grid it should then fill it out for you.

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: a simple error? (creating sudoku grid)

    Hi,

    Here's thread about how to create a Sudoku generator:

    http://www.vbforums.com/showthread.p...ghlight=sudoku

    Hope it will help you,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width