Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A common memory matching game played by young children is to start with a deck o

ID: 3671169 • Letter: A

Question

A common memory matching game played by young children is to start with a deck of cards that contains identical pairs. For example, given six cards in the deck, two might be labeled 1, two labeled 2 and two labeled 3. The cards are shuffled and placed face down on a board. A player then selects two cards that are face down, turns them face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all cards are face up.

Write a program that plays the memory matching game. When it start, the program prompts the user for the number of rows and columns for the game board that contains the cards. The total number of cards must be even. Assume that the board dimensions are at most 8 by 9 or 9 by 8. Your cards must be numbered from 1 through (number of rows * number of columns) / / 2. Your program allows the player to specify the cards that she would like to select through a coordinate system as shown in the sample run below. All the cards that are face down are indicated by *. For example, in the following layout of a 4 by 4 game board

the pair of 8 which are face up are at coordinates (1,1) and (3,2). Pay attention to how the coordinates are specified: row number followed by column number, both start at index 1, not zero. A sample run of the game is as follows:

Another sample run is as follows:

A third sample run is as follows:

Design Requirments: You need to use three classes: Card, Deck and Game. Card stores both the card's value and face (a string or Boolean variable to indicate whether the card is facing up or down). Deck contains the cards needed for the game. It will contain among its methods a method to deal a card, another for shuffling the deck, and a method that returns number of cards left in the deck. These two classes are not identical to the classes Card and Deck discussed in the book but have many things in common. The class Game simulates playing a single game and represents the interaction between the user and the other classes. Its instance members store a 2D list (of card objects) representing the game board where the cards are placed, number of rows and number of columns of the game board. Among the instance methods of the Game class: play(), which simulates playing the game; isGameOver(), which checks whether or not the game is over; displayBoard(), which displays the board; populateBoard(), which creates the initial 2D list of identical pairs of cards with all the cards facing down. Most probably, you will need to write other instance methods as you see appropriate.    

Use the following test program:

The format of the output and input of your program must be the same as what is shown in the sample runs above.

Explanation / Answer

Hi i written a sample program for memory matching game for your reference,

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int X = 4, Y = 4;

void arrangement(int cards[][Y], bool faceUp[][Y]);
void showCards(int cards[][Y], bool faceUp[][Y], int card1Row, int card1Col, int card2Row, int card2Col);
void shuffle(int cards[][Y]);
void pickCard(int cards[][Y], bool faceUp[][Y]);

int _tmain(int argc, _TCHAR* argv[])
{
int cards[X][Y];
bool faceUp[X][Y], gameOver = false;
char ans = 'Y';

//The code filling the array grade goes here, but is not shown.

cout << "Would you like to play the memory matching game? ";
cin >> ans;

do{


arrangement(cards, faceUp);
shuffle(cards);

do{
showCards(cards, faceUp, -1, -1, -1, -1);
pickCard(cards, faceUp);
gameOver = true;
for(int x = 0; x < X; ++x)
{
for(int y = 0; y < Y; ++y)
{
if (!faceUp[x][y])
//game isn't over
gameOver = false;

}
cout << endl;
}
}while(gameOver == false);
cout << "Would you like to play again?" << endl;
cin >> ans;
}while (ans == 'y' || ans == 'Y');
return 0;

}
void arrangement(int cards[][Y], bool faceUp[][Y])
{
for (int x = 1; x <= 2; x++)
{
for (int y = 1; y <= Y; y++){
cards[x-1][y-1] = y;
faceUp[x-1][y-1] = false;
}

for (int x = 3; x <= Y; x++){
for (int y = 1; y <= Y; y++){
cards[x-1][y-1] = y+4;
faceUp[x-1][y-1] = false;
}
}
}
}

void shuffle(int cards[][Y])
{

int x1, y1, x2, y2, temp, i = 0 ;
srand(time(NULL));
do{
x1 = rand() % 4;
y1 = rand() % 4;
x2 = rand() % 4;
y2 = rand() % 4;
temp = cards[x1][y1];
cards[x1][y1] = cards[x2][y2];
cards[x2][y2] = temp;
i++;
}while(i < 50);

}

void showCards(int cards[][Y], bool faceUp[][Y], int card1Row, int card1Col, int card2Row, int card2Col)
{
for(int x = 0; x < X; ++x)
{
for(int y = 0; y < Y; ++y)
{
if (faceUp[x][y] || (x == card1Row && y == card1Col) || (x == card2Row && y == card2Col))
cout << cards[x][y];
else
cout << "*";
cout << " ";
}
cout << endl;
}


}
void pickCard(int cards[][Y], bool faceUp[][Y])
{

char ans;


int card1Row, card1Col, card2Row, card2Col;
cout << "Enter row of card 1" << endl;
cin >> card1Row;
cout << "Enter column of card 1" << endl;
cin >> card1Col;
cout << "Enter row of card 2" << endl;
cin >> card2Row;
cout << "Enter column of card 2" << endl;
cin >> card2Col;

showCards(cards,faceUp, card1Row, card1Col, card2Row, card2Col);
if(cards [card1Row][card1Col] == cards [card2Row][card2Col]){
cout << "The cards match" << endl;
faceUp [card1Row][card1Col] = true;
faceUp [card2Row][card2Col] = true;
}

else
cout << "The cards do not match" << endl;
cout << "Make a well-educated guess" << endl;
cin >> ans;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote