locked
Random RRS feed

  • Question

  • Hello, I'm a student and our teacher gave us an assignment without really explaining anything. He asked us to create a memory game in visual basic. I must use 16 PictureBox (4 x 4), and each one is assigned an image (8 pairs of images). If the user clicks on two same images, then they stay and he can continue, but if he clicks on two different images, they get hidden back. There must be a message at the end saying that the user won if he unhid all the boxes. Also, when the game starts back the PictureBoxes must randomly change places. We just started using this program about a week ago so can you please help me I only know how to assign the images to each picture box. Keep in mind I'm a beginner so my code has to be simple. Thank you very much.




    • Edited by Peanut123 Thursday, March 3, 2016 3:07 PM
    Saturday, February 13, 2016 11:25 PM

Answers

  • Welcome to the forum.  Please be aware that we aren't here to give you code to do your homework.  But we can help you with any problems you have with code you have written yourself.

    This may help to get you started.

    After you have designed your user interface:

    Declare a list to hold your 16 pictureboxes and another list to hold your 8 images.

    Add the pictureboxes and the images to the lists.

    Choose an image at random from the list of images.  Once chosen remove that image from the list.

    Then choose two pictureboxes at random from the list of pictureboxes.  Remove the chosen pictureboxes from the list.

    Assign the chosen image to both pictureboxes.

    Continue in a loop until all pictureboxes have images and the lists are empty.

    Start the game by letting the player click on a picturebox.  When he does display the image.  Then when he clicks on a second picturebox show that image and if they don't match hide them both after a short delay.  If they do match then leave them showing.

    Repeat the above in a loop until all pictureboxes are displaying images.  Game over.

    Try writing some code to do the above and post back with any problems you have.

    Sunday, February 14, 2016 12:07 AM
  • If the user clicks on two same images, then they stay and he can continue, but if he clicks on two different images, they get hidden back.

    The particular problem you are going to run into, and probably the reason for this sort of assignment, is knowing which image the user clicked.  It's easy to know which Picturebox is clicked, but since the images are assigned to the picture boxes randomly, unless you have somehow kept track of what went where, you won't know whether the images match or not.

    Probably the simplest way to solve this is to use the tag property of the picture box.
    https://msdn.microsoft.com/en-au/library/system.windows.forms.control.tag%28v=vs.110%29.aspx

    Note that this is an Object - it needs to be cast to/from an Integer to use it like this.

    When you assign an image to a pair of picture boxes, update their tag properties with the index number for the image that was randomly selected from your list of images for these two picture boxes.

    Then, when the user has selected two picture boxes, simply compare their tag properties to determine whether or not it was a match.

    The other part that is a bit complex is knowing, when the user clicks in a picture box, whether this is a first selection or a second selection.  The code for comparing is obviously only going to execute on the second selection.  So you will need a variable to keep track of this, and decide whether this is a first click, and all you need to do is show the image and note what got clicked, or it's the second click, and you need to show the image, check whether the images match, and process either a match or fail.

    Sunday, February 14, 2016 1:16 AM

All replies

  • Hello, I'm a student and our teacher gave us an assignment without really explaining anything. He asked us to create a memory game in visual basic. I must use 16 PictureBox (4 x 4), and each one is assigned an image (8 pairs of images). If the user clicks on two same images, then they stay and he can continue, but if he clicks on two different images, they get hidden back. There must be a message at the end saying that the user won if he unhid all the boxes. Also, when the game starts back the PictureBoxes must randomly change places. We just started using this program about a week ago so can you please help me I only know how to assign the images to each picture box. Keep in mind I'm a beginner so my code has to be simple. Thank you very much.

    Given that this is homework, it's difficult to give you any help without outright providing an answer.

    You might consider starting with a TableLayoutPanel for your "grid", then work through the logic once you have that. If you get stuck on some part of it, explain it in a question here and post the relevant code.

    For what it's worth...


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Saturday, February 13, 2016 11:57 PM
  • Welcome to the forum.  Please be aware that we aren't here to give you code to do your homework.  But we can help you with any problems you have with code you have written yourself.

    This may help to get you started.

    After you have designed your user interface:

    Declare a list to hold your 16 pictureboxes and another list to hold your 8 images.

    Add the pictureboxes and the images to the lists.

    Choose an image at random from the list of images.  Once chosen remove that image from the list.

    Then choose two pictureboxes at random from the list of pictureboxes.  Remove the chosen pictureboxes from the list.

    Assign the chosen image to both pictureboxes.

    Continue in a loop until all pictureboxes have images and the lists are empty.

    Start the game by letting the player click on a picturebox.  When he does display the image.  Then when he clicks on a second picturebox show that image and if they don't match hide them both after a short delay.  If they do match then leave them showing.

    Repeat the above in a loop until all pictureboxes are displaying images.  Game over.

    Try writing some code to do the above and post back with any problems you have.

    Sunday, February 14, 2016 12:07 AM
  • Dave,

    Great answer. :)


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Sunday, February 14, 2016 12:25 AM
  • Thank you very much to all of you I'm pretty lost but I will try to do what you said...


    • Edited by Peanut123 Thursday, March 3, 2016 2:51 AM
    Sunday, February 14, 2016 1:12 AM
  • Ok I am now trying to do as you said but have a few questions... How do you declare a list? How do you choose an image at random?

    Sunday, February 14, 2016 1:14 AM
  • If the user clicks on two same images, then they stay and he can continue, but if he clicks on two different images, they get hidden back.

    The particular problem you are going to run into, and probably the reason for this sort of assignment, is knowing which image the user clicked.  It's easy to know which Picturebox is clicked, but since the images are assigned to the picture boxes randomly, unless you have somehow kept track of what went where, you won't know whether the images match or not.

    Probably the simplest way to solve this is to use the tag property of the picture box.
    https://msdn.microsoft.com/en-au/library/system.windows.forms.control.tag%28v=vs.110%29.aspx

    Note that this is an Object - it needs to be cast to/from an Integer to use it like this.

    When you assign an image to a pair of picture boxes, update their tag properties with the index number for the image that was randomly selected from your list of images for these two picture boxes.

    Then, when the user has selected two picture boxes, simply compare their tag properties to determine whether or not it was a match.

    The other part that is a bit complex is knowing, when the user clicks in a picture box, whether this is a first selection or a second selection.  The code for comparing is obviously only going to execute on the second selection.  So you will need a variable to keep track of this, and decide whether this is a first click, and all you need to do is show the image and note what got clicked, or it's the second click, and you need to show the image, check whether the images match, and process either a match or fail.

    Sunday, February 14, 2016 1:16 AM
  • PB,

    Lots of good information here - I hope you're getting a lot of ideas from this. :)


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Sunday, February 14, 2016 1:23 AM
  • I'm very lost I have no idea how to do anything of this all I've done up to now is a very simple calculator... And our teacher helped us for it...
    Sunday, February 14, 2016 1:43 AM
  • I'm very lost I have no idea how to do anything of this all I've done up to now is a very simple calculator... And our teacher helped us for it...

    Declaring Lists is really easy:

      Dim Images as New List(Of Image)
      Dim Boxes as New List(Of PictureBox)

    Then use the Add method to append each item onto the end of the list.  It grows as required. Exactly what you add to your Images list depends on where your images are coming from - a file is easiest, although resources are a bit tidier.

    Don't worry about any user input yet.  Start with creating your lists, and the first tricky bit after that will be generating the random number to pluck a random image from the list and put it into the pictureboxes.

    Use the 'Insert Code Block' feature in the tool bar at the top of your message to insert code into your posts- that way it stays readable.

    Sunday, February 14, 2016 2:05 AM
  • Public Class Form1
        Dim Images As NewList (PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11, PictureBox12, PictureBox13, PictureBox14, PictureBox15, PictureBox16)
        Dim Boxes As New NewList(pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8)
    
    End Class

    Sorry here it is
    Sunday, February 14, 2016 2:21 AM
  •    
       If (numero = 1) Then PB1.Visible = False

    I also have this.

    • Edited by Peanut123 Thursday, March 3, 2016 1:22 PM
    Sunday, February 14, 2016 2:23 AM
  • Hi can you please be more specific I have no idea how to write codes all I know now is how to create a list.
    Sunday, February 14, 2016 2:29 AM
  • I know it's pretty late and it's saturday but i'm determined...
    • Edited by Peanut123 Thursday, March 3, 2016 1:23 PM
    Sunday, February 14, 2016 2:32 AM
  • Sorry here it is

    Use the resources available to you:

    Collections:
    https://msdn.microsoft.com/en-us/library/ybcx56wz.aspx

    Lists:
    https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx

    Adding:
    https://msdn.microsoft.com/en-us/library/3wcytfd1(v=vs.110).aspx

    (I was expecting that Boxes would be your list of picture boxes, and Images would be your list of images).

    Sunday, February 14, 2016 3:05 AM
  • Public Class Form1
        Dim Images As NewList (PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11, PictureBox12, PictureBox13, PictureBox14, PictureBox15, PictureBox16)
        Dim Boxes As New NewList(pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8)
    
    End Class

    Sorry here it is

    To get started simply, this is a way to accomplish what you showed above (even though it seems backwards)

            Dim images As New List(Of PictureBox)
            images.Add(PictureBox1)
            images.Add(PictureBox2)
    

    and so on for all the PictureBoxes. There are fancier ways to do that but this I think is a good start and is easier to understand. Besides, your teacher would spot anything fancy in a hurry.


     
    Sunday, February 14, 2016 5:04 AM
  • Peanut,

    Use your lists now instead of all those ifs.

       boxes(numero).visible = false

    then you can use the indexes of the lists instead of looping through the controls.

    Sunday, February 14, 2016 2:07 PM
  • Revisting my original suggestion it isn't necessary to pick images randomly and pick boxes randomly.  Simply going through the image list in order and assigning to two boxes at random will suffice.

    So in pseudo code:

    For Each Image in the list of images
        Pick a Box at random from the list
        Assign the image to the box
        Remove that box from the list(so it doesn't get chosen twice)    
        Pick another box at random
        Assign the image to the box
        Remove that box from the list

    If you assign the images to the Image property of the PictureBoxes then obviously all the images will be visible at the start which rather defeats the object of the game.  So you could use the Tag property to store the images and only display them when the box is clicked.

    Try coding the routine above and post back.  Initially assign the images to the Image properties of the boxes, that will let you check that the routine is working as you will see the images.  Once you are happy that it is working change it so that the image is stored in the Tag property instead.

    Study the help for the Random class Next method carefully - take note of which parameters are inclusive and which are exclusive.


    • Edited by Dave299 Sunday, February 14, 2016 2:32 PM
    Sunday, February 14, 2016 2:22 PM
  • I think you should have started sooner?

    Here is a working example to get you going with some of the basic tools you need.

    There are many ways to do this. I am just showing some of the possible ways.

    It is up to you to decide what parts you need to use and know how to use. Then you need to continue with the rest.

    Public Class Form3
        Private rand As New Random
        Private boxes As New List(Of PictureBox)
        Private SelectedIndex As Integer = -1
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            boxes.Add(PictureBox1)
            boxes.Add(PictureBox2)
            boxes.Add(PictureBox3)
            boxes.Add(PictureBox4)
    
            boxes(0).BackColor = Color.Red
            boxes(1).BackColor = Color.Green
            boxes(2).BackColor = Color.Red
            boxes(3).BackColor = Color.Blue
    
            HideBoxes()
    
        End Sub
    
        Private Sub HideBoxes()
            For Each box In boxes
                box.Visible = False
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SelectedIndex = rand.Next(0, 3)
            boxes(SelectedIndex).Visible = True
            Button1.Enabled = False
        End Sub
    
        Private Sub Form3_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            'draw outline around the box so they are seen when not visible
            For Each box In boxes
                e.Graphics.DrawRectangle(Pens.Black, box.Bounds)
            Next
        End Sub
    
        Private Sub Form3_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
            If SelectedIndex = -1 Then
                MsgBox("Click GO to Start")
            Else
                For i = 0 To boxes.Count - 1
                    If boxes(i).Bounds.Contains(e.X, e.Y) Then
                        boxes(i).Visible = True
    
                        If boxes(SelectedIndex).BackColor = boxes(i).BackColor Then
                            MsgBox("Correct. You Win!")
                        Else
                            MsgBox("Sorry. That is not Correct.")
                        End If
                        SelectedIndex = -1
                        HideBoxes()
                        Button1.Enabled = True
    
                    End If
                Next
            End If
        End Sub
    End Class

    Sunday, February 14, 2016 3:01 PM
  • Well I guess that's one way of getting round the issue that you can't click on an invisible box but really Tom.....
    Sunday, February 14, 2016 3:11 PM
  • Well I guess that's one way of getting round the issue that you can't click on an invisible box but really Tom.....

    Hi Dave!

    All right I'll bite. What is the problem if that is what you mean?

    Sunday, February 14, 2016 4:04 PM
  • Hi Tom

    No problem at all - I was just wondering what the teacher would think if something like that was handed in.

    I wonder where the OP has got to - he'll have to go some to get this finished by tomorrow.

    Sunday, February 14, 2016 4:55 PM
  • Hi Tom

    No problem at all - I was just wondering what the teacher would think if something like that was handed in.

    I wonder where the OP has got to - he'll have to go some to get this finished by tomorrow.

    I see. Well I was trying to make it so the OP could not use it as is for this exact assignment but showed the basic controls and events etc they seem to be leaving out?

    As someone said to me one time, some students need an example to follow.

    Of course some just turn in what we give them without understanding it and then ... busted!

    :)

    Sunday, February 14, 2016 5:14 PM
  • What does the -1 stand for ?
    Sunday, February 14, 2016 6:18 PM
  • What does the -1 stand for ?

    In my example it is just used in the selectedindex when nothing has been selected such as when the game first starts or when the user does not click a picturebox.

    When the user clicks the Go button then we give selectedindex the picturebox list index from 0 to 3 for the picturebox that was (picked at random).


    Sunday, February 14, 2016 6:39 PM
  • and also what is a selected index ? should i use one i was planning to use random but don't exactly understand what it is and if there's an actual difference.
    Sunday, February 14, 2016 8:42 PM
  • how can i check if the person won or not ?
    Sunday, February 14, 2016 8:57 PM
  • how can i check if the person won or not ?

    You haven't set up your picture boxes yet, so you cannot consider the actual gameplay.

    You have also been led somewhat astray in the setup.

    A picture box supports several images.  You use that feature to load each picture box with two images.  One is the default image, or the 'back' of the tile.   The other is the image the user needs to match.

    You haven't properly considered how you are going to keep track of which image is where - critical to the game play.

    To use the lists properly, your resources need to be named so that they can be referenced like the list index is referenced.  And you need to add the default image to your image resources.

    So start by setting up your resources correctly. This example assumes image resources 'BackImage.png' and 'Imagexx.png' where xx is 00 to 05, 10-15 and 20-25.  The reason for that odd numbering comes clear from the code.

    Set you form-level variables up at the start of the code:

    Option Strict On
    
    ''' <summary>
    ''' Memory game 
    ''' The game uses a 6x6 grid which hides 18 pairs of images. At each play the player
    ''' can reveal two images.  If the images match, they stay revealed.  If they do not
    ''' match then they are both hidden, and the next player plays.
    ''' </summary>
    ''' <remarks></remarks> 
    Public Class Concentrate
        'Constants to define the layout size.
        Const Wide As Integer = 80
        Const High As Integer = 80
    
        Dim Pics As New List(Of PictureBox)
        Dim Places(35) As Integer ' This array will define the position of each image
        Dim Rand As New Random()
    
    

    Now add the code to create and display your pictureboxes.  Position each one, and set its image properties to suit.  Also create a restart button, and do a restart (actually, a start, but that's the same).

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Create the 36 pictureboxes.
            For I = 0 To 5
                For j = 0 To 5
                    Dim P As New PictureBox
                    P.Visible = True
                    'Position and size for the new picturebox
                    P.Location = New Point(I * Wide, j * High)
                    P.Size = New Size(Wide, High)
                    P.SizeMode = PictureBoxSizeMode.StretchImage
                    'Set the tag to the image name
                    P.Tag = "Image" & (I \ 2).ToString & j.ToString
                    'Load the image to the picturebox InitialImage property
                    P.InitialImage = CType(My.Resources.ResourceManager.GetObject(P.Tag.ToString), Image)
                    'Set the default image
                    P.ErrorImage = CType(My.Resources.ResourceManager.GetObject("BackImage"), Image)
                    'Set the handler for the picture box click event
                    AddHandler P.Click, AddressOf PictureBox_Click
                    'Add the picture box to the form controls
                    Me.Controls.Add(P)
                    'Add the picturebox to the picturebox list.
                    Pics.Add(P)
                Next
            Next
            'Create the Restart button
            Dim B As Button = New Button
            B.Text = "Restart"
            B.Location = New Point(40, 6 * High)
            AddHandler B.Click, AddressOf Restart_Click
            Me.Controls.Add(B)
    
            'Randomize and display the images
            Restart()
        End Sub
    
        Private Sub Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Restart()
        End Sub

    To get that to compile you will need to comment out the restart call and add a dummy PictureBoxClick event handler.

        Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        End Sub

    The randomizing comes next. The Places array is used to determine which picture box goes where. The picturebox images are set to the default.

        Private Sub Restart()
            ' Reset the places index
            Dim Counter As Integer = 0
            ' Clear the Places array
            For I As Integer = 0 To 35
                Places(I) = -1
            Next I
            ' Randomise the Places array values. This routine places the values
            '  0 to 35 at random positions within the Places array.
            Do
                ' Create a random number in the range of empty places 
                Dim randomNumber As Integer = Rand.Next(0, Places.Length)
                '  If this number hasn't already been used, insert it here.
                If (Not Places.Contains(randomNumber)) Then
                    Places(Counter) = randomNumber
                    Counter += 1
                End If
            Loop While Counter < Places.Length
            ' Position the pictureboxes according to the Places array.
            For I = 0 To Places.Count - 1
                Dim X As Integer = (Places(I) Mod 6) * Wide
                Dim Y As Integer = (Places(I) \ 6) * High
                Pics(I).Location = New Point(X, Y)
                'Set the picturebox image to the error image
                Pics(I).Image = Pics(I).ErrorImage
            Next I
        End Sub

    Note the use of integer divide and Mod to get a 6x6 layout.

    Now you can write the picturebox click event handler.  Remember that you need to keep track of whether this is the first or second click - a Static variable is really useful for that. Use Static variables to remember the image of the first clicked picture box (from the Tag property) to compare to the image name from the second clicked picture box.  For now, just show a messagebox to indicate matched or not matched - that can be upgraded to a dialog with timing later.

    Sunday, February 14, 2016 9:43 PM
  • Peanut,

    You dont have any events. Windows is event driven. That means nothing happens until something happens. You are initializing your controls in form load, which is the event we can use that is executed when the application starts.

    So your form loads and appears on the screen but then nothing happens? Right?

    What is suppose to happen? Normally the user will do something and that is an event. In my example the user clicks the go button. Vb.net then executes the button click event which we can use to do the next step. In my example that is generate the random number and save it in our varaiable SelectedIndex. Then we know what the random number was later on.

    You need to make your other controls now and get a simple form laid out and start clicking buttons and looking at what happens. Make my example and put a stop in the button click. Step through the code one line at a time using the visual studio debugger tools set break look at the variables and other processes.

    Search and read the docs for each statement like random and random.next. Get familar with how the documents are laid out.

    Its all part of learning to program and it takes time and practice. There are many things to learn. Just try to go one step at a time. Get something working. Then add to it.

    Sunday, February 14, 2016 9:50 PM
  • Ok I've done this

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer, J As Integer, Compteur As Integer, Testpasse As Boolean
            Dim Tableau(16) As Integer
            For i = 1 To 16
                Tableau(i) = vbNull
            Next i
    
            Randomize()
            For i = 1 To 16
                MsgBox(i)
                Testpasse = False
                Do Until Testpasse
                    Tableau(i) = CInt(Int((8 * Rnd()) + 1))
                    MsgBox("Valeur teste" & Tableau(i))
                    Compteur = 0
                    For J = 1 To 16
                        MsgBox("J=" & J)
                        If Tableau(i) = Tableau(J) Then Compteur = Compteur + 1
                    Next J
                    If Compteur < 3 Then
                        Testpasse = True
                    Else
                        Testpasse = False
                    End If
                    MsgBox(Testpasse)
                Loop
            Next i
        End Sub

    Sunday, February 14, 2016 11:01 PM
  • Ok I've done this

    You are using out-of-date code. You have already been provided with several routines that provide the required random sequence. Your code produces 17 values, not the required 16, and includes one value only once (as it must do, if there is an odd number of elements in the array).  Mixing .Net and VB6 code will be very confusing. It is also much more complex than required.

    Sunday, February 14, 2016 11:48 PM
  • Have a look at this - it's a complete solution to your assignment (it assumes you have 8 images added to your resources named Pic1....Pic8), but needs (at least) two additions to make it work properly; a) it doesn't tell you when you have finished, b) it doesn't work if you click the same box twice.

    Both issues are easily fixed but to do that you'll have to work out how it works first.  Good luck.  If you hand it in remember your teacher may ask you how it works.

    Public Class Form1
       Private Boxes As New List(Of PictureBox)
       Private Images As New List(Of Image)
       Private Rnd As New Random
       Private FirstClick As Boolean
       Private WithEvents Tmr As New Timer
       Private FirstClickedBox As PictureBox
       Private SecondClickedBox As PictureBox
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          Dim PB As PictureBox
          SetClientSizeCore(800, 600)
          CenterToScreen()
          Tmr.Interval = 500
          For count As Integer = 1 To 16
             PB = New PictureBox With {.Size = New Size(200, 150), .Location = New Point(200 * ((count - 1) Mod 4), 150 * ((count - 1) \ 4)), .BackColor = Color.LightYellow, .SizeMode = PictureBoxSizeMode.Zoom, .BorderStyle = BorderStyle.FixedSingle}
             Boxes.Add(PB)
             Controls.Add(PB)
             AddHandler PB.Click, AddressOf PictureBoxClicked
          Next
          For count As Integer = 1 To 8
             Dim Img As Image = CType(My.Resources.ResourceManager.GetObject("Pic" & count.ToString), Image)
             Images.Add(Img)
          Next
          For Each Img As Image In Images
             PB = Boxes(Rnd.Next(0, Boxes.Count))
             PB.Tag = Img
             Boxes.Remove(PB)
             PB = Boxes(Rnd.Next(0, Boxes.Count))
             PB.Tag = Img
             Boxes.Remove(PB)
          Next
       End Sub
       Private Sub PictureBoxClicked(sender As Object, e As EventArgs)
          Dim PB As PictureBox = CType(sender, PictureBox)
          FirstClick = Not FirstClick
          PB.Image = CType(PB.Tag, Image)
          PB.Refresh()
          If FirstClick Then
             FirstClickedBox = PB
          Else
             SecondClickedBox = PB
             If FirstClickedBox.Image IsNot SecondClickedBox.Image Then
                Tmr.Start()
             End If
          End If
       End Sub
       Private Sub Tmr_Tick(sender As Object, e As System.EventArgs) Handles Tmr.Tick
          Tmr.Stop()
          FirstClickedBox.Image = Nothing
          SecondClickedBox.Image = Nothing
          FirstClickedBox.Refresh()
          SecondClickedBox.Refresh()
       End Sub
    End Class
    

    Sunday, February 14, 2016 11:56 PM
  • I don't understand why does my code produe 17 values and not 16?
    Sunday, February 14, 2016 11:57 PM
  • I'm working on visual basic by the way so I don't know what is VB6 or .NET.
    Sunday, February 14, 2016 11:58 PM
  • Is this code still ok then? Or should I restart?
    Monday, February 15, 2016 12:49 AM
  • I'm working on visual basic by the way so I don't know what is VB6 or .NET.

    VB 6 is Visual Basic version 6 which used what is now considered some old syntax,
    functions, etc.

    .NET is the .NET Framework which is what current versions of Visual Basic (and Visual C#)
    use for much of its functionality, objects, syntax, etc.

    - Wayne

    Monday, February 15, 2016 12:53 AM
  • Ok thank you but do some people still use it or not really?
    Monday, February 15, 2016 12:58 AM
  • do some people still use it or not really?


    Still use what? Do you mean Visual Basic 6? If so, do you mean the compiler or the syntax,
    functions, etc.? There may be some old legacy applications still being supported by VB 6.
    Most new development in VB for the past several years has made the transition to .NET,
    but there is still support for some old VB 6 functionality in the latest VB compilers
    so legacy applications may be built until they're updated or rewritten.

    Examples of older functions from VB 6 era:

    Randomize()
    MsgBox()

    Newer .NET replacements:

    Random Class
    MessageBox Class

    - Wayne

    Monday, February 15, 2016 1:16 AM
  • Thank you very much but I have decided that I wanted to make my own code not just copy so can you please tell me how I can change the code I just posted so that it's in "up-to-date" language ? Or is it completely useless now ? In that case, I'll restart but I'm not allowed to use somebody else's code. Sorry if I wasn't clear, but I am a student.
    • Edited by Peanut123 Thursday, March 3, 2016 1:26 PM
    Monday, February 15, 2016 1:28 AM
  • No - if you tested it yourself you would know that.

    The first line says if the first box clicked does not have the same image as the second box clicked then .....

    If you click the same box twice then that first test will fail so your additional code will never run.

    Monday, February 15, 2016 1:42 AM
  • I don't understand why does my code produe 17 values and not 16?

        Dim Tableau(16) As Integer

    creates an array with elements numbered 0 to 16.  That's 17 elements.

    • Edited by Acamar Monday, February 15, 2016 1:53 AM sp
    Monday, February 15, 2016 1:51 AM
  • I'm working on visual basic by the way so I don't know what is VB6 or .NET.
    Then you need to be sure that the code you use comes from a .Net source.  This forum is a good source for .Net code. There are many places where the difference doesn't matter, but arrays is one place it is critical.
    Monday, February 15, 2016 1:54 AM
  • Is this code still ok then? Or should I restart?

    You should discard the code you posted, although you can use it for ideas, that shouldn't be necessary as the other examples are better.

    Start again, but describe exactly what you want that bit of code to do.  Not in programmer terminology, but in plain language, and be absolutely precise (because when you come to code it you will have to be just as precise).

    Monday, February 15, 2016 1:56 AM
  • Ok so here is what i have to do

    create 16 picture box

    choose 8 images

    make a list of pictures

    the user clicks the start button, all the images are hidden

    the user clicks one picture box, a random image is assigned to it and is shown for 5 seconds

    the user clicks a second picture box, a random image is assigned t it and is shown for 5 seconds

    if the first clicked image = second clicked image, then they stay visible and they are removed from the list

    else, if the two are different, they stay in the liste nd get hidden

    this must continue until the list is empty

    when the list is empty, a message box must congratulate the user for winning the game

    if the user wants to play again, he must click on the start button

    when he clicks on that button, all the images are hidden and the list if filled back up

    I know i must use 16 picture box and 8 images. i also have to use a timer probably and a random ? (I have no idea of any of this)... Can you please give me the code1some parts of it i really want to do this, just for myself, because my deadline is way passed.

    Sunday, February 21, 2016 10:26 PM
  • Hi can you please give me some parts of the code ? My deadline is already passed but I would still like to try to figure out how to do it.

    If you are figuring out how to do it, then it's not code that you need - it's a design.  You already have lots of code above - you need to decide what parts of it you want to use.

    You also have some design ideas already.  I would recommend using the picturebox initial image and error image properties.  Start by assigning images to these properties.  Don't worry about randomising the initial images yet - you want to start with something you can see and test, and randomising makes testing difficult.  Just assign pairs of images.  All the error images are the same. 

    Now write code that flips the image when you click on it (changes the displayed image from error image to initial image).

    Once you have that working you can consider what you want to do next.

    For instance, you will need to compare two pictureboxes to see if they have been allocated the same image. The picture box Tag property is probably useful for that.  You can update it when you assign the image to the picture box.  It can also be used to indicate which images have been matched (when you get to the matching part of your code) so you can test when the player clicks on a picture box that is already matched, and when the payer has matched all the images.

    Sunday, February 21, 2016 11:01 PM
  • Can you please give me the code for what you just explained to me ?
    Sunday, February 21, 2016 11:07 PM
  • Can you please give me the code for what you just explained to me ?

    You already have most of it with your two lists (images and pictureboxes).

    You need code that sets the error image property of each picture box to the image you are using for the back of the tile.   Set the image property to error image at the same time.  That's a simple For/Each loop through the pictureboxes list.

    You need code that sets the initial image for each picture box to the image you selected for that picturebox. That's a simple For/Next loop through the images list. Forget random for now.  Just work through the list of images and assign each of them to two picture box initial image properties, in sequence.  That makes testing much easier - it can be changed to randomise later on.  Make sure you update the picturebox tag property with the image index as you go.

    https://msdn.microsoft.com/en-AU/library/system.windows.forms.picturebox.errorimage(v=vs.110).aspx
    https://msdn.microsoft.com/en-AU/library/system.windows.forms.picturebox.initialimage(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image(v=vs.110).aspx

    Now write the code to flip the image when you click on it.


    • Edited by Acamar Sunday, February 21, 2016 11:24 PM sp
    Sunday, February 21, 2016 11:22 PM
  • Ok can you give me a code to randomly change the position of the 16 picture boxes when I click on a button ? Thank you!
    Monday, February 22, 2016 1:37 AM
  • Ok can you give me a code to randomly change the position of the 16 picture boxes when I click on a button ? Thank you!

    Why?  The design I am proposing does not require moving picture boxes.

    You should get the basic procedure described above in place and working before worrying about making it random.

    Monday, February 22, 2016 2:05 AM
  • Is this better ? plz help

    Private Sub Top1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Top1.Click

    If Timer1.Enabled = False Then

    Top1.Visible = True

    Else : Top1.Visible = False

    End If

    End Sub

    Private Sub Top2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Top2.Click

    If Timer1.Enabled = False Then

    Top2.Visible = True

    Else : Top2.Visible = False

    End If

    End Sub

    Private Sub Top3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Top3.Click

    If Timer1.Enabled = False Then

    Top3.Visible = True

    Else : Top3.Visible = False

    End If




    • Edited by Peanut123 Thursday, March 3, 2016 3:13 PM
    Monday, February 29, 2016 4:58 PM
  • Just to simplify the code for the 16 tops (I used only 6 here...):

       Private Sub MesTops_Click(sender As Object, e As EventArgs) Handles Top1.Click, Top2.Click, Top3.Click, Top4.Click, Top5.Click, Top6.Click
            'Let s capture the sender: it is a button so casting the type to Button
            Dim btn As Button = CType(sender, Button)
            'Now we can do whatever we want on that button....
            If Timer1.Enabled = False Then
                btn.Visible = True
            Else
                btn.Visible = False
            End If
        End Sub




    Monday, February 29, 2016 5:25 PM
  • Ok so I have now done what sqlguy and cyrille suggested. It's so much simpler ! I know you all must find me pretty crazy for repeating my code like 16 times... Anyways thank you ! Ok now here were the things I wrote down on my paper :

    1. Create a timer. That timer will start at 1000 and count down to 0. The number of seconds left will be written in a label.

    2. Create the start button. If the timer isn't on, we put it on and the start button will say "pause". If it's already on, we put it off and the start button will say "start". When we click on the button, the images will be assigned to the labels in the table panel layout. Then we play the game, and we can win so the code has to check for that. When we press on in all the top pictureboxes hide the labels.

    3. Assign the images. Create a list with 16 images (8 pairs of images). This is where i have some trouble. For each label in the tablelayoutpanel, a randomnumber from 1 to to  number of items in the image list which is 16 is generated. That number is linked to an image in that list. Here, i don't know how to make the image appear (I took this part from a website, but that website used icons so it was text but i'm using images please help me ).

    4. Check if the user won. For each label in the tablelayoutpanel, the computer has to check wether an image is assigned to it. (I'm not sure if it will work for the last pair?). If all the labels have been assigned an image, then a message congratulates the user.

    5. Top pictureboxes. At the beginning, these 16 top pictureboxes hide all of the labels in the tablelayoutpanel. If the timer is on, so the user is playing, when he click on a top picture box, it disappears. If the timer is off, the top picture box doesn't disappear it stays there to cover the labels Under.

    6. Labels in the tablelayoutpanel. I have a lot of trouble with this too but basically when a label is clicked a picture will appear. Then user clicks two labels at a time. If the image in the first label is the same as the image on the secon label clicked, then they stay. If they aren't the same, then the top boxes come back to cover them (you first have to click the top picture box, then the label in the tablelayoutpanel to see an image).

    Tuesday, March 1, 2016 12:48 AM
  • You are going in the right direction by outlining the steps of your software: that's the design phase.

    Do more of that and find a way to write the design inside your code with #Region for exemple to keep your work organized.

    You should report the error description when there are squiggles under code. For 1 & 2 check what is exactly RandomNumber...is it an integer? is it constrained properly to ImageList.Count-1??

    You should qualify your objects more with things like ListOfRandomImages instead of Images so to prevent any confusion with reserved keywords...

    How about you do preparatory projects to test parts of your project. Start with something like this to test the effect of images on Label; quick short sweet project to evaluate the issues

    and the code is short enough to see everything at once:

    Public Class Form2
        Private myRandomNumbers As New Random
        Private ListOfImages As List(Of Bitmap)
        'Add a random Image to the Label1.Image
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Find a random value
            Dim WhichImageInList As Integer
            WhichImageInList = myRandomNumbers.Next(ListOfImages.Count - 1)
            'Set the Image to that random image
            Label1.Image = ListOfImages(WhichImageInList)
    
        End Sub
        'Remove the Label1.Image
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Label1.Image = Nothing
        End Sub
        'Loading with initializing stuff...
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Load the images into the list
            ListOfImages = New List(Of Bitmap) From {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6, My.Resources._7, My.Resources._8}
            Me.Location = New Point(200, 200)
        End Sub
    End Class

    Now it becomes evident that Label ARE NOT the best way to display images....

    You can then do a quick change, making label1.visible=false and changing Label1 to Tops1 button: Much better looking.

    So organize your work, test your parts keeping them simple, comment the code, identify the errors, come back with problems in specific chunks.

    Here is a snippet to dynamically create a 4x4 tile of buttons:

    'Creating the tiles of buttons
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim intNumberOfTiles As Integer = 16
            Dim indexControl As Integer
            Dim TopPoint As Point = New Point(12, 55) 'Set those by trial and error but a logic for centering could be used
    
            For indexControl = 1 To intNumberOfTiles
                'This debug helped setting the math...for other tiling replace 4 by the grid number...
                Debug.Print(indexControl.ToString & " X,Y=" & (indexControl - 4 * ((indexControl - 1) \ 4)) & "," & (indexControl - 1) \ 4)
                'Dynamically create a button to place on the Form
                Dim newButton As New Button
                'Just for debugging purposes display the button number
                newButton.Text = "N°" & (indexControl - 1).ToString
                'Naming the button: it can be reused later to access the button by its numbered name
                newButton.Name = "AButton" & Strings.Format((indexControl - 1), "00")
                newButton.Size = New Size(55, 55)
                'The tiling is 4x4: so X= Width x (i-4 x (i\4)) and Y= Heigth x (i-1)\4
                newButton.Location = New Point(TopPoint.X + newButton.Width * (indexControl - 4 * ((indexControl - 1) \ 4)),
                                               TopPoint.Y + newButton.Height * ((indexControl - 1) \ 4))
                'The Handler code the behavior of the new button on Click event
                AddHandler newButton.Click, AddressOf Me.TileButton_Click
                'Add the button to the Form
                Me.Controls.Add(newButton)
            Next
        End Sub
    'Button Handler: here it loads an image on itself
        Private Sub TileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As Button = CType(sender, Button)
            Dim WhichImageInList As Integer
            WhichImageInList = myRandomNumbers.Next(ListOfImages.Count - 1)
            btn.Image = ListOfImages(WhichImageInList)
        End Sub

    Tuesday, March 1, 2016 8:46 AM
  • Ok and if I decide to use picture boxes instead of labels or buttons to make everything much simpler could you give me another example using picture boxes please? Thank you very much!
    Tuesday, March 1, 2016 1:36 PM
  • Ok is this good ? I can't try my code yet without deleting plenty of stuff that have errors...
        Private Sub AssignImagesToPictureBoxes()
    
            Dim ListofImages =
                    New List(Of Image) From {My.Resources.banane, My.Resources.banane, My.Resources.poisson, My.Resources.poisson, My.Resources.arcenciel, My.Resources.arcenciel, My.Resources.ballon,
                                             My.Resources.ballon, My.Resources.soulier, My.Resources.soulier, My.Resources.poule, My.Resources.poule, My.Resources.soleil, My.Resources.soleil,
                                             My.Resources.parapluie, My.Resources.parapluie}
    
            Dim ListofBoxes =
                    New List(Of PictureBox) From {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5,
                                                  PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10,
                                                  PictureBox11, PictureBox12, PictureBox13, PictureBox14, PictureBox15,
                                                  PictureBox16}
    
            For Each PictureBox In ListofBoxes 'Pour chaque picturebox du dessous
                Dim WhichImageInList As Integer 'On va créer une variable qui représentera l'image choisie
                WhichImageInList = Random.Next(ListofImages.Count) 'Cette image sera générée au hasard par Random (entre 1 et 16)
                PictureBox.Image = ListofImages(WhichImageInList) 'L'image du picturebox sera déterminée par le nombre 1 à 16
                ListofImages.RemoveAt(WhichImageInList) 'Une fois assignée à son picturebox, on retire l'image de la liste pour ne pas qu'elle soit choisie une deuxième fois
            Next
    
        End Sub

    Tuesday, March 1, 2016 7:32 PM
  • Just to say it does work but does it make sense to you ?
    Tuesday, March 1, 2016 8:33 PM
  • It doesn't make sense to reload the List(of Image) or of picturebox. Do them at Load time once and for all using a Private.

    Using the List(Of).RemoveAt is a bit using a hammer to kill a fly...

    You should

    • initiate the Lists once : List(of Bitmap) with 8 elements
    • initiate the list of picturebox with the 16 elements
    • create a table for bitmap assigments for pairs

    There are many ways to do the table assignments: this is an algorithmic problem that you should solve with paper and pen, it is a O(n) problem but here is a quick exemple:

    •  loop for 8 value
    •  pick a random first number for the pair: if it already has been assigned pick again
    •  pick a random second number for the pair: if it already has been assigned pick again
    •  assign the value to first and second in the pair

    That is crude but it works: plenty of other ways to do it more clever than that one.

    Here are codes for this:

            Dim TableAssign(15) As Integer
            Dim myRnd As New Random
    
            Dim intIndex As Integer
            Dim FirstOneInPair As Integer, SecondOneInPair As Integer
    
            
                For intIndex = 1 To 8
                    'Select one at random
                    FirstOneInPair = myRnd.Next(0, 16)
                    While TableAssign(FirstOneInPair) <> 0
                        'Already used try again
                        FirstOneInPair = myRnd.Next(0, 16)
                    End While
                    TableAssign(FirstOneInPair) = intIndex
                    SecondOneInPair = myRnd.Next(0, 16)
                    While TableAssign(SecondOneInPair) <> 0
                        'Already used try again
                        SecondOneInPair = myRnd.Next(0, 16)
                    End While
                    TableAssign(SecondOneInPair) = intIndex
                Next

    Once you have the table of assign images, just do your For Each

    Bonne chance


    Tuesday, March 1, 2016 10:12 PM