Page 1 of 2 12 LastLast
Results 1 to 40 of 65

Thread: Looping to put numbers in a 9x9 grid of Text Boxes

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Looping to put numbers in a 9x9 grid of Text Boxes

    I'm trying to make a sub that can cycle through all of the text boxes and input a number into each one (a square grid 9x9). I thought of possibly using two loops (one vertical, one horizontal) to go through and make a string by making it equal to e.g. "Row" & v & "Col" & h (where v & h are variable integers). What I'm not sure on here is how to get the code to look up that object as I would normally do something like "Text.value = String" but how could I get the code to replace "Text" in this phrase? Or would there be a better way of doing it? In case something similar like this has been said before, I just want to say that I wasn't exactly sure what to search on for this. Thanks in advance.
    Last edited by AdmiralJonB; May 30th, 2005 at 03:28 PM. Reason: New Title

  2. #2
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    I would put all 9 text boxes in a group panel and name it gp then name your text boxes like this textbox1 = textbox9

    then do
    for each x as textbox in gp.controls
    select case x.name
    case textbox1:
    textbox1.text = 1
    case textbox2:
    textbox2.text = 2
    next

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Quote Originally Posted by Porsche944
    I would put all 9 text boxes in a group panel and name it gp then name your text boxes like this textbox1 = textbox9

    then do
    for each x as textbox in gp.controls
    select case x.name
    case textbox1:
    textbox1.text = 1
    case textbox2:
    textbox2.text = 2
    next
    I don't quite think that this would solve what I am trying to do, I suppose I should go into a little more detail. I have an array size 81 (for the 9x9 grid of text boxes) by which I have inputted a number into each one. I then need to put these into each box, and if i understand what you're saying correctly, surely this would take a lot of code? I'm not too sure what you're saying there though (I'm pretty new to programming and have done a few basic programs, I looked up msdn and think i understood what select case is about, and for each).

    Is there a way to set the value in a variable as the name of the textbox?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    There is nothing wrong with Porsche944's solution but remeber that you can use Is in a Case expression, i.e.
    VB Code:
    1. For Each x As TextBox In gp.Controls
    2.     Select Case x
    3.         Case Is Me.TextBox1
    4.         '...
    5.     End Select
    6. Next
    You wouldn't really notice a difference but it is more efficient.

    The problem is, you are referring to each TextBox by name anyway so you may as well just write one line of code for each TextBox and assign it a text value. The only way it is worthwhile using a loop is if you are placing the same text in each control or you are using some logical series like incrementing a number. Then you don't have to refer to the control by name so you write less code. Otherwise a loop is pointless.
    Last edited by jmcilhinney; May 30th, 2005 at 06:38 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    So are you saying that I will have to write out every text box? I was thinking that possibly you could do something like this(pseudo)...

    Code:
    For x = 1 to 9
         For y = 1 to 9
              Merge = "Row" & x & "Col" & y
              Value stored in merge as object name = array calculated by x & y
         Next
    Next
    Is there any possible way to do this or will I have to type in all 81 box names in the end?

    Edit: Just a thought, could you possibly use the tab index to select the box I want?
    Last edited by AdmiralJonB; May 31st, 2005 at 02:17 AM.

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    VB Code:
    1. Dim iCount as int32 = 11
    2. For Each x As TextBox In gp.Controls
    3.     x.text = icount.tostring
    4.     iCount += 1
    5. Next

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Quote Originally Posted by AdmiralJonB
    So are you saying that I will have to write out every text box? I was thinking that possibly you could do something like this(pseudo)...

    Code:
    For x = 1 to 9
         For y = 1 to 9
              Merge = "Row" & x & "Col" & y
              Value stored in merge as object name = array calculated by x & y
         Next
    Next
    Is there any possible way to do this or will I have to type in all 81 box names in the end?

    Edit: Just a thought, could you possibly use the tab index to select the box I want?
    You can store a Point object in the Tag property of each TextBox that refers to its X and Y coordinates in the grid. Then loop through all TextBoxes and assign the Text based on the Tag.

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    Am I missing the point of this or stating the obvious??

    You are using an array for the values, presumably

    Dim arrValues(8,8) As Integer

    So why not put your texboxes in a similar array

    Dim arrTextBoxes(8,8) As textBox


    You can easily create the textboxes in code and add each one into the correct element of arrTextBoxes and then simply use something like:

    VB Code:
    1. Dim RowCount, ElementCount1 as Integer
    2.  
    3. For RowCount = 0 to 8
    4.   For ElementCount = 0 to 8
    5.     arrTextBox(RowCount,ElementCount).Text=arrValues
    6. (RowCount,ElementCount).ToString
    7.   Next
    8. Next
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    How did you get on? You did not say if you wanted the numbers in numerical or random order. Check the following against what you have done or want to do.

    VB Code:
    1. Dim RowCount, ElementCount As Integer
    2.         Dim arrtextbox(9, 9) As TextBox
    3.         Dim arrvalues(9, 9) As Integer
    4.         For RowCount = 0 To 8
    5.             For ElementCount = 0 To 8
    6.                 arrvalues(RowCount, ElementCount) = ((RowCount * 9) + (ElementCount + 1))
    7.             Next
    8.         Next
    9.         Dim iHor, iVert As Integer
    10.         iHor = 1
    11.         iVert = 1
    12.         Dim TempText As TextBox
    13.         Dim icount As Integer
    14.         Dim iLeft As Integer = 1
    15.         Dim iTop As Integer = 1
    16.         For RowCount = 0 To 8
    17.             For ElementCount = 0 To 8
    18.                 TempText = New TextBox
    19.                 TempText.Width = 20
    20.                 TempText.Visible = True
    21.                 TempText.Location = New Point(iLeft * 10, iTop * 5)
    22.                 TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
    23.                 Me.Controls.Add(TempText)
    24.                 arrtextbox(RowCount, ElementCount) = TempText
    25.                 If (ElementCount + 1) Mod 9 = 0 Then
    26.                     iLeft = 1
    27.                     iTop = iTop + 4
    28.                 Else
    29.                     iLeft += 3
    30.                 End If
    31.             Next
    32.         Next
    33.  
    34.  
    35.  
    36.         For RowCount = 0 To 8
    37.             For ElementCount = 0 To 8
    38.                 arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
    39.             Next
    40.         Next
    41.     End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    The numbers I have are calculated randomly and checked through validation (which I haven't been able to iron out the bugs in yet so i haven't actually tried much of this part yet). I thought I had all of it sorted out but it appears not. It gets stuck in a large loop and i haven't quite figured out what's wrong yet.

  11. #11
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Post the code that initializes your random number

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    This what you're looking for?

    VB Code:
    1. Dim arrrandom(80) As Integer  '
    2.         Dim iCount, icount1 As Integer
    3.  
    4.         For iCount = 1 To 81   '  store numbers 1 to 81 in array
    5.             arrrandom(iCount - 1) = iCount
    6.         Next
    7.         Dim r As New Random
    8.         Dim arrTemp(80) As Integer  ' create array to hold random numbers
    9.                                                ' selected
    10.         Dim iRand As Integer
    11.         For iCount = 0 To arrrandom.GetUpperBound(0)  'get 81 random numbers
    12.             iRand = r.Next(81- iCount)  'select array index as random number
    13.             arrTemp(iCount) = arrrandom(iRand) ' get actual random number
    14.                                                                   ' and store in order of
    15.                                                                   ' selection
    16.             For icount1 = iRand To arrrandom.GetUpperBound(0) - 1
    17.                 arrrandom(icount1) = arrrandom(icount1 + 1) ' remove selected
    18.                                                                              'index item and
    19.                                                                              'move all higher
    20.                                                                              'indexes down 1
    21.             Next
    22.             ReDim Preserve arrrandom(arrrandom.GetUpperBound(0) - 1)
    23.                                          'remove highest remaining indexno
    24.         Next
    25.  'arrTemp  now holds all the numbers from 1 to 81 in selected random order
    26.  
    27.         Dim RowCount, ElementCount As Integer
    28.         Dim arrtextbox(9, 9) As TextBox
    29.         Dim arrvalues(9, 9) As Integer
    30.         icount1 = 0
    31.  
    32.    'Transfer random numbers to array
    33.         For RowCount = 0 To 8
    34.             For ElementCount = 0 To 8
    35.                 arrvalues(RowCount, ElementCount) = arrTemp(icount1)
    36.                 icount1 += 1
    37.             Next
    38.         Next
    39.         Dim iHor, iVert As Integer
    40.         iHor = 1
    41.         iVert = 1
    42.         Dim TempText As TextBox
    43.         Dim iLeft As Integer = 1
    44.         Dim iTop As Integer = 1
    45.  
    46.   'create 81 textboxes in 9 * 9 block
    47.         For RowCount = 0 To 8
    48.             For ElementCount = 0 To 8
    49.                 TempText = New TextBox
    50.                 TempText.Width = 20
    51.                 TempText.Visible = True
    52.                 TempText.Location = New Point(iLeft * 10, iTop * 5)
    53.                 TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
    54.                 Me.Controls.Add(TempText)
    55.                 arrtextbox(RowCount, ElementCount) = TempText
    56.                 If (ElementCount + 1) Mod 9 = 0 Then
    57.                     iLeft = 1
    58.                     iTop = iTop + 4
    59.                 Else
    60.                     iLeft += 3
    61.                 End If
    62.             Next
    63.         Next
    64.  
    65.  
    66.  
    67.         For RowCount = 0 To 8
    68.             For ElementCount = 0 To 8
    69.                 arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
    70.             Next
    71.         Next

    EDIT. Sorry. A slight bug in that. It can produce the number 0. I'll work on it tomorrow unless you can find it first.

    OK I've solved it. I Altered

    For iCount = 1 To arrrandom.GetUpperBound(0)
    iRand = r.Next(82 - iCount)
    arrTemp(iCount-1) = arrrandom(iRand)

    To

    For iCount = 0 To arrrandom.GetUpperBound(0)
    iRand = r.Next(81 - iCount)
    arrTemp(iCount) = arrrandom(iRand)

    Goodnight, or rather morning.!!!!
    Last edited by taxes; May 31st, 2005 at 08:57 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    Just reslised above code only allows you to create one set of random numbers and you might want to do it several times. Actually it doesn't but you will never see anything else but the first set as all the others will be hidden behind the first set. Split the code into two separate events

    VB Code:
    1. Dim arrtextbox(9, 9) As TextBox  'Form scope
    2.  
    3.    Private Sub btnCreateTextBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    4.         Dim iHor, iVert As Integer
    5.         Dim RowCount, ElementCount As Integer
    6.  
    7.         iHor = 1
    8.         iVert = 1
    9.         Dim TempText As TextBox
    10.         Dim iLeft As Integer = 1
    11.         Dim iTop As Integer = 1
    12.         For RowCount = 0 To 8
    13.             For ElementCount = 0 To 8
    14.                 TempText = New TextBox
    15.                 TempText.Width = 20
    16.                 TempText.Visible = True
    17.                 TempText.Location = New Point(iLeft * 10, iTop * 5)
    18.                 TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
    19.                 Me.Controls.Add(TempText)
    20.                 arrtextbox(RowCount, ElementCount) = TempText
    21.                 If (ElementCount + 1) Mod 9 = 0 Then
    22.                     iLeft = 1
    23.                     iTop = iTop + 4
    24.                 Else
    25.                     iLeft += 3
    26.                 End If
    27.             Next
    28.         Next
    29.     End Sub
    30.  
    31.  Private Sub btnCreateNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    32.  
    33.         Dim arrrandom(80) As Integer
    34.         Dim iCount, icount1 As Integer
    35.  
    36.         For iCount = 1 To 81
    37.             arrrandom(iCount - 1) = iCount
    38.         Next
    39.         Dim r As New Random
    40.         Dim arrTemp(80) As Integer
    41.         Dim iRand As Integer
    42.         For iCount = 0 To arrrandom.GetUpperBound(0)
    43.             iRand = r.Next(81 - iCount)
    44.             arrTemp(iCount) = arrrandom(iRand)
    45.             For icount1 = iRand To arrrandom.GetUpperBound(0) - 1
    46.                 arrrandom(icount1) = arrrandom(icount1 + 1)
    47.  
    48.             Next
    49.             ReDim Preserve arrrandom(arrrandom.GetUpperBound(0) - 1)
    50.         Next
    51.         Dim RowCount, ElementCount As Integer
    52.         Dim arrvalues(9, 9) As Integer
    53.         icount1 = 0
    54.         For RowCount = 0 To 8
    55.             For ElementCount = 0 To 8
    56.                 arrvalues(RowCount, ElementCount) = arrTemp(icount1)
    57.                 icount1 += 1
    58.             Next
    59.         Next
    60.  
    61.  
    62.  
    63.  
    64.         For RowCount = 0 To 8
    65.             For ElementCount = 0 To 8
    66.                 arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
    67.             Next
    68.         Next
    69.  
    70.  
    71.     End Sub

    First click on btnCreateTextboxes and then every time you click btnCreateNumbers you will get a new set of random numbers
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    It's slightly more complicated than that when creating the random number. Nor actually creating the text boxes. It's going to be making a logic game (not one i made) where the numbers have to be specific in a certain way.

    The numbers are only from 1 to 9 to start with. Each row must only have each number once, each column must only have each number once. And finally each 3x3 square can only have 1 of each number. Then I'll have to take out a certain random amount (which will eventually be variable), to post on the screen while making those textboxes that it outputs read only (which will have to be countered when a new grid is made). After the user types in what numbers they think are to be in place, they'll be able to check their answer. All I've done so far is try to generate the numbers, which I've done here.
    VB Code:
    1. Do
    2.             Do
    3.                 Do
    4.                     Do
    5.                         GridVar(V1, H1) = Int(Rnd() * 10)
    6.                     Loop Until GridVar(V1, H1) > 0
    7.  
    8.                     Test(0) = TestHorizontal(V1, H1)
    9.                     Test(1) = TestVertical(V1, H1)
    10.                     Test(2) = TestSquare(V1, H1)
    11.  
    12.                 Loop Until Test(0) = True
    13.             Loop Until Test(1) = True
    14.         Loop Until Test(2) = True

    In this instance, Test(2) are all boolean. TestHorizontal, TestVertical and TestSquare are all functions returning a boolean. Here are the other functions. Oh, and there's another loop going around two loops going round the one above to determine V1 and H1.

    VB Code:
    1. Private Function TestHorizontal(ByVal V1 As Integer, ByVal H1 As Integer)
    2.  
    3.         Dim V2, H2, Count As Integer
    4.         Dim Test(8) As Boolean
    5.  
    6.         V2 = V1
    7.         Test(H1 - 1) = True
    8.         For H2 = 1 To 9
    9.             If H2 <> H1 Then
    10.                 If GridVar(V2, H2) = GridVar(V1, H1) Then
    11.                     Test(H2 - 1) = False
    12.                 Else
    13.                     Test(H2 - 1) = True
    14.                 End If
    15.             End If
    16.         Next
    17.  
    18.         Count = -1
    19.         Do
    20.             Count += 1
    21.             If Count = 9 Then
    22.                 Exit Do
    23.             End If
    24.         Loop Until Test(Count) = False
    25.  
    26.         If Count = 9 Then
    27.             Return True
    28.         Else
    29.             Return False
    30.         End If
    31.  
    32.     End Function
    33.  
    34.  
    35.     Private Function TestVertical(ByVal V1 As Integer, ByVal H1 As Integer)
    36.  
    37.         Dim V2, H2, Count As Integer
    38.         Dim Test(8) As Boolean
    39.  
    40.         H2 = H1
    41.         Test(V1 - 1) = True
    42.         For V2 = 1 To 9
    43.             If V2 <> V1 Then
    44.                 If GridVar(V2, H2) = GridVar(V1, H1) Then
    45.                     Test(V2 - 1) = False
    46.                 Else
    47.                     Test(V2 - 1) = True
    48.                 End If
    49.             End If
    50.         Next
    51.  
    52.         Count = -1
    53.         Do
    54.             Count += 1
    55.             If Count = 9 Then
    56.                 Exit Do
    57.             End If
    58.         Loop Until Test(Count) = False
    59.  
    60.         If Count = 9 Then
    61.             Return True
    62.         Else
    63.             Return False
    64.         End If
    65.  
    66.     End Function
    67.  
    68.  
    69.     Private Function TestSquare(ByVal V1 As Integer, ByVal H1 As Integer)
    70.  
    71.         Dim V2, H2, V3, H3 As Integer
    72.         Dim Count(4) As Integer
    73.         Dim Test(8) As Boolean
    74.  
    75.         For V3 = 1 To 7 Step 3
    76.             For H3 = 1 To 7 Step 3
    77.                 If V3 <= V1 < (V3 + 2) Then
    78.                     If H3 <= H1 < (H3 + 2) Then
    79.                         Count(0) = V3
    80.                         Count(1) = V3 + 2
    81.                         Count(2) = H3
    82.                         Count(3) = H3 + 2
    83.                         Count(4) = 0
    84.                         For H2 = Count(2) To Count(3)
    85.                             For V2 = Count(0) To Count(1)
    86.                                 If V2 <> V1 Then
    87.                                     If H2 <> H1 Then
    88.                                         If GridVar(V2, H2) = GridVar(V1, H1) Then
    89.                                             Test(Count(4)) = False
    90.                                         Else
    91.                                             Test(Count(4)) = True
    92.                                         End If
    93.                                         Count(4) += 1
    94.                                     End If
    95.                                 End If
    96.                             Next
    97.                         Next
    98.                     End If
    99.                 End If
    100.             Next
    101.         Next
    102.  
    103.         Count(4) = -1
    104.         Do
    105.             Count(4) += 1
    106.             If Count(4) = 9 Then
    107.                 Exit Do
    108.             End If
    109.         Loop Until Test(Count(4)) = False
    110.  
    111.         If Count(4) = 9 Then
    112.             Return True
    113.         Else
    114.             Return False
    115.         End If
    116.  
    117.     End Function

    I've probably messed up completely somewhere as it continues going round everything until I get bored and stop the program. If you could help, it would be great! I haven't figured out why This isn't working. It's probably a very inefficient program as it is without the problem, but my processor is 2.8GHz so I think it can handle this.

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Well well. THAt game!!! It is nothing to do with random numbers. It is all about mathematical patterns. You won't get anywhere by taking random numbers and doing validity checks on the rows, colums and miniblocks. The biggest, fastest computor in the World couldn't do it that way. The only way that approach wold work, except by luck, would be to run through every possible combination of sequences. The incidence of the miniblocks, by the way, is irrelevant. They do make solving the puzzle easier.

    Do a google search and ask Dr. math how it should be done.

    I'm not up to that maths standard but I see one way of doing it. It took me 2 minutes to make up the 81 number sequence using paper & pencil so I'll try and replicate it in code.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    http://sudoku.sourceforge.net/

    source and binaries for a java composer/solver

    I've thrown together c# program that imports the library file that the composer creates if your interested.
    I had started to port the composer but quickly got fed up.

  17. #17
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Quote Originally Posted by DeadEyes
    http://sudoku.sourceforge.net/

    source and binaries for a java composer/solver

    I've thrown together c# program that imports the library file that the composer creates if your interested.
    I had started to port the composer but quickly got fed up.

    I feel the same. Once you spot the pattern they are easy to solve in your brain, but very difficult by computer. A simple pattern is to start in the top right with any number 1 to 9. Then complete that row sequentially . When you get to number 9 start again at 1

    The next row commences with the number 3 above the first number in the row above and the third row with 3 above that number.

    The fourth row starts with 1 above the first number in the first row; then repeat the above sequence until you get to the seventh row, which starts with 1 above the first number in the fourth row.

    You can do this accross in rows going left or right or in columns going up or down and you can start from any corner square. And you can swap any row or column with any other row or column within the same miniblock. It becomes very boring.

    Very easy for a human brain to compile in under a minute but you try and write the code for it!! I bet it takes several hours to do efficiently.
    Last edited by taxes; Jun 1st, 2005 at 08:01 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  18. #18

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    I know you said that you can't compose using random numbers, but surely a computer could create a random number within certain parameters. I've tested that both the horizontal and vertical work, just not together... But if what you say is true, then can you maybe write some pseudo code of what you're thinking?

  19. #19
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Quote Originally Posted by AdmiralJonB
    I know you said that you can't compose using random numbers, but surely a computer could create a random number within certain parameters. I've tested that both the horizontal and vertical work, just not together... But if what you say is true, then can you maybe write some pseudo code of what you're thinking?
    OK, What's your other hobby? Banging your head against a brick wall???

    I'll code what I am thinking of. Give me an hour.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  20. #20

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Oh ha ha, very funny. The creating of the textboxes with my own modification seems to be working so I can't be that bad now! I also did some programming in VBA in Microsoft Access for a project, and i've done a few basic programs. I'm working up towards a big project though eventually starting around october so I need the practice.

  21. #21
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi

    Try this.

    VB Code:
    1. Dim arrtextbox1(9, 9) As TextBox   'Form scope
    2.  
    3.   Private Sub btnCreateTextBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    4.         Dim iHor, iVert As Integer
    5.         Dim RowCount, ElementCount As Integer
    6.  
    7.         iHor = 1
    8.         iVert = 1
    9.         Dim TempText As TextBox
    10.         Dim iLeft As Integer = 1
    11.         Dim iTop As Integer = 1
    12.         For RowCount = 0 To 8
    13.             For ElementCount = 0 To 8
    14.                 TempText = New TextBox
    15.                 TempText.Width = 20
    16.                 TempText.Visible = True
    17.                 TempText.Location = New Point(iLeft * 10, iTop * 5)
    18.                 TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
    19.                 Me.Controls.Add(TempText)
    20.                 arrtextbox1(RowCount, ElementCount) = TempText
    21.                 If (ElementCount + 1) Mod 9 = 0 Then
    22.                     iLeft = 1
    23.                     iTop = iTop + 4
    24.                 Else
    25.                     iLeft += 3
    26.                 End If
    27.             Next
    28.         Next
    29.     End Sub
    30.  
    31.  Private Sub btnFillBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    32.    
    33.  
    34.         Dim arrvalues(9, 9) As Integer
    35.         Dim arrTemp(81) As Integer
    36.         Dim r As New Random
    37.         Dim iStartCorner, iFirstNumber, iCount, iCount1, iRowCount, iElementCount As Integer
    38.         Dim arrSquare(81) As Integer
    39.         ' Select starting corner for sequence
    40.         iStartCorner = r.Next(4)
    41.         ' Select number to be placed in starting square
    42.         iFirstNumber = r.Next(1, 9)
    43.         For iCount = 0 To 8
    44.             For iCount1 = 0 To 8
    45.                 'Limit number to range  1 to 9
    46.                 If iFirstNumber = 10 Then iFirstNumber = 1
    47.                 'Store number sequence in array
    48.                 arrTemp(iCount * 9 + iCount1) = iFirstNumber
    49.                 iFirstNumber += 1
    50.             Next
    51.             'sets first number of next row
    52.             Select Case iCount
    53.                 Case 2
    54.                     iFirstNumber = arrTemp(1)
    55.                 Case 5
    56.                     iFirstNumber = arrTemp(28)
    57.                 Case Else
    58.                     iFirstNumber = arrTemp(iCount * 9 + 3)
    59.             End Select
    60.         Next
    61.         iCount = 0
    62.         For iRowCount = 0 To 8
    63.             For iElementCount = 0 To 8
    64.                 arrtextbox1(iRowCount, iElementCount).Text = arrTemp(iCount).ToString
    65.                 iCount = iCount + 1
    66.             Next
    67.         Next
    68.  
    69.     End Sub

    You owe me a bottle of aspirin

    EDIT: I just had a thought (rare occurence!!) There is nothing sacred about the order of numbers. Provided the pattern is maintained the number base need not be in numerical order. So, try filling out the first line in random numbers (use my early post to do this) and then refer to that numerical sequence to provide the pattern for the other rows. This will give a semblence of being random. If you can't manage this I will look later or tomorrow. Let me know please. Another way of doing this would be to complete the arrTemp and then change all the numbers identically. e.g. all the 4's become 7's etc.
    Last edited by taxes; Jun 1st, 2005 at 11:31 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  22. #22

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    It's not that I don't appreciate it or anything, but i have a few queries about this. First, why do you use the iStartCorner as all i can see you do is set a value for it and nothing else, then from what i can see, doesn't this only create an ascending sequence of numbers (until 9 then back to 1 obviously). Am I missing something?

    Edit: Oops, just seen you're edit, One moment. Also, I probably seem stupid, but is there a way to set font size? The only commands I've found only get the size of the text and not set it.

    Edit 2: Forget about the font size
    Last edited by AdmiralJonB; Jun 1st, 2005 at 01:42 PM.

  23. #23
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    I put the start corner bit in to give you a pointer of how to vary things a bit. I have not written the code for it but it will be quite simple. Once you have the pattern established you can vary it in all sorts of ways and I have tried to give you a few ideas. For example, if you start at the top left corner, you could swap the rows and columns around and fill in the grid by going from top to bottom. You will have to slightly change the code but I thought you would want to experiment with it. I have given you lots of ideas on how to vary the obvious appearance of the pattern to make it appear random, but as I said before, you cannot construct the whole grid on a random number basis because you are restricted as to which numbers you can place in a particular textbox by the numbers previously entered in the same row/column/miniblock. As I have said, you could use random numbers to complete the top row. You may be able to work out an entirely different pattern from mine but that is just one which I could see very quickly.

    There is an enormous number of possible combinations of the numbers, the vast majority of which will not fulfill your requirements ( no repetition of the same number in the same row or column or miniblock). There are 362880 ways of arranging 9 numbers in just 1 row!!! With 2 rows there are 131,681,890,000 ways. With 3 rows there are 47,784,726,000,000,000. You can work out for yourself how many possibilities there are with 9 rows!! No wonder your attempt went into what appeared to be an infinite loop It wasn't infinite but it will take longer than your lifetime to complete it
    Last edited by taxes; Jun 1st, 2005 at 07:12 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  24. #24

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    After some debugging, and refining, My program seems to be getting close to working. Surprised? Well, there are still a few bugs to iron out, especially seeming as for some reason the last number is always 9 in a row... Oh, and your text function has managed to get me to understand how it works and re-write it. And surprisingly enough, it works! Also, I've managed to condense the coding. I've still got more work on the square testing though...

    Here's basically how it works at the moment...

    VB Code:
    1. For V1 = 0 to 8
    2.     For H1 = 0 to 8
    3.         If H1 = 8 Then  'This is done to easily calculate the final number
    4.  
    5.             For H2 = 0 To 7
    6.                 arrGridVar(V1, H1) += arrGridVar(V1, H2)
    7.             Next
    8.             arrGridVar(V1, H1) = 45 - arrGridVar(V1, H1)
    9.  
    10.         Else
    11.  
    12.            
    13.             Do   'This is the standard testing
    14.  
    15.                 arrGridVar(V1, H1) = Random.Next(1, 10)
    16.  
    17.                 arrTest(0) = TestHorizontal(V1, H1) 'These are the tests for both  Horizontal and Vertical
    18.                 arrTest(1) = TestVertical(V1, H1) 'which are basically loops that check whether there are any numbers in the previous row/column
    19.  
    20.                 If arrTest(0) = True Then
    21.                     If arrTest(1) = True Then
    22.                         arrTest(3) = True
    23.                     End If
    24.                 End If
    25.  
    26.             Loop Until arrTest(3) = True
    27.  
    28.         End If
    29.     Next
    30. Next

    The problem I have, is when it gets to a bit later on, it has problems determining the random number as e.g. the chance of getting the 9th value on a horizontal is 1/9, the second line would make the 1/18 because of the line above is, then 1/27, 1/36, 1/45... 1/81 etc. After working all this out, I'm believing your statement, just I think that there is still a chance that some of this can be salvaged.

    I was thinking that it could possibly be that if testing fails after a couple of times, that maybe there could be a few extra parts in for to prosuce a number based on what is already placed, but I don't have much time to think of it now. Thanks for all your help and all btw, I'll make sure to have a credits section with your name on it, and any others that have/will contributed.

  25. #25
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    No, I'm not surprised it is getting CLOSE to working. It is possible to do it but I think it will just take too may loops before you get there. We shall see.
    The following will show you the difficulty. This will fill in each textbox with a truly random number which qualifies for insertion BUT if there is no such number available it will leave a blank. Once you have created the textboxes keep pressing the Compile button and see how the number of blanks varies.

    I am going to try looping through the code and see how many attempts it needs to produce a full result (if it ever does)

    An interesting exercise.

    VB Code:
    1. Private Sub btn Compilegrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    2.        
    3.      Dim iCount, iCount1, iNewNumber, iNewRandom As Integer
    4.         For iCount = 0 To 8
    5.             For iCount1 = 0 To 8
    6.                 arrtextbox1(iCount, iCount1).Text = ""
    7.                 arrNumbers(iCount, iCount1) = 0
    8.             Next
    9.         Next
    10.  
    11.         'select valid range of numbers for next space
    12.         For iCount = 0 To 8
    13.             For iCount1 = 0 To 8
    14.                 SelectValidRange(iCount, iCount1)
    15.                 If arrTempRow.GetUpperBound(0) < 0 Then Exit For
    16.                 iNewRandom = r.Next(0, arrTempRow.GetUpperBound(0))
    17.                 iNewNumber = arrTempRow(iNewRandom)
    18.                 arrNumbers(iCount, iCount1) = iNewNumber
    19.                 arrtextbox1(iCount, iCount1).Text = iNewNumber.ToString
    20.             Next
    21.         Next
    22.  
    23.  
    24.     End Sub
    25.  
    26.  
    27.  
    28.  Private Sub SelectValidRange(ByVal iRow As Integer, ByVal iCol As Integer)
    29.  
    30.  
    31.         Dim icount, icount1, icount2 As Integer
    32.         ReDim arrTempRow(8)
    33.         For icount = 0 To 8
    34.             arrTempRow(icount) = icount + 1
    35.         Next
    36.         'Check the current row
    37.         For icount = 0 To 8
    38.             For icount1 = 0 To arrTempRow.GetUpperBound(0)
    39.                 If icount1 > arrTempRow.GetUpperBound(0) Then Exit For
    40.                 If arrNumbers(iRow, icount) = arrTempRow(icount1) Then
    41.                     For icount2 = icount1 To arrTempRow.GetUpperBound(0) - 1
    42.                         arrTempRow(icount2) = arrTempRow(icount2 + 1)
    43.                     Next
    44.                     ReDim Preserve arrTempRow(arrTempRow.GetUpperBound(0) - 1)
    45.                 End If
    46.             Next
    47.         Next
    48.         For icount = 0 To 8
    49.             For icount1 = 0 To arrTempRow.GetUpperBound(0)
    50.                 If icount1 > arrTempRow.GetUpperBound(0) Then Exit For
    51.                 If arrNumbers(icount, iCol) = arrTempRow(icount1) Then
    52.                     For icount2 = icount1 To arrTempRow.GetUpperBound(0) - 1
    53.                         arrTempRow(icount2) = arrTempRow(icount2 + 1)
    54.                     Next
    55.                     ReDim Preserve arrTempRow(icount2 - 1)
    56.                 End If
    57.             Next
    58.         Next
    59.  
    60.     End Sub
    Last edited by taxes; Jun 2nd, 2005 at 08:40 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  26. #26
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    Just to check...it looks as though you're trying to make a SuDoku generator rather than a solver, non? If so, then I think this may be somewhat more difficult to do properly than just putting numbers in the boxes so that it conforms to the rules regarding one of each in every row, column and grid. The specific additional rule I'm thinking of is the one that, with the information provided to the user, there is only one possible solution. You need to make sure that the player can in fact make a first move, and then a second and so on until the grid is complete.
    If there is more than one legitimate solution, will you just check the user's solution against the 3 rules and see if it fits, allowing him to win if it is correct even if not the one generated by the program. Also, you'll have no control over the level of difficulty that way.

    Not trying to pour the water on, but it's something worth bearing in mind right from the start because you really don't want to spend ages coding it in a particular way and then find that you need to do it in an entirely different way. For example, maybe it would be better to work out the sequence of moves first and then add the numbers in, rather than work out a number arrangement and try to find a sequence of moves that leads to it. An additional benefit that working out the moves first has is that you're guaranteed to satisfy the 3 rules, because these are incorporated into the premise behind making a legitimate move.

    Just a thought, anyway.

    zaza
    Last edited by zaza; Jun 2nd, 2005 at 02:28 PM.

  27. #27

    Thread Starter
    New Member
    Join Date
    May 2005
    Location
    UK
    Posts
    15

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    It's certainly a thought. I'll be thinking about it.

  28. #28
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi zaza,

    I'm afraid I must disagree with you. When you produce the full Sudoku square, it is ALWAYS possible to change it by a small or large degree. The numbers are always merely interchangeable with each other, provided you change all the same numbers to the same other number. e.g. you could change all the 2's to 6's: all the 6's to 3's and all the 3's to 2's and you would still get a correct grid. Whether or not you produce a puzzle which is unique depends on how many numbers you reveal and where they are, not on how the numbers are first positioned. No matter how many numbers are left unrevealed, it is ALWAYS possible for the correct moves to be made - it may take a long time but the ones I have seen are easily completed. Usually there are only 2 or 3 possible entries for any one particular space and the existence of the miniblocks makes it easier to solve.

    The problem we are considering is how to construct the grid from a series of random number generations rather than by mathmetical patterns. I don't know how AdmiralJohnB is getting on but my best effort so far (30000 attempts) is 70 correctly situated numbers. As he posted, 9 appears to be the fly in the ointment!!!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  29. #29
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    If you need to put numbers in the textboxes over and over again, I think this will get you what you want. I did it for a 4x4 array to save time, but it is really easy to expand and works perfectly. I realize it's a pain to put 81 textboxes on a form and assign all 81 of them, but it really isn't that bad with cut and paste. Good Luck.

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5. 'I cut this out to make it readable
    6. #End Region
    7.     Dim array(4, 4) As TextBox
    8.     Dim data(4, 4) As Int32  'data to be displayed in text boxes
    9.  
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         SetControlArray()  'define textbox array
    13.         LoadUpDataArray()  'put data in data array to be displayed later
    14.         DisplayOutputs()  'now display the data as required
    15.     End Sub
    16.  
    17.     Sub LoadUpDataArray()  'this is just to show operation, you could pass this in as a property from another form.
    18.         Dim i As Int16
    19.         Dim j As Int16
    20.         Dim k As Int32 = 0
    21.         For i = 1 To 4
    22.             For j = 1 To 4
    23.                 k = k + 1
    24.                 data(i, j) = k
    25.             Next
    26.         Next
    27.     End Sub
    28.  
    29.     Sub DisplayOutputs()
    30.         Dim i As Int16
    31.         Dim j As Int16
    32.         For i = 1 To 4
    33.             For j = 1 To 4
    34.                 array(i, j).Text = data(i, j).ToString
    35.             Next
    36.         Next
    37.     End Sub
    38.  
    39.     Sub SetControlArray()  'define text box array
    40.         array(1, 1) = TextBox1
    41.         array(1, 2) = TextBox2
    42.         array(1, 3) = TextBox3
    43.         array(1, 4) = TextBox4
    44.         array(2, 1) = TextBox5
    45.         array(2, 2) = TextBox6
    46.         array(2, 3) = TextBox7
    47.         array(2, 4) = TextBox8
    48.         array(3, 1) = TextBox9
    49.         array(3, 2) = TextBox10
    50.         array(3, 3) = TextBox11
    51.         array(3, 4) = TextBox12
    52.         array(4, 1) = TextBox13
    53.         array(4, 2) = TextBox14
    54.         array(4, 3) = TextBox15
    55.         array(4, 4) = TextBox16
    56.     End Sub
    57. End Class

  30. #30
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi rickford66,

    I guess you haven't bothered to read the earlier posts in this thread. What you are suggesting can be done quicker and easier in code which has been posted, but that is not what is wanted. Have a look and you will save yourself hours in future

    AdmiraljonB wants to complete a legitimate Sudoku grid using random numbers. Read the thread and you will see the problem.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  31. #31
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    I'm not sure AdmiraljonB does want to complete a legitimate sudoku grid using random numbers. I do agree with you that this approach will eventually work as a solver. But my point is that I think he's after a generator, ie he wants to create the puzzles. In this instance, I don't think that this approach will work.

    The sudoku is a sequnce of logical moves from start, at which point the player has a partially filled grid, to finish at which the numbers are filled in in the only possible pattern. On the approach currently under discussion, the puzzle will be generated by compiling a 9x9 grid which conforms to the rules of 1-9 in every row, column and mini-grid, followed by randomly selecting some numbers to display to the player and covering up the rest. I agree, taxes, that the numbers initially shown are of crucial importance, and that given a certain "pattern", you can interchange 1 for 2 and 2 for 1, for example, and still have a complete puzzle. However, what needs to be done is to ensure that given the initial numbers, the player can actually complete the puzzle logically. If you were to give 9 numbers, distributed randomly, the player might get just the first row. In that case, the puzzle would be unsolveable, at least uniquely, but 6 numbers suitably placed might be sufficient to solve it so. I suspect that is why so many puzzles you've seen are easy - in order to make the puzzle solvable a lot of information needs to be uncovered and then the puzzle just becomes simple.
    If you're going to check that the puzzle is solveable, then I reckon you might as well work on a move generator and once the puzzle is complete, randomly assign each of the pieces of patchwork with one of the numbers from 1 to 9. To put it another way, if you consider one of the puzzles you see in the newspapers, often if just one of those numbers hadn't been given to you, the whole puzzle would fall apart. Also, you don't need to use all of the numbers to make the first move, and often the positioning of a number will only prove necessary for a move halfway through the game. If this situation occurs in your generated puzzle, you need to have determined this from the start and ensured that you have given the player the number necessary to make the move.

    I know I'd be a bit miffed if I spent an hour or two on a puzzle only to find that I got to a point where I couldn't go any further because I needed a number which hadn't been given to me.

    zaza

  32. #32
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi Taxes,
    I did read most of the posts, but I didn't pick through the code too much. I guess I was in too much of a hurry. As for saving hours, I just did this in a project I was working on and so I just renamed the variables generic and pasted it in... almost no time at all. It would have taken me longer to read all the posts. hehe

  33. #33
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi AdmiraljonB

    How are you getting on? I've managed to get a grid with 78 boxes satisfied in one batch of 10,000 attempts (It takes about 5 minutes for the full 10,000). 75 boxes occurs approximately every 100 attempts. I reckon I've made over 100,000 attempts so far. The interesting thing is that having to comply with the miniblock rule makes both the compiling and the solving easier!

    I'll try leaving it running overnight and see what happens. It should get through over 500,000 attempts

    Hi zaza,

    Slight misunderstado not think for a secon that we have been discussing a Solver. It has been very clear, once the sudhoko connection emerger, that we were looking at a generator and that of a random, not a pattern, base.

    You may be right but I can't see it at the moment. As I understand it, there is no such thing as a sequence of logical moves involved. Depending on how your brain works (mathmetically, visually or by instinct) you might choose to adopt such an approach but the selection of one number does not determine the choice of the next number. It is mathmetically possible for there to be a choice of the same two numbers in the same two different slots but it is much more likely that would not occur. In my very limited experience of the game, you can usually limit the number of possibilities for each slot to 2 or 3 and if you list those possibilities there is often an obvious selection.

    If you start off with a legally generated grid and simply delete the required number of slots, the puzzle is bound to be solevable. The question of a sequence of moves just does not arise. If you have a particular example in mind perhaps you could e-mail it to me. IF I manage to solve it I will e-mail my steps back to you and compare it with your approach.

    I must say that I find the problem of producing a random generated grid much more interesting than solving one
    Last edited by taxes; Jun 3rd, 2005 at 12:50 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  34. #34
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Quote Originally Posted by rickford66
    Hi Taxes,
    I did read most of the posts, but I didn't pick through the code too much. I guess I was in too much of a hurry. As for saving hours, I just did this in a project I was working on and so I just renamed the variables generic and pasted it in... almost no time at all. It would have taken me longer to read all the posts. hehe

    Hi, If you have, say, 15 or more of the same controls to create, then it is quicker to create them in code. If you are then going to create references to them in an array, then it is far quicker in code. There is no provision for a handler in the code in this thread (it wasn't requested) but that is covered in the Codebank post and takes just one line of code.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  35. #35
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Grrrrrrrrrrrrrrrrrrrrrrr. Is there a cunning device in this forum to stop people entering messages that are too long by timing them out? That's the third time today I've submitted a post only to be told "You need to log in, blah blah blah" and then being dumped out, having lost the text of my post. Grrrrrrrrrrrrrrrrrrrrrr.

    Rant over.



    Anyway, try this:

    123456789
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP

    P is a number you have to find.

    Hint, you've already thought of a solution.








    But I bet you aren't right. And that's giving you 9 numbers. How about a few more:

    123456789
    456789123
    789123456
    234567891
    567891234
    891234567
    345678912
    PPPPPPPPP
    PPPPPPPPP

    63 numbers now, and you still can't tell me? But now we're at the point at which if I give you just one more number, you'll have a unique solution. The question is, how many numbers do you need, and where do they have to go? One in each row? Column? Minigrid? One of each type? Anything else?

    When you start the game, you begin by making all the moves which depend solely on the numbers given to you. When these moves are exhausted, any further moves must rely on the fact that you have positioned some numbers. However, there is no reason that the moves made so far require all of the initial numbers. There may be a move you need to make in, say, twenty moves' time, which requires that a particular box has been filled in, to eliminate all bar one possibility. But because that is the first time that you have needed for that box to be filled, you don't realise that it is necessary until then. On the other hand, when the program is setting up the puzzle, it does need to know that you will require that box to be filled in, otherwise the player is going to get stuck at that point.

    I don't know what the likelihood of this is, but I reckon that if you just pick a random number of boxes to reveal in random locations, your chances of completing the puzzle get very small very quickly.


    HTH

    zaza

  36. #36
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    HI,

    123456789
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP


    I don't need a hint. It took me 2 seconds to think of the answer an 30 seconds to type it.


    123456789
    456789123
    789123456
    234567891
    567891234
    891234567
    345678912
    678912345
    912345678


    There are other possibilities of course

    123456789
    789123456
    456789123
    234567891
    891234567
    567891234
    345678912
    912345678
    678912345

    This is a simple mathematical pattern. It based on a group of 3. If you know the first line, no matter what the order of that line, you can easily calculate all the other slots. I don't think I've made any typing errors in that

    BTW I did the above before looking at your second sequence. As you say, you have simply copied out my previously posted solution using the pattern method. Why did you do that?????????? You obtained that sequence by using my posted solution. I'm not quite that daft The pattern is obvious.

    BTW HOW much did you bet????????


    Another BTW. Are you sure you've spelt your handle correctly. Shouldn't it be gaga ??? Or is that term local to England?


    Hi admiraljonb,

    I left my programme running for over 2 hours and it generated 79 legitimate numbers!!
    Last edited by taxes; Jun 3rd, 2005 at 03:53 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  37. #37
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Y'awright moy luvver?

    Having spent the past 8 years living in Clifton, I moved to Oxford a few months ago. So I guess I'm local enough to understand.

    You've illustrated my point perfectly in your post, and the reason I used your suggestion is that it is a) the most obvious way to satisfy the requirements of the puzzle and b) you clearly already knew this.
    If I was to try to solve a puzzle which started me off with just the first line

    123456789

    then there is no way I could logically and unambiguously fill out the rest of the grid with the correct hidden numbers. Sure, I could fill it out in many different ways by guesswork, even educated guesswork , but the point of these puzzles is that there should only be one way.

    So, regarding my bet, you submitted as your solution:

    123456789
    456789123
    789123456
    234567891
    567891234
    891234567
    345678912
    678912345
    912345678

    My actual solution, behind all the hidden P's was:

    123456789
    456789123
    789123456
    234567891
    567891234
    891234567
    345678912
    912345678
    678912345

    We may both be right, but mine was the solution, as I posed the puzzle. Can I have my prize please, as long as it isn't a copy of mendhak's autobiography . I already have that.

    Let's say that the random number generator came up with the pattern above - a perfectly valid pattern if only one of many. How do you determine how many numbers, and where, to reveal so that the player can get started? If you choose 9 numbers, randomly distributed, you might get just my first line, and then there's no way you can arrive at my solution unambiguously. After all, you'd feel a bit cheated if you submitted your solution and were told you were wrong, even though you satisfied the requirements . I also showed that I could, for example, give you 63 numbers and still you couldn't give me unambiguously the correct solution.
    On the other hand, you obviously can't just reveal certain cells each time you create a puzzle: R1C3, R4C8 etc, because there's still no guarantee that the player would even be able to make the first move. And even if you revealed enough cells so that the player can get going, how do you know that they won't come unstuck in a few moves time:

    123456789
    PP6PPPPPP
    PP9PPPPP6
    PPPP6PP9P
    P6PP9PPPP
    PPPPPPPPP
    PPPPPPPPP
    PPPPPPPPP
    9PPPPP6PP

    Given this, you can make a couple of moves, but then you reach a point where you have to guess. I can still swap numbers about on rows 7 and 8, for example, and get a perfectly valid solution but only one of those is the "true" solution.

    So, once you've generated a successful number pattern, to turn that into a sudoku you need to blank out enough numbers that there's actually a puzzle to solve, but not so many that it is unsolvable. Not only that, but you need to blank them out in the right places. And that, I think, is where the problem will lie. One check to ensure that you have a solvable puzzle would be to program a "user solver", i.e., to try to make all the logical moves that the user would make and see if you can come up with a full solution. No guessing by filling it in with random numbers. If you can do this bit, then why not just run it in reverse to go from the completed puzzle to the original puzzle, and then you don't need to check?

    Incidentally, from a programming perspective, I suspect that that would also be a faster, more elegant and more interesting way to generate a random grid of 9x9 numbers subject to the rules than the "brute force" method.

    Why is your heart in Virginia? Did she have a transplant?

    zaza
    Last edited by zaza; Jun 4th, 2005 at 06:12 AM.

  38. #38
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    ANY way of completing the grid is a correct way. If you insist that the ONLY correct solution is the one you decide is so, then you must make sure there IS only one solution. You can do this by revealing key slots. Otherwise, you are inventing your own rules.

    BUT, that is not the point of this thread, which is to design a legal grid using random number generation. My stance is that, whilst theoretically possible, it would take millions of attempts. What may be feasible is to generate a grid with 75 legally placed random numbers, which seems to occur at least once every 100 attempts, and then juggle with the missing numbers, not more than 1 per row, to see if you can hit the jackpot.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  39. #39
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Evening,

    Indeed taxes, my point was simply that determining exactly which slots are key and how many of them you need for a given grid is going to be an issue eventually with this approach and given that AdmiraljonB is trying to go the whole hog, I thought it was worth a mention now.

    Anyway, as you say, to return to the problem of putting random numbers in grids, can I make a suggestion?
    Start off by filling the minigrids in a cross shape, ie the top middle, the centre row, and the bottom middle. There should be very little trouble with finding a combination to fill these in. Then, work on enlarging the cross. For example, with rows 4,5,6 fully filled and columns 4,5,6 also, there are at least 3 possible choices for R3C3. Then, there are at least 2 choices for R3C2. Finally, there is at least 1 choice for R3C1. Then you can see if you can fill in R3C7, R3C8 and R3C9. As a result, you've learned immediately whether or not you can get this far, minimising the iterations to reach a possible problem point. Then do the same around the other arms and work outwards to the corners. If not, then you might be able to rectify the problem by simply swapping two or three adjacent numbers within the cross to sort it out.

    What do you reckon?

    zaza

  40. #40
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Looping to put numbers in a 9x9 grid of Text Boxes

    Hi,

    There are several ways in which you can approach the coding using random as much as possible. I have reached the stage wher my approach produces a grid with 79 slots filled legitimately, but that took over 2 hours runtime and over 300,000 attempts. Previously running the programme for 5 minutes, some 10,000 attempts, produced a grid with 76 numbers. Running for 5 seconds, 101 attempts regularly gave gride of 70 legitimate slots so the attempts jump from 70 to 76 is a magnitude of 16 per slot whilst the jump from 76 to 79 is a magnitude of 10. So I would assume that to get to 81 legitimate slots you would need 2,500,000 attempts, over 10 hours in my programme. But that is guesswork. It is considerably less than if using an entirely random aproach because my programme prevents the selection of numbers which are not legitimate for the slot being considered, so saving a lot of time. It depends how random you want it to be.

    I am now investigating the possibility of, having found a grid of 75 legitimate numbers, juggling the existing empty slots.

    Once you have got the legitimate grid, deciding which and how many slots to reveal depends on how difficult you want to make the puzzle. If you are going to computerise the puzzle, it seems logical to allow the player to determine his/her own level of difficulty.

    Try your suggestions and see if you can improve on that. You would certainly take that approach if you were using the Pattern method of development but I, personally, don't think that is best when using a random approach.

    Turning to your comments on revealing sufficient numbers to ensure only one possible solution, as in any legitimate grid it is always possible to exchange complete columns or rows so long as they do not move outside the limits of the miniblocks, you will ALWAYS have to reveal numbers for at least 2 columns and rows passing through EVERY miniblock, i.e a minimum of 10 slots, but that would be a very difficult puzzle to solve. A legal grid can also ALWAYS be vaired by simply swapping two numbers wherever they appear so to make a solution unique you also have to make sure 8 of the 9 numbers are revealed. If you want your puzzle to be solved logically - using no guesswork - you have to give sufficient information to allow 1 slot only one possibility but that then becomes a simple puzzle. There appears to be no halfmeasures with this puzzle. It is either completely logical and, therefore, easy or requires guesswork but may be more difficult. It will never replace Chess or Go


    Oh, I forgot to answer your

    "Why is your heart in Virginia? Did she have a transplant?"

    It's I who want the transplant...... to Virginia
    Last edited by taxes; Jun 5th, 2005 at 06:39 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Page 1 of 2 12 LastLast

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