Hi, im new in vb and im trying to make a program to list certain numbers of players in a listbox, shuffle them into a bracket to manage the tourtament results, i will be doing all of this with the very little knowledge i have of programming but im sure i can make my way with this.
Ive been checking the forum for a couple of hours and ive found this
Code:
Sub Shuffle()
Dim order(1 To 52) As Integer
Dim colCardsLeft As New Collection
Dim c As Integer
Dim rndCard As Integer
' initialise collection of cards
' t0 make use of handly features of collections
For c = 1 To 8
colCardsLeft.Add Str(c)
Next c
' now shuffle by placing a card in each position
' in the deck. Using the collection's remove method
' to prevent any repeats
c = 1
Randomize Timer
While colCardsLeft.Count > 0
rndCard = Int(Rnd() * colCardsLeft.Count) + 1
order(c) = CInt(colCardsLeft(rndCard))
colCardsLeft.Remove (rndCard)
c = c + 1
Wend
' take a look at the shuffle
For c = 1 To 8
Debug.Print order(c); " ";
Next c
Debug.Print
End Sub
this works great to shuffle players into the brackets but i need to change some things and i have no clue how;
For c = 1 To 8 this i need to change it from textboxs or a list value to get all the names, for exmaple
text1.text = "Alex"
text2.text = "Marco"
text3.text = "Polo"
etc
i need the names in the textbox to be shuffled and the results
Debug.Print order(c); " ";
to be set in some labels, for example, the results from 1 to 8 taken from the textbox to be placed in 8 labels from 1 to 8
Label1.caption = First shuffle result
Label2.Caption = Second shuffle result
etc
if anyone can help me out with this i will apreciatte it alot!
thx in advance
1. Are you sure you are using Visual Basic 6.0? Maybe you have one of the newer versions of VB, which would be in the .Net variety.
2. IF you are using Visual Basic 6.0, get a book and read it. Use MSDN (you really need this help program if you are starting out in Visual Basic 6.0). Learn the basics.
3. If you are NOT using Visual Basic 6.0, so say, and we'll ask the Moderators to move it to the appropriate section of this Forum.
Sub Shuffle() ' #1
Dim colCards As Collection: Set colCards = New Collection ' #3
Dim tb as Variant ' TextBox
For Each tb in txtNames
Call colCards.Add( tb.Text, tb.Text ) ' #2
Next c
Randomize Timer
Dim c As Integer
Dim rndCard As Integer
c = 0
Do While colCards.Count > 0
rndCard = Int(Rnd() * colCardsLeft.Count) + 1
lblShuffledNames( c ).Caption = colCards(rndCard)
colCards.Remove colCards(rndCard)
c = c + 1
Loop
Dim lbl as Variant ' Label
For Each lbl in lblSuffledNames
Debug.Print lbl.Index, lbl.Caption
Next
Debug.Print
End Sub
1. I'm assuming the use of Control Arrays here.
Both the textboxes (txtNames) and labels (txtShuffledNames) are Control Arrays (i.e. they each have their Index property is set) and I'm assuming that these indexes run consecutively (from zero to however many you've got, less one).
2. Use Keys when working with Collections.
It's just so much easier to work with things by name, rather than worrying about indexes.
Not strictly an issue here, but one to watch out for ...
3. Never use Dim .. As New ... in VB6.
Yes, it makes perfect sense and works you think it should in Visual Basic [.Net] but, in VB "Proper", it has a nasty sting in the tail.
Code:
Dim objNeverNothing As New Collection
... has the nasty side-effect of "invisibly" prefixing any and all uses of that object with
Code:
If objNeverNothing Is Nothing Then Set objNeverNothing = New Collection
If you try to test that object for Nothing-ness (as Object-Oriented code often needs to), it will never, ever, be so!
Code:
If objNeverNothing Is Nothing Then Set objNeverNothing = New Collection ' Silently and invisibly added by the compiler!
If objNeverNothing Is Nothing Then
' You'll never, ever, get here!
End If
1. Are you sure you are using Visual Basic 6.0? Maybe you have one of the newer versions of VB, which would be in the .Net variety.
2. IF you are using Visual Basic 6.0, get a book and read it. Use MSDN (you really need this help program if you are starting out in Visual Basic 6.0). Learn the basics.
3. If you are NOT using Visual Basic 6.0, so say, and we'll ask the Moderators to move it to the appropriate section of this Forum.
Sam
Hi, yes this is vb 6.0 and the code is for vb 6 too
I already read a book thats why i have some knownledge for the basics.
Dont need to move it anywhere, look at the code, its vb 6 and it works in vb6 i actually have it in a form right now working, but its just not working the way i want it too.
Sub Shuffle() ' #1
Dim colCards As Collection: Set colCards = New Collection ' #3
Dim tb as Variant ' TextBox
For Each tb in txtNames
Call colCards.Add( tb.Text, tb.Text ) ' #2
Next c
Randomize Timer
Dim c As Integer
Dim rndCard As Integer
c = 0
Do While colCards.Count > 0
rndCard = Int(Rnd() * colCardsLeft.Count) + 1
lblShuffledNames( c ).Caption = colCards(rndCard)
colCards.Remove colCards(rndCard)
c = c + 1
Loop
Dim lbl as Variant ' Label
For Each lbl in lblSuffledNames
Debug.Print lbl.Index, lbl.Caption
Next
Debug.Print
End Sub
1. I'm assuming the use of Control Arrays here.
Both the textboxes (txtNames) and labels (txtShuffledNames) are Control Arrays (i.e. they each have their Index property is set) and I'm assuming that these indexes run consecutively (from zero to however many you've got, less one).
2. Use Keys when working with Collections.
It's just so much easier to work with things by name, rather than worrying about indexes.
Not strictly an issue here, but one to watch out for ...
3. Never use Dim .. As New ... in VB6.
Yes, it makes perfect sense and works you think it should in Visual Basic [.Net] but, in VB "Proper", it has a nasty sting in the tail.
Code:
Dim objNeverNothing As New Collection
... has the nasty side-effect of "invisibly" prefixing any and all uses of that object with
Code:
If objNeverNothing Is Nothing Then Set objNeverNothing = New Collection
If you try to test that object for Nothing-ness (as Object-Oriented code often needs to), it will never, ever, be so!
Code:
If objNeverNothing Is Nothing Then Set objNeverNothing = New Collection ' Silently and invisibly added by the compiler!
If objNeverNothing Is Nothing Then
' You'll never, ever, get here!
End If
Hi thank you very much for the response, this looks like the way i need it to work, but its not working for me i get an error when i call the function shuffle on this line of code
I cant get it to work, i removed the c and now i get the error on line
lblShuffledNames(c).Caption = colCards(rndCard
lblShuffledNames function not denifed
i tried some modification of my own but nope it doesnt run, it looks like it should do exactly what i want it too but i dont have the experience to make it work xD
I'm not going to try to re-code what Phill gave you. But, one thing that MIGHT be a problem is: Do you have your textboxes in an ARRAY? Or are they like 'text1', 'text2', 'text3', etc?
Not wanting to take time to do this for you, I will suggest one thing...if you want to have a list of names and want to randomly select them all and put in a bracket, you can randomize a listbox by doing this:
Code:
Private Sub randomizeListBox(formName As Form, listName As ListBox)
Dim i As Integer, j As Integer, colTemp As New Collection
For i = 0 To listName.ListCount - 1
colTemp.Add listName.List(i)
Next
listName.Clear
Do While colTemp.Count
Randomize
j = Int((colTemp.Count * Rnd) + 1)
listName.AddItem colTemp(j)
colTemp.Remove j
Loop
End Sub
Had a little time so put together an EXAMPLE of a Tournament Bracket (based ONLY on 8 players). If there were more or less, a lot of modifications would have to be done...but, just thought I throw this out there.
Notice I loaded EIGHT names from a textfile into a hidden listbox. Then I randomized (mixed up the players) and set them in the opening 8 bracket slots.
Had a little time so put together an EXAMPLE of a Tournament Bracket (based ONLY on 8 players). If there were more or less, a lot of modifications would have to be done...but, just thought I throw this out there.
Notice I loaded EIGHT names from a textfile into a hidden listbox. Then I randomized (mixed up the players) and set them in the opening 8 bracket slots.
To advance a player, you click on their name.
Wow, this is beatiful, thank you very much this is exactly what i wanted to do, its amazing how extremly hard for me to even understand the code and how easy for you to do what i exactly had in mind, its like you were inside my mind, i really appreaciate it thank you very much, i will put alot of time to study vb6 and make this app the best i can, but this you just did for me was an inmense boost. cant thank you enough
You are welcome...but remember, that little program was only for 8 players/teams. It can get pretty complicated if you want to do it for an 'n' number of players. I might try one someday.
You are welcome...but remember, that little program was only for 8 players/teams. It can get pretty complicated if you want to do it for an 'n' number of players. I might try one someday.
Yes thank you, im already working on it, im using the o 2^x y x>1 format, so it can only be 4, 8, 16, 32 etc, its theres 7 players instead of 8 it will fill with one void and one player gets a free pass to the next round, it would be really interesting to do with "n" amount of players u want but im sure i cant pull that off, but this way will work great for me, i don
Omg, ive been working for like 12 hours straigths adding alot of stuff from codes i found here in the forum i actually got it in a point when im really happy and i was thinking how to do it with more players and you just saved me again, sir theres gotta be something i can do for you, u are a life safer if you ever need something i can help you with please let me know, i thank you from the bottom of my hearth, this is how the final product is looking so far.
Ive have added some pretty interesting functions like write stuff and get stuff from an .ini files so i can save the tournament results and read from them if i want to load an old tournament, a log system to record the results of each tournament and ive added 3rd place round, i still need to add double elimination format and some other things, when im finished ill be updating the final source code here for any chance anyone is looking for something similar in the future ive gathered some really amazing codes from others developers in this forum and ill be giving them all credit when all is said and done.
this proves what an amazing programming language is vb6 even being so old but it has so many tools to help people build their ideas on codes and is so elegant simple and schistically complex that allows people with very little knowledge like me build a tool that i was so much in need of.
I want to thanks this community for existing and for users like SamOscarBrown for helping selflessly others in need of guidance.
Such a Tournament (Bracket) only works if the number of players is a Power of 2
have you thought about using a "reverse" TreeView (invisible)? The "root" being winner, first 2 child-nodes final, next level in (again 2 childnodes) semifinal and so on?
8 Players = 2 ^ 3 --> You need 3 "Matches" (Levels below root) --> that 3rd Match being the Final --> With the Winner progressing to "root"
16 Players = 2 ^ 4 --> You need 4 "Matches" (Levels below root) --> that 4th Match being the Final --> With the Winner progressing to "root"
and so on
btw: In Codebank is my Lottery-Algorithm with a ready-made call to get a full shuffle
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
So, sitting at Firestone to get my F150's frontend aligned, using my laptop I added a command button to the form so that if there were an odd number of players, one could delete from the 8, 12, or 16 list of names. It will replace that player's name with the word "BYE". Of course, a fake name could be added to the textfile, which could be "BYE" as well, hence no need for the command button. Simply click on the opponent in the BYE game.
Code:
Private Sub Command1_Click()
If List1.ListIndex > -1 Then
Dim i As Integer
For i = 0 To grid1.Rows - 1
If List1.Text = grid1.TextMatrix(i, 0) Then
grid1.TextMatrix(i, 0) = "BYE"
End If
Next i
List1.RemoveItem (List1.ListIndex)
End If
End Sub
So, sitting at Firestone to get my F150's frontend aligned, using my laptop I added a command button to the form so that if there were an odd number of players, one could delete from the 8, 12, or 16 list of names. It will replace that player's name with the word "BYE". Of course, a fake name could be added to the textfile, which could be "BYE" as well, hence no need for the command button. Simply click on the opponent in the BYE game.
Code:
Private Sub Command1_Click()
If List1.ListIndex > -1 Then
Dim i As Integer
For i = 0 To grid1.Rows - 1
If List1.Text = grid1.TextMatrix(i, 0) Then
grid1.TextMatrix(i, 0) = "BYE"
End If
Next i
List1.RemoveItem (List1.ListIndex)
End If
End Sub
this is really intersting because you get alot of times when theres no enough people for 12 or 16 so i was just adding EMPTY in the list of names to fill up the spots so some players get a free pass in that round, i tried that but it always add the BYE to the last grid in any case 8 12 or 16, and it does it even if u have the full list of players, or maybe im doing something wrong, but i think it should fill up the free slots in the listbox or you can hit that command after you randomize and populate the brackers to fill the empty spaces, i dont know if i explaned myself very well, im going to try to do it because its a really good idea so u save some time to fill up EMPTY names in the players list when you dont have enough players for the tournament
Just try it by using all 16, 12 or 8 in the textiles. But before you delete from that list, use the Randomize and load button. On mine here, I made that delete command button enabled =false, but set it to true on the click of the Randomize/load button