Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Need Help with Shuffle of a deck

Need Help with Shuffle of a deck

Scheduled Pinned Locked Moved Visual Basic
help
40 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ymilan
    wrote on last edited by
    #1

    Hi, I'm shuffling a deck of cards, but seem to keep getting more than one of the same card, after the shuffle. So, I tried quite a bit trying to set the value of the card to not equal the subnum of the card, but it didn't work. Any ideas would be much appreciated. Thanks. Here's part of the code: Private Sub Start_Click() Dim arrDeck(52) Randomize CardCount = 1 Do While CardCount < 53 arrDeck(CardCount) = -1 CardCount = CardCount + 1 Loop CardCount = 1 Counter = 0 Do While CardCount < 53 And Counter < 53 3 CardVal = Int((52 * Rnd) + 1) If arrDeck(CardVal) = -1 Then arrDeck(CardVal) = CardCount Subnum = Counter Image1(Subnum) = Image1(CardVal) Image1(Subnum).Visible = True CardCount = CardCount + 1 Counter = Counter + 1 Else: GoTo 3 End If Loop End Sub

    D D R 4 Replies Last reply
    0
    • Y ymilan

      Hi, I'm shuffling a deck of cards, but seem to keep getting more than one of the same card, after the shuffle. So, I tried quite a bit trying to set the value of the card to not equal the subnum of the card, but it didn't work. Any ideas would be much appreciated. Thanks. Here's part of the code: Private Sub Start_Click() Dim arrDeck(52) Randomize CardCount = 1 Do While CardCount < 53 arrDeck(CardCount) = -1 CardCount = CardCount + 1 Loop CardCount = 1 Counter = 0 Do While CardCount < 53 And Counter < 53 3 CardVal = Int((52 * Rnd) + 1) If arrDeck(CardVal) = -1 Then arrDeck(CardVal) = CardCount Subnum = Counter Image1(Subnum) = Image1(CardVal) Image1(Subnum).Visible = True CardCount = CardCount + 1 Counter = Counter + 1 Else: GoTo 3 End If Loop End Sub

      D Offline
      D Offline
      Dalek Dave
      wrote on last edited by
      #2

      have you tried applying a number between 1 and 52 to each card, the as each number is picked randomly it is added to an array. on the next random call, it is checked to the contents of the array, and if found to have been picked already is rejected. It will slow doown as the cards are dealt, but then that array will be filled from 1 to 52 in random order, just apply the numbers back to the cards to give a random shuffle.

      ------------------------------------ "I am always serious about what I do, not necessarily about how I do it." Tom Baker

      Y 1 Reply Last reply
      0
      • Y ymilan

        Hi, I'm shuffling a deck of cards, but seem to keep getting more than one of the same card, after the shuffle. So, I tried quite a bit trying to set the value of the card to not equal the subnum of the card, but it didn't work. Any ideas would be much appreciated. Thanks. Here's part of the code: Private Sub Start_Click() Dim arrDeck(52) Randomize CardCount = 1 Do While CardCount < 53 arrDeck(CardCount) = -1 CardCount = CardCount + 1 Loop CardCount = 1 Counter = 0 Do While CardCount < 53 And Counter < 53 3 CardVal = Int((52 * Rnd) + 1) If arrDeck(CardVal) = -1 Then arrDeck(CardVal) = CardCount Subnum = Counter Image1(Subnum) = Image1(CardVal) Image1(Subnum).Visible = True CardCount = CardCount + 1 Counter = Counter + 1 Else: GoTo 3 End If Loop End Sub

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        You can add your cards to a collection and then pull the cards from random positions out of that collection and add them to another collection.

        Private Function GetShuffledDeck() as List(Of Integer)
        Dim cards As New List(Of Integer)
        For i As Integer = 0 To 51
        cards.Add(i)
        Next

            Dim RNG As New Random()
        
            Dim deck As New List(Of Integer)
            While cards.Count > 0
                Dim cardNum As Integer
                cardNum = RNG.Next(0, cards.Count)
                deck.Add(cards(cardNum))
                cards.RemoveAt(cardNum)
            End While
        
            Return deck
        

        End Function

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        Y 1 Reply Last reply
        0
        • Y ymilan

          Hi, I'm shuffling a deck of cards, but seem to keep getting more than one of the same card, after the shuffle. So, I tried quite a bit trying to set the value of the card to not equal the subnum of the card, but it didn't work. Any ideas would be much appreciated. Thanks. Here's part of the code: Private Sub Start_Click() Dim arrDeck(52) Randomize CardCount = 1 Do While CardCount < 53 arrDeck(CardCount) = -1 CardCount = CardCount + 1 Loop CardCount = 1 Counter = 0 Do While CardCount < 53 And Counter < 53 3 CardVal = Int((52 * Rnd) + 1) If arrDeck(CardVal) = -1 Then arrDeck(CardVal) = CardCount Subnum = Counter Image1(Subnum) = Image1(CardVal) Image1(Subnum).Visible = True CardCount = CardCount + 1 Counter = Counter + 1 Else: GoTo 3 End If Loop End Sub

          R Offline
          R Offline
          riced
          wrote on last edited by
          #4

          Here's a possible solution. Note that I have not Dimmed all the variables. It's a classic way to do shuffling of card decks.

          Private Sub Start_Click()

          Dim arrDeck(52)
          Randomize

          ' Fill deck with cards in order 1 to 52
          CardCount = 1
          Do While CardCount <= 52
          arrDeck(CardCount) = Cardcount
          CardCount = CardCount + 1
          Loop

          ' Shuufle the deck - end value (100) could be anything, bigger it is the more shuffled deck is
          Counter = 0
          Do While Counter <= 100
          ' Get two random card positions
          card1 = Int((52 * Rnd) + 1)
          card2 = Int((52 * Rnd) + 1)

            ' Swap cards at these positions
            tmpCard        = arrDeck(card1)
            arrDeck(card1) = arrDeck(card2)
            arrDeck(card2) = tmpCard
          

          Loop

          ' Now output immages in order of deck
          CardCount = 1
          Do While CardCount <= 52
          Subnum = arrDeck(CardCount)
          Image1(Subnum).Visible = True
          CardCount = CardCount + 1
          Loop
          End Sub

          Regards David R

          Y 3 Replies Last reply
          0
          • Y ymilan

            Hi, I'm shuffling a deck of cards, but seem to keep getting more than one of the same card, after the shuffle. So, I tried quite a bit trying to set the value of the card to not equal the subnum of the card, but it didn't work. Any ideas would be much appreciated. Thanks. Here's part of the code: Private Sub Start_Click() Dim arrDeck(52) Randomize CardCount = 1 Do While CardCount < 53 arrDeck(CardCount) = -1 CardCount = CardCount + 1 Loop CardCount = 1 Counter = 0 Do While CardCount < 53 And Counter < 53 3 CardVal = Int((52 * Rnd) + 1) If arrDeck(CardVal) = -1 Then arrDeck(CardVal) = CardCount Subnum = Counter Image1(Subnum) = Image1(CardVal) Image1(Subnum).Visible = True CardCount = CardCount + 1 Counter = Counter + 1 Else: GoTo 3 End If Loop End Sub

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            I clicked 'Post Message' when I meant to click 'Preview' :) I was going to add that you should look at how Dave K gets random number sequence. It's really how you should do it as well.

            Regards David R

            Y 1 Reply Last reply
            0
            • R riced

              I clicked 'Post Message' when I meant to click 'Preview' :) I was going to add that you should look at how Dave K gets random number sequence. It's really how you should do it as well.

              Regards David R

              Y Offline
              Y Offline
              ymilan
              wrote on last edited by
              #6

              Thanks for your comments and explanation of your code. It was so easy to understand and made much more sense than what I had. I was trying to do it much more difficult by saying "if one value does not equal the other value, than go ahead and display the images...." I appreciate it. Thanks. Yvonne

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                You can add your cards to a collection and then pull the cards from random positions out of that collection and add them to another collection.

                Private Function GetShuffledDeck() as List(Of Integer)
                Dim cards As New List(Of Integer)
                For i As Integer = 0 To 51
                cards.Add(i)
                Next

                    Dim RNG As New Random()
                
                    Dim deck As New List(Of Integer)
                    While cards.Count > 0
                        Dim cardNum As Integer
                        cardNum = RNG.Next(0, cards.Count)
                        deck.Add(cards(cardNum))
                        cards.RemoveAt(cardNum)
                    End While
                
                    Return deck
                

                End Function

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                Y Offline
                Y Offline
                ymilan
                wrote on last edited by
                #7

                Wow, thanks! This does make total sense, except I have one question which might be a "newbie type:" Cards.Add(i) would produce 0 -51 cards right? Then deck.Add(cards(cardNum" would produce a random ineger and remove at cardnum....since it is in the same function, then there is an end value of 52 right? It seems confusing to me on how the end value is represented in your shuffle procedure. Thanks very much for your time! You are AWESOME!

                D 1 Reply Last reply
                0
                • D Dalek Dave

                  have you tried applying a number between 1 and 52 to each card, the as each number is picked randomly it is added to an array. on the next random call, it is checked to the contents of the array, and if found to have been picked already is rejected. It will slow doown as the cards are dealt, but then that array will be filled from 1 to 52 in random order, just apply the numbers back to the cards to give a random shuffle.

                  ------------------------------------ "I am always serious about what I do, not necessarily about how I do it." Tom Baker

                  Y Offline
                  Y Offline
                  ymilan
                  wrote on last edited by
                  #8

                  Thank you; I will try this...I appreciate your time.

                  1 Reply Last reply
                  0
                  • R riced

                    Here's a possible solution. Note that I have not Dimmed all the variables. It's a classic way to do shuffling of card decks.

                    Private Sub Start_Click()

                    Dim arrDeck(52)
                    Randomize

                    ' Fill deck with cards in order 1 to 52
                    CardCount = 1
                    Do While CardCount <= 52
                    arrDeck(CardCount) = Cardcount
                    CardCount = CardCount + 1
                    Loop

                    ' Shuufle the deck - end value (100) could be anything, bigger it is the more shuffled deck is
                    Counter = 0
                    Do While Counter <= 100
                    ' Get two random card positions
                    card1 = Int((52 * Rnd) + 1)
                    card2 = Int((52 * Rnd) + 1)

                      ' Swap cards at these positions
                      tmpCard        = arrDeck(card1)
                      arrDeck(card1) = arrDeck(card2)
                      arrDeck(card2) = tmpCard
                    

                    Loop

                    ' Now output immages in order of deck
                    CardCount = 1
                    Do While CardCount <= 52
                    Subnum = arrDeck(CardCount)
                    Image1(Subnum).Visible = True
                    CardCount = CardCount + 1
                    Loop
                    End Sub

                    Regards David R

                    Y Offline
                    Y Offline
                    ymilan
                    wrote on last edited by
                    #9

                    I went to compile this code in MS VB and it rejected as Compile Error all the values that were not dimmed....any ideas why? Thanks in advance.

                    1 Reply Last reply
                    0
                    • R riced

                      Here's a possible solution. Note that I have not Dimmed all the variables. It's a classic way to do shuffling of card decks.

                      Private Sub Start_Click()

                      Dim arrDeck(52)
                      Randomize

                      ' Fill deck with cards in order 1 to 52
                      CardCount = 1
                      Do While CardCount <= 52
                      arrDeck(CardCount) = Cardcount
                      CardCount = CardCount + 1
                      Loop

                      ' Shuufle the deck - end value (100) could be anything, bigger it is the more shuffled deck is
                      Counter = 0
                      Do While Counter <= 100
                      ' Get two random card positions
                      card1 = Int((52 * Rnd) + 1)
                      card2 = Int((52 * Rnd) + 1)

                        ' Swap cards at these positions
                        tmpCard        = arrDeck(card1)
                        arrDeck(card1) = arrDeck(card2)
                        arrDeck(card2) = tmpCard
                      

                      Loop

                      ' Now output immages in order of deck
                      CardCount = 1
                      Do While CardCount <= 52
                      Subnum = arrDeck(CardCount)
                      Image1(Subnum).Visible = True
                      CardCount = CardCount + 1
                      Loop
                      End Sub

                      Regards David R

                      Y Offline
                      Y Offline
                      ymilan
                      wrote on last edited by
                      #10

                      Hmm, seems to compile, but when I click the start button, the program hangs....any ideas?

                      R 1 Reply Last reply
                      0
                      • Y ymilan

                        Hmm, seems to compile, but when I click the start button, the program hangs....any ideas?

                        R Offline
                        R Offline
                        riced
                        wrote on last edited by
                        #11

                        Not without seeing the actual code you did and where it is called. I don't think there's anything in it that should cause a hang. I did not Dim all the variables because I was not sure what you had already declared. Obviously they need to be Dimmed somewhere.

                        Regards David R

                        1 Reply Last reply
                        0
                        • R riced

                          Here's a possible solution. Note that I have not Dimmed all the variables. It's a classic way to do shuffling of card decks.

                          Private Sub Start_Click()

                          Dim arrDeck(52)
                          Randomize

                          ' Fill deck with cards in order 1 to 52
                          CardCount = 1
                          Do While CardCount <= 52
                          arrDeck(CardCount) = Cardcount
                          CardCount = CardCount + 1
                          Loop

                          ' Shuufle the deck - end value (100) could be anything, bigger it is the more shuffled deck is
                          Counter = 0
                          Do While Counter <= 100
                          ' Get two random card positions
                          card1 = Int((52 * Rnd) + 1)
                          card2 = Int((52 * Rnd) + 1)

                            ' Swap cards at these positions
                            tmpCard        = arrDeck(card1)
                            arrDeck(card1) = arrDeck(card2)
                            arrDeck(card2) = tmpCard
                          

                          Loop

                          ' Now output immages in order of deck
                          CardCount = 1
                          Do While CardCount <= 52
                          Subnum = arrDeck(CardCount)
                          Image1(Subnum).Visible = True
                          CardCount = CardCount + 1
                          Loop
                          End Sub

                          Regards David R

                          Y Offline
                          Y Offline
                          ymilan
                          wrote on last edited by
                          #12

                          ' I x'd out the lines to show what I am trying to do... Option Explicit Public Sub Main() Start_Game.Show End Sub Private Sub Exit_Click() MsgBox ("Thank you for playing! Good bye") End End Sub Private Sub Start_Click() Dim arrDeck(52) Randomize Dim Cardcount As Integer Dim Counter As Integer Dim Card1 As Integer Dim Card2 As Integer 'Dim CardVal As Integer 'Dim SubNum As Integer ' Fill deck with cards in order 1 to 52 Cardcount = 1 Do While Cardcount <= 52 arrDeck(Cardcount) = Cardcount Cardcount = Cardcount + 1 Loop ' Shuffle the deck - end value (100) Counter = 0 Do While Counter <= 100 ' Get two random card positions Card1 = Int((52 * Rnd) + 1) Card2 = Int((52 * Rnd) + 1) ' Swap cards at these positions Tmpcard = arrDeck(Card1) arrDeck(Card1) = arrDeck(Card2) arrDeck(Card2) = Tmpcard 'CardVal = Tmpcard ? Looking for a single value out of the random cards you made... Loop ' Now output images in order of deck Cardcount = 1 Counter = 0 ' It seems I need a counter in here for Subnum, 0-52 so it can display the cards from 0-52 slots, ' But the array of a single value out of the random cards you made need to be set to another counter, ' Right? I'm not sure...I x'd out the lines to show what I meant... 'Do While Cardcount <= 52 And Counter < 53 'If arrDeck(CardVal) = -1 Then ' arrDeck(CardVal) = Cardcount 'Image1(Cardcount) = CardVal ' Subnum = Counter 'Image1(Subnum).Visible = True Cardcount = Cardcount + 1 'Counter = Counter + 1 ' End If Loop End Sub

                          R 1 Reply Last reply
                          0
                          • Y ymilan

                            Wow, thanks! This does make total sense, except I have one question which might be a "newbie type:" Cards.Add(i) would produce 0 -51 cards right? Then deck.Add(cards(cardNum" would produce a random ineger and remove at cardnum....since it is in the same function, then there is an end value of 52 right? It seems confusing to me on how the end value is represented in your shuffle procedure. Thanks very much for your time! You are AWESOME!

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #13

                            ymilan wrote:

                            since it is in the same function, then there is an end value of 52 right?

                            I don't get what your referring to. There is a COUNT of 52 items, 0 to 51 inclusive. The reason I wrote the method this way is because you can eliminate the top part the populates Cards with 0 to 51 and just pass in a collection of cards of any order. This method would then just "shuffle" a randomized deck. For example, you could have a method that passes a deck into this shuffle method a few times, passing in the deck that was returned, to really shuffle the deck. Also, you can create your own Card class and replace the List(Of Integer) references with List(Of Card) instead.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007, 2008

                            Y 1 Reply Last reply
                            0
                            • Y ymilan

                              ' I x'd out the lines to show what I am trying to do... Option Explicit Public Sub Main() Start_Game.Show End Sub Private Sub Exit_Click() MsgBox ("Thank you for playing! Good bye") End End Sub Private Sub Start_Click() Dim arrDeck(52) Randomize Dim Cardcount As Integer Dim Counter As Integer Dim Card1 As Integer Dim Card2 As Integer 'Dim CardVal As Integer 'Dim SubNum As Integer ' Fill deck with cards in order 1 to 52 Cardcount = 1 Do While Cardcount <= 52 arrDeck(Cardcount) = Cardcount Cardcount = Cardcount + 1 Loop ' Shuffle the deck - end value (100) Counter = 0 Do While Counter <= 100 ' Get two random card positions Card1 = Int((52 * Rnd) + 1) Card2 = Int((52 * Rnd) + 1) ' Swap cards at these positions Tmpcard = arrDeck(Card1) arrDeck(Card1) = arrDeck(Card2) arrDeck(Card2) = Tmpcard 'CardVal = Tmpcard ? Looking for a single value out of the random cards you made... Loop ' Now output images in order of deck Cardcount = 1 Counter = 0 ' It seems I need a counter in here for Subnum, 0-52 so it can display the cards from 0-52 slots, ' But the array of a single value out of the random cards you made need to be set to another counter, ' Right? I'm not sure...I x'd out the lines to show what I meant... 'Do While Cardcount <= 52 And Counter < 53 'If arrDeck(CardVal) = -1 Then ' arrDeck(CardVal) = Cardcount 'Image1(Cardcount) = CardVal ' Subnum = Counter 'Image1(Subnum).Visible = True Cardcount = Cardcount + 1 'Counter = Counter + 1 ' End If Loop End Sub

                              R Offline
                              R Offline
                              riced
                              wrote on last edited by
                              #14

                              I'm not sure why you need all the counters. Your Counter is always one less than Cardcount so is effectively redundant. In Do While Cardcount <= 52 And Counter < 53 both parts of the condition will have same logical value. What is Image1() - it looks like an array but has a Visible property. And this line implies that it is an array of integer: Image1(Cardcount) = CardVal In any case here's a new version of the code that shuffles the images directly. You would need to Dim tmpImage as an appropriate type. To get a single random image after shuffling you could just use Image1(1) i.e. the top of the pack; or you could use tmpImage.

                              Private Sub Start_Click()
                              Randomize

                              'Dim tmpImage As ????
                              Dim card1 As Integer
                              Dim card2 As Integer
                              Dim counter As Integer

                              ' Shuffle the images - end value (100)
                              Counter = 0
                              Do While Counter <= 100
                              ' Get two random image positions
                              card1 = Int((52 * Rnd) + 1)
                              card2 = Int((52 * Rnd) + 1)

                                ' Swap images at these positions
                                tmpImage      = Image1(card1)
                                Image1(card1) = Image1(card2)
                                Image1(card2) = tmpImage
                              

                              Loop

                              ' Now output images in order
                              counter = 1
                              Do While counter <= 52
                              Image1(counter).Visible = True
                              counter = counter + 1
                              Loop
                              End Sub

                              What version of VB are you using? I'd assumed VB.NET but I can't see where Image1() would fit in. But, IIRC, in VB6 there is an Image control (not used VB6 for about 5 years). EDIT: forgot to Dim counter :)

                              Regards David R

                              Y 2 Replies Last reply
                              0
                              • D Dave Kreskowiak

                                ymilan wrote:

                                since it is in the same function, then there is an end value of 52 right?

                                I don't get what your referring to. There is a COUNT of 52 items, 0 to 51 inclusive. The reason I wrote the method this way is because you can eliminate the top part the populates Cards with 0 to 51 and just pass in a collection of cards of any order. This method would then just "shuffle" a randomized deck. For example, you could have a method that passes a deck into this shuffle method a few times, passing in the deck that was returned, to really shuffle the deck. Also, you can create your own Card class and replace the List(Of Integer) references with List(Of Card) instead.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007, 2008

                                Y Offline
                                Y Offline
                                ymilan
                                wrote on last edited by
                                #15

                                Ok, I get it now. I was thinking along those lines, but got confused. Thanks.

                                1 Reply Last reply
                                0
                                • R riced

                                  I'm not sure why you need all the counters. Your Counter is always one less than Cardcount so is effectively redundant. In Do While Cardcount <= 52 And Counter < 53 both parts of the condition will have same logical value. What is Image1() - it looks like an array but has a Visible property. And this line implies that it is an array of integer: Image1(Cardcount) = CardVal In any case here's a new version of the code that shuffles the images directly. You would need to Dim tmpImage as an appropriate type. To get a single random image after shuffling you could just use Image1(1) i.e. the top of the pack; or you could use tmpImage.

                                  Private Sub Start_Click()
                                  Randomize

                                  'Dim tmpImage As ????
                                  Dim card1 As Integer
                                  Dim card2 As Integer
                                  Dim counter As Integer

                                  ' Shuffle the images - end value (100)
                                  Counter = 0
                                  Do While Counter <= 100
                                  ' Get two random image positions
                                  card1 = Int((52 * Rnd) + 1)
                                  card2 = Int((52 * Rnd) + 1)

                                    ' Swap images at these positions
                                    tmpImage      = Image1(card1)
                                    Image1(card1) = Image1(card2)
                                    Image1(card2) = tmpImage
                                  

                                  Loop

                                  ' Now output images in order
                                  counter = 1
                                  Do While counter <= 52
                                  Image1(counter).Visible = True
                                  counter = counter + 1
                                  Loop
                                  End Sub

                                  What version of VB are you using? I'd assumed VB.NET but I can't see where Image1() would fit in. But, IIRC, in VB6 there is an Image control (not used VB6 for about 5 years). EDIT: forgot to Dim counter :)

                                  Regards David R

                                  Y Offline
                                  Y Offline
                                  ymilan
                                  wrote on last edited by
                                  #16

                                  Ok, sure then, I can just pass the images through directly...then having overkill of counting. I see now. Thanks. As far as the version, perhaps this would be good to ask this question also, I'm using VB6 because that's all I have. It costs for VB.NET right? Not sure. I'm doing a specific game for my Mom, that's why I haven't upgraded am just using an older version. Images() was the visual image of the card I created on the form.

                                  R 1 Reply Last reply
                                  0
                                  • R riced

                                    I'm not sure why you need all the counters. Your Counter is always one less than Cardcount so is effectively redundant. In Do While Cardcount <= 52 And Counter < 53 both parts of the condition will have same logical value. What is Image1() - it looks like an array but has a Visible property. And this line implies that it is an array of integer: Image1(Cardcount) = CardVal In any case here's a new version of the code that shuffles the images directly. You would need to Dim tmpImage as an appropriate type. To get a single random image after shuffling you could just use Image1(1) i.e. the top of the pack; or you could use tmpImage.

                                    Private Sub Start_Click()
                                    Randomize

                                    'Dim tmpImage As ????
                                    Dim card1 As Integer
                                    Dim card2 As Integer
                                    Dim counter As Integer

                                    ' Shuffle the images - end value (100)
                                    Counter = 0
                                    Do While Counter <= 100
                                    ' Get two random image positions
                                    card1 = Int((52 * Rnd) + 1)
                                    card2 = Int((52 * Rnd) + 1)

                                      ' Swap images at these positions
                                      tmpImage      = Image1(card1)
                                      Image1(card1) = Image1(card2)
                                      Image1(card2) = tmpImage
                                    

                                    Loop

                                    ' Now output images in order
                                    counter = 1
                                    Do While counter <= 52
                                    Image1(counter).Visible = True
                                    counter = counter + 1
                                    Loop
                                    End Sub

                                    What version of VB are you using? I'd assumed VB.NET but I can't see where Image1() would fit in. But, IIRC, in VB6 there is an Image control (not used VB6 for about 5 years). EDIT: forgot to Dim counter :)

                                    Regards David R

                                    Y Offline
                                    Y Offline
                                    ymilan
                                    wrote on last edited by
                                    #17

                                    Ok, I set tmpImage to As Image, is that correct? Then would I have the change the last few lines to reflect Do While Counter <= 52 Image1(TmpImage).Visible = True Counter = Counter + 1 Loop If so, gives an error like object variable or with block variable not set debugs on TmpImage = Image1(Card1)

                                    R 1 Reply Last reply
                                    0
                                    • Y ymilan

                                      Ok, sure then, I can just pass the images through directly...then having overkill of counting. I see now. Thanks. As far as the version, perhaps this would be good to ask this question also, I'm using VB6 because that's all I have. It costs for VB.NET right? Not sure. I'm doing a specific game for my Mom, that's why I haven't upgraded am just using an older version. Images() was the visual image of the card I created on the form.

                                      R Offline
                                      R Offline
                                      riced
                                      wrote on last edited by
                                      #18

                                      Glad to be of some help - my VB6 is a bit rusty but I should have spotted the use of Image1() :) You'll probably find that Dave K's solution won't work - it's .NET stuff. You can download VB.NET Express editions for free. There's two versions - 2005 and 2008. Probably best to go for 2008. Worth looking into. You can see what's available at http://www.microsoft.com/express/[^]

                                      Regards David R

                                      1 Reply Last reply
                                      0
                                      • Y ymilan

                                        Ok, I set tmpImage to As Image, is that correct? Then would I have the change the last few lines to reflect Do While Counter <= 52 Image1(TmpImage).Visible = True Counter = Counter + 1 Loop If so, gives an error like object variable or with block variable not set debugs on TmpImage = Image1(Card1)

                                        R Offline
                                        R Offline
                                        riced
                                        wrote on last edited by
                                        #19

                                        If tmpImage is an Image (it has to be whatever is in Image1() so what type of object does Image1() store?) then you can't use it as an index to Image1(). Using counter as the index will make all the images visible. I don't have VB6 running anymore so I'm relying on memory. :)

                                        Regards David R

                                        Y 1 Reply Last reply
                                        0
                                        • R riced

                                          If tmpImage is an Image (it has to be whatever is in Image1() so what type of object does Image1() store?) then you can't use it as an index to Image1(). Using counter as the index will make all the images visible. I don't have VB6 running anymore so I'm relying on memory. :)

                                          Regards David R

                                          Y Offline
                                          Y Offline
                                          ymilan
                                          wrote on last edited by
                                          #20

                                          Image1() stores an array... 0-52, was created automatically when I set up the cards on the form.

                                          R 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups