WL_07

I am a student studying VB for the first time and I've run into a snag programming as assignment. It's a matching game consisting of 8 pairs of matching words randomly assigned to 16 labels. When a label is clicked, the word currently assigned to it will display. If two labels are clicked and the words match, the labels are disabled and change their color. This is done until all 8 pairs of words are found, after which a message box is displayed indicating the game is over. I am confused as to how I can randomize the list of words and assign them to the labels, as well as how to check for matching. Any help would be appreciated.

Re: Visual Basic Express Edition Help with "Concentration" Game

ahmedilyas

Randomizing isnt tricky really. you pretty much need to have a list of words in an array for example. Randomize a number between 0 and the capacity of the array, get the number and pick it out from the array (the position of the item in the array) then assign it to a label or whatever.

Small example:

Dim theWords as new ArrayList()

theWords.Add("hello")

theWords.Add("my")

theWords.Add("name")

theWords.Add("is")

 

Dim theRNG as new Random()

Dim result as Integer = theRng.Next(0, theWords.Count)

 

MessageBox.Show(theWords(result).ToString())

 

this should randomly pick a word from the arraylist and show it to you in a messagebox. Does this make sense so far Once the words have matched, you should remove it from the list to prevent it from re-picking that word when displaying it to a label for example.

to check for matching well, you simply use the Text property of the controls and see if it matches your other label. Example....

 

private function DoCheckMatch(byval originalWord as String, byval wordToCheck as String) as Boolean

if originalWord.Equals(wordToCheck) = true then

   return true

else

   return false

end if

end function

 

private sub button1_Click(byval sender as object, byval e as eventargs) handles button1.Click

Dim originalWord as String = "bob"

Dim wordToCheck as String = "tango"

Dim result as Boolean = Me.DoCheckMatch(originalWord, wordToCheck)

MessageBox.Show("Words match " & result.ToString()) 'this should show false, since the 2 words do not match

end sub

 

does this help






Re: Visual Basic Express Edition Help with "Concentration" Game

WL_07

Sounds a lot clearer. I'll give it a shot. If I run into any snags, I'll post them.

Thanks.





Re: Visual Basic Express Edition Help with "Concentration" Game

WL_07

Is there a way to condense the sub for each click so I don't have to do it for each button For example, using this piece of code:

Private Sub TestForMatch(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.Click, _

Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click, Label7.Click, _

Label8.Click, Label9.Click, Label10.Click, Label11.Click, Label12.Click, Label13.Click, _

Label14.Click, Label15.Click, Label16.Click





Re: Visual Basic Express Edition Help with "Concentration" Game

ahmedilyas

you could but wouldnt recommend it. If I understand correctly, you want the clicks to be in 1 sub right Tried this (the above should be ok too I Think, but not sure)

private sub Label1_Click(byval sender as object, byval e as eventargs) handles Label1.Click, Label2.Click, Label3.Click...............

end sub

is this what you mean






Re: Visual Basic Express Edition Help with "Concentration" Game

WL_07

Right. I just thought it might simplify the code to have them all under one sub. Also, I may be confused, but the sample code you initially posted as an example had the words displaying in a message box. For my program, the words must display when each label is clicked. If they match, the labels are disabled and change color. If they don't match, the words disappear and the labels go blank again.



Re: Visual Basic Express Edition Help with "Concentration" Game

WL_07

*bump*

Still need help with this. If it helps, the assignment comes from the book Microsoft Visual Basic 2005: RELOADED, Second Edition by Diane Zak. It's located at the end of Chapter 7.





Re: Visual Basic Express Edition Help with "Concentration" Game

ahmedilyas

sure, well like i said that was an example which you need to modify to your needs :-) Simply change the MessageBox to a label

Me.theLabel.Text = theWords(result).ToString()

to disable a label....

Me.theLabel.Enabled = false

to change the color...

Me.theLabel.BackColor = Color.Red 'for example






Re: Visual Basic Express Edition Help with "Concentration" Game

WL_07

So here's the array I've got with the words to be used:

Dim sItems(15) As String

sItems(0) = "Refrigerator"

sItems(1) = "Range"

