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;
}
|