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 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
                  • Y ymilan

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

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

                    It must be an array of something - the same type as the cards? Can yo see the code that fills the array? That should tell you what the type is. As an alternative, you could try Dimming tmpImage As Variant - that would allow it to hold anything. But you still can't use it as an index into the array.

                    Regards David R

                    Y 2 Replies Last reply
                    0
                    • R riced

                      It must be an array of something - the same type as the cards? Can yo see the code that fills the array? That should tell you what the type is. As an alternative, you could try Dimming tmpImage As Variant - that would allow it to hold anything. But you still can't use it as an index into the array.

                      Regards David R

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

                      It's an array of images, as in I click on one of the images on the form and it states image1(0) Image That's what it states, so I should set tmpImage to Image, but it still produces the same error. Should I try a third integer variable to use with tmpImage, as in tmpImage(Card3) and have Card3 be a random number?

                      1 Reply Last reply
                      0
                      • R riced

                        It must be an array of something - the same type as the cards? Can yo see the code that fills the array? That should tell you what the type is. As an alternative, you could try Dimming tmpImage As Variant - that would allow it to hold anything. But you still can't use it as an index into the array.

                        Regards David R

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

                        Should I dim tmpimage as image and set it using a set statement to an array of an index? or integer? How would I do that?

                        R 1 Reply Last reply
                        0
                        • Y ymilan

                          Should I dim tmpimage as image and set it using a set statement to an array of an index? or integer? How would I do that?

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

                          I'd forgotten you need to use Set when dealing with 'objects - mea culpa. :) So in the shuffle loop you need this:

                            Set tmpImage      = Image1(card1)
                            Set Image1(card1) = Image1(card2)
                            Set Image1(card2) = tmpImage
                          

                          tmpImage must be of the same type as whatever the Image1() array holds (or it could be Variant). It can't be an Integer nor an array of anything. All it's used for is to allow items in the Image1() array to be swapped. It has no other use in the code. How many images do you want to display at once? If you only want one then the second loop is not needed. You could just do something like Image1(1).Visible = True to display the first image.

                          Regards David R

                          Y 1 Reply Last reply
                          0
                          • R riced

                            I'd forgotten you need to use Set when dealing with 'objects - mea culpa. :) So in the shuffle loop you need this:

                              Set tmpImage      = Image1(card1)
                              Set Image1(card1) = Image1(card2)
                              Set Image1(card2) = tmpImage
                            

                            tmpImage must be of the same type as whatever the Image1() array holds (or it could be Variant). It can't be an Integer nor an array of anything. All it's used for is to allow items in the Image1() array to be swapped. It has no other use in the code. How many images do you want to display at once? If you only want one then the second loop is not needed. You could just do something like Image1(1).Visible = True to display the first image.

                            Regards David R

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

                            Ok, great! I had to put in counter + 1 though for it to partially work... Set tmpImage = Image1(Card1) Set Image1(Card1) = Image1(Card2) Set Image1(Card2) = tmpImage Counter = Counter + 1 Loop But also, it doesn't show the first card; tried changing counter to equal 0, but didn't help; any ideas?

                            R 1 Reply Last reply
                            0
                            • Y ymilan

                              Ok, great! I had to put in counter + 1 though for it to partially work... Set tmpImage = Image1(Card1) Set Image1(Card1) = Image1(Card2) Set Image1(Card2) = tmpImage Counter = Counter + 1 Loop But also, it doesn't show the first card; tried changing counter to equal 0, but didn't help; any ideas?

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

                              Can't believe I made such a blunder :doh: - but it's because I would normally use a For loop like so (and I'm writing the code in Notepad so no help from the editor :) ).

                              For counter = 0 to 100
                              ' Get two random image positions
                              card1 = Int((52 * Rnd) + 1)
                              card2 = Int((52 * Rnd) + 1)

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

                              Next counter

                              Then I don't have to increase counter, the loop does it for me. Note that counter is just counting the numbers of swaps done - it has nothing to do with the number of images it could be set to do 1000 or whatever you like. All this loop does is shuffle the images. It does not display them in any way - that's down to the Image1(x).Visible = True where x is the number of the image you want to display. Not sure why it's not displaying. You might try refreshing the the form (i.e add Form.Refresh or a DoEvents after setting Image1(1).Visible = True.

                              Regards David R

                              Y 3 Replies Last reply
                              0
                              • R riced

                                Can't believe I made such a blunder :doh: - but it's because I would normally use a For loop like so (and I'm writing the code in Notepad so no help from the editor :) ).

                                For counter = 0 to 100
                                ' Get two random image positions
                                card1 = Int((52 * Rnd) + 1)
                                card2 = Int((52 * Rnd) + 1)

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

                                Next counter

                                Then I don't have to increase counter, the loop does it for me. Note that counter is just counting the numbers of swaps done - it has nothing to do with the number of images it could be set to do 1000 or whatever you like. All this loop does is shuffle the images. It does not display them in any way - that's down to the Image1(x).Visible = True where x is the number of the image you want to display. Not sure why it's not displaying. You might try refreshing the the form (i.e add Form.Refresh or a DoEvents after setting Image1(1).Visible = True.

                                Regards David R

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

                                No problem; you'll always be smarter at this than me. Anyhow, I'll try refreshing the form then. Thanks so much! Yvonne

                                R 1 Reply Last reply
                                0
                                • Y ymilan

                                  No problem; you'll always be smarter at this than me. Anyhow, I'll try refreshing the form then. Thanks so much! Yvonne

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

                                  Not necessarily smarter - probably just more experienced (been doing this more years than I care to remember :-D ) I'm off to bed in a little while so if there's anything else you have a query with I'll reply sometime tomorrow.

                                  Regards David R

                                  1 Reply Last reply
                                  0
                                  • R riced

                                    Can't believe I made such a blunder :doh: - but it's because I would normally use a For loop like so (and I'm writing the code in Notepad so no help from the editor :) ).

                                    For counter = 0 to 100
                                    ' Get two random image positions
                                    card1 = Int((52 * Rnd) + 1)
                                    card2 = Int((52 * Rnd) + 1)

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

                                    Next counter

                                    Then I don't have to increase counter, the loop does it for me. Note that counter is just counting the numbers of swaps done - it has nothing to do with the number of images it could be set to do 1000 or whatever you like. All this loop does is shuffle the images. It does not display them in any way - that's down to the Image1(x).Visible = True where x is the number of the image you want to display. Not sure why it's not displaying. You might try refreshing the the form (i.e add Form.Refresh or a DoEvents after setting Image1(1).Visible = True.

                                    Regards David R

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

                                    Hello, Well it still shows two of a kinds after the shuffle. I've tried various things with While and Do nested loops and same with If Then EndIf, but nothing seems to work...I'm trying though. Any ideas?

                                    R 1 Reply Last reply
                                    0
                                    • R riced

                                      Can't believe I made such a blunder :doh: - but it's because I would normally use a For loop like so (and I'm writing the code in Notepad so no help from the editor :) ).

                                      For counter = 0 to 100
                                      ' Get two random image positions
                                      card1 = Int((52 * Rnd) + 1)
                                      card2 = Int((52 * Rnd) + 1)

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

                                      Next counter

                                      Then I don't have to increase counter, the loop does it for me. Note that counter is just counting the numbers of swaps done - it has nothing to do with the number of images it could be set to do 1000 or whatever you like. All this loop does is shuffle the images. It does not display them in any way - that's down to the Image1(x).Visible = True where x is the number of the image you want to display. Not sure why it's not displaying. You might try refreshing the the form (i.e add Form.Refresh or a DoEvents after setting Image1(1).Visible = True.

                                      Regards David R

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

                                      This doesn't work, but this is what I'm trying to say: ' Shuffle the deck - end value (100) Card1 = 0 Card2 = 1 Counter = 0 If Card1 <> Card2 And Counter <= 100 Then ' Get two random image positions 3 Card1 = Int((52 * Rnd) + 1) Card2 = Int((52 * Rnd) + 1) ' Swap images at these positions Set tmpImage = Image1(Card1) Set Image1(Card1) = Image1(Card2) Set Image1(Card2) = tmpImage Counter = Counter + 1 ElseIf Card1 = Card2 Then GoTo 3 End If

                                      1 Reply Last reply
                                      0
                                      • Y ymilan

                                        Hello, Well it still shows two of a kinds after the shuffle. I've tried various things with While and Do nested loops and same with If Then EndIf, but nothing seems to work...I'm trying though. Any ideas?

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

                                        I'm not sure what the program is supposed to do so not sure why it's not working. Can you tell me what the form looks like (e.g. how many image controls it displays)? What should happen when the Start button is clicked (I assume Start is a button)? Should it display one card or more than one?

                                        Regards David R

                                        Y 1 Reply Last reply
                                        0
                                        • R riced

                                          I'm not sure what the program is supposed to do so not sure why it's not working. Can you tell me what the form looks like (e.g. how many image controls it displays)? What should happen when the Start button is clicked (I assume Start is a button)? Should it display one card or more than one?

                                          Regards David R

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

                                          Well, it is displaying all the cards 4 across and 13 down in full suits w/out jokers, of course. Then when the cards are random and shuffled, they should display the same amount of cards in same positions, but random ones. What happens when I compile and run the program, is that more of one of the same cards are displayed, thus showing not totally random cards. Somehow, I'm thinking Card1 should not equal Card2 from the random procedure, then the cards are shuffled, and then distributed out....but there is no code that shows Card1 should not equal to Card2 at all, for to be randomly displayed. Any ideas? Thank you.

                                          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