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

                                      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 Offline
                                      R Offline
                                      riced
                                      wrote on last edited by
                                      #33

                                      So you have 52 Image1() controls and want to display the deck in them?. Do the indexes for the controls go from 1 to 52 or from 0 to 51? You should be able to find that from the properties in the designer. Depending on whether it's 0-51 or 1-52 there could be a simple solution. How do the Image1() controls get filled with pictures when the program starts up? Is there any code that fills them (probably in Form_Load event if there is)? Or did you set them in the designer? I managed to dig out an old machine that still had VB6 on and had a little play - frightening how much I'd forgotten about Image controls!

                                      Regards David R

                                      Y 2 Replies Last reply
                                      0
                                      • R riced

                                        So you have 52 Image1() controls and want to display the deck in them?. Do the indexes for the controls go from 1 to 52 or from 0 to 51? You should be able to find that from the properties in the designer. Depending on whether it's 0-51 or 1-52 there could be a simple solution. How do the Image1() controls get filled with pictures when the program starts up? Is there any code that fills them (probably in Form_Load event if there is)? Or did you set them in the designer? I managed to dig out an old machine that still had VB6 on and had a little play - frightening how much I'd forgotten about Image controls!

                                        Regards David R

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

                                        Firstly, I would like to thank you for being so cool for helping me on this project. I truly appreciate it. As far as the image1() controls, they were created in the design page all lined up with different bitmaps representing them. Then the first image card would be image1(0) to image1(52) for the final card. Wait a second....just came back to reading this and realized in the designer the first card of the array is 0, then the final card is labeled 52. Isn't it supposed to be 51? Why is it doing that....it looks like I set up the suits perfectly in order..... No specific code that fills the array of images, just designed in designer form and added that way. Thought that would be easier, but perhaps there is a disadvantage of doing it that way?

                                        R 1 Reply Last reply
                                        0
                                        • R riced

                                          So you have 52 Image1() controls and want to display the deck in them?. Do the indexes for the controls go from 1 to 52 or from 0 to 51? You should be able to find that from the properties in the designer. Depending on whether it's 0-51 or 1-52 there could be a simple solution. How do the Image1() controls get filled with pictures when the program starts up? Is there any code that fills them (probably in Form_Load event if there is)? Or did you set them in the designer? I managed to dig out an old machine that still had VB6 on and had a little play - frightening how much I'd forgotten about Image controls!

                                          Regards David R

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

                                          Should there be a -1 somewhere in the code, making the array of images to go back one integer?

                                          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