Results 1 to 9 of 9

Thread: [RESOLVED] Concentration game 2D picturebox array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    30

    Resolved [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
    Last edited by PlaystationMasterBoy; Mar 13th, 2012 at 05:10 PM. Reason: Edited to make it a bit clearer

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    30

    Re: Concentration game 2D picturebox array

    Thanks for your reply.

    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
    Last edited by PlaystationMasterBoy; Mar 13th, 2012 at 06:36 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    30

    Re: Concentration game 2D picturebox array

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

    vb Code:
    1. Dim Cards() As Integer
    2.  
    3. Private Sub shuffle()
    4.         Dim numberOfCards = 52
    5.         Cards = Enumerable.Repeat(0, numberOfCards ).ToArray()
    6.         Dim Tmp As Integer
    7.         Randomize()
    8.         Dim i As Integer = 0
    9.         Do While i < numberOfCards
    10.             Tmp = Int((numberOfCards + 1) * Rnd())
    11.             If Array.IndexOf(Cards, Tmp) < 0 Then
    12.                 Cards(i) = Tmp
    13.                 ListBox1.Items.Add(Tmp)
    14.                 i += 1
    15.             End If
    16.         Loop
    17.     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:
    1. PictureBox(Row, Column).Image = Image.FromFile(path & "cards\Red.png")
    2.  
    3.                 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. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    30

    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. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width