Thread: [RESOLVED] Concentration game 2D picturebox array

1. [RESOLVED] Concentration game 2D picturebox array

Hello,

I am required to make a game of concentration, whereby cards are shown for x amount of seconds and then truned over. The user then needs to remember where the cards were and pick two at a time in order to win points - whilst being timed.

Any pointers on where to start, or how to make a 2D control array of pictureboxes that can display the cards into a grid of (10x10) pictureboxes randomly?

Thanks
PMB

2. Re: Concentration game 2D picturebox array

Add a TableLayoutPanel to your form with the appropriate numbers of rows and columns, then add a PictureBox to each cell.Handle the Click event of each control to change the Image and use a Timer to change it back.

3. Re: Concentration game 2D picturebox array

Will this method allow me to somehow shuffle the cards, randomly, so that they're not in the same lcoation each time a New Game is selected?

I need to have a deck of 52 cards, in some sort of type that then related to an integer in the pictureboxes, or coordinates?, but I am unsure as to the best way to go about it. It uses to randomize function, or so I believe, as no number can be repeated.

Thanks
PMB

4. Re: Concentration game 2D picturebox array

If you have a list of 52 items then you just randomise that first and then place them into position one by one. It's just like dealing a pack of cards. You shuffle the cards and then deal from the top. You might like to follow the CodeBank link in my signature and check out my thread on Randomising A List.

5. Re: Concentration game 2D picturebox array

I've successfully managed to randomize the cards, using this code:

vb Code:
`Dim Cards() As Integer Private Sub shuffle()        Dim numberOfCards = 52        Cards = Enumerable.Repeat(0, numberOfCards ).ToArray()        Dim Tmp As Integer        Randomize()        Dim i As Integer = 0        Do While i < numberOfCards            Tmp = Int((numberOfCards + 1) * Rnd())            If Array.IndexOf(Cards, Tmp) < 0 Then                Cards(i) = Tmp                ListBox1.Items.Add(Tmp)                i += 1            End If        Loop    End Sub`

This puts the numbers 1 to 52 in a litsbox randomly - which will be removed, it was simply for confiration. How would I now convert this to randomize the display of images on PictureBoxes that were added into a Panel? It's a 10x10 grid.

Also, at the minute I have them all showing red back using this code:

vb Code:
`PictureBox(Row, Column).Image = Image.FromFile(path & "cards\Red.png")                 PictureBox(Row, Column).SizeMode = PictureBoxSizeMode.StretchImage`

So, what I am trying to do is randomize the face cards that will show upon clicking each PictureBox. I need two of each at any one time so that there's a pair.

Thanks
PMB

6. Re: Concentration game 2D picturebox array

Well, if you have a 10x10 grid, then you need 100 cards. However, depending on how you want to represent those 100 cards, your shuffling code may or may not work. If you represent them as the numbers 1-100 it's fine, but then you have to match up the pairs. If you represent them as the numbers 1-50 with each value appearing twice, it's simpler to represent and match each card, but your shuffling algorithm won't work.

Then it's simply a case of mapping each card's integer value to the relevant picture to display as its face. (e.g. {1,2} map to card face 1, {3,4} map to card face 2, ... (if you decide to go with numbers 1-100))

Oh, well there is of course mapping index of the collection to the relevant picture box as well. That is, for the first element in your shuffled collection, you need to take the value, work out the picture to show, and work out which picture box to show it in, and show that picture in that picture box. Repeat for every element in your collection.

7. Re: Concentration game 2D picturebox array

Or you could do as suggested in the first place and randomise like this:
Code:
```Dim rng As New Random

myArray = myArray.OrderBy(Function(x) rng.NextDouble()).ToArray()```
That will randomise the list no matter what it contains. The original list could be sequential numbers generated by Enumerable.Range or it might be an array of instances of a Card class or whatever you like.

8. Re: Concentration game 2D picturebox array

Apologies for the slow understanding but I am not an avid user of VB.NET or Visual Studio 2010 - this task is an assignment at College.

I have located the 100 pictureboxes to the panel also - using the AddHandler and AddressOf. Now I need 50 pairs, out of the 52 to be displayed in any of the pictureboxes at random. I've had a good play with it and don't seem to be able to get it right.

Also, the images for the cards are in the 'Resources' part of the program, would is be better to have these imported from file using 'Image.FromFile()' ?

Any code to acheive the random deposition of card in the pictureboxes would be massively advantageous.

Thanks
PMB

9. Re: Concentration game 2D picturebox array

You've alreay got the code. Put your PictureBoxes into an array or collection and randomise it as you have alreay been shown. You can then use a loop to place your randomised cards into the randomised PictureBoxes.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured