Hi guys, I have to make a program for my programming class and I have no idea of how to do it. Can someone help me?
Here's the program that I have to make in C++.

A common memory matching game played by children is to start with a deck of identical pairs face down on a table. A player selects two cards and turns them face up. If they match they remain face up. If they don’t match they are flipped face down. The game continues until all of the cards are face up.

Write a program that plays the memory game. Use 16 cards laid out in a 4 x 4 square and labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that he or she would like to select through a coordinate system.

For example, suppose the cards are in the following layout:
1 2 3 4
-----------
1 | 8 * * *
2 | * * * *
3 | * 8 * *
4 | * * * *

All of the cards are face down except the pair of 8 which the player found by entering (1,1) and (2,3). To hide cards that have been placed temporarily up, output a large number of blank lines to force the old board off the screen.

Hint: Use a two-dimensional array for the arrangement of cards and another two-dimensional array that indicates if a card is face up of face down. Write a function that “shuffles” the cards in the array by repeatedly selecting two cards at random and swapping them.

Recommended Answers

All 4 Replies

Read this closely.

No one is going to post-fed you a solution to your homework, so show some effort or prepare to be ignored.

After all it's your programming class, not ours.

Ok, I started and have an idea but it doesn't work. Here's what I have:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    char comma;
    int r1, c1, r2, c2, cards[4][4];
    srand((unsigned)time(NULL));
    //fill board
    for (int r=0; r<4; r++)
    {
        for (int c=0; c<4; c++)
        {
            cards[r][c]=rand()%8+1;
            cout<<cards[r][c];
        }
        cout<<endl;
    }
    //display board
    cout<<"    1 2 3 4\n";
    cout<<"  ";
    for (int i=0; i<=8; i++)
    {
        cout<<"-";
    }
    cout<<endl;
    for (int r=0; r<4; r++)
    {
        cout<<r+1<<" | ";
        for (int c=0; c<4; c++)
        {
            cout<<"* ";
        }
        cout<<endl;
    }
    cout<<endl;
    //selection
    cout<<"Please insert the first card row and column seperated by a comma.\n";
    cin>>r1>>comma>>c1;
    cout<<"Please insert the second card row and column seperated by a comma.\n";
    cin>>r2>>comma>>c2;
    //fix
    r1--;
    c1--;
    r2--;
    c2--;
    //reveal
    cout<<"    1 2 3 4\n";
    cout<<"  ";
    for (int i=0; i<=8; i++)
    {
        cout<<"-";
    }
    cout<<endl;
    for (int r=0; r<4; r++)
    {
        cout<<r+1<<" | ";
        for (int c=0; c<4; c++)
        {
            if ((r==r1)&&(c==c1))
            {
                cout<<cards[r][c]<<" ";
            }
            else if((r==r2)&&(c==c2))
            {
                cout<<cards[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
    //match?
    if (cards[r1][c1]==cards[r2][c2])
    {
    }
    else
    {
    }
    //this pushes the next board onto a blank screen
    for (int b=0; b<=20; b++)
        cout<<endl;
    //repeat
    return 0;
}

Read the bulletin about code tags too.

Chris

As a note, I love using functions to help break things up. You have several things which would support that well.

1) The 'assignment' asks you to fill the board with pairs of numbers from 1-8. On line 15, you fill with a random number from 1 to 8. Where are the pairs?

2) The hints recommended a second array to indicate whether or not a specific card was hidden. That's what you should be updating.
If the two cards match, leave the cards face up, if not, turn them back face down.

You should probably be doing more input validation. (Can the player 're-pick' something where there has already been a match?)

Add a 'while there are cards to match' loop to the code.

commented: Nicely put +25
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.