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

C++ please dont use globar variables Children often play a memory game in which

ID: 3715923 • Letter: C

Question

C++ please dont use globar variables

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. Use appropriate functions in your program, and the main program should be merely a call to functions.

In order to play this game, the program will need to create a board with a random distribution of the card pairs. For each card location, the game will need to remember whether the card is face up or face down. Initially, all the cards are face down. If a matching pair is turned over by a player, those cards need to remain face up, if a matching pair is not turned over, the cards should go back to being face down. The card values can be indicated by numbers 1 - 8 with the face down position indicated by a 0 (or some other symbol). After each turn, the current state of the board should be "cleared" and the board redrawn showing the matches that have already been made and putting non-matches in the "face down" position. The game is played between two players. The game should be played in a loop until all the card pairs have been found.

At a minimum you will need functions to create the game board, display the game board, and play the game.

To create the game board you will need to use the rand() and srand() functions which are located in <cstdlib>. For srand you will also need to use the time() function from <ctime>. To review how to use these functions, look them up on cplusplus.com.

Include a debugging option with your function to display the board which will show the initial board configuration. Use a parallel array to keep track of which locations are face up and which locations are face down.

To play the game you will need functions to turn a card face up, see if the two face up cards match, and determine if the game is over. There are preconditions on the inputs in that you cannot allow a player to choose a card location that is out of bounds and you cannot allow a player to choose a location where the card is already face up.

Construct IPO charts for the main function and all your user-defined functions. Incorporate the IPO charts as comments in the code. Create a structure chart showing the functional decomposition of your program.

Explanation / Answer

#include <iostream>

#include <stdlib.h>

#include <windows.h>

using namespace std;

#define ROWS 4

#define COLS 4

#define CARDS (ROWS*COLS/2)

class MemoryGame{

private:

int array[ROWS][COLS];

int hidden[ROWS][COLS];

int pr, pc;

int prev_card;

int cards_revealed;

int moves;

public:

MemoryGame(void){

resetdeck();

}

//For shuffling the cards

void resetdeck(void){

int cnt[CARDS], card_no;

for( int i=0; i< CARDS; i++ )

cnt[i] = 0;

for( int i=0; i< ROWS; i++ )

for( int j=0; j< COLS; j++){

do{

card_no = rand() % CARDS+1;

}while ( cnt[card_no-1] == 2);

array[i][j] = card_no;

hidden[i][j] = 1;

cnt[card_no-1]++;

}

cards_revealed = 0;

pr =0;

pc =0;

prev_card =0;

moves =0;

}

int pickCard(int r, int c){

if( r<0 | r>= ROWS | c<0 | c>=COLS){

cout << "Index out of bounds"<<endl;

Sleep(1000);

return -1;

}

if( hidden[r][c] == 0){

cout<< "Card already in revealed position"<<endl;

Sleep(2000);

return -2;

}

if( prev_card == 0 ){

hidden[r][c] = 0;

pr = r;

pc = c;

prev_card = array[pr][pc];

}

else if( array[r][c] == prev_card ){

hidden[r][c] = 0;

hidden[pr][pc] = 0;

prev_card = 0;

pr=0;

pc=0;

cards_revealed+=2;

displaycards();

cout<<"===>Matched";

Sleep(2000);

}

else{

hidden[r][c] = 0;

displaycards();

cout<<"===>Unmatched";

Sleep(2000);

hidden[r][c] = 1;

hidden[pr][pc] = 1;

prev_card =0;

pr=0;

pc=0;

}

moves++;

if( cards_revealed == CARDS*2)

return 1;

else

return 0;

}

void displaycards(void){

for( int i=0; i< 50; i++)

cout<<endl;

for( int i=0; i<ROWS; i++){

for( int j=0; j<COLS; j++)

if( hidden[i][j])

cout<< "*" << " ";

else

cout << array[i][j] << " ";

cout<<endl;

}

cout<< "Cards Matched: "<<cards_revealed << " Moves: "<< moves<<endl;

}

};

int main()

{

MemoryGame* m = new MemoryGame();

int exit = 0;

int r, c, ret_code;

while(!exit){

m->displaycards();

cout << "Enter row: ";

cin >> r;

cout<< "Enter Col:";

cin >>c;

ret_code = m->pickCard(r-1, c-1);

if( ret_code == 1){

cout<<"GAME_OVER";

exit = 1;

}

};
return 0;

}

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