Creating a Memory Game in Visual Basic 2010

Answered Creating a Memory Game in Visual Basic 2010

  • 2012年4月16日 14:55
     
     

    Hello,

    I am a noob to Visual Basic and I am currently learning it at my college. I have a final project to do and I am having some trouble deciding on how to attack it. First off, I have done a lot of research and I cannot find my answer, so I am using this(post) as a last resort. The book my prof is having me use is outdated, its coding on vb6, and we are using vb2010...hence my problem. I am not looking for an answer in code, but more to be pointed in the right direction, or maybe some classes that I do not know about.

    I have to create the game of Memory, and my book tells me to use a control array....which is nice and easy to use, however, since I am using VB2010 that option is no longer an option to me. So my main question is, what can I use that is similar to a control array in VB2010? I did some research on an image list, but I don't think that is exactly what I can use considering I need to compare the two value to see if they match or not. 

    Can I use an array somehow? I need to be able to randomize the order of the images in the beginning of each game. So my code will look something like this:

    Randomize the 16 images

    Display all the images for 2-3 seconds

    Hide all images

    User chooses first image

    User chooses second image

    if images are equal

    hide both images

    ask user to choose a new photo

    else

    hide all photos

    ask user to choose a new photo

全部回复

  • 2012年4月16日 16:06
     
     已答复 包含代码

    Randomize the 16 images

    Display all the images for 2-3 seconds

    Hide all images

    User chooses first image

    User chooses second image

    if images are equal

    hide both images

    ask user to choose a new photo

    else

    hide all photos

    ask user to choose a new photo

    Sounds like a fun project. Here's some help:

    1. Use the random class and loop through it 16 times to get the random images.
    2. For each random image, assign it to a picturebox or control of your choice.
    3. See code:
      For Each pBox As PictureBox In Me.Controls
           pBox.Visible = False
      Next
    4. Add an event handler for each picturebox to check which PictureBox was clicked by the user. Get the random image that is stored in that picturebox and assign it to a variable.
    5. Check if the image in the second picturebox matches the variable. If it does, then the user must have picked the same "images". If not, then they did not.

    Hopefully this will help you as a starting place. If you need more help, feel free to reply back.

    Best regards,

    - Jordan


    Jordan St. Godard | Microsoft® Community Contributor 2011

    double twoCents = .02;
    Console.WriteLine("$" + twoCents.ToString());

  • 2012年4月16日 16:14
     
     已答复 包含代码

    Duck,

    First of all, if you're studying VB6 - now deprecated - you'll be confused at every turn. Throw that book away!

    As Jordan showed, there's a bunch of ways to go about doing anything with VB Net, and following is just one way to get you started (if you want to do it this way). It's not finished - see if you can take this and complete it.

    I started by putting sixteen pictureboxes on the form (note that all of them are set to "zoom" in properties):

    As for going about getting started:

    Option Strict On Option Explicit On Public Class Form1 Public rand As New Random Public Structure PictureLocation Public ImageFilePath As String Public PictureBox1_Name As String Public PictureBox2_Name As String End Structure Private pictureLocationList As New List(Of PictureLocation) Private pbNamesList As New List(Of String) Private imagePathToUseList As New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load btn_Hide.Enabled = False Dim imgFolder As String = My.Computer.FileSystem.SpecialDirectories.Desktop & _ "\106_1120" Dim imgPathList As New List(Of String) For Each img As String In My.Computer.FileSystem.GetFiles(imgFolder, _ FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") imgPathList.Add(img) Next ' Choose 8 images at random from the list of image path names For i As Integer = 1 To 8 Dim imgPath As String = imgPathList(rand.Next(0, imgPathList.Count)) Do While imagePathToUseList.Contains(imgPath) imgPath = imgPathList(rand.Next(0, imgPathList.Count)) Loop imagePathToUseList.Add(imgPath) Next ' Fill the list of picturebox names For i As Integer = 1 To 16 Dim pbName As String = "PictureBox" & i.ToString pbNamesList.Add(pbName) Next ' Construct a random selection of one image and two pictureboxes, ' then store that as a New PictureLocation in the list of ' PictureLocations (pictureLocationList declared at the top) For i As Integer = 1 To 8 Dim imgPath As String = imagePathToUseList(i - 1) Dim p1 As Integer = rand.Next(0, pbNamesList.Count) Dim pbFirstName As String = pbNamesList(p1) pbNamesList.RemoveAt(p1) Dim p2 As Integer = rand.Next(0, pbNamesList.Count) Dim pbSecondName As String = pbNamesList(p2) pbNamesList.RemoveAt(p2) Dim thisLocation As New PictureLocation thisLocation.ImageFilePath = imgPath thisLocation.PictureBox1_Name = pbFirstName thisLocation.PictureBox2_Name = pbSecondName pictureLocationList.Add(thisLocation) Next End Sub Private Sub btn_Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn_Show.Click With btn_Show .Enabled = False .Refresh() End With For Each pbl As PictureLocation In pictureLocationList For Each ctl As Control In Me.Controls If TypeOf ctl Is PictureBox Then Dim pb As PictureBox = DirectCast(ctl, PictureBox) If ctl.Name = pbl.PictureBox1_Name OrElse ctl.Name = _ pbl.PictureBox2_Name Then pb.ImageLocation = pbl.ImageFilePath pb.Visible = True End If End If Next Next btn_Hide.Enabled = True End Sub Private Sub btn_Hide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn_Hide.Click With btn_Hide .Enabled = False .Refresh() End With For Each ctl As Control In Me.Controls If TypeOf ctl Is PictureBox Then Dim pb As PictureBox = DirectCast(ctl, PictureBox) pb.Visible = False End If Next btn_Show.Enabled = True End Sub End Class

    ...and finally, what it looks like running:

    This is, of course, my interpretation of what you're wanting. If there are sixteen pictureboxes then there should only be eight - as two will appear on the form at one time. I assume the object is to remember where the missing "pair" is?

    For what it's worth - I hope it helps. :)


    Please call me Frank :)

  • 2012年4月16日 16:20
     
     

    1) I understand the random class, how can I apply that to assign an image to pBox1, pBox2 and so on?

    3) Could you elaborate one what this for statment does? Or where I can read up on "In Me.Controls" <--- that is the only part i dont understand.

  • 2012年4月16日 16:24
     
     

    I wish I could throw that book away....but my prof is determined to have use do assignments out of it(he is a great prof, so I can't complain). I am not sure why, the class is on VB2010. I am using Visual Studio Express 2010.

    @Frank, to be honest, this is a bit over my head. I just spent some time trying to understand this codeand i kind of got overwhelmed. I guess I should start by asking where I am stumped. Is there anything like(or as simple) to use like control array(that can be used in vb6) in 2010?

  • 2012年4月16日 16:51
     
     

    Regarding the controls: Use a TableLayoutPanel with 4 rows and 4 columns. Put a Picturebox with dock=fill in every cell.



    Armin

  • 2012年4月16日 16:58
     
     

    1) I understand the random class, how can I apply that to assign an image to pBox1, pBox2 and so on?

    3) Could you elaborate one what this for statment does? Or where I can read up on "In Me.Controls" <--- that is the only part i dont understand.

    The Me keyword behaves like either an object variable or a structure variable referring to the current instance. Using Me is particularly useful for passing information about the currently executing instance of a class or structure to a procedure in another class, structure, or module. For example, suppose you have the following procedure in a module.

    • "For Each pBox As PictureBox in Me.Controls" is declaring pBox as a pictureBox and searching for any pictureBox within the classes controls (Me.Controls).

    Jordan St. Godard | Microsoft® Community Contributor 2011

    double twoCents = .02;
    Console.WriteLine("$" + twoCents.ToString());

  • 2012年4月16日 21:13
     
     

    I have to create the game of Memory, and my book tells me to use a control array....which is nice and easy to use, however, since I am using VB2010 that option is no longer an option to me.

    The control array is available in .Net, in several different forms.   One that you can use directly is the Controls collection of the form.  The problem with that control array is that it includes all controls, and they are in no particular order.  The other option is to create one for yourself

      Dim Pictureboxes as List(Of Picturebox) = New List(Of Picturebox)
      PictureBoxes.add(PB1)
      PictureBoxes.Add(PB2)
      ' etc.

    Then use it just like you would use any other list of objects.   You can also create a rectangular array using similar code with an array instead of a list, but in your case you are only interested in identfying the pictureboxes using an index - the actual game play will take place in some virtual structure (perhaps a rectangular array) that contains objects that include all the information you need, such as which images are pairs and whch have already been matched.