Click here to Skip to main content
Licence CPOL
First Posted 29 Nov 2009
Views 6,322
Downloads 376
Bookmarked 9 times

"Memory". Playing in Order to Improve your Memory

By | 29 Nov 2009 | Article
Making a simple game in C# and WinForms

Introduction

When I was a child, I used to play a game called "Memory". It consisted of finding couples of images hidden among a mixture of items. It was funny and it served to improve your memory.

memory

Now, I have developed a similar game. It is easy, but if you want, you can make it more difficult, increasing the number of similar items that you have to search or decreasing the number of allowed mistakes.

memory

Background

Well, it is simple.

There is the class Table that has a two-dimensional matrix. The bounds of this matrix will depend on the number of groups that the user had configured. When the app is thrown, this class is instantiated in order to fill this matrix with a group of random numbers from 1 to 10. Each group has one number defined.

Then, the app generates the GUI, creating one panel per number. At the same time, it links the OnClick event for every panel created.

At the same time that you are discovering items, each panel is put into a collection. When the number of tries for each turn is completed, the items into the collection are compared.

If they are equal, they stay unconcealed, by the contrary, they are removed from the collection and the panels are hidden again.

The game lasts until the number of errors allowed is exceeded.

Using the Code

Filling the matrix with random numbers:

private void Initialize()
{
    Random rnd = new Random();
    
    int x = 0;
    int y = 0;
    int currentNum = 0;
    int totalNums = (this.panel.GetLength(0) * this.panel.GetLength(1)) / this.groups;
    
    while (x < this.panel.GetLength(0))
    {
        while (y < this.panel.GetLength(1))
        {
            do
            {
                currentNum = rnd.Next(1, totalNums+1);
            }
            while (CheckGroup(currentNum, this.groups));
            this.panel[x, y] = currentNum;
            y++;
        }
        y = 0;
        x++;
    }
}

Creating the GUI:

private void ShowTable()
{
    int x = 0;
    int y = 0;
    int xStart = 20;
    int yStart = 30;
    
    int width = this.tb.Panel.GetLength(1);
    int height = this.tb.Panel.GetLength(0);
    
    this.Width = width * 50;
    this.Height = height * 62;
    
    while (x < height)
    {
        while (y < width)
        {
            Panel p = new Panel();
            p.Name = x.ToString() + "-" + y.ToString();
            p.BorderStyle = BorderStyle.Fixed3D;
            p.Location = new Point(xStart, yStart);
            p.Size = new Size(40, 40);
            p.BackColor = Color.Gray;
            p.Click += new EventHandler(this.ClickPanel);
            this.Controls.Add(p);
            xStart += 40;
            y++;
        }
        
        yStart += 40;
        xStart = 20;
        y = 0;
        x++;
    }
}

Interacting:

private void ClickPanel(object sender, EventArgs e)
{
    Panel p = (Panel)sender;
    if (!p.HasChildren)
    {
        string[] axis = p.Name.Split('-');
        int x = int.Parse(axis[0]);
        int y = int.Parse(axis[1]);
        
        PictureBox pBox = this.GetNumber(this.tb.Panel[x, y]);
        p.Controls.Add(pBox);
        p.BackColor = Color.Beige;
        this.Refresh();
        this.openPanels.Add(p);
        
        if (this.find == 0)
        {
            this.find = this.tb.Panel[x, y];
            this.tries++;
        }
        else if (this.tries == this.groups)
        {
            if (!this.AreEqual())
            {
                this.CleanMistake();
                this.SetMistake();
                if (awdMistakes == mistakes)
                {
                    if (MessageBox.Show("YOU LOSE!" + 
			Environment.NewLine + "A new game will start!", "Lose",
                        	MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == 
			DialogResult.OK)
                        this.Restart();
                }
            }
            this.find = 0;
            this.tries = 1;
            this.openPanels.Clear();
        }
        else
        {
            this.tries++;
        }
    }
}

History

  • 29th November, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gerard Castelló Viader

Software Developer
BlueIT
Spain Spain

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberSagar6191:14 5 Nov '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 29 Nov 2009
Article Copyright 2009 by Gerard Castelló Viader
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid