In C++ Children often play a memory game in which a deck of cards containing mat
ID: 3856466 • Letter: I
Question
In C++
Children often play a memory game in which a deck of cards containing matching pairs is used. The cards are shuffled and placed face down on a table. The players then 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 positions. 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 and 4 columns for a deck of 16 cards with 8 matching pairs. You can use numbers 1 to 8 to mark the cards. (If you use a 6 by 6 array, then you will need 18 matching pairs, and so on.) Use random number generators to randomly store the pairs in the array. Design a modular program that allows two players to play this game on a computer.
Needed.
Your structure chart as a screen shot or picture
source code for with
The name of your program as a comment at the top of the file
Your IPO chart incorporated as comments after the name of the file
IPO charts for your functions as comments before the prototypes of your functions
A screen shot or picture of the results after running your program with your test data
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int ROWSIZE = 4;
const int COLSIZE = 4;
int i = 0;
int j = 0;
int x = 0;
int k = 0;
int p = 0;
int m = 0;
int n = 0;
int cardFront[ROWSIZE][COLSIZE];
char cardBack[ROWSIZE][COLSIZE];
int row1, col1, row2, col2, count = 0;
bool allCardsOpened;
srand(time(0));
// Displays back of cards
cout<<"Back of Cards..."<<endl;
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
cardBack[i][j] = '#';
cout << cardBack[i][j]<<" ";
}
cout << endl;
}
cout<<endl<<"Front of Cards..."<<endl;
// displays front of cards
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
if (i < 2)
{
cardFront[i][j] = COLSIZE * i + j + 1;
}
else
{
cardFront[i][j] = (COLSIZE * i + j + 1) - 8;
}
cout << cardFront[i][j]<<" ";
}
cout << endl;
}
// randomizing numbers
for (i = 0; i < 10; i++)
{
k = rand() % 4;
p = rand() % 4;
m = rand() % 4;
n = rand() % 4;
x = cardFront[k][p];
cardFront[k][p] = cardFront[m][n];
cardFront[m][n] = x;
}
//Lets not show this to the players and let them play from here onwards...
//This code should be concealed from here...
/*cout<<"After randomizing, Front of Cards..."<<endl;
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
cout << cardFront[i][j]<<" ";
}
cout << endl;
} */
//This code should be concealed till here...
cout<<"Welcome to the game of Memory...";
while(1)
{
allCardsOpened = true;
for(i = 0; i < ROWSIZE; i++)
for(j = 0; j < COLSIZE; j++)
if(cardBack[i][j] == '#')
allCardsOpened = false;
if(allCardsOpened == true)
{
cout<<"Game Over. You took "<<count<<" turns to finish the game."<<endl;
exit(0);
}
count++;
cout<<"Select any 2 cards: "<<endl;
cout<<"Card 1 (row, col): ";
cin>>row1>>col1;
cout<<"Card 2 (row, col): ";
cin>>row2>>col2;
if(row1 >= ROWSIZE || row2 >= ROWSIZE || col1 >= COLSIZE || col2 >= COLSIZE)
{
cout<<"This board is of size: "<<ROWSIZE<<" x "<<COLSIZE<<"."<<endl;
}
if(row1 == row2 && col1 == col2)
{
cout<<"Please select two different positions...."<<endl;
continue;
}
if(cardBack[row1][col1] != '#' || cardBack[row2][col2] != '#')
{
cout<<"Please select a position that is not opened...."<<endl;
continue;
}
if(cardFront[row1][col1] == cardFront[row2][col2])
cardBack[row1][col1] = cardBack[row2][col2] = '0'+cardFront[row1][col1];
else
{
cout<<"Card1: "<<cardFront[row1][col1]<<" ";
cout<<"Card2: "<<cardFront[row2][col2]<<" ";
}
cout<<"Current Board State: "<<endl;
for(i = 0; i < ROWSIZE; i++)
{
for(j = 0; j < COLSIZE; j++)
cout<<cardBack[i][j]<<" ";
cout<<endl;
}
}
return 0;
}