Memory game

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2474 Views - Last Post: 22 April 2012 - 11:58 PM Rate Topic: -----

#1 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Memory game

Posted 13 April 2012 - 11:18 AM

Hey everyone. You have probably seen this program multiple times. Its the classic memory game where you try to make matches of the same images or value. For mine, we are using half a deck of cards including the jokers. Right now, I have used a two-dimensional array to organize my cards or images. I have made another two-dimensional array to give each card a value. I will compare the values later in the program to find matches. Here is what it looks like so far:

Public cardImage(4, 7) As Image
      Public cardValue(4, 7) As Integer


cardImage(1, 1) = My.Resources._2d
      cardImage(1, 2) = My.Resources._2h
      cardImage(1, 3) = My.Resources._3d
      cardImage(1, 4) = My.Resources._3h
      cardImage(1, 5) = My.Resources._4d
      cardImage(1, 6) = My.Resources._4h
      cardImage(1, 7) = My.Resources._5d
      cardImage(2, 1) = My.Resources._5h
      cardImage(2, 2) = My.Resources._6d
      cardImage(2, 3) = My.Resources._6h
      cardImage(2, 4) = My.Resources._7d
      cardImage(2, 5) = My.Resources._7h
      cardImage(2, 6) = My.Resources._8d
      cardImage(2, 7) = My.Resources._8h
      cardImage(3, 1) = My.Resources._9d
      cardImage(3, 2) = My.Resources._9h
      cardImage(3, 3) = My.Resources._10d
      cardImage(3, 4) = My.Resources._10h
      cardImage(3, 5) = My.Resources.Jd
      cardImage(3, 6) = My.Resources.Jh
      cardImage(3, 7) = My.Resources.Qd
      cardImage(4, 1) = My.Resources.Qh
      cardImage(4, 2) = My.Resources.Kd
      cardImage(4, 3) = My.Resources.Kh
      cardImage(4, 4) = My.Resources.Ad
      cardImage(4, 5) = My.Resources.Ah
      cardImage(4, 6) = My.Resources.Joker1
      cardImage(4, 7) = My.Resources.Joker2


      cardValue(1, 1) = 1
      cardValue(1, 2) = 1
      cardValue(1, 3) = 2
      cardValue(1, 4) = 2
      cardValue(1, 5) = 3
      cardValue(1, 6) = 3
      cardValue(1, 7) = 4
      cardValue(2, 1) = 4
      cardValue(2, 2) = 5
      cardValue(2, 3) = 5
      cardValue(2, 4) = 6
      cardValue(2, 5) = 6
      cardValue(2, 6) = 7
      cardValue(2, 7) = 7
      cardValue(3, 1) = 8
      cardValue(3, 2) = 8
      cardValue(3, 3) = 9
      cardValue(3, 4) = 9
      cardValue(3, 5) = 10
      cardValue(3, 6) = 10
      cardValue(3, 7) = 11
      cardValue(4, 1) = 11
      cardValue(4, 2) = 12
      cardValue(4, 3) = 12
      cardValue(4, 4) = 13
      cardValue(4, 5) = 13
      cardValue(4, 6) = 14
      cardValue(4, 7) = 14


I guess my first question is, how do I randomize an array of images? Ive already declared it on top. And Ive used random generators before, but Ive never worked with randomizing an array. . . And sorry about the formatting. I tried fixing it but no luck. Thank you in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Memory game

#2 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 13 April 2012 - 12:22 PM

I actually figured it out. Wasnt so hard :)

  Randomize()
        Dim x As Integer = Int(Rnd() * 4) + 1
        Dim y As Integer = Int(Rnd() * 7) + 1

        PictureBox1.Image = cardImage(x, y)


Now, how do I keep PictureBox1.Image from changing to a different image? And how would I go about making its pair?

This post has been edited by NessValdez: 13 April 2012 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 13 April 2012 - 01:24 PM

Is there a way to make PictureBox1.Image a readonly?? I know you can't technically "read" an image but is there something similar I can do?
Was This Post Helpful? 0
  • +
  • -

#4 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 13 April 2012 - 02:46 PM

Sorry. Im probably going to keep trying to answer my own questions so it bumps my thread up. I hope someone will eventually take a crack at this. Once I figure out how to keep PictureBox1.Image from changing, I can move on to the better part of the code. The next part is figuring out an algorithm so that the same card doesnt display twice . . . although I already have an idea for that :)

This post has been edited by NessValdez: 13 April 2012 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Memory game

Posted 13 April 2012 - 03:40 PM

