7 Replies - 452 Views - Last Post: 05 February 2012 - 02:27 AM Rate Topic: -----

Topic Sponsor:

#1 infinitum3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-February 12

random picture selection without repeats

Posted 03 February 2012 - 07:38 AM

Hi. I'm new to dreamInCode so I apologize if this has been asked before.

I have a Form (frmcards) with six Images on it. Each Image has a different picture set into it. I also have a Form (frmMain) with three 'blank' Images, and a Label (Label1) and a Command Button (Command1).

When I click the Command Button, a random number is generated as the Label1.Caption. It does this three times and assigned a picture from frmCards to each of the three 'blank' Images. This works fine, however, it sometimes uses the same picture twice (there are only six to choose from, afterall.) I think I need to use a For/Next Loop to prevent the duplication of the random number, but I really don't know how. Any suggestions?

Thanks!
-I3D


Option Explicit


Private Sub Command1_Click()

    Randomize
  
        Label1.Caption = Round((6 - 1 + 1) * Rnd + 1)
    
    
            Select Case Label1.Caption
 
                Case 1
                imgChoice1.Picture = frmCards.imgRedOne.Picture
 
                Case 2
                imgChoice1.Picture = frmCards.imgBlueTwo.Picture
 
                Case 3
                imgChoice1.Picture = frmCards.imgGreenThree.Picture

                Case 4
                imgChoice1.Picture = frmCards.imgHeroGold.Picture

                Case 5
                imgChoice1.Picture = frmCards.imgHeroGray.Picture

                Case 6
                imgChoice1.Picture = frmCards.imgHeroBlue.Picture

            End Select
    
Randomize
  
        Label1.Caption = Round((6 - 1 + 1) * Rnd + 1)
    
    
            Select Case Label1.Caption
 
                Case 1
                imgChoice2.Picture = frmCards.imgRedOne.Picture
 
                Case 2
                imgChoice2.Picture = frmCards.imgBlueTwo.Picture
 
                Case 3
                imgChoice2.Picture = frmCards.imgGreenThree.Picture

                Case 4
                imgChoice2.Picture = frmCards.imgHeroGold.Picture

                Case 5
                imgChoice2.Picture = frmCards.imgHeroGray.Picture

                Case 6
                imgChoice2.Picture = frmCards.imgHeroBlue.Picture

            End Select
            
Randomize
  
        Label1.Caption = Round((6 - 1 + 1) * Rnd + 1)
    
    
            Select Case Label1.Caption
 
                Case 1
                imgChoice3.Picture = frmCards.imgRedOne.Picture
                
                Case 2
                imgChoice3.Picture = frmCards.imgBlueTwo.Picture
 
                Case 3
                imgChoice3.Picture = frmCards.imgGreenThree.Picture

                Case 4
                imgChoice3.Picture = frmCards.imgHeroGold.Picture

                Case 5
                imgChoice3.Picture = frmCards.imgHeroGray.Picture

                Case 6
                imgChoice3.Picture = frmCards.imgHeroBlue.Picture

            End Select
End Sub







Is This A Good Question/Topic? 0
  • +

Replies To: random picture selection without repeats

#2 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,896
  • Joined: 02-June 10

Re: random picture selection without repeats

Posted 03 February 2012 - 07:54 AM

First I would urge you to rethink what you are doing so far. Can you image what this would be like if you wanted 250 images? What you have here really isn't maintainable for the long haul. If you're picture cards were part of an array then you could directly access a picture by the random number selected.

imgChoice1.Picture = myPictureList[RandomNumberSelected];

That's it. No huge switch constructs, no hard coding of references from another class/form (pure evil by the way).


As to the no repeats... What would you do on paper? You'd make a list of all the available selections, and cross out the ones already picked.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Do the same thing in code... sort of.

Make a List<int> that contains all the available picture.

[0] = 1
[1] = 2
[2] = 3
...
As you randomly select an element from the list, use it, then take it out of the list

imgChoice1.Picture = myPictureList[RandomNumberSelected];
AvailablePicturesList.RemoveAt[RandomNumberSelected];


Programming is just puzzle solving. So many times if you think about how you would do it in the physical world (pen and paper for example) that will show you the logic you need. Then just code that.

The way that you have hardcoded your two forms so tightly really concerns me. This really is just bad, bad, bad. It is a habit you need to break TODAY.

