# Shuffle Cards

Hi Guys - Just writting some to shuffle up a pack of cards - I have made this to set myself up, but want to know what would be the best way - I wanted to display its progress on a progress bar and slider, as well as calculate the time taken... Any ideas ?

I also have an image list for all the cards. Hearts, Spades etc. So I wanted to show the end result in an image combo box.

If you don't like the code you welcome to change it... :)

Option Explicit
Private Type tCard
Card As String
Suit As String
End Type

Private mstrCards(1 To 52) As tCard

Call ShuffleCards
End Sub

Dim pstrCards As String
Dim pstrSuits As String
Dim pstrCardArray() As String
Dim pstrSuitArray() As String
Dim i As Integer
Dim x As Integer
x = 1

pstrCards = ",Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King"
pstrCardArray = Split(pstrCards, ",")
pstrSuitArray = Split(pstrSuits, ",")

Do While Not x = 5
For i = 1 To 13
mstrCards(i).Card = pstrCardArray(i)
mstrCards(x).Suit = pstrSuitArray(x)
Debug.Print mstrCards(i).Card
Debug.Print mstrCards(x).Suit
Next
x = x + 1
Loop
End Sub
###### Who is Participating?

Commented:
Type typCard
Card As Long
Suit As Long
End Type

Dim Deck(52) As typCard
Dim Cards
Dim Suits
Cards = Split(",Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King", ",")
For i = 1 To 13
For j = 1 To 4
With Deck(i + (j - 1) * 13)
.Card = i
.Suit = j
End With
Next j
Next i
Randomize

For i = 1 To 52
Dim x As typCard
Dim y As Long
y = Int(Rnd * 52) + 1
x = Deck(y)           ' Swap cards
Deck(y) = Deck(i)
Deck(i) = x
Next i

For i = 1 To 52
Debug.Print Cards(Deck(i).Card) & " - " & Suits(Deck(i).Suit)
Next i
0

Author Commented:
How I want to do it, but am not really sure, is have a loop that will swap the cards based upon the current value of the UpDown control instance (and the corresponding buddy text box). The code the loop should randomly select two elemenets fgrom the array and then exchange the contents of those two elements...

0

Author Commented:
How can I get this into my Image Combo Box with the pics... Say it will have a pic of a heart, and then the word Ace for example...

PS. In the image list itself, I have called them Hearts, Clubs, Diamonds and Spades as the key... :-)
0

Commented:
Option Explicit
Private Type tCard
Card As String
Suit As String
End Type

Private mstrCards(1 To 52) As tCard

dim t1 as long
t1=timer            'save the timer value before beginning
Call ShuffleCards
t2=timer            'save the new timer value when done
msgbox "Time elapsed:" & (t2-t1)  'calculate the diffrence
End Sub

Dim pstrCards As String
Dim pstrSuits As String
Dim pstrCardArray() As String
Dim pstrSuitArray() As String
Dim i As Integer
Dim x As Integer
x = 1

pstrCards = ",Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King"
pstrCardArray = Split(pstrCards, ",")
pstrSuitArray = Split(pstrSuits, ",")
progressbar1.value=0
progressbar1.min=0
progressbar1.max=65    ' x * i = 13 * 5
Do While Not x = 5
For i = 1 To 13
mstrCards(i).Card = pstrCardArray(i)
mstrCards(x).Suit = pstrSuitArray(x)
Debug.Print mstrCards(i).Card
Debug.Print mstrCards(x).Suit
progressbar1.value=progressbar1.value + 1'update bar
Next
x = x + 1
Loop
End Sub
0

Commented:
Here is a technique that I use to shuffle that I find gives a good random order:

pstrCards = ",Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King"
pstrCardArray = Split(pstrCards, ",")
pstrSuitArray = Split(pstrSuits, ",")

J = 1
Do While Not x = 5
For i = 1 To 13
mstrCards(j).Card = pstrCardArray(i)
mstrCards(j).Suit = pstrSuitArray(x)
j = j + 1
Next
x = x + 1
Loop

Dim cCards as collection
Dim aDeck(1 to 52) as Long

' just get a list of the 52 numbers
For i = 1 to 52
Next i

'Int(([upperbound] - [lowerbound] + 1) * Rnd + [lowerbound])

i = 1
Do While cCards.Count > 0
' select a collection's index randomly from the number of collection entries left in the list
index = Int((cCards.Count - 1) + 1 * Rnd + 1)
' take the value in the collection at this index and move to the deck
' now remove that card number from the collection
cCards.RemoveItem(index)
i = i + 1
Loop

Then, you can simply iterate through the aDeck array, from 1 to 52 to retrieve the cards, which will be in random order:

0

Author Commented:
I would rather not use a Timer event if possible... Still need to handle the progress and slider bars...

And that text box with the up and down arrows to increase its number by 1000 each time... Thats the only thing I need on this...

Sorry if I was not clear before... :(
0

Author Commented:
Just wanted to know how it has been going guys ?
0

Commented:
Sorry hollstar, I guess it's still not clear what you have and what you still need.  Selim007 gave you code for updating the progress bar.  If by textbox with updown arrows you mean that you have a textbox and a spinner control next to it, then the code for changing the value in the textbox goes in the spinup and spindown events of the spinner control, and that code would just be:

'In SpinUp
text1.Text = CInt(text1.Text) + 1000

In SpinDown
text1.Text = CInt(text1.Text) - 1000

Of course, you'd have to make sure that the text in the textbox started out as some number.

You have to take a look at which controls you are using, and be specific with us, as we're just guessing.  If you are not sure, click on the control to highlight it, then click F4 to bring up the property box, and look at what it says next to the control's name in the drop-down list.  For text1 the combo would have:

Text1  textbox

So, tell us if you are using a scrollbar, or a spinner button or what, and then what you want to do with it.
0

Author Commented:
Sorry Guys... Been busy and had not had a chance to get back here... Been working on this on and off, and I think I found 'Gunsen' and his answer to be the best for me.

For this reason I will accept his comment. Should anyone else feel they should get points, let both myself and EE know... ;-)
0

Author Commented:
Overall not to bad - The reason I said 'B' was not really due to the code, but due to the fact you missed parts of my question. While I learned in the process, I did feel you did not really fully answer my question at the time...

If I could say 'A' for code I would... Top Stuff... :-)
0

Commented:
Thanx, hollstar.
0

Author Commented:
No Worries... :-) I think I did change something in your code, but I did have the skills (while very basic lol) to make it work for me...

Overall, I was happy with the outcome... Just took a while mainly because of me... Sorry for that once again...

Hollstar...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.