Results 1 to 26 of 26

Thread: Tournament Bracket Help with shuffle

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Tournament Bracket Help with shuffle

    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

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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
    Sam I am (as well as Confused at times).

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,960

    Re: Tournament Bracket Help with shuffle

    @SamOscarBrown - I believe this is VB6. Rather, I believe it isn't VB.NET.

    The reason I say this is because in VB.NET you need the parenthesis in order to make a method call, i.e.
    Code:
    colCardsLeft.Add Str(c)
    ' becomes
    colCardsLeft.Add(Str(c))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    782

    Re: Tournament Bracket Help with shuffle

    How about something like this?

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

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    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.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    I know the code is not .Net. Just wanted to make sure OP was using VB6.
    Sam I am (as well as Confused at times).

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by Phill.W View Post
    How about something like this?

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

    " Next c" variable next not valid

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    oops

    leave off the 'c' ???
    Sam I am (as well as Confused at times).

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    alr ill try it out thank you

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    oops

    leave off the 'c' ???
    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

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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?
    Sam I am (as well as Confused at times).

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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
    Sam I am (as well as Confused at times).

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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.
    Attached Images Attached Images  
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  14. #14

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    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

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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.
    Sam I am (as well as Confused at times).

  16. #16

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    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

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    Just thinking out of the box....might be wise to use a FlexGrid instead of textboxes.
    Sam I am (as well as Confused at times).

  18. #18

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    Just thinking out of the box....might be wise to use a FlexGrid instead of textboxes.
    i will look into that thanks

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    Here is an example of a multi-player tournament bracket for 8, 12 or 16 players, using an MSFlexGrid to display it.

    Tounament Bracket Multiple.zipName:  Tournament Bracket.jpg
Views: 50
Size:  23.6 KB
    Sam I am (as well as Confused at times).

  20. #20

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    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.

    Attachment 192697

    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.

    Sorry for my english.

  21. #21
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    I took my little example and greatly reduced the code in the module, and made a small correction (not an error tho) in the group of 12.

    Take a look.

    Tounament Bracket Multiple.zip
    Attached Images Attached Images  
    Sam I am (as well as Confused at times).

  22. #22
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,676

    Re: Tournament Bracket Help with shuffle

    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

  23. #23

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    I took my little example and greatly reduced the code in the module, and made a small correction (not an error tho) in the group of 12.

    Take a look.

    Tounament Bracket Multiple.zip
    Ok thanks i will take a lookt at it right now

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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
    Sam I am (as well as Confused at times).

  25. #25

    Thread Starter
    New Member
    Join Date
    Sep 2024
    Posts
    11

    Re: Tournament Bracket Help with shuffle

    Quote Originally Posted by SamOscarBrown View Post
    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

  26. #26
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,419

    Re: Tournament Bracket Help with shuffle

    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
    Sam I am (as well as Confused at times).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width