Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C# Memory Game

  1. #1
    Reilly911 is offline Newbie
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    C# Memory Game

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Memory Game

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: C# Memory Game

    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
    Last edited by gaylo565; 06-01-2008 at 11:15 AM.

  5. #4
    Reilly911 is offline Newbie
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Re: C# Memory Game

    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.

  6. #5
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: C# Memory Game

    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.
    Code:
    //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:
    Code:
    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
    Last edited by gaylo565; 06-02-2008 at 07:56 AM.

  7. #6
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: C# Memory Game

    repeat message...sorry

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Memory Game

    I'm not sure the checkNumbers() function is the most efficient, though. Perhaps a switch statement, or perhaps a loop?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: C# Memory Game

    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.
    Last edited by gaylo565; 06-02-2008 at 08:44 AM.

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Memory Game

    Hee hee - I like being right.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  11. #10
    Reilly911 is offline Newbie
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Re: C# Memory Game

    Quote Originally Posted by gaylo565 View Post
    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.

    Code:
    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;
    
    
    
    
            }
        }
    }
                   }
                }
        }
    }

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Memory game with ASP.net with C# script
    By Umeshtharun in forum ASP, ASP.NET and Coldfusion
    Replies: 1
    Last Post: 04-06-2011, 05:33 AM
  2. Need help with the memory game.
    By shakeeldreams in forum C# Programming
    Replies: 1
    Last Post: 12-18-2010, 04:55 AM
  3. C# memory game tutorial
    By gaylo565 in forum CSharp Tutorials
    Replies: 41
    Last Post: 09-17-2010, 05:14 AM
  4. Memory game
    By blank in forum C# Programming
    Replies: 5
    Last Post: 10-12-2009, 12:25 PM
  5. Memory Game C# VB 2005
    By thawkins in forum C# Programming
    Replies: 2
    Last Post: 06-07-2008, 11:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

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