Need Help with Shuffle of a deck
-
Image1() stores an array... 0-52, was created automatically when I set up the cards on the form.
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
-
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
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?
-
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
-
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?
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
-
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
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?
-
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?
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 thatcounter
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 theImage1(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 settingImage1(1).Visible = True
.Regards David R
-
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 thatcounter
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 theImage1(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 settingImage1(1).Visible = True
.Regards David R
-
No problem; you'll always be smarter at this than me. Anyhow, I'll try refreshing the form then. Thanks so much! Yvonne
-
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 thatcounter
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 theImage1(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 settingImage1(1).Visible = True
.Regards David R
-
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 thatcounter
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 theImage1(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 settingImage1(1).Visible = True
.Regards David R
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
-
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?
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
-
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
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.
-
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.
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
-
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
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?
-
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
-
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?
It looks like you have missed an index somewhere. The only way to find out is to go through them one by one. The reason I asked was that I thought if the indexes go from 1 to 52 you could have an Image1(0) that can be made invisible (you could do this in the Form_Load event, and it wouldn't matter where it is on the screen). You could then use this in the shuffle instead of tmpCard when doing the swap. That way you know it is the correct type. So you could have something like this for swapping:
If (card1 <> card2) Then
Image1(0).Picture = Image1(card1).Picture
Image1(card1).Picture = Image1(card2).Picture
Image1(card2).Picture = Image1(0).Picture
End IfIn your loop I'd avoid using the goto, you need something like:
For counter = 1 to 100 'Do 100 swaps to shuffle deck
'do the shuffling here
Next counterA disadvantage of setting the bitmaps in the designer is that it would be tedious to set the images to use a different set of cards (but that probably isn't a worry). Another one is you have to make sure you don't have gaps in the indexes and it looks like you have a gap (else the indexes would be 0 to 51). Because of the way card1 and card2 are calculated they will be between 1 and 52 so your indexes for Image1() need to be in the same range. This avoids having to do -1 somewhere to get the correct range (in fact you could just drop the +1 when calculating card1 and card2 instead of using -1).
Regards David R
-
It looks like you have missed an index somewhere. The only way to find out is to go through them one by one. The reason I asked was that I thought if the indexes go from 1 to 52 you could have an Image1(0) that can be made invisible (you could do this in the Form_Load event, and it wouldn't matter where it is on the screen). You could then use this in the shuffle instead of tmpCard when doing the swap. That way you know it is the correct type. So you could have something like this for swapping:
If (card1 <> card2) Then
Image1(0).Picture = Image1(card1).Picture
Image1(card1).Picture = Image1(card2).Picture
Image1(card2).Picture = Image1(0).Picture
End IfIn your loop I'd avoid using the goto, you need something like:
For counter = 1 to 100 'Do 100 swaps to shuffle deck
'do the shuffling here
Next counterA disadvantage of setting the bitmaps in the designer is that it would be tedious to set the images to use a different set of cards (but that probably isn't a worry). Another one is you have to make sure you don't have gaps in the indexes and it looks like you have a gap (else the indexes would be 0 to 51). Because of the way card1 and card2 are calculated they will be between 1 and 52 so your indexes for Image1() need to be in the same range. This avoids having to do -1 somewhere to get the correct range (in fact you could just drop the +1 when calculating card1 and card2 instead of using -1).
Regards David R
-
It looks like you have missed an index somewhere. The only way to find out is to go through them one by one. The reason I asked was that I thought if the indexes go from 1 to 52 you could have an Image1(0) that can be made invisible (you could do this in the Form_Load event, and it wouldn't matter where it is on the screen). You could then use this in the shuffle instead of tmpCard when doing the swap. That way you know it is the correct type. So you could have something like this for swapping:
If (card1 <> card2) Then
Image1(0).Picture = Image1(card1).Picture
Image1(card1).Picture = Image1(card2).Picture
Image1(card2).Picture = Image1(0).Picture
End IfIn your loop I'd avoid using the goto, you need something like:
For counter = 1 to 100 'Do 100 swaps to shuffle deck
'do the shuffling here
Next counterA disadvantage of setting the bitmaps in the designer is that it would be tedious to set the images to use a different set of cards (but that probably isn't a worry). Another one is you have to make sure you don't have gaps in the indexes and it looks like you have a gap (else the indexes would be 0 to 51). Because of the way card1 and card2 are calculated they will be between 1 and 52 so your indexes for Image1() need to be in the same range. This avoids having to do -1 somewhere to get the correct range (in fact you could just drop the +1 when calculating card1 and card2 instead of using -1).
Regards David R
Ok, I still had a little trouble with getting double cards, so I tried this and it seems to work as I test it; must do some more testing, but I think I'm ready to move on to the next task. Thanks so much. Here's the code I have: 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 ' Fill deck with cards in order 1 to 52 Cardcount = 0 Do While Cardcount <= 51 arrDeck(Cardcount) = Cardcount Cardcount = Cardcount + 1 Loop ' Shuffle the deck - end value (100) Counter = 0 For Counter = 0 To 100 Card1 = Int(52 * Rnd) Card2 = Int(52 * Rnd) If (Card1 <> Card2) Then Image1(0).Picture = Image1(Card1).Picture Image1(Card1).Picture = Image1(Card2).Picture Image1(Card2).Picture = Image1(0).Picture End If Next Counter ' Re-shuffle the deck Counter = 0 For Counter = 0 To 100 Card1 = Int(52 * Rnd) Card2 = Int(52 * Rnd) If (Card1 <> Card2) Then Image1(0).Picture = Image1(Card1).Picture Image1(Card1).Picture = Image1(Card2).Picture Image1(Card2).Picture = Image1(0).Picture End If Next Counter ' Now output images in order Counter = 0 For Counter = 0 To 51 Image1(Counter).Visible = True Next Counter End Sub
-
Ok, I still had a little trouble with getting double cards, so I tried this and it seems to work as I test it; must do some more testing, but I think I'm ready to move on to the next task. Thanks so much. Here's the code I have: 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 ' Fill deck with cards in order 1 to 52 Cardcount = 0 Do While Cardcount <= 51 arrDeck(Cardcount) = Cardcount Cardcount = Cardcount + 1 Loop ' Shuffle the deck - end value (100) Counter = 0 For Counter = 0 To 100 Card1 = Int(52 * Rnd) Card2 = Int(52 * Rnd) If (Card1 <> Card2) Then Image1(0).Picture = Image1(Card1).Picture Image1(Card1).Picture = Image1(Card2).Picture Image1(Card2).Picture = Image1(0).Picture End If Next Counter ' Re-shuffle the deck Counter = 0 For Counter = 0 To 100 Card1 = Int(52 * Rnd) Card2 = Int(52 * Rnd) If (Card1 <> Card2) Then Image1(0).Picture = Image1(Card1).Picture Image1(Card1).Picture = Image1(Card2).Picture Image1(Card2).Picture = Image1(0).Picture End If Next Counter ' Now output images in order Counter = 0 For Counter = 0 To 51 Image1(Counter).Visible = True Next Counter End Sub
Glad to hear you're making progress. Here's a version I knocked up that populates the Image1() array from files on disk. It also does the shuffle and (I think!) it works - just done some quick checking and can't see duplicates. It's got some constants in that I used to get spacing for the images. It also has hard coded file path to where I put the bitmap files. It's been a bit of fun going back to VB6 - I've been using C# for the last few years (with a little bit of VB.NET). If you decide to try VB.NET be warned it's only superficially the same as VB6 - it's really a very different language.
Option Explicit
Private Const OFFSET_X As Integer = 600
Private Const OFFSET_Y As Integer = 600
Private Const CARD_WIDTH As Integer = 1305
Private Const CARD_HEIGHT As Integer = 1425
Private Const COL_SPACE As Integer = 150
Private Const ROW_SPACE As Integer = 150'---------------------------------------------------------------------------------------
' Initialise the Image1() control array when the Form is loaded.
' Has to be done somewhere and this seems sensible place.
'---------------------------------------------------------------------------------------
Private Sub Form_Load()
' Need to fill the Image() control array with bitmap pictures of cards
' Assume that we have 52 bitmaps - Card_1.bmp to Card_52.bmp.
' This makes it possible to fill the control array in a loop
Dim left As Long
Dim top As Long
Dim fileroot As String
Dim i As Integer
Dim j As Integer
Dim index As Integerfileroot = "C:\VB6_Progs\Cards\Card_" 'This is where I store the bitmaps
SetImageZero (fileroot) 'Sets Image1(0) with dummy picture and makes it invisibleFor i = 0 To 12 'NOTE: Using 0-12 and 0-3 makes calculation of index simpler
top = i * (CARD_HEIGHT + COL_SPACE) + OFFSET_Y
For j = 0 To 3
left = j * (CARD_WIDTH + ROW_SPACE) + OFFSET_X
index = 4 * i + j + 1
Load Image1(index) 'Create the image control for the card
Image1(index).left = left
Image1(index).top = top
Image1(index).Picture = LoadPicture(fileroot & index & ".bmp") 'Set picture for Image1(i)
Image1(index).Visible = True
Next j
Next iEnd Sub
'--------------------------------------------------
' When the button is clicked, shuffle the cards
'--------------------------------------------------
Private Sub Start_Click()
ShuffleCardNumbers
End Sub'--------------------
-
Glad to hear you're making progress. Here's a version I knocked up that populates the Image1() array from files on disk. It also does the shuffle and (I think!) it works - just done some quick checking and can't see duplicates. It's got some constants in that I used to get spacing for the images. It also has hard coded file path to where I put the bitmap files. It's been a bit of fun going back to VB6 - I've been using C# for the last few years (with a little bit of VB.NET). If you decide to try VB.NET be warned it's only superficially the same as VB6 - it's really a very different language.
Option Explicit
Private Const OFFSET_X As Integer = 600
Private Const OFFSET_Y As Integer = 600
Private Const CARD_WIDTH As Integer = 1305
Private Const CARD_HEIGHT As Integer = 1425
Private Const COL_SPACE As Integer = 150
Private Const ROW_SPACE As Integer = 150'---------------------------------------------------------------------------------------
' Initialise the Image1() control array when the Form is loaded.
' Has to be done somewhere and this seems sensible place.
'---------------------------------------------------------------------------------------
Private Sub Form_Load()
' Need to fill the Image() control array with bitmap pictures of cards
' Assume that we have 52 bitmaps - Card_1.bmp to Card_52.bmp.
' This makes it possible to fill the control array in a loop
Dim left As Long
Dim top As Long
Dim fileroot As String
Dim i As Integer
Dim j As Integer
Dim index As Integerfileroot = "C:\VB6_Progs\Cards\Card_" 'This is where I store the bitmaps
SetImageZero (fileroot) 'Sets Image1(0) with dummy picture and makes it invisibleFor i = 0 To 12 'NOTE: Using 0-12 and 0-3 makes calculation of index simpler
top = i * (CARD_HEIGHT + COL_SPACE) + OFFSET_Y
For j = 0 To 3
left = j * (CARD_WIDTH + ROW_SPACE) + OFFSET_X
index = 4 * i + j + 1
Load Image1(index) 'Create the image control for the card
Image1(index).left = left
Image1(index).top = top
Image1(index).Picture = LoadPicture(fileroot & index & ".bmp") 'Set picture for Image1(i)
Image1(index).Visible = True
Next j
Next iEnd Sub
'--------------------------------------------------
' When the button is clicked, shuffle the cards
'--------------------------------------------------
Private Sub Start_Click()
ShuffleCardNumbers
End Sub'--------------------