sItems(2) = "Television"

sItems(3) = "Computer"

sItems(4) = "Washer/Dryer"

sItems(5) = "Dishwasher"

sItems(6) = "Car"

sItems(7) = "Trip"

sItems(8) = "Refrigerator"

sItems(9) = "Range"

sItems(10) = "Television"

sItems(11) = "Computer"

sItems(12) = "Washer/Dryer"

sItems(13) = "Dishwasher"

sItems(14) = "Car"

sItems(15) = "Trip"

How do I randomize this list Then, how do I assign a word from that randomized list to a label





Re: Visual Basic Express Edition Help with "Concentration" Game

ahmedilyas

  • pick an item from the array randomly....

    Dim theRNG as new Random()

    Dim positionChosen as Integer = theRNG.Next(0, sItems.Length - 1)

  • assign item chosen to label:

    Me.theLabel.Text = sItems(positionChosen).ToString()

    this will randomly pick a number between 0 and the number of items in the array. When we get this random number generated we simply index the item from the array, pull out the item from the position in the array (index) and then put it into a label! :-)






  • Re: Visual Basic Express Edition Help with "Concentration" Game

    WL_07

    ahmedilyas wrote:

  • pick an item from the array randomly....

    Dim theRNG as new Random()

    Dim positionChosen as Integer = theRNG.Next(0, sItems.Length - 1)

  • assign item chosen to label:

    Me.theLabel.Text = sItems(positionChosen).ToString()

    this will randomly pick a number between 0 and the number of items in the array. When we get this random number generated we simply index the item from the array, pull out the item from the position in the array (index) and then put it into a label! :-)

  • So when I'm assigning the selected item to the label, it should look something like:

    Me.lbl1.Text = sItems(positionChosen).To String

    right And I should do that for all labels (16 total)





    Re: Visual Basic Express Edition Help with "Concentration" Game

    ahmedilyas

    thats correct, the code posted would do just that my friend :-) you should do it for whatever number of times you want. But at the same time you want to be sure that you remove the selected item from the array so next time you pick a random item, it does not pick the same one some time soon. The best way is to store it in an ArrayList, as shown earlier, then when you select an item and show it in the label, remove the item from the array so next time when you generate a random number, it will pick the entry again from the array but without the previous one chosen




    Re: Visual Basic Express Edition Help with "Concentration" Game

    WL_07

    ahmedilyas wrote:
    thats correct, the code posted would do just that my friend :-) you should do it for whatever number of times you want. But at the same time you want to be sure that you remove the selected item from the array so next time you pick a random item, it does not pick the same one some time soon. The best way is to store it in an ArrayList, as shown earlier, then when you select an item and show it in the label, remove the item from the array so next time when you generate a random number, it will pick the entry again from the array but without the previous one chosen

    How do I remove the items Do I use a for...next statement





    Re: Visual Basic Express Edition Help with "Concentration" Game

    ahmedilyas

    if you used an ArrayList as shown earlier, you simply do this to remove an item:

    theArrayList.Remove("item")

    and thats it!






    Re: Visual Basic Express Edition Help with "Concentration" Game

    WL_07

    ahmedilyas wrote:

    if you used an ArrayList as shown earlier, you simply do this to remove an item:

    theArrayList.Remove("item")

    and thats it!

    I put everything in an arraylist as you suggested:

    Dim aItems As New ArrayList

    aItems.Add("Refrigerator")

    aItems.Add("Range")

    aItems.Add("Television")

    aItems.Add("Computer")

    aItems.Add("Washer/Dryer")

    aItems.Add("Dishwasher")

    aItems.Add("Car")

    aItems.Add("Trip")

    aItems.Add("Refrigerator")

    aItems.Add("Range")

    aItems.Add("Television")

    aItems.Add("Computer")

    aItems.Add("Washer/Dryer")

    aItems.Add("Dishwasher")

    aItems.Add("Car")

    aItems.Add("Trip")

    Dim theRNG As New Random

    Dim iChosen As Integer = theRNG.Next(0, aItems.Count)

    Where would I put in the remove code How would I know what word to remove, since they are being assigned randomly