Jump to content

Memory game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
blank

blank

    Newbie

  • Members
  • Pip
  • 3 posts
Hey

I am trying to create a memory game and need some help with it. I have a few picture boxes on a form and stored my images in a image list. Each image is assigned a number and I use the random number generator to randomly select the images. This code is all written in the picture box click event. My problem is how do I get just a pair on the board and not random pictures? Should I even use picture boxes? Picture box was easier to do. I'm new to c# so can you use a 2d array? I was going to do that but didn't really know where to put the code.

I just need a walk through not code.

Thank you

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Can we see your code?

#3
blank

blank

    Newbie

  • Members
  • Pip
  • 3 posts
Yeah I should've put some code but I was working on it and found some answers to my problems but then generated new problems that needed answering.

The code I have here is in it's own method. It gets me a random number of cards to display on the board which is what I want but the problem is when I run the program and try and click on the first card all the cards are "turned over" meaning the initial back of the card is flipped. I need a way that it will call the individual cards. If I put this code in the main load form then all the cards will be turned over at the start of the game.

I have tried creating a single event handler to handle all the pictureboxes but that wouldn't work. I know I should break this up but can't see how to.

Thanks



newGame{

// Stores the cards that are on the "board"

            PictureBox[] holdCards = new PictureBox[12] { card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12 };     


            // Gets a random position on the "board" 

            int RandomPositionOnBoard = random.Next(1, 13);


            for (int i = 0; i <= 5; i++)

            {

                // Gets a new random card from the 52 deck

                int randomCard = random.Next(52);


                // Stores the random card in a array so can refer to it when need to duplicate cards

                img[i] = randomCard;


                // Sets the random card to be equal to the selected position on the board

                holdCards[RandomPositionOnBoard].Image = Cards.Images[randomCard];

                Board[i] = randomCard;


                // Randomly select another random position on the board                       

                RandomPositionOnBoard = random.Next(12);

            }


// This loop check to see if there are any cells in the array that are equal to 0 and if so add the card in the array. This allows pairs of cards on the board

            for (int index = 0; index < 12; index++)

            {

                bool isCheck = false;

                holdCards[index].Image = Cards.Images[Board[index]];


                for (int ind = 0; ind < 12; ind++)

                {

                    if (Board[ind] == 0)

                    {

                        Board[ind] = img[index];

                        isCheck = true;

                    }

                    if (isCheck)

                    {

                        break;

                    }

                }

            }

        }


I am just learning about c# so don't be too hard on me guys :blushing:

#4
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
If you look through the old tutorials for c# there should be a memory game tutorial. I wrote it a while back but it has the complete source code. Now there are many different ways to do this game but I think looking at a copy of working code will help you out some. Also I didn't look at your code to thouroughly but I am pretty sure that you are going to have problems using all 52 cards and only 12 picture boxes. I would recommend just using 6 card objects and using them all twice (so that you end up with your matches.) Good luck and check out that tutorial, I think it should help you out a bit.

#5
blank

blank

    Newbie

  • Members
  • Pip
  • 3 posts
I seen your tutorial but I wanted to try my own way (guess it isn't working too well for me lol). I tried looking at it to see how it can benefit my solution but I just can't see it. This is why I posted a question. I just need some insight how to handle each picture box separately. When the first card is pressed then just that card is flipped and not all the other cards on the board.

It worked where I can randomly select 6 pairs of cards from the image file and place them on the board.

#6
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
If you have it setup where you can select your pairs of images then you just need to add a common click handle event to all of the pictureboxes. Once you have a click event just use the sender object to tell which button was clicked.
example:

private void createPcBox()

{

    //code to create picture boxes set location and other properties of pic box here

    for(int i=0;i<12,i++)

        {

            PictureBox pcBox= new PictureBox();

            pcBox.Name = "name" + i.ToString();

            pcBox.Location = new point (However you want to lay them out using i to increment)

            pcBox.Size = new Size(Size goes here);

            //add the pic box to a panel on your form (or just the form if you like)

            panel1.Controls.Add(pcBox);

            pcBox.Click += new EventHandler(pcBox_Click);

        }

}

private void pcBox_Click(object sender, EventArgs e)

        {

//cast the sender object as a Picture Box and show a message box saying wich

//pictureBox was clicked; of course this is where you would want to check for card

//matches

PictureBox pb = (PictureBox)sender;

MessageBox.Show(pb.Name + " was clicked!");

If you are still having questions show us your updated code and I will try and help out as much as possible.