Please take a look at these tutorials and see how different forms can interact without that level of knowledge of each other. You want your different forms to be black boxes that barely perceive each other's existance. They certainly can't know the intimate details of each other's inner guts.


Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Was This Post Helpful? 0
  • +
  • -

#3 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: random picture selection without repeats

Posted 03 February 2012 - 08:02 AM

Albeit, tlhIn`toq's examples are .Net based, the concepts and logic described is equally valid.

Just a note, if you're starting as a new programmer, unless you have been told to use VB6 by a tutor for your course, or you have some other overwhelming desire to use VB6, I would really consider channelling your enthusiasm of learning into a newer programming language such VB.Net or C#.Net.
Was This Post Helpful? 0
  • +
  • -

#4 infinitum3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-February 12

Re: random picture selection without repeats

Posted 03 February 2012 - 02:04 PM

@'toq- I whole heartedly agree that Arrays would be better for this. And while I understand the concept of a 2 dimensional and 3 dimensional array, I don't understand how to assign the numbers to the array, nor can I withdraw the information from the array. Imagine I'm a 6-year old. How would you explain using Arrays?

@Maj-
I have no formal training in programming. I'm a hobbyist. 15 years ago I learned a little VB4 on my own. I made a few simple apps to convert Farenheit to Celcius and Meters to Miles. Then I programmed a PONG clone, then Space Invaders. I was hooked.

But I never learned anything about real programming. I don't expect to code the next Skyrim or GTA. I just want to make a simple game now and then. Angry Birds and Reversi are fine by me =) It would take FAR too long to learn even the simplest game programming techniques in C#, and I just don't have the time. I'm old, married, kids in college, fulltime job and mortgage. I'll stick with simple and fun. Thanks for the advice. I do understand.
Was This Post Helpful? 0
  • +
  • -

#5 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 265
  • Joined: 21-April 09

Re: random picture selection without repeats

Posted 03 February 2012 - 02:37 PM

Create an integer or boolean array PicUsed(6)
When you start a round of game play initialize the array to zero.
When you generate a random number to select one of the pictures
check the array element to see if it has already been used.
Then assign that element a one to indicate it was picked.
The next random number is checked against the array and if
it is a one then it has already been used so you generate
another random number.

Put Option Explicit at the top of every form 'never code without it
rem make an array of 3 pictureboxes (or image)
Dim PicUsed(6) as integer 'or could be Boolean
dim X as integer
dim I as integer
for I=1 to 3 
  picused(i)=0  'initialize array to zero
next
for I=1 to 3
PickAgain:
  X=int(rnd*5)+1
  if picused(x)=1 then goto PickAgain
  picused(x)=1
  picturebox(I)=loadpicture(your_picture(x))
next I

Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: random picture selection without repeats

Posted 04 February 2012 - 01:36 AM

View Postinfinitum3d, on 03 February 2012 - 09:04 PM, said:

@Maj-
I have no formal training in programming. I'm a hobbyist. 15 years ago I learned a little VB4 on my own. I made a few simple apps to convert Farenheit to Celcius and Meters to Miles. Then I programmed a PONG clone, then Space Invaders. I was hooked.

But I never learned anything about real programming. I don't expect to code the next Skyrim or GTA. I just want to make a simple game now and then. Angry Birds and Reversi are fine by me =) It would take FAR too long to learn even the simplest game programming techniques in C#, and I just don't have the time. I'm old, married, kids in college, fulltime job and mortgage. I'll stick with simple and fun. Thanks for the advice. I do understand.


Hey mate, I wasn't trying to belittle you in anyway, and as for the personal circumstances, I'm there (minus the wife!), so I know exactly where you're at. VB.Net shouldn't be too much of a step for you (in my opinion) for your level of programming, you might even surprise yourself.

Good luck either way.
Was This Post Helpful? 0
  • +
  • -

#7 infinitum3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-February 12

Re: random picture selection without repeats

Posted 04 February 2012 - 06:48 PM

@Rhymer- Thanks! I'll try that out as soon as I can.

@maj- Thanks for the advice. After sleeping on it, I decided to check out VB.net 2010 ee. After running through a tutorial for a picture viewer, I realized it's a natural progression from VB6, so I think I'll stick with VB.net.

Also, I can do the same tutorial for C#.net, so maybe I'll broaden my horizons and try C# out too.

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#8 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: random picture selection without repeats

Posted 05 February 2012 - 02:27 AM

No problem, I've been through the same learning curve with C# (and still going through it as I don't use it everyday).

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1