-
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: