Click here to Skip to main content
Click here to Skip to main content

Game Application "- Memory Plus +"

, 17 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple memory game

Introduction

I wanted to recall my knowledge in MFC, so after a two year break, I started with developing this application as Game software + Memory Plus - . It is a simple game application which you have probably come across. I have always been interested in coding game software. I initially got logic for implementing this memory game. Later I brought in an additional option and made it work gracefully. And I wanted to explore more of VC++ and MFC libraries so I did all development using only VC++ & MFC libraries.

About Game

Memory plus is a generic game and simple to play. The interface shows a set of bricks or buttons with no images. Upon clicking any one brick a particular image will be shown then followed with the next immediate click its image will be shown. Now if both the images match, then both the bricks will get cleared from the view. Else, the user has to try matching the images by remembering the matches. The rule of thumb is to clear all the bricks to finish the game with a minimal number of clicks and in a minimum time then log your name for a highscore.

The time will start once the game has been initiated — either by clicking on the "Start" menu or clicking any bricks. The game can be paused at any time, in which case time slice will also be stopped. The game can also be restarted by clicking on the "New" menu. Upon clicking the "Highscore" menu, highscore information will be available.

Game Option

The game provides the following options

  • Images: the images option provides user flexibility for changing the images used on the bricks at anytime even while playing the game or before starting the game.
  • Size: the size option has been provided to modify the even matrix either to reduce or increase the bricks area.

    Note: If the size is modified, the game will restart as new game.

  • "Game help" will provide a brief instruction for the user regarding the game rules and options.

Using the Code

Understanding the code will be easy for the user. This code will guide you on how to:

  1. Write a small game application using MFC.
  2. Load image on buttons.
  3. Load image on text control.
  4. Load image on dialog background.
  5. Changes background color and text color of the static and text control.

Core code of the game:

void CSundar_GameDlg::LoadIcons(int NoBtns)
{
    int hIndex, index1;
    int ran;

    index1 = hIndex = 0;
    ran = 0;

    srand( (unsigned)time( NULL ) );
    for(int index = 0; index < (NoBtns) ; index++)
    {
        do
        {
            ran = rand();
            ran %= (NoBtns);
            index1 = 0;
            for(index1 = 0; index1 < index ; index1++)
            {
                if(ran == Array[index1])
                {
                    index1 = -1;
                    ran = rand();
                    ran %= (NoBtns);
                    continue;
                }
            }
        }while ( ran < 0 || ran >= NoBtns);

        Array[hIndex] = ran;

        hIndex++;
    }

    for(index = 0; index < NoBtns ; index++)
    {
        if(Array[index] >= (NoBtns/2))
        {
            Array[index] -= (NoBtns/2);
        }
    }
}

void CSundar_GameDlg::CheckMatchIcon(int BtnIndex)
{
    NumClick++;

    if(Init == FALSE)
    {
        Init = TRUE;
        OnGamePause();
    }

    if(ClickNo > 2)
    {
        DisplayIcon(CurBtnIndex, NULL);
        DisplayIcon(SelBtnIndex, NULL);
        SelBtnIndex = BtnIndex;
        CurBtnIndex = BtnIndex;
        DisplayIcon(CurBtnIndex, hIcon[Array[BtnIndex]]);
        ClickNo = 2;
    }
    else
        if(ClickNo == 2)
        {
            CurBtnIndex = BtnIndex;
            DisplayIcon(CurBtnIndex, hIcon[Array[BtnIndex]]);
            ClickNo++;

            if(CurBtnIndex != SelBtnIndex)
            {
                unsigned int diff = Array[CurBtnIndex] - Array[SelBtnIndex];
                if( diff == 0)
                {
                    DisplayIcon(CurBtnIndex, NULL, TRUE);
                    DisplayIcon(SelBtnIndex, NULL, TRUE);
                    if(CheckNoOfButton <= 0)
                    {
                        DlgState = 1;
                        Init = FALSE;
                        KillTimer(2);

                        TimeScore = Min;
                        NoOfClick = NumClick;
                        CScoreDlg Dlg;
                        Dlg.DoModal();

                        DlgState = 2;
                        Dlg.DoModal();

                    }
                }
                else
                {
                    SetTimer(1, 1500, NULL);
                }
            }
        }
        else if(ClickNo == 1)
        {
            SelBtnIndex = BtnIndex;
            CurBtnIndex = BtnIndex;
            ClickNo++;

            DisplayIcon(CurBtnIndex, hIcon[Array[BtnIndex]]);
        }
}

Conclusion

I would like to thank all my friends and colleagues who gave better ideas and suggestions in making this game better and more user friendly. Hope you enjoy this game.

License

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

Share

About the Author

Shanmuga Sundar.V
Architect
India India
Module Lead working in leading MNC company, india - bangalore.

Comments and Discussions

 
GeneralUI needs more professionalism. PinmemberSreekanth Muralidharan5-Oct-08 23:55 
GeneralRe: UI needs more professionalism. PinmemberShanmuga Sundar.V14-Oct-08 21:55 
GeneralDone a good job... Pinmemberjalaldrg22-Jul-08 5:06 
GeneralHI PAN SHOD PinmemberMrGoodly18-Jul-08 16:58 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 17 Jul 2008
Article Copyright 2008 by Shanmuga Sundar.V
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid