Jump to content

C# Memory Game

- - - - -

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

#1
Reilly911

Reilly911

    Newbie

  • Members
  • Pip
  • 4 posts
I Have to create a memory game for school, but i have got no idea about how to go about it, can anyone help me? This is my first year of programming so we are only covering the basics and stuff so don't get to complex.


Application Requirements
In the game of MATCH’EM a set of image pairs are hidden within a grid. Players of the game must
choose two locations at a time within the grid. If the two locations contain matching images then the
images are displayed permanently and the player scores a point. If the two locations do not match
then the images are displayed for a fixed amount of time, after which the images are hidden. This
project requires you to build a version of the game of MATCH’EM. A complete description of the game
is found in Appendix A.

And here is the other information

Objectives
This assignment has been designed to assess your:
1. understanding of variables, variable assignment and arithmetic.
2. use of variable scope to separate global and local variables in methods.
3. event handling capabilities.
4. understanding of control structures such as selection and loops.
5. use of classes.
6. use of functions.
7. use of arrays.
8. ability to translate problem specifications into a product.
9. design of user interfaces.
10. documentation of software applications.



If u guys can provide any help that would be fantastic, i've been trying to get help on other forums but their are people out their who are just mean angry little kids.

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
It's not too bad. Design a user interface with lots of picture boxes, then write code (Image.FromFile()) to load pictures from wherever. Then, perform a logical condition to test if the two images are the same, and if so, change the picture box's Visible property to "true". Code can be written in the picture box's Click event handler, and to simplify things, use a method that can be used to compare images, to return a boolean result.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
I made a memory game not to long ago and it presents some interesting problems. Looks like Xav mapped out most of the basics for ya. Most of it is fairly straight forward but I ran into problems with randomizing the pictures for the picture boxes so that the sequence is different from one game to the next. Using a random #'s array and assigning different pictures based on the random #'s worked for that. The other problem that took me a bit to figure out was using a timer so that one could see the pictures of their guess even if it wasn't a match. You have to be precise with were in the code you put the pause (I tried 7 or 8 different things before I found a way that worked for me.) Also the new game method was a little difficult to get working properly. I used a control array for the all the pic boxes but had a hard time getting it to reset with different pictures than the originals. I ended up having to go back through all my declarations to figure it out (A little careful planning will eliminate this problem.) A very good programming assignment all together. If you need any help with any of the methods or figuring out how to make something specific work pls post it...I am interested to see if you run into the same problems:)

Edited by gaylo565, 01 June 2008 - 10:15 AM.


#4
Reilly911

Reilly911

    Newbie

  • Members
  • Pip
  • 4 posts
OK Xav that helps me out a bit, but i have no idea how to randomise the pictures from each new round.

Any idea of how to do this and an example of the code would be good.

#5
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Here is some code for my memory game for randomizing pics. Mine was a 6x7 grid originally. You need to pass the value for intCount2 and use it as the parameter to call the checkNumbers method. The rest can go in your form load or in its own method.
//create variable to hold corresponding pic box's index value
int intCount2;

//create array to hold random numbers to randomize pics
int[] intBoard = new int[43]; 

//declare random number object
Random generateRandom;

//create array to hold random numbers to fill board array...size doesnt matter
//much but must be a few larger than 42 though
int[] intStorage = new int[150];

//seed the random number function
DateTime dtmCurrent = DateTime.Now;
generateRandom = new Random(dtmCurrent.Millisecond);

//create variables to hold places for setting array values
int intCount1, intAdd;
bool bln_isSame;

