Thread: Memory Game!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Angry Memory Game!

    Hi~~~ I am doing my computer project.......
    and...I need your help!
    this computer project is about "Memory Game"

    this is following:

    There are 20 cards. Each card has a number between 1- 10
    each numbers occurs twice. Look at two cards,if they are the same, they are marked as taken. Game goes on until all cards are marked as taken

    e.g of output:

    "Enter two cards to be looked at:" // then user put 2 numbers

    3,5

    "card 3 has value of 7"
    "card 5 has value of 6"


    "Enter two cards to be looked at:"//user put 2 numbers again
    3,6

    "Both cards have value of 7,You found a pair!"

    "Enter two cards to be looked at:"//user put 2 numbers again
    3,4

    "card 3 was already turned out!"



    Please give me a solution..............

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    First, you need a structuce:
    Code:
    struct Card
    {
        int number, value;
        bool found;
    };
    And initialization:

    Code:
    Card deck[20];
    for(int i = 0;  i < 20;  i++)
    {
        deck[i].found = 0;
        deck[i].number = i;
        if(i < 10)
            deck[i].value = i+1;
        else
            deck[i].value = i+1-10;
    }
    Perhaps a shuffle:

    Code:
    Card temp;
    int swap;
    for(int i = 0;  i < 123;  i++)
    {
        temp = deck[rand()%20];
        for(;;)
        {
            i = rand()%20;
            if(i != swap.number)
                break;
        }
        deck[temp.number] = deck[swap];
        deck[swap] = temp;
    }
    (write in a hurry - beware of errors!)

    Now just flag the cards that have been found and compare the values to find a pair.

    Code:
    get input to a and b
    if((deck[a].value == deck[b].value) && (deck[a].found = 0))
    {
        Match found!
        deck[a].found = deck[b].found = 1;
    }
    And for a final check

    Code:
    int i;
    for(i = 0;  i < 20;  i++)
        if(deck[i].found == 0)
            break;
    if(i == 19)
        all cards found

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. A snake game - Memory problem
    By gavra in forum C Programming
    Replies: 29
    Last Post: 11-23-2008, 12:58 PM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM