Click here to Skip to main content
6,822,613 members and growing! (15,733 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » Games     Beginner License: The Code Project Open License (CPOL)

"Memory". Playing in order to improve your memory.

By Gerard Castelló Viader

Making a simple game in C# and WinForms.
C#, .NET (.NET3.5), Visual-Studio (VS2008), WinForms, Dev
Posted:29 Nov 2009
Views:1,811
Bookmarked:4 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.17 Rating: 3.60 out of 5

1

2
2 votes, 50.0%
3
1 vote, 25.0%
4
1 vote, 25.0%
5

Introduction

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

memory

Now, I have developed a similar game. It is easy, but if you want, you can do 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 it have a two-dimensional matrix. The bounds of this matrix will depend of 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 number 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++;
                }
            }
        }

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


Member
I've been a programmer since 2002 using languages such as C/C++, JAVA, C#, VB.NET, T-SQL and others.

By the moment, I'm looking for a job in London!
Occupation: Software Developer
Location: Spain Spain

Other popular Game Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 29 Nov 2009
Editor:
Copyright 2009 by Gerard Castelló Viader
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project