//fill array with random #'s from 1 to 42
int intAdd = 0;
for(int r=0; r < 250; r++)
{
	intStorage[r] = generateRandom.Next(1,43);
}
for(intCount1=0; intCount1 < 250; intCount1++)
{
	bln_isSame = false;
	if(intAdd < 43)
	{
		//check to see if array contains generated #
		foreach(int intBoard1 in intBoard)
		{
			if(intStorage[intCount1] == intBoard1)
			{
				bln_isSame = true;
			}
		}
		//fill board array with random non matching #'s
		if(bln_isSame == false && intStorage[intCount1] != 0)
		{
			intBoard[intAdd] = intStorage[intCount1];
			intAdd++;
		}
	}
}
private string checkNumbers(int intCount2)
{
	//get file name for button image
	if(intCount2 == 1 || intCount2 == 2 || intCount2 == 3 || intCount2 == 4 || intCount2 == 5 || intCount2 == 6)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic1.bmp";
	}
		else if(intCount2 == 7 || intCount2 == 8 || intCount2 == 9 || intCount2 == 10 || intCount2 == 11 || intCount2 == 12)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic2.bmp";
	}
	else if(intCount2 == 13 || intCount2 == 14 || intCount2 == 15 || intCount2 == 16 || intCount2 == 17 || intCount2 == 18)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic3.bmp";
	}
	else if(intCount2 == 19 || intCount2 == 20 || intCount2 == 21 || intCount2 == 22 || intCount2 == 23 || intCount2 == 24)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic4.bmp";
	}
	else if(intCount2 == 25 || intCount2 == 26 || intCount2 == 27 || intCount2 == 28 || intCount2 == 29 || intCount2 == 30)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic5.bmp";
	}
	else if(intCount2 == 31 || intCount2 == 32 || intCount2 == 33 || intCount2 == 34 || intCount2 == 35 || intCount2 == 36)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic6.bmp";
	}
	else if(intCount2 == 37 || intCount2 == 38 || intCount2 == 39 || intCount2 == 40 || intCount2 == 41 || intCount2 == 42 || intCount2 == 43)
	{
		strFileName = "C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\pic7.bmp";
	}
	return strFileName;
}
You simply create a bunch of random #'s between 1 and 42 and store them in an array. Then use another array to hold the numbers once you check for redundancy. This should give you an array of numbers from 1 to 42 where each one occurs exactly once. Then use an if else block to make each number point to a picture. The checkNumbers method will return the desired file location to set as your image. When you call the checkNumbers method you will need to set the value for intCount2 to its corresponding random # in your board array. example:
int intCount3 = picBox.TabIndex;
//store last int Count2 for checking match;
int intMatch = intCount2;
			
//set random number for passing to find file name 
intCount2 = intBoard[intCount3];
ImgLocation = checkNumbers(intCount2);
picBox.Image = Image.FromFile(ImgLocation);
Hope this helps a bit

Edited by gaylo565, 02 June 2008 - 06:56 AM.


#6
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
repeat message...sorry

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I'm not sure the checkNumbers() function is the most efficient, though. Perhaps a switch statement, or perhaps a loop?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
if else may not be the best way to go:) It was easy enough to write that way though and it was the first way that came to mind when I originally wrote it. It works well enough with this short program but your right that it isn't the most efficient.

Edited by gaylo565, 02 June 2008 - 07:44 AM.


#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Hee hee - I like being right. :D
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#10
Reilly911

Reilly911

    Newbie

  • Members
  • Pip
  • 4 posts

gaylo565 said:

Here is some code for my memory game for randomizing pics. Mine was a 6x7 grid originally. You need to pass the value for intCount2 and use it as the parameter to call the checkNumbers method. The rest can go in your form load or in its own method.




Yeh that helps me understand what to do but i don't know how to write the code for me to use.

This is my whole code i have so far, it doesn't work and it is all over the place, i don;t know how the buttons get the pictures they are ment to have and stuff, little help.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;


namespace Assignment_4

{

    public partial class Form1 : Form

    {



        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            Random generateRandom;

            int[] intStorage = new int[250];


            //seed the random number function

            DateTime dtmCurrent = DateTime.Now;

            generateRandom = new Random(dtmCurrent.Millisecond);


            //create variables to hold places for setting array values

            int intCount1, intAdd;

            bool bln_isSame;


            //fill array with random #'s from 1 to 16

            int intAdd = 0;

            for (int r = 0; r < 250; r++)

            {

                intStorage[r] = generateRandom.Next(1, 16);

            }

            for (intCount1 = 0; intCount1 < 250; intCount1++)

            {

                bln_isSame = false;

                if (intAdd < 17)

                {

                    

                    

            //check to see if array contains generated #

            foreach (int intBoard1 in intBoard)

            {

            if (intStorage[intCount1] == intBoard1)

            {

            bln_isSame = true;

            }

            }

            //fill board array with random non matching #'s

            if (bln_isSame == false && intStorage[intCount1] != 0)

            {

            intBoard[intAdd] = intStorage[intCount1];

            intAdd++;

            }

                }

            }

        }



        private string checkNumbers(int intCount2)

{

       //get file name for button image

       if(intCount2 == 1 || intCount2 == 2)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic1.png";

       }

               else if(intCount2 == 3 || intCount2 == 4)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic2.png";

       }

               else if(intCount2 == 5 || intCount2 == 6)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic3.png";


               else if(intCount2 == 7 || intCount2 == 8)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic4.png";


               else if(intCount2 == 9 || intCount2 == 10)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic5.png";


               else if(intCount2 == 11 || intCount2 == 12)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic6.png";


               else if(intCount2 == 13 || intCount2 == 14)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic7.png";

                                  

               else if(intCount2 == 15 || intCount2 == 16)

       {

               strFileName = "O:\\SIT102\\Assignments\\Assignment 4\\Images\\pic8.png";

       }


       return strFileName;

}




            GamePieces[0, 0] = picbtn1;

            GamePieces[0, 1] = picbtn2;

            GamePieces[0, 2] = picbtn3;

            GamePieces[0, 3] = picbtn4;


            GamePieces[1, 0] = picbtn5;

            GamePieces[1, 1] = picbtn6;

            GamePieces[1, 2] = picbtn7;

            GamePieces[1, 3] = picbtn8;


            GamePieces[2, 0] = picbtn9;

            GamePieces[2, 1] = picbtn10;

            GamePieces[2, 2] = picbtn11;

            GamePieces[2, 3] = picbtn12;


            GamePieces[3, 0] = picbtn13;

            GamePieces[3, 1] = picbtn14;

            GamePieces[3, 2] = picbtn15;

            GamePieces[3, 3] = picbtn16;





        }

    }

}

               }

            }

    }

}




#11
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
I posted most of my game before I was quite finished a little while back (it actually worked but once you click new game from the menu the problems were happening)...If you look through the c# forum you will find it under "help finishing memory game." This code is almost complete except the new game method wasn't fixed yet. It should give you plenty of ideas on how to deal with each challenge you will come across. If you get that far and need help with the new game method let me know.

#12
Reilly911

Reilly911

    Newbie

  • Members
  • Pip
  • 4 posts
Gaylo i haven't given your thread a read yet but i will after this.

I have managed to get a randomization thing occuring but with numbers. All i need now is to replace the numbers with pictures. Can you guys help?


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;


namespace Form1

{

    public partial class Form1 : Form

    {

        int[] Cards;


        Label[] Number;


            

        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            Number = new Label[16];

            Cards = new int[16];

            Number[0] = lblArray0;

            Number[1] = lblArray1;

            Number[2] = lblArray2;

            Number[3] = lblArray3;

            Number[4] = lblArray4;

            Number[5] = lblArray5;

            Number[6] = lblArray6;

            Number[7] = lblArray7;

            Number[8] = lblArray8;

            Number[9] = lblArray9;

            Number[10] = lblArray10;

            Number[11] = lblArray11;

            Number[12] = lblArray12;

            Number[13] = lblArray13;

            Number[14] = lblArray14;

            Number[15] = lblArray15;


            int Index;

            for (Index = 0; Index < 16; Index++)

            {

                Cards[Index] = Index;

                Number[Index].Text = Index.ToString();

            }

        }


        private void btnNewGame_Click(object sender, EventArgs e)

        {

            Random RandGen = new Random();

            int A;

            int B;

            int C;

            int D;

      

            for (D=0; D<200000; D++)

            {

                A = RandGen.Next(16);

                B = RandGen.Next(16);


                

                C = Cards[A];

                Cards[A] = Cards[B];

                Cards[B] = C;

            }


            for (D=0; D < 16; D++)

            {

                Number[D].Text = Cards[D].ToString();

            }

        }

    }

}