-
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.
-
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
-
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?
-
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:
For Each x As TextBox In gp.Controls
Select Case x
Case Is Me.TextBox1
'...
End Select
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.
-
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?
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
VB Code:
Dim iCount as int32 = 11
For Each x As TextBox In gp.Controls
x.text = icount.tostring
iCount += 1
Next
-
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.
-
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:
Dim RowCount, ElementCount1 as Integer
For RowCount = 0 to 8
For ElementCount = 0 to 8
arrTextBox(RowCount,ElementCount).Text=arrValues
(RowCount,ElementCount).ToString
Next
Next
-
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:
Dim RowCount, ElementCount As Integer
Dim arrtextbox(9, 9) As TextBox
Dim arrvalues(9, 9) As Integer
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrvalues(RowCount, ElementCount) = ((RowCount * 9) + (ElementCount + 1))
Next
Next
Dim iHor, iVert As Integer
iHor = 1
iVert = 1
Dim TempText As TextBox
Dim icount As Integer
Dim iLeft As Integer = 1
Dim iTop As Integer = 1
For RowCount = 0 To 8
For ElementCount = 0 To 8
TempText = New TextBox
TempText.Width = 20
TempText.Visible = True
TempText.Location = New Point(iLeft * 10, iTop * 5)
TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
Me.Controls.Add(TempText)
arrtextbox(RowCount, ElementCount) = TempText
If (ElementCount + 1) Mod 9 = 0 Then
iLeft = 1
iTop = iTop + 4
Else
iLeft += 3
End If
Next
Next
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
Next
Next
End Sub
-
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.
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Post the code that initializes your random number
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
This what you're looking for?
VB Code:
Dim arrrandom(80) As Integer '
Dim iCount, icount1 As Integer
For iCount = 1 To 81 ' store numbers 1 to 81 in array
arrrandom(iCount - 1) = iCount
Next
Dim r As New Random
Dim arrTemp(80) As Integer ' create array to hold random numbers
' selected
Dim iRand As Integer
For iCount = 0 To arrrandom.GetUpperBound(0) 'get 81 random numbers
iRand = r.Next(81- iCount) 'select array index as random number
arrTemp(iCount) = arrrandom(iRand) ' get actual random number
' and store in order of
' selection
For icount1 = iRand To arrrandom.GetUpperBound(0) - 1
arrrandom(icount1) = arrrandom(icount1 + 1) ' remove selected
'index item and
'move all higher
'indexes down 1
Next
ReDim Preserve arrrandom(arrrandom.GetUpperBound(0) - 1)
'remove highest remaining indexno
Next
'arrTemp now holds all the numbers from 1 to 81 in selected random order
Dim RowCount, ElementCount As Integer
Dim arrtextbox(9, 9) As TextBox
Dim arrvalues(9, 9) As Integer
icount1 = 0
'Transfer random numbers to array
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrvalues(RowCount, ElementCount) = arrTemp(icount1)
icount1 += 1
Next
Next
Dim iHor, iVert As Integer
iHor = 1
iVert = 1
Dim TempText As TextBox
Dim iLeft As Integer = 1
Dim iTop As Integer = 1
'create 81 textboxes in 9 * 9 block
For RowCount = 0 To 8
For ElementCount = 0 To 8
TempText = New TextBox
TempText.Width = 20
TempText.Visible = True
TempText.Location = New Point(iLeft * 10, iTop * 5)
TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
Me.Controls.Add(TempText)
arrtextbox(RowCount, ElementCount) = TempText
If (ElementCount + 1) Mod 9 = 0 Then
iLeft = 1
iTop = iTop + 4
Else
iLeft += 3
End If
Next
Next
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
Next
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.!!!!
-
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:
Dim arrtextbox(9, 9) As TextBox 'Form scope
Private Sub btnCreateTextBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim iHor, iVert As Integer
Dim RowCount, ElementCount As Integer
iHor = 1
iVert = 1
Dim TempText As TextBox
Dim iLeft As Integer = 1
Dim iTop As Integer = 1
For RowCount = 0 To 8
For ElementCount = 0 To 8
TempText = New TextBox
TempText.Width = 20
TempText.Visible = True
TempText.Location = New Point(iLeft * 10, iTop * 5)
TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
Me.Controls.Add(TempText)
arrtextbox(RowCount, ElementCount) = TempText
If (ElementCount + 1) Mod 9 = 0 Then
iLeft = 1
iTop = iTop + 4
Else
iLeft += 3
End If
Next
Next
End Sub
Private Sub btnCreateNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim arrrandom(80) As Integer
Dim iCount, icount1 As Integer
For iCount = 1 To 81
arrrandom(iCount - 1) = iCount
Next
Dim r As New Random
Dim arrTemp(80) As Integer
Dim iRand As Integer
For iCount = 0 To arrrandom.GetUpperBound(0)
iRand = r.Next(81 - iCount)
arrTemp(iCount) = arrrandom(iRand)
For icount1 = iRand To arrrandom.GetUpperBound(0) - 1
arrrandom(icount1) = arrrandom(icount1 + 1)
Next
ReDim Preserve arrrandom(arrrandom.GetUpperBound(0) - 1)
Next
Dim RowCount, ElementCount As Integer
Dim arrvalues(9, 9) As Integer
icount1 = 0
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrvalues(RowCount, ElementCount) = arrTemp(icount1)
icount1 += 1
Next
Next
For RowCount = 0 To 8
For ElementCount = 0 To 8
arrtextbox(RowCount, ElementCount).Text = arrvalues(RowCount, ElementCount).ToString
Next
Next
End Sub
First click on btnCreateTextboxes and then every time you click btnCreateNumbers you will get a new set of random numbers
-
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:
Do
Do
Do
Do
GridVar(V1, H1) = Int(Rnd() * 10)
Loop Until GridVar(V1, H1) > 0
Test(0) = TestHorizontal(V1, H1)
Test(1) = TestVertical(V1, H1)
Test(2) = TestSquare(V1, H1)
Loop Until Test(0) = True
Loop Until Test(1) = True
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:
Private Function TestHorizontal(ByVal V1 As Integer, ByVal H1 As Integer)
Dim V2, H2, Count As Integer
Dim Test(8) As Boolean
V2 = V1
Test(H1 - 1) = True
For H2 = 1 To 9
If H2 <> H1 Then
If GridVar(V2, H2) = GridVar(V1, H1) Then
Test(H2 - 1) = False
Else
Test(H2 - 1) = True
End If
End If
Next
Count = -1
Do
Count += 1
If Count = 9 Then
Exit Do
End If
Loop Until Test(Count) = False
If Count = 9 Then
Return True
Else
Return False
End If
End Function
Private Function TestVertical(ByVal V1 As Integer, ByVal H1 As Integer)
Dim V2, H2, Count As Integer
Dim Test(8) As Boolean
H2 = H1
Test(V1 - 1) = True
For V2 = 1 To 9
If V2 <> V1 Then
If GridVar(V2, H2) = GridVar(V1, H1) Then
Test(V2 - 1) = False
Else
Test(V2 - 1) = True
End If
End If
Next
Count = -1
Do
Count += 1
If Count = 9 Then
Exit Do
End If
Loop Until Test(Count) = False
If Count = 9 Then
Return True
Else
Return False
End If
End Function
Private Function TestSquare(ByVal V1 As Integer, ByVal H1 As Integer)
Dim V2, H2, V3, H3 As Integer
Dim Count(4) As Integer
Dim Test(8) As Boolean
For V3 = 1 To 7 Step 3
For H3 = 1 To 7 Step 3
If V3 <= V1 < (V3 + 2) Then
If H3 <= H1 < (H3 + 2) Then
Count(0) = V3
Count(1) = V3 + 2
Count(2) = H3
Count(3) = H3 + 2
Count(4) = 0
For H2 = Count(2) To Count(3)
For V2 = Count(0) To Count(1)
If V2 <> V1 Then
If H2 <> H1 Then
If GridVar(V2, H2) = GridVar(V1, H1) Then
Test(Count(4)) = False
Else
Test(Count(4)) = True
End If
Count(4) += 1
End If
End If
Next
Next
End If
End If
Next
Next
Count(4) = -1
Do
Count(4) += 1
If Count(4) = 9 Then
Exit Do
End If
Loop Until Test(Count(4)) = False
If Count(4) = 9 Then
Return True
Else
Return False
End If
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.
-
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.
-
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.
-
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.
-
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?
-
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??? :bigyello:
I'll code what I am thinking of. Give me an hour.
-
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.
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi
Try this.
VB Code:
Dim arrtextbox1(9, 9) As TextBox 'Form scope
Private Sub btnCreateTextBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim iHor, iVert As Integer
Dim RowCount, ElementCount As Integer
iHor = 1
iVert = 1
Dim TempText As TextBox
Dim iLeft As Integer = 1
Dim iTop As Integer = 1
For RowCount = 0 To 8
For ElementCount = 0 To 8
TempText = New TextBox
TempText.Width = 20
TempText.Visible = True
TempText.Location = New Point(iLeft * 10, iTop * 5)
TempText.Name = "txt" & ((RowCount + 1) * (ElementCount + 1)).ToString
Me.Controls.Add(TempText)
arrtextbox1(RowCount, ElementCount) = TempText
If (ElementCount + 1) Mod 9 = 0 Then
iLeft = 1
iTop = iTop + 4
Else
iLeft += 3
End If
Next
Next
End Sub
Private Sub btnFillBoxes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
Dim arrvalues(9, 9) As Integer
Dim arrTemp(81) As Integer
Dim r As New Random
Dim iStartCorner, iFirstNumber, iCount, iCount1, iRowCount, iElementCount As Integer
Dim arrSquare(81) As Integer
' Select starting corner for sequence
iStartCorner = r.Next(4)
' Select number to be placed in starting square
iFirstNumber = r.Next(1, 9)
For iCount = 0 To 8
For iCount1 = 0 To 8
'Limit number to range 1 to 9
If iFirstNumber = 10 Then iFirstNumber = 1
'Store number sequence in array
arrTemp(iCount * 9 + iCount1) = iFirstNumber
iFirstNumber += 1
Next
'sets first number of next row
Select Case iCount
Case 2
iFirstNumber = arrTemp(1)
Case 5
iFirstNumber = arrTemp(28)
Case Else
iFirstNumber = arrTemp(iCount * 9 + 3)
End Select
Next
iCount = 0
For iRowCount = 0 To 8
For iElementCount = 0 To 8
arrtextbox1(iRowCount, iElementCount).Text = arrTemp(iCount).ToString
iCount = iCount + 1
Next
Next
End Sub
You owe me a bottle of aspirin :eek:
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.
-
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
-
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 :eek: It wasn't infinite but it will take longer than your lifetime to complete it :bigyello:
-
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:
For V1 = 0 to 8
For H1 = 0 to 8
If H1 = 8 Then 'This is done to easily calculate the final number
For H2 = 0 To 7
arrGridVar(V1, H1) += arrGridVar(V1, H2)
Next
arrGridVar(V1, H1) = 45 - arrGridVar(V1, H1)
Else
Do 'This is the standard testing
arrGridVar(V1, H1) = Random.Next(1, 10)
arrTest(0) = TestHorizontal(V1, H1) 'These are the tests for both Horizontal and Vertical
arrTest(1) = TestVertical(V1, H1) 'which are basically loops that check whether there are any numbers in the previous row/column
If arrTest(0) = True Then
If arrTest(1) = True Then
arrTest(3) = True
End If
End If
Loop Until arrTest(3) = True
End If
Next
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.
-
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. :wave:
VB Code:
Private Sub btn Compilegrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Dim iCount, iCount1, iNewNumber, iNewRandom As Integer
For iCount = 0 To 8
For iCount1 = 0 To 8
arrtextbox1(iCount, iCount1).Text = ""
arrNumbers(iCount, iCount1) = 0
Next
Next
'select valid range of numbers for next space
For iCount = 0 To 8
For iCount1 = 0 To 8
SelectValidRange(iCount, iCount1)
If arrTempRow.GetUpperBound(0) < 0 Then Exit For
iNewRandom = r.Next(0, arrTempRow.GetUpperBound(0))
iNewNumber = arrTempRow(iNewRandom)
arrNumbers(iCount, iCount1) = iNewNumber
arrtextbox1(iCount, iCount1).Text = iNewNumber.ToString
Next
Next
End Sub
Private Sub SelectValidRange(ByVal iRow As Integer, ByVal iCol As Integer)
Dim icount, icount1, icount2 As Integer
ReDim arrTempRow(8)
For icount = 0 To 8
arrTempRow(icount) = icount + 1
Next
'Check the current row
For icount = 0 To 8
For icount1 = 0 To arrTempRow.GetUpperBound(0)
If icount1 > arrTempRow.GetUpperBound(0) Then Exit For
If arrNumbers(iRow, icount) = arrTempRow(icount1) Then
For icount2 = icount1 To arrTempRow.GetUpperBound(0) - 1
arrTempRow(icount2) = arrTempRow(icount2 + 1)
Next
ReDim Preserve arrTempRow(arrTempRow.GetUpperBound(0) - 1)
End If
Next
Next
For icount = 0 To 8
For icount1 = 0 To arrTempRow.GetUpperBound(0)
If icount1 > arrTempRow.GetUpperBound(0) Then Exit For
If arrNumbers(icount, iCol) = arrTempRow(icount1) Then
For icount2 = icount1 To arrTempRow.GetUpperBound(0) - 1
arrTempRow(icount2) = arrTempRow(icount2 + 1)
Next
ReDim Preserve arrTempRow(icount2 - 1)
End If
Next
Next
End Sub
-
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
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
It's certainly a thought. I'll be thinking about it.
-
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!!!
-
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. :afrog: 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:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'I cut this out to make it readable
#End Region
Dim array(4, 4) As TextBox
Dim data(4, 4) As Int32 'data to be displayed in text boxes
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetControlArray() 'define textbox array
LoadUpDataArray() 'put data in data array to be displayed later
DisplayOutputs() 'now display the data as required
End Sub
Sub LoadUpDataArray() 'this is just to show operation, you could pass this in as a property from another form.
Dim i As Int16
Dim j As Int16
Dim k As Int32 = 0
For i = 1 To 4
For j = 1 To 4
k = k + 1
data(i, j) = k
Next
Next
End Sub
Sub DisplayOutputs()
Dim i As Int16
Dim j As Int16
For i = 1 To 4
For j = 1 To 4
array(i, j).Text = data(i, j).ToString
Next
Next
End Sub
Sub SetControlArray() 'define text box array
array(1, 1) = TextBox1
array(1, 2) = TextBox2
array(1, 3) = TextBox3
array(1, 4) = TextBox4
array(2, 1) = TextBox5
array(2, 2) = TextBox6
array(2, 3) = TextBox7
array(2, 4) = TextBox8
array(3, 1) = TextBox9
array(3, 2) = TextBox10
array(3, 3) = TextBox11
array(3, 4) = TextBox12
array(4, 1) = TextBox13
array(4, 2) = TextBox14
array(4, 3) = TextBox15
array(4, 4) = TextBox16
End Sub
End Class
-
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 :bigyello:
AdmiraljonB wants to complete a legitimate Sudoku grid using random numbers. Read the thread and you will see the problem.
-
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. :( :confused: :sick: :mad:
zaza
-
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
-
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 :wave:
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 :wave:
-
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.
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Grrrrrrrrrrrrrrrrrrrrrrr. :mad: 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
-
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 :bigyello:
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 :wave: The pattern is obvious.
BTW HOW much did you bet???????? :wave:
Another BTW. Are you sure you've spelt your handle correctly. Shouldn't it be gaga ??? Or is that term local to England? :bigyello:
Hi admiraljonb,
I left my programme running for over 2 hours and it generated 79 legitimate numbers!!
-
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
-
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.
-
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
-
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 :bigyello:
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 :bigyello:
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi AdmiralJonB,
Have you given up on this one?
I've rejigged my programme and it now makes 1,000,000 attempts in 28 minutes. That produced 9 unique grids containing 79 legitimate numbers. In each of those 9, the missing numbers were the same; 8 and 9. Also, the vacant slots were always the same, slot 9 on row1 and slot 6 on row 3.
Row 1 needed a 9 but the only vacant slot in that row was slot 9 and column 9 already had a 9 in it. Column 9 was missing an 8.
Row 3 needed an 8 in slot 6 but column 6 already had an 8. Column 6 was missin a 9.
It looks like there is a pattern to the vacant slots, which leads me to think that these puzzles can only, for all practical purposes, be generated using a predetermined pattern.
If you want to generate a grid which looks as if it is random then you could use my first posted code ( you could randomly generate the first line if you wished); carry on with the pattern and then do something like the following to the completed grid:
Replace all 3's with 0's
Replace all 7's with 3's
Replace all 2's with 7's
etc
Replace all ?'s with
replace all 0's with ?'s
I would assume that the random number system would eventually produce a legal grid, but how long it will take is anybody's guess.
-
1 Attachment(s)
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Have any of you made any headway?
I started working on this last weekend, and this weekend I've achieved a 75% Success rate, building 3 good SODUKO setups for every Invalid setup.
Instead of Randomly assigning a number to a SQUARES cell {I've been calling every 3x3 set of cells in the SODUKOS overall 9x9 cell grid a SQUARE. I'm referencing each SQUARE 2 diminsionally, {0-2, 0-2}, and each SQUARE has the numbers 0-8 entries {Or, You could callthem 1 thru 9}, which can be in that SQUARES Cell_ROW 0 - 2, and CELL_COL 0-2}, I've been Randomly creating a CELL_ROW setup pattern for Each SQUARE number entry, on a SQUARE ROW By SQUARE ROW basis, then once thats been done, I've been randomly assigning each numbers CELL COLUMN location, on a SQUARE COLUMN by SQUARE Column basis.
Subject, of course, to a verification system.
Can't explain how I'm going about it much more than that, at least at this time. {8 PM, Sunday Night} But I'll be back sometime tomorrow. {Gotta Clean up my development project, get rid of the flotsom}
-Lou
BTW, What do you think of this setup sheet I Progied? {This sheet has a specific ROW assignment already applied} Each SQUARE contains 9 Cells. The Cells Number is in the upper left, its CELL ROW assignment is to the right of that, the Middle row of each cell contains the numbers 0 to 2, representing that the number has available for assignment the column 0 thru 2, and the bottom row in that cell is a space for the column that you eventually would assign it to.
Its a scratch sheet to help me visuallize what must be done once a number has been assigned a column.
SDequentially, for each SQUARE COLUMN:
Step 1: Find the first ROW in this Column that has unassigned cells. Assign an available column to an unassigned cell.
Step 2: Cross off that numbers other Possible Columns from the Middle Row.
Step 3: For each other SQUARE in that column, Cross off the chosen column from its corresponding numbers possible choices.
Step 4: For each Number in the Same SQUARE as the number thats just been assigned, Any Number that has the same ROW assignment must have the chosen column crossed off.
Step 5: For each SQUARE in this column, Inspect each set of 3 cells which have a particular ROW assignment. If Any 2 of those cells have 2 identical possible column choices, then the third cell with that same row assignment MUST BE the un_common column number. {cross off those two possible columns in the 3rd cells ROW of possible coilumns, leaving {hopefully} 1 column it MUST_BE}
Step 6: Once Steps 2 thru 5 have eliminated the possibles, any unassigned cell that has only 1 available choice left is a MUST_BE. Selecting One of these, Place its MUST_BE column value in that cells BOTTOM area, and repeat steps 2 thru 6, until you don't have any MUST_BE's left.
Then Go back and do STEP 1.
:wave:
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
HI,
I'm completely lost with that :eek: I assume you are saying you are looking for legitimate patterns. The problem posted in this thread is producing a legitimate puzzle using random number generation, which I think for all practical purposes is impossible.
There are probably several different patterns which will produce legitimate grids, which appears to be what you are working on, but I can't see it. Mind you it is 01:45 here!!!
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
The problem posted in this thread is producing a legitimate puzzle using random number generation
I don't believe there's a difference.
Lets say you Generate the number "3" In the 3x3 SQUARE "A,B", in the SQUARES Cell "X, Y".
We Know 3 is going to be in that SQUARE anyways. What is Random is its cell position.
I'm generating the position randomly. The Progie is saying, "Ok. I've got this three, Where will it Go?"
Of Course you can't Put a number on top of another number, so the Position Generation has a Verification system, ONLY draw from available positions.]
Now, if a number has only 1 spot it can be put, well, you must put it in that spot.
:wave:
-Lou
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
Yes, that is random within the 3 * 3 block. Are you saying that by continuing like that you have produced a complete, legitimate Sudoku 9 * 9 grid 75% of the time?
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
I think that with this random number method, there is inevitably trouble. Let's say you go SQUARE by SQUARE, starting with the one in the top left. In the first cell, there are 9 possible numbers available, so your fraction of available numbers is 9/9, i.e. 1. The next cell has 8/9, then 7/9 etc. Hence you can always complete the first SQUARE, subject to the rule that 1-9 occur only once in that SQUARE.
Moving on to the next square (horizontally), in the first cell you now have 6/9 choices. By the time you've finished the 6th cell in this SQUARE, you've potentially used up all your choices, but of course you may not have depending on the numbers you've put in the above rows.
In the 3rd SQUARE, you start the first cell with 3/9 choices, and so can always complete the first row. But you may have 0/9 choices to fill in the 4th cell.
If you then go to the second row of SQUARES, you start the first cell with 6/9 choices. Filling in this SQUARE is like filling in the middle SQUARE in the top row. The second SQUARE is like filling in the 3rd SQUARE in the top row. The 3rd SQUARE in the middle row is even more difficult.
As for the final 3 SQUARES, you can complete the first 3 cells of the first SQUARE for certain, but that's about it. There are no guarantees thereafter.
Hence in each row of SQUARES, the number of potentially unfillable cells increases substantially.
A way around this would be to restrict the mini-rows of 3 cells within a SQUARE to hold particular combinations, i.e. if the first row of the first SQUARE contains 1,2,3, then make sure that 1,2,3 are in the same mini-row in the second and third SQUARES. That way you can't get cross-contamination of the rows, which is how the difficulties arise. Rearrangement of these from one row of SQUARES to the next would enable a complete grid to be formed. But then, essentially, you'd have a permutation of that original design that taxes described.
I find it difficult to believe that with a random number assignment and no pattern inbuilt that 75% of attempts generate a valid solution. But I'd certainly be interested to see it.
I suspect that whichever way it is tried, be it filling in rows, columns, SQUARES or number by number, it'll end up in this state. It may be possible to take corrective action as soon as a problem is spotted, e.g. even before the first row of SQUARES is completed, but by the lower portion of the grid there'll almost inevitably be trouble, unless somebody has found a cunning way around it.
zaza
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by taxes
Hi,
Yes, that is random within the 3 * 3 block. Are you saying that by continuing like that you have produced a complete, legitimate Sudoku 9 * 9 grid 75% of the time?
Yes.
Like I said, first I select the row assignments for the cells in the 3x3 SQUARES across 1 row of squares.
Consider a generic Row of 3 3x3 SQUARES.
The Number 1 will be assigned Row A in the first 3x3 SQUARE.
In the 2nd and 3rd, {ie... this Row N of 3 3x3 SQUARES has 3 SQUARE Columns, SQ_COL 0, 1 and 2, so each SQUARE has the coordinates {0,N}, {1,N} and {2,N}. Therefore the "First" 3x3 Square is that wich is in SQ_COL 0, and the 2nd and 3rd are those in this SQ_ROW=N, whose SQ_COL = 1 and 2}
Let me start again.
The Number 1 will be assigned CELL Row A in the first 3x3 SQUARE.
In the 2nd and 3rd, it must be one of the other 2 CELL Rows, B, or C.
Assigning CELL ROW b to the number 1 in the 2nd SQUARE forces the number 1 to be in CELL ROW c in the 3rd SQUARE.
Now, {a, b, c} is 1 of the 6 combinations that the set{0,1,2} can attain.
Lets list these sets:
C1 = {0,1,2}
C2 = {0,2,1}
C3 = {1,0,2}
C4 = {1,2,0}
C5 = {2,0,1}
C6 = {2,1,0}
The first number in one of those combinations is the CELL ROW assigned to a number in SQUARE 0 of a SQUARE ROW.
The Second is the Selected CELL ROW assigned to the same number in SQUARE 1 of a SQUARE ROW.
Which makes the 3rd one to be the Selected CELL ROW assigned to the same number in SQUARE 2 of a SQUARE ROW.
Now, in Square 0 of Row N, there can be only 3 numbers that can be assigned to the squares CELL ROW 0.
Similarly, 3 Numbers must be assigned CELL ROW 1 of SQUARE 0
and 3 numbers must be assigned CELL ROW 2 of SQUARE 0
Ditto for SQUARES {1, N} and {2, N} {Or, SQUARES 1 And 2 of SQUARE ROW N}
Since any Number must always be in a CELL ROW Once and only Once across ANY SQUARE ROW, then:
Since there are 9 numbers we are assigning CELL ROWS to, and the Assignments is represented by the combinations C1 thru C6, we see that if we draw D1 of C1, and D2 of C2, then D1+D2 = 3, {Again, because only 3 Numbers can be assigned CELL ROW 0 in SQUARE 0, and whatever CELL ROW those end up in in SQUARES 1 and 2, they MUST Form C1 OR C2, since C1 and C2 are the only C's that start with 0.
Similarly, if we say C3 and C4 are drawn D3 and D4 times, then D3+D4 = 3
Also, ... D5 + D6 = 3.
In SQUARE 1, 3 CELL ROW 0's must be assigned, 3 1's, and 3 2's.
So, since those assignments are the second number in the sets C1 thru C6, we see that
0 is assigigned D3 and D5 times, 1 is assigned D1 and D6 times, and 2 is assigned D2 and D4 times. resulting in the following three formulas:
D3+D5=3
D1+D6=3
D2+D4=3
Similarly, considering the third position, the CELL ROW assignements of the numbers in SQUARE 2:
D4+D6=3
D2+D5=3
D1+D3=0
Lets consolidate those formulas:
D1+D2=3
D3+D4=3
D5+D6=3
D3+D5=3
D1+D6=3
D2+D4=3
D4+D6=3
D2+D5=3
D1+D3=3
Lets set as many of these D's in terms of D1:
Code:
D1+D2=3 => D2=3-D1
D3+D4=3
D5+D6=3
D3+D5=3
D1+D6=3 => D6=3-D1
D2+D4=3
D4+D6=3
D2+D5=3
D1+D3=3 => D3=3-D1
Now, lets substitute:
Code:
D3+D4=3 => 3-D1+D4=3 => D4=D1
D5+D6=3 => D5+3-D1=3 => D5=D1
D3+D5=3 => 3-D1+D5=3 => D5=D1
D2+D4=3 => 3-D1+D4=3 => D4=D1
D4+D6=3 => d4+3-d1=3 => D4=D1
D2+D5=3 => 3-D1+D5=3 => D5=D1
So, in summary:
If we draw C1 D1 times, we automatically know how many the rest of the C's were drawn, from:
D2=3-D1
D3=3-D1
D4=D1
D5=D1
D6=3-D1.
We can therefore Say, Assigning to D1 a Randome Number between 0 to 3 will produce all the possible assignements that the generic numbers n1 thru n9 will be assigned, across the three SQUARES in SQUARE ROW N.
Lets say we randomly assign 1 to D1.
Therefore
C1 will be used 1 time
C2 will be used 2 times
C3 will be used 2 times
C4 will be used once.
C5 will be used once
and C6 will be used 2 times.
So, we have 9 C's that can be assigned to the numbers 1 thru 9.
We can assign them Randomly.
How about, for example:
2=C1
4=C2
1=C2
7=C3
6=C3
5=C4
9=C5
8=C6
3=C6
Therefore, in Square 0, we see that in its CELL ROW 0 exists the numbers 1,2, and 4
In Its CELL ROW 1 exists the numbers 5,6, and 7, and in its CELL ROW 2, 3,8,9.
Well, lets just say that:
Code:
CellRow SQUARE_0 SQUARE_1 SQUARE_2
0 1,2,4 6,7,9 3,5,8
1 5,6,7 2,3,8 1,4,9
2 3,8,9 1,4,5 2,6,7
Now, Doing this with the other 2 SQUARE ROWS will result in nonconflicting RANDOM row assignements for ALL Numbers in ALL the Squares.
Any Questions?
:)
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
Nicely done so far. I'm guessing that having created all the rows, you then jiggle your choice of numbers so that you get the columns to match as well.
Actually, you didn't need to go into as much detail as that, because effectively what you've done is to pick a pair of numbers per row and keep these pairs together. There are only two ways to arrange these pairs:
First pair: SQ1 R1, SQ2 R2, SQ3 R3
or
First pair: SQ1 R1, SQ2 R3, SQ3 R2
The other pairs then have no choice as to which rows they're in to satisfy the conditions. In your example, the 3 pairs are (1,4), (6,7), (3,8), using the second pattern. So basically, you just have to pick 1 from 2, and the pattern is decided. The sorting out of columns is then a bit of jiggling, and of course you can assign the numbers as you choose.
Indeed, if you think about it in those terms, you can see that this is the only way it can be done:
Consider the first number in the first row of the first SQUARE. In the second SQUARE, it must go in the second or third row. Similarly for the second number in the first row of the first SQUARE.
The first and second numbers are in the first row of the first SQUARE by definition. If they are also both in the second row of the second SQUARE, then they must also both be in the third row of the third SQUARE by elimination, hence we have achieved pattern 1. Likewise if they are both in the third row of the second SQUARE then they must both be in the second row of the third SQUARE and we have achieved pattern 2.
If they are not both in the same row of the second SQUARE, then we consider the third number in the first row of the first SQUARE. This number in the second SQUARE must be in the same row as either the first or the second numbers, because there are only 2 rows left and there is one of them in each. Hence we have a pair in 2 out of the 3 SQUARES and hence a pair in the third as well.
So, the pairing of numbers is the only possible way for sudoku to be generated. Thus, subject to the successful jiggling of columns, this is indeed the foundation for creating sudoku by random numbers.
Nice one. :thumb:
zaza
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
That is not producing the grid from random number generation. It is simply using a predetermined pattern, exactly as I set out in my earlier post, and as cited by Zaza on 06/04/2005 at 12.05 am. You may be randomly selecting which actual numbers to use in the first determined sequence, but from then on you are bound to a set pattern, although, as I previously posted, you can swap entire rows or entire columns, provided both the rows or columns swapped run through the same miniblocks of 3*3. You can also swap the numbers around, provided you swap ALL of the selected numbers around. You can, by this method, make the grid seem random, but you could never produce it by genuine, consistent random number generation.
Using a fully random number generation and discarding illegal numbers as they arise seems to have a maximum limit of 79 legal number selections. The missing slots seem to be the same in every case, i.e Row 2, column 9 and row 3 column 6. One of the missing numbers always seems to be a 9 and the other 6, 7 or 8. In half of the grids where 79 numbers have been produced, it is quickly possible to swap 2 or 3 numbers around to produce a legitimate grid, just by looking at them, but I can't generate the code to do this i.e. it is a visual solution rather than a logical one. In the other half it seems to be impossible to complete.
Post a grid generated by your system and I will see if I can tell you the pattern.
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Heh.
Well, I guess we must disagree.
You Do agree that, if a number is in position 0 in the first square, then it must be in position 1 or 2 in the 2nd square, and in position 2 or 1 in the third?
Likewise, 1, {0,2} or {2,0}, or 2 {0,1} or {1,0}?
Now, you can randomly say that a number in the first square IS in position 0, 1, or 2. However, isn't it equally as random if you say K Numbers in Square 0 will be placed, not only at Cell Row 0, but k times throughout the whole row {0,1,2} and L times whroughout the whole SQUARE Row as {0, 2, 1}.
AND, once you've defined what K and L are, ALL the other sequences MUST be as I mathematically proven?
Its NOT creating a pattern. Its Selecting THE ONLY Viable pattern the possible rows could be from a random draw, and then assigning those draws randomly to the existing numbers to be placed?
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by taxes
Hi,
You may be randomly selecting which actual numbers to use in the first determined sequence, but from then on you are bound to a set pattern, although, as I previously posted, you can swap entire rows or entire columns, .
Point #1) That post had nothing to do with assigning columns. Assigning colomns is much more complex {Unless you do that first, then assign the rows. no differance.}
Point #2) Each row of SQUARES is independant from each other when it comes to assigning CELL ROWS! What does SQUARE ROW 1 care about how Its numbers were assigned to cell rows in the SQUARE ROW above it? THEY're INDEPENDENT. No Influence if the number 1 in the above SQUARE is assigned row 0,1, or 2! Row Choices don't influence ther SQUARE ROWS Above or below each other, when you hav'nt chosen CELL Columns yet.
So, You Randomly assign the rows per my method 3 times, per each of the three SQUARE ROWS that exist.
No "Shifting, flipping,or swapping" whatsoever!
:)
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
HI Guys,
I suggest we agree to differ on this one. We obviously have a different understanding of the meaning of "Random". Whilst I regard limiting a choice to certain numbers as being random within that range, I do not regard pre-selecting the actual numbers as being random. You obviously differ and are entitled to your view.
Even my view of "Random" would be challenged by most academics who would protest that once you limit the next choice you have departed from the selection being Random.
EDIT: I do agree that you have produced a method of creating legitimate grids and that it is probably the closest you can get to a truly random number generation, to do so. My Pattern suggestion is less Random than yours but will produce a legitimate grid every time.
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Good Morning.
:wave:
A Number will always be in Rows 0, 1, and 2 in ALL three SQUARE ROWS.
So, That isn't capable of being random. As to which SQUARE in a ROW will it be in row 0, 1, or 2, well, that IS handled randomly, via which combination is randomly applied to it.
You can only have only 4 sets of unique combinations, and which of the four is being determined randomly.
Applying the drawn combinations to the numbers is being done randomly.
At what point is the Number:CellRow assignment using predetermined numbers?
BTW, You suggested that I attach some examples of my successes, to see if you can see the pattern. I'll attach them in a few hours back at work.
:)
-Lou
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by zaza
Hi,
Nicely done so far. I'm guessing that having created all the rows, you then jiggle your choice of numbers so that you get the columns to match as well.
....Nice one. :thumb:
zaza
Sorry, didn't see you up there above Taxes.
Thanks for the :thumb:
If you go back up there to where I posted my PDF, Thats where I explained basically my technique used in my progie for how I chose the column assignments, although presented in terms of how I used the scratch sheet in developing the column choosing technique.
I feel like there might be a more "Combination of 3" Method for assigning the columns, so next weekend, {and the upcoming days} I'll be looking at that.
:wave:
-Lou
-
1 Attachment(s)
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by taxes
Post a grid generated by your system and I will see if I can tell you the pattern.
Here's a ZIPPED PDF with 756 Successful generations after 1000 attempts.
:wave:
-Lou
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
I think that the difference of opinion here is slightly misplaced. As I argued in my earlier post, any legitimate grid has to pick numbers pairwise for each of the three rows as you go across the SQUARES. In any legitimate grid, once generated, you can swap rows or columns within a SQUARE and still have a legitimate grid. This is obvious, because if you swap columns you are keeping the pairs in their rows, and similarly if you swap rows you are keeping the pairs in their columns.
However, I agree with taxes that this is not generating a grid RANDOMLY. There is some structure behind the method which helps to generate the pattern. I suggested another method earlier insofar as building the grid by moves so that it makes it easier to remove squares. My method would be more strugglesome to get a grid, but I bet it would be easier to remove squares and leave a solvable puzzle. This, however, is not the point.
The point is to generate it simply by putting random numbers in the cells, subject to the 3 rules, rather than working out the correlation between one cell and the next. Thus what we are effectively doing is reducing the probability of generating a successful grid by doing it randomly, rather than determining the positions of the numbers in the next SQUARE according to where they are in the first SQUARE.
In fact, given the method that NotLKH or SomethingElse (whatever your name is :ehh: ) has proposed, I reckon we can work out the probability of doing it randomly. Given that the pair method MUST be present if the grid is successful, once we've chosen the numbers in the first 3 cells of the first SQUARE, there are a limited number of possibilities as to where the numbers can go in the next SQUARE. The probability of a successful generation by a random number method is the number of ways of allocating numbers pairwise in all these rows (and columns) divided by the total permutations.
Worknig that out is another matter, but it's a start.
The random number generation will diverge from the pairwise generation if there are a pair of numbers in SQ1R1 and SQ2R2, and also a pair of numbers in SQ1R2 and SQ2R1. This means that there must be a duplicate in the third row. Even once this is past for the first row of SQUARES, there arises the problem that you are not deliberately generating pairs in the columns of SQUARES, so unless in generating the first SQUARE in the second row you also assign pairs columnwise to match up with the first SQUARE in the first row, you will cause an error in the first SQUARE in the third row.
Incidentally, taxes is correct in that once you have generated the pattern, and decided what the numbers will be in the first SQUARE, the rest are determined because the correlations persist throughout the grid.
The point of the RANDOM nature of this is that it will only achieve these pairs by chance, rather than deliberately.
Does that make sense?
zaza
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by zaza
Incidentally, taxes is correct in that once you have generated the pattern, and decided what the numbers will be in the first SQUARE, the rest are determined because the correlations persist throughout the grid.
The ROW assignment Does Indeed determine the row assignments of the numbers across a row by determining the Row assignments of the numbers in the first SQUARE of the row, if you are using my method, which is determining the ROW assignments of a number across three rows at one Assignment of the 3 rows on the number.
BUT, having determined the row assignments of the numbers in the first SQUARE ROW does not determine the row assignments in the 2nd and 3rd SQUARE ROWS. Those each are independant of each other.
BTW, It sounds like, if you were given the task to draw 52 cards randomly, you wouldn't remove the card from the stack after you drew it? In other words, if your draw affects the source of that which you are drawing from, you wouldn't program it to alter your source? You'd still allow that card to be drawn?
Or, alternatively, if you had a "magic" deck, that started with 18 cards, but some cards would magically disappear based on the sequence that the existing cards were drawn, and you knew the rules how they disappeared, and your task was to draw 9 random cards, you wouldn't simulate the changing of the deck as you drew the cards in your progie?
BTW, I'm NotLKH at work, and usually something else only when I'm at home.
-Back to work!
:)
-Lou
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi Lou,
many thanks for your effort & input. There is a discernible pattern in the grids you posted but it was very difficult to spot. In all cases it is based on a pattern of 3 (not necessarily consecutive numbers numerically) and then swapping. In some cases the pattern of 3 is once removed and in others twice removed. When you first look at your grids they actually look as though they conform to a simple pattern, but then when you go into it they appear more and more random!!! Got any headache pills?
To summarise. I think you have produced an excellent method of creating these grids 'cos my pattern methods can be easily cracked. Well done
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Quote:
Originally Posted by taxes
Hi Lou,
many thanks for your effort & input. There is a discernible pattern in the grids you posted but it was very difficult to spot. In all cases it is based on a pattern of 3 (not necessarily consecutive numbers numerically) and then swapping. In some cases the pattern of 3 is once removed and in others twice removed. When you first look at your grids they actually look as though they conform to a simple pattern, but then when you go into it they appear more and more random!!! Got any headache pills?
To summarise. I think you have produced an excellent method of creating these grids 'cos my pattern methods can be easily cracked. Well done
Thanks!
I figured you'd find a "pattern".
I'm not sure what you mean by Once or Twice "Removed", but what would "Third" Removed look like?
Would you surmise this "pattern" is inherently a result of my programming, or is this pattern you discern something you'd be able to see in any valid SODUKO Layout?
:)
-Lou
[EDIT]
Just had a thought.
If I gave you a data file of SODUKO generations, could you build a progie that automatically determines the "pattern" of each?
-
Re: Looping to put numbers in a 9x9 grid of Text Boxes
Hi,
By first & second removed I mean that the sequence moves one step of 3 forward or two steps. Three steps rempved would, in the case of a Soduko grid, mean you repeat the exact numbers which, of course, would not be permissible.
In the restricted random generation code I developed (which never produced a legitimate grid :blush: ) I could not detect any pattern, but whilst my largest number of attempts in any one session was in excess of 1,400,000 I never checked more than 4 for evidence of a pattern and even then I was looking for a pattern in the sequence of unfilled slots. Unless the pattern is clearly visual, it takes a long time to work it through, because there are always possible alternative adjustments to any pattern (or any grid for that matter).
Using my pattern generation programme, it was. of course, relatively easy to spot the pattern, even if I gave the computer many possible patterns to choose from.
Solving Soduko puzzles does not interest me. I was originally intrigued to see if, as the first post requested, it would be practically possible to use totally random number generation to produce a grid, but soon came to the conclusion that it would, statistically, take a lifetime. My eventual solution is far less random than yours. I might, out of interest, try producing code along the lines you suggested, but I don't intende to spend any more time on this at the moment.
The originator of this thread seems to have disappeared completely :wave: