SuDoKu challenge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eibwenbv
    New Member
    • Sep 2007
    • 7

    SuDoKu challenge

    I am totally new to Visual Basic and started out with a copy of version 4 that I was given. I am hooked. I enjoy writing code and am looking forward to creating my own spins on different code projects just like the rest of you.

    I have put together a small program for the totally beat-to-death term SuDoKu and am having trouble with generating the boards (arrays) to display.

    I have did this thus far:

    [Dim MyNum(9,9) As Integer]

    [ for x = 1 to 9 ]

    [ for y = 1 ro 9 ]


    [newZ:]
    [Randomize]
    [z = Int( ( 9 * RND) + 1)]

    [if y = 1 then goto getnewnum]
    [ for w = ( y - 1) to 1 Step -1]
    [ if MyNum(x , y) = MyNum(x , w) then goto newZ]
    [ next w]

    [if x = 1 then goto getnewnum]
    [ for v = (x - 1) to 1 Step -1]
    [ if MyNum(x , y) = MyNum(v , y) then goto newZ]
    [ next v]

    [getnewnum]
    [next y, x]


    I have done the "homework" suggested by the web page authors. There are many posts that I see that are written for you more experienced programmers, but none for me. Does anyone have a clue how to assist me or even want to try?
    I would appreciate some assistance.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    This might be of help:

    Random Filled Array

    Comment

    • eibwenbv
      New Member
      • Sep 2007
      • 7

      #3
      I have alreaady viewed that post and it seemed way to confusing. I did not wish to convert any of the numbers in the arrays to just "1's" and "2's".

      Additionally, trying the code before I made my original post left me with duplicates in my 3 x 3 arrays and in my vertical arrays (columns).

      No one seems to go past the first row. Do only SuDoKu book makers have the ability to do this? I would guess that VB4 is powerful enough to handle this task,
      but fully willing to admit that I could be wrong because I am new.

      Confused!!!!!!! !!!!

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by eibwenbv
        I have alreaady viewed that post and it seemed way to confusing. I did not wish to convert any of the numbers in the arrays to just "1's" and "2's".

        Additionally, trying the code before I made my original post left me with duplicates in my 3 x 3 arrays and in my vertical arrays (columns).

        No one seems to go past the first row. Do only SuDoKu book makers have the ability to do this? I would guess that VB4 is powerful enough to handle this task,
        but fully willing to admit that I could be wrong because I am new.

        Confused!!!!!!! !!!!
        Check out the las option, in this case, for the sudoku, only consider the 3x3 squares, start generating the 1st, 4th and 7th row:

        123456789
        234567891
        345678912

        then the 2nd, 5th and 8th, and finaly the 3rd, 6th and 9th, will be something like:

        123456789
        456789123
        879123456
        234567891
        567891234
        891235467
        345678912
        678912345
        912345678

        Then just randomly change columns and rows, but be sure that you'll only change column
        1 with 2 or 3,
        4 with 5 or 6,
        7 with 8 or 9 and the same for rows..
        you can also change sets of 3 columns and sets of 3 rows
        do this a random number of times and you'll have a nice sudoku board

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          oh, and i forgot to tell you
          if you want it even more randomly, remember that 123456789 is not the only original sequence, you can create an alternative one at random, lets say:

          159362874

          and follow the same procedure, so you'll get:

          159 362 874
          362 874 159
          874 159 362

          593 628 741
          628 741 593
          741 593 628

          936 287 415
          287 415 936
          415 936 287

          And then swap columns and rows.

          Comment

          • eibwenbv
            New Member
            • Sep 2007
            • 7

            #6
            Please forgive me for taking so long to reply.

            I also do not want to sound like an idiot, lazy or selfish, but exactly how do you do the things that you have described in code?

            Please remember two things:

            1) I am running an older version of VB (version 4, heck I got it for free!)
            2) I am new to coding

            I do realize that all of your time is valuable to you, so can you sorta "block" out the steps for me? I have already tried several approaches on my machine but they all take too long to come back with a properly filled in board quickly (yes my machine is fast enough).

            Thank you fourm for your kindness, understanding and help.

            eibwenbv

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by eibwenbv
              ...
              1) I am running an older version of VB (version 4, heck I got it for free!)
              2) I am new to coding
              VB 4?!! yeah!! i loved that version, it was the first one i had.

              And i apologize if im being confusing.

              What im saying is that if you try to randomly generate an array with 1 to 9 x 1 to 9 numbers, it will be harder to make it a sudoku board. So what you should do is to generate a "basic board" and then swap some rows and columns, but swap them in a way that it'll keep the "sudoku's properties".

              a basic sudoku board will be something like this

              Code:
               F G            HI
              123    456    789
              456    789    123
              789    123    456
              
              234    567    891
              567    891    234       A
              891    234    567       B
              
              345    678    912
              678    912    345       D
              912    345    678       E
              And you can swap any column or any row in the same "block of three"
              i.e. you can swap the whole row A with the B or D with E, but you cannot swap A with E.
              Same, you can swap columns F and G but not F with H

              And as you can see, you can replace every 1 with 3 and every 3 with 1 in the matrix... or every 9 with a 6, every 6 with a 4 and every 4 with a 9... it doesnt matter which number you have as long as they follow an order, so it will be nice that istead of creating this "original sudoku board" based in 123456789 sequence, use it as the "index map"

              So, generate a 9x1 array with numbers 1 to 9 and use the "index map" e.g.

              Generate a random array that could look like this

              Arr1 = {4,7,5,6,8,2,1, 9,3}

              And the "basic board" will be (supose the indexes go from 1 to 9 instead of 0 to 8) i.e.

              Code:
              arr1(1)arr1(2)arr1(3)      arr1(4)arr1(5)arr1(6)     arr1(7)arr1(8)arr1(9)
              arr1(4)arr1(5)arr1(6)      arr1(7)arr1(8)arr1(9)     arr1(1)arr1(2)arr1(3)
              arr1(7)arr1(8)arr1(9)      arr1(1)arr1(2)arr1(3)     arr1(4)arr1(5)arr1(6)
              (..)
              And so on, so it'll look like:

              Code:
              745    682    193
              682    193    745
              193    745    682
              
              456    821    937
              821    937    456
              937    456    821
              
              568    219    374
              219    374    568
              374    568    219
              And then make the swaps on this array.

              That should be the main idea.

              with a coulpe of arrays and some FOR or DO you should be able to do it, give it a try and if you have any further question just post it here.

              HTH

              Comment

              • Tig201
                New Member
                • Mar 2007
                • 103

                #8
                where is that pesky subscribing button.
                BTW Nifty Trick kadghar.

                Comment

                • eibwenbv
                  New Member
                  • Sep 2007
                  • 7

                  #9
                  Wow, I am so sorry I asked. I am so still confused
                  I thought my arrays (in the code above) allowed me to put these numbers
                  in any order I wanted, in any group of three I wanted, row, column or box
                  I wanted. Am I wrong?


                  When these sudoku book publishers create puzzles, how do they do it?
                  I appreciate all you guys, if no more posts appear here, have the admin
                  remove my access.


                  thanks for your time

                  Comment

                  • kadghar
                    Recognized Expert Top Contributor
                    • Apr 2007
                    • 1302

                    #10
                    Originally posted by eibwenbv
                    Wow, I am so sorry I asked. I am so still confused
                    I thought my arrays (in the code above) allowed me to put these numbers
                    in any order I wanted, in any group of three I wanted, row, column or box
                    I wanted. Am I wrong?
                    I tried to run your code but it didnt asigned the Z to the array, so i assumed (sorry if i mistaked) that what you wanted to do is something like this:

                    [CODE=vb]Dim MyNum(9, 9) As Integer
                    For x = 1 To 9
                    For y = 1 To 9
                    newZ:
                    Randomize
                    z = Int((9 * Rnd) + 1)
                    If y = 1 Then GoTo getnewnum
                    For w = (y - 1) To 1 Step -1
                    If z = MyNum(x, w) Then GoTo newZ
                    Next w
                    If x = 1 Then GoTo getnewnum
                    For v = (x - 1) To 1 Step -1
                    If z = MyNum(v, y) Then GoTo newZ
                    MyNum(x, y) = z
                    Next v
                    getnewnum:
                    Next y, x[/CODE]

                    Even then, the array will not be finished in most of the cases, this is because the probability of having a "free space" is decreasing.

                    lets say you have the 5x5 board and you are asigning the number 5 and your array is something like:

                    1 3 4 5 2
                    4 2 5 1 3
                    3 1 2 4 5
                    2 4 1 3 X

                    The only free space for that number is X but you cannot put there a 5 or any other number, then your code will enter into an infinite loop. That's why i recomended you to generate a table with one of the permutations of the problem and make columns and rows swaping. of course this method has many many limitations but will give you a solution after all.

                    Comment

                    • eibwenbv
                      New Member
                      • Sep 2007
                      • 7

                      #11
                      Like I said, I am so sorry, I give up.

                      I just don't get it.

                      I know why it don't work

                      ADMIN: please remove my access from the forum

                      Thanks guys!

                      later

                      Comment

                      Working...