Question: C++ prgramming There is a memory game that children play in which a deck of cards containing matching pairs is used. The cards are shuffled
C++ prgramming
There is a memory game that children play in which a deck of cards containing matching pairs is used. The cards are shuffled and placed face down on a table. The players take turns and select two cards at a time. If both cards match, they are left face up; otherwise, the cards are placed face down at the same position. Once the players see the selected pair of cards and if the cards do not match, then they can memorize the cards and use their memory to select the next pair of cards. The game continues until all the cards are face up.
Write a program to play the memory game. Use a two-dimensional array of 4 rows by 4 columns with a deck of 16 cards (8 matching pairs). Use the random number generator to store the cards into the array. Use matching pairs of letters (A through H) for the card names. For cards not turned overuse '*'. Display the array and ask the user to select two cards by giving array's row and column. Display the array with the two cards exposed. If the two cards don't match, tell the user and then display the array again with the two cards no longer turned over. If the two cards do match, leave them exposed. Play continues until all cards are exposed.
//My problem: How can I display matching pairs of letters (A through H) for the card names.
#include
#include
#include
using namespace std;
void shuffle(int[][4]);
int main() {
char comma, ans = 'y';
int r1, c1, r2, c2, cards[4][4], i(0);
bool cardstatus[4][4];
bool gameover = false;
int moves;
do {
moves = 0;
shuffle(cards);
cout << " 1 2 3 4 ";
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 << "* ";
cardstatus[r][c] = false;
}
cout << endl;
}
cout << endl;
do {
do {
cout << "Please insert the first card row and column seperated by a comma. ";
cin >> r1 >> comma >> c1;
if (cardstatus[r1 - 1][c1 - 1] == true) {
cout << "This card is already flipped! Select another card.";
}
} while (cardstatus[r1 - 1][c1 - 1] != false);
do {
cout << "Please insert the second card row and column separated by a comma. ";
cin >> r2 >> comma >> c2;
if (cardstatus[r2 - 1][c2 - 1] == true) {
cout << "This card is already flipped! Select another card.";
}
if ((r1 == r2) && (c1 == c2)) {
cout << "You can't select same card twice!";
continue;
}
} while (cardstatus[r2 - 1][c2 - 1] != false);
r1--;
c1--;
r2--;
c2--;
//reveal
cout << " 1 2 3 4 ";
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 if (cardstatus[r][c] == true) {
cout << cards[r][c] << " ";
}
else {
cout << "* ";
}
}
cout << endl;
}
if (cards[r1][c1] == cards[r2][c2]) {
cout << "Cards Matched!" << endl;
cardstatus[r1][c1] = true;
cardstatus[r2][c2] = true;
}
cin.get();
cin.get();
for (int b = 0; b <= 20; b++)
cout << endl;
for (int r = 0; r<4; r++) {
cout << r + 1 << " | ";
for (int c = 0; c<4; c++) {
if (cardstatus[r][c] == true) {
cout << cards[r][c] << " ";
}
else {
cout << "* ";
}
}
cout << endl;
}
gameover = true;
for (int r = 0; r<4; r++) {
for (int c = 0; c<4; c++) {
if (cardstatus[r][c] == false) {
gameover = false;
break;
}
}
if (gameover == false) {
break;
}
}
moves++;
} while (gameover != true);
cout << "Congratulations, You won!!!" << endl;
cout << "It Required " << moves << " moves to finish it." << endl << endl;
cout << "Would you like to play again? (y=Yes/n=No) : ";
ans = cin.get();
} while (ans == 'y' || ans == 'Y');
cin.get();
return 0;
}
void shuffle(int cards[][4]) {
int start[16] = { 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8 };
for (int s = 0; s <= 20; s++)
{
for (int x = 0; x<16; x++) {
srand((unsigned)time(NULL));
int i = rand() % 15 + 1;
int temp = start[x];
start[x] = start[i];
start[i] = temp;
}
}
int i = 0;
for (int r = 0; r<4; r++) {
for (int c = 0; c<4; c++) {
cards[r][c] = start[i];
cout << cards[r][c];
i = i + 1;
}
cout << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
