Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, I have a Visual Basic program running the memory game. it basically will let the user click on 2 cards, and then they flip, if they're the same, they disappear, if not, they turn back. So my program will look like there are 3 pairs of images under the cards, I already code the cards to disappear when the user clicks on them. but there is a problem, the hidden image will stay the same each match, so I have an idea that when the 2 cards cover the 2 same images disappear when we click on them, the 2 images will disappear too, and of course if not, they turn back. This is the easiest way I can figure out to make the memory game and I don't know how to code to make this. please help this program worth half my score, and I'm learning in high school so I only use the basic statement like if, case, do-while, or do until, etc...And I'm using Visual Basic 2013 if it helps.

What I have tried:

I already code the cards to disappear when the user clicks on them.
Posted
Updated 5-Jan-23 1:41am

1 solution

Without your code we're pretty limited in what we can tell you - and your description is not very clear.

The way I'd do it is to have a Card class (based on a UserControl) that knows it's Value and has two Image properties: Front and Back.
When I clicked it, it would raise a "Clicked" event if it was showing teh Back image and do no more.

Then I'd have a PlaySpace class which contains multiple Card instances, and which handles the "Clicked" event for them all. It knows how many cards have been revealed, and when it handles the event it would check that count:
0 : instruct that Card to show the Front face, increment revealed, save the Card as Card1
1 : instruct that Card to show the Front face, increment revealed, save the Card as Card1, start a two second timeout.
2 or greater : do nothing.

When the timeout elapsed, check if the Cards are the same Value
If they are, remove them from the PlaySpace and check if there are any left. End of game if not.
Otherwise, instruct both cards to show the Back image.
In either case, reset revealed

This way, you are letting the Cards be responsible for themselves, and the Playspace be responsible telling cards what to do and playing the actual game.

Have a think about it, and I think you'll see what I mean.
 
Share this answer
 
Comments
pham thao 2022 5-Jan-23 13:28pm    
Hi, I read through your solution, and I already thought about it before. but I don't know how to set the value of the cards and that is why I do this way. I already use the tag property to check if the cards are the same or not, but something was wrong and it didn't work out. Can you tell me how to set the value for the cards?
Ralf Meier 5-Jan-23 13:43pm    
The Description from Griff for me is quiet clear - but it seams not for you ...
I suggest you provide some of your code because without it it's complete senseless to give more advises.
I suggest also that you read the solution from Griff once more and this time more carefully ...
pham thao 2022 5-Jan-23 13:51pm    
Hi, thank you for your response, I'm still reading Griff's response to seeing if anything works out for me. But anyway this is the code I have done, as you see I compare 2 cards' tags to check if the cards are the same or not. but it didn't work. Can you see why?
'Private Sub Easy_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Declare Variable
Dim imgCardback(5) As PictureBox
Dim imgCardfront(5) As PictureBox
Dim intX As Integer
Dim intY As Integer

'Initialize variables
imgCardback(0) = imgJHeart
imgCardback(1) = imgJHeart2
imgCardback(2) = imgKHeart
imgCardback(3) = imgKHeart2
imgCardback(4) = imgQHeart
imgCardback(5) = imgQHeart2

imgCardfront(0) = imgCard1
imgCardfront(1) = imgCard2
imgCardfront(2) = imgcard3
imgCardfront(3) = imgcard4
imgCardfront(4) = imgcard5
imgCardfront(5) = imgcard6

'Check images
For intX = 0 To 5
If imgCardfront(intX).Visible = False Then
For intY = 0 To 5
If imgCardfront(intY).Visible = False Then
If intX <> intY And imgCardback(intX).Tag = imgCardback(intY).Tag Then
imgCardback(intX).Visible = False
imgCardback(intY).Visible = False
imgCardfront(intX).Visible = False
imgCardfront(intY).Visible = False
End If
If intX <> intY And imgCardback(intX).Tag <> imgCardback(intY).Tag And imgCardback(intX).Visible = True And imgCardback(intY).Visible = True Then
imgCardfront(intX).Visible = True
imgCardfront(intY).Visible = True
End If
End If
Next
End If
Next
End Sub

Private Sub imgCard1_Click(sender As Object, e As EventArgs) Handles imgCard1.Click
imgCard1.Visible = False
End Sub

Private Sub imgCard2_Click(sender As Object, e As EventArgs) Handles imgCard2.Click
imgCard2.Visible = False
End Sub

Private Sub imgcard3_Click(sender As Object, e As EventArgs) Handles imgcard3.Click
imgcard3.Visible = False
End Sub

Private Sub imgcard4_Click(sender As Object, e As EventArgs) Handles imgcard4.Click
imgcard4.Visible = False
End Sub

Private Sub imgcard5_Click(sender As Object, e As EventArgs) Handles imgcard5.Click
imgcard5.Visible = False
End Sub

Private Sub imgcard6_Click(sender As Object, e As EventArgs) Handles imgcard6.Click
imgcard6.Visible = False
End Sub
End Class
Ralf Meier 6-Jan-23 13:22pm    
Sorry - this code doesn't help ... :(
What I would do :
- create your owh (customized) Picturebox which derives from Picturebox and has some additional Properties as :
- Back-Image (which as to be seen when the Picturebox is not activated)
- Active-Image (which is to be seen when the Picturebox was activated)
- Active-Image-Code (same Images has the same code)
- Status (tell you that the Picturebox was activated)
and also additional Methods as :
- Change_to_Active_Image (when Clicked change the Image from Background to Active)
- Change_to_Back_Image (when called change the Image from Active to Background)
- now assign the Images (randomly) to those Pictureboxes and give them a code for identifying
- if you click the 1st PB memorise it and it's code
- when you click the 2nd PB memorise it and it's code
- compare the both codes - if they are matching switch them to unvisible
- if they are not matching change both to the Backimage
- the game is over when no PB is visible
pham thao 2022 9-Jan-23 5:27am    
sorry, I'm new to vb but what do you mean by memorizing it? does vb know how to memorize all the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900