Link to home
Start Free TrialLog in
Avatar of craigpike
craigpike

asked on

VBA Probably a simple solution??!! Please help

Hi Folks

I am reasonably new to VB and as a project i am trying to create a game of pairs! So far i have randomly generated 16 images on a form. Each image is covered by a CommandButton. When the button is clicked .visible = false and so the picture is revealed!

Now i am stuck! i don't know the code which I need to check to see if the two visible images on the screen are the same.

Just to confirm what i need, something like this

userform.visible.images = same then....!! (i know the syntax to the left is incorrect but i wanted to demo how i wanted to go about it)

Does anyone have any idea's?? Please

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What are you storing your picutres in (image control, picturebox, image list...), and how are you randomly displaying them?
rather than trying to compare the images, you need to think a bit more ABSTRACTLY.  The images are simply a representation of the item, to be compared.  Let's say for instance, that you have a 4 x 4 grid of items.  This is 16 total, or 8 pairs, represented by 8 pairs of images.  But the 8 pairs could just as easily be 8 pairs of NUMBERS, and still be REPRESENTED by the 8 pairs of images.

so a 2 dimensional array  Items(4,4) to hold the 8 pairs   now each button corresponds to one cell in the array, and you simply compare the values associated with the two buttons, so see if the SAME number is in both cells.  The IMAGE that is displayed on the screen is associated with the NUMBER in the corresponding cell in the array.

so you have 8 numbers (1,2,3,4,5,6,7,8), and 8 Images (img1,img2,img3,img4,img5,img6,img7,img8)

you place the 8 NUMBERS randomly in the array, so that you have 2 occurences on any one NUMBER, and then using that array, place the images in the corresponding positions on the screen. so if the NUMBER at Items(1,1) is 8, then the image placed in the UPPER LEFT corner of the screen would be img8, under the Button#1 - I would also use a Control array for the Buttons (so that ALL of the Buttons have the same NAME, but are indexed 0 to 15 - thus button(0) corresponds to Items(1,1), button(3) corresponds to Items(1,4), and button(15) coresponds to Items(4,4))

AW

AW

ASKER CERTIFIED SOLUTION
Avatar of akari2000
akari2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of craigpike
craigpike

ASKER

I am actually doing this project in works time so i am using VBA which use imagebox i believe.

The images are 100% random, as you can see below they are not tagged in any way. The code that i have used to generate the images is probably very inefficient, but it works!

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

20  X2 = Int((8 * Rnd) + 1) ' Fills image frame B with rand pic!
        Select Case X2
            Case Is = X1
                GoTo 20
            End Select
    PicString = "c:\pix\pic" & X2 & ".jpg"
    B.Picture = LoadPicture(PicString)

etc....
You have....

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

if you added...

A.tag = PicString
.
.
.
B.tag = PicString

Then you just have to compare the .tag properties of the two pictures in a simple "if ... = ... then"
so do I understand that you are picked ALL of the images randomly?  and howmany images are possible.  What do you do if there are THREE instances of the same image (this is a possibility with what you have now).  What do you do if there is NO pair (that is an Image that DOES NOT have another copy)?

what you should do is this:

1) set up a single dimensioned array (Vals(8)) to hold the values 1 through 8

then scramble this array (put the 8 values in random order) this is also called SHUUFLING (as in shuffling a deck of cards - which scrambles the 52 cards into a random order - you just have 8 cards to put into a random order)

that way you are sure that you will use ALL 8 images, and then you can also be sure that EVERY image is present EXACTLT twice - hence EVERY image will make a PAIR.

now you go through the Vals array one at a time, and place each value in two distinct locations in the Items array (or place each image in your ImageBox).

This approach will make your overall programming problem MUCH easier to deal with.

AW

if you used an "image list" to do what AW is recommending then you would make this simpler.

I would think, in the case of an image list, all you need to do is...

for i = 0 to x (number of image controls)
  select an image (doing that in order would be fine)
  do while j <= 2
    randomly pick an image control
      if there is no image designated
        load image
        increment j
      else
        randomly pick an image control
   loop
next

doing this until you have loaded all the images will allow you to control the amount of time you spend randomly picking image controls to load a picture into. picking more images than you have image controls means that you would only do the first loop for the number of times that you have image controls to handle images, in reverse you might need to kick out of the for if you have reached that point.

hope that works for you.
Thanks every one for your help, they are all great answers. I have learn't a lot from reading your articles.I am gonna have to digest some of these principles so I will be rewarding the points soon.

Thanks again, they are all very very helpfull so I will increase the points to 200

cheers
yeah, but who gets em? or are you going to give "assisted" solutions?

good luck
InTheWind, if you take ALL of the points and about $4.50 US, then you can go to Starbucks and get a Medium Mocha Latte!

If it's that important to you, then you can have mine, becuase I ca't use them for anything 'meaningful'

AW
haha

you're so right... and you need them so badly so you would feel that way of course! ;-)

Cheers... I mean, slurp.
Sorry, I have been STUPID and allocated the points to the wrong person. No offence akari2000 but i ment to give the points yo AW. Does any one know if this can be rectified??!!

Sorry AW
he said he didn't care about the points, so I wouldn't worry about it.
i appreciate the fact that I *should* hae gotten the points (though, as I indicated, they aren't worth much, so no big deal).

After all, with all those points, can I retire?  Can I buy a new car? Can I even buy a small bag of French Fries at McDonald's .  I think not.  Bit it si the appreciation of 'peers' that IS a big deal.

AW
exactly and I thought he was being very nice by offering the points to someone... I thought that your reply to me was a little bit harsh considering his kind offer to increase the points. I was just trying to make the point that he had to actually make the offer into an "accepted answer"...

Enjoy your new "digital car" ;-)

TTFN