Instead of thinking in term of controls, think of the concept you trying to code.

1. Waiting for to be picked.
2. Card (I'll call Card_A) has been picked
3. Waiting for to be picked
4. Card (I'll call Card_B)/> has been picked.
5. Is Card_B as match of Card_B?
  Yes: Do something for match
   No: Re hide cards {Card_A, Card_B}



Then think about the design of the interacting classes without using controls.
Now design a class to encompass the concept of a Card
Public Class Card
  Public Property FaceUp() As Boolean
  Public ReadOnly Value As T
...
End Class



Now design a class to control the game, raising Event to reflect changes in internal state.
Public Class GameController
  Private _Cards(4,7) As Card
  

  Public Sub CardPicked(c as Card)
    Select Case noCardsPicked
      Case 0
       noCardsPicked = 1
       PickedCard.Add(c)
       c.TurnFaceUp()
       RaiseEvent CardTurnedFaceUp(c)
      Case 1
       noCardsPicked = 2
       PickedCard.Add(c)
       c.TurnFaceUp()
       RaiseEvent CardTurnedFaceUp(c)
       DoCardsMatch()
     End Select
...
  End Sub
...
End Class



Now you can create a GUI on top of it.

Look at the following Simon Example
Was This Post Helpful? 1
  • +
  • -

#6 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 16 April 2012 - 02:31 PM

Thank you Adam! I like how you paint a picture first. I will work on that. But first, can someone help me come up with a better random generator using for loops. This way, instead of randomizing each individual card, I could take the cards in my array and randomize to different positions until each card is passed out . . . I was thinking maybe something like this but converted to visual basic . . .

For(x=1, x<=4, x++)
        For(y=1, y<=7, y++)

This post has been edited by NessValdez: 16 April 2012 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,675
  • Joined: 24-July 11

Re: Memory game

Posted 16 April 2012 - 02:34 PM

Need help converting it?
For x As Integer = 1 To 4
For y As Integer = 1 To 7


Was This Post Helpful? 1
  • +
  • -

#8 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 16 April 2012 - 02:47 PM

The syntax is that easy? Thank you Dimitri! Now I have to figure out how to incorporate that to make a randomized function for cardImage(x, y) to distribute all the cards . . .
Was This Post Helpful? 1
  • +
  • -

#9 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,675
  • Joined: 24-July 11

Re: Memory game

Posted 16 April 2012 - 02:51 PM

You know how to use the Random class? I'd use that and store the cards that have been dealt into an array so they don't get re-dealt. If you know what I mean :)
Was This Post Helpful? 1
  • +
  • -

#10 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 18 April 2012 - 12:48 AM

Sorry, Im not sure what you mean. Sadly, my book only talks about using the Rnd function and adding Randomize statement to the Form_Load procedure. It doesnt talk about the random class at all. I know a little bit more from using java, but then again Im still in Java 1. From what I looked up, do you mean something like . . .

Dim RandomClass As New Random()
Dim RandomNumber As Integer
For x As Integer = 1 To 4
For y As Integer = 1 To 7
RandomNumber = cardImage(x,y)



I have ideas, but Im not really sure how to store that information into an array. Let me know if Im way off :P
Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,675
  • Joined: 24-July 11

Re: Memory game

Posted 18 April 2012 - 02:51 AM

Kinda. You've declared a Random but you don't use it. Ie:
Dim rnd As New Random
Dim result As Integer = rnd.Next(0, 6) 'a random between 0 and 6


That's how you do it :)
Was This Post Helpful? 1
  • +
  • -

#12 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 18 April 2012 - 12:47 PM

Alrighty. So this might be a silly question, but what is the rnd.Next actually generating? What I mean is, am I using it to generate a random number for x and y? Im probably over thinking this.
Was This Post Helpful? 0
  • +
  • -

#13 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,675
  • Joined: 24-July 11

Re: Memory game

Posted 18 April 2012 - 02:15 PM

rnd.Next - the example I have in my last post was saying this essentially:
Create a random number between minvalue (0) and maxvalue (6).
Is it 7 cards to a 'player'?
Was This Post Helpful? 0
  • +
  • -

#14 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Memory game

Posted 18 April 2012 - 10:12 PM

No. Its a memory game. Or matching game. We only use half the deck, the hearts and the diamonds plus the jokers. So theres 28 cards passed out. We only have to use one player.
Was This Post Helpful? 0
  • +
  • -

#15 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,675
  • Joined: 24-July 11

Re: Memory game

Posted 18 April 2012 - 11:13 PM

Oh, sure! Sorry, I was a bit confuzzled there :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2