Memory Game Program

Nov 6, 2008 at 4:59am
I need some help with this problem:

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.

I wrote most of the program but for some reason nothing happens once the values for the selected card are entered.

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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;
}
Dec 15, 2008 at 7:47pm
Today I got the same proyect for my class. Does anybody knows how to finish this program? Thanks
Dec 15, 2008 at 8:16pm
Yeah, I know how to finish it, but you can try working on it yourself (and not copying others' code).

@dm10: Hmm, your code looks correct...I can't see anything wrong with it :/
Although I would suggest you avoid using 1, l, O, and 0 in variable names because they can look like other each other.
Dec 16, 2008 at 4:27am
 
cards[r][c]=rand()%8+1;


the way you initialize the cards isn't correct, wasn't there supposed to be pairs of numbers? but as you have done the same number could turn up 16 times!

well you can solve this in multiple ways in more or less complex/elegant ways. you have started in a non-OOP approach but anyway you should structure up your program a bit. e.g. create one function that displays the board, another to enter coordinates. it is easier to read instead of having one big function.

Something like

1
2
3
4
5
6
7
create_board( cards )
shuffle( cards )
while( more_cards_left( cards ) )
{
   display_board( cards );
   enter_coordinates( cards );
}


e.g. (again, u can do this in multiple ways)

1
2
3
4
5
6
7
8
9
10
11
12
13
void create_board( int cards[4][4] )
{
  int pairs[16] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
  int p = 0;

  for (int r = 0; r < 4; ++r )
  {
     for (int c = 0; c < 4; ++c )
    {
      cards[r][c] = pairs[p++];
    }
  }
}


Last edited on Dec 16, 2008 at 4:47am
Topic archived. No new replies allowed.