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

Objective To understand and implement software using arrays and previously learn

ID: 3539729 • Letter: O

Question

Objective

To understand and implement software using arrays and previously learned programming constructs.

Instructions

For this assignment you will
create a Memory Game similar to the card game that many of us probably
played as kids. For the Memory Game, you will allow for the user to select an
index of an array of characters and try to find the matching character. If the
guess is wrong, you %u201Cflip the card back over%u201D and do not reveal it. If they get
a matching pair, then you display both of the characters. You should keep track
of how many guesses (rounds) the user requires to unveil all of the matches.

The array will have 10
elements, thus, five pairs. You could choose A,B,C,D, and E just to keep things
simple or use a different set of characters. Behind the scenes, you may have an
array that looks like this, for example:


[ %u2018A%u2019 %u2018B%u2019 %u2018B%u2019 %u2018A%u2019 %u2018C%u2019 %u2018D%u2019 %u2018D%u2019%u2018C%u2019 %u2018E%u2019 %u2018E%u2019 ]


Or anything of the sort. However, the user will (at startup) see the following:

[ - - - - - - - - - - ]


To indicate that none of the %u201Ccards%u201D have been unveiled yet.

Print the results at each stage of interaction with the user. For example, a sample interaction could be as follows:

[ - - - - - - - - - - ] Tries: 0

>>0

[ A - - - - - - - - - ] Tries: 1

>>1

[ A B - - - - - - - - ] Tries: 2

No match, sorry.

[ - - - - - - - - - - ]

>>3

[ - - - A - - - - - - ] Tries: 3


>>0

[ A - - A - - - - - - ] Tries: 4

You%u2019ve made a match! Please continue!

[ A - - A - - - - - - ]

Etc.

So the user continues playing until all the letters are recovered. At the end you should say, %u201Ccongratulations, you%u2019ve matched all the cards in someNumber tries!

Recommendations/Hints

Use parallel arrays One contains the actual letters One is used to
display the appropriate cards that have been revealed (or not, i.e. %u2018-%u2018 back to
the user)

You should design a class called CMemoryGame to maintain the state of the
game, interact with the user, and determine winning state. Failure to use a
class will result in significant loss of points. You must also write a driver in main.cpp that will use objects of type CMemoryGame that will allow me to test your programs.

Explanation / Answer

/* You can reuse the logic in this very easily . Please make changes as required*/



#include<iostream.h>

#include<string.h>



void main()

{

int cardnum;

CMemoryGame o;

string orig[10];


o.prinCards();

origCards[0]="A";

origCards[1]="B";

origCards[2]="C";

origCards[3]="D";

origCards[4]="E";

origCards[5]="A";

origCards[6]="B";

origCards[7]="C";

origCards[8]="D";

origCards[9]="E";

/* This is only for example, you can use your own method to set the original cards list */

o.setArray(origCards);

while(o.gameover!=1)

{

cin>> cardnum;

o.showCard(cardnum);

}

getch();



}


class CMemoryGame

{

public:

string original[10];

string tempArray[10];

int paircounter=0;

int firstcard=0;

int checkcount=0;

int turncount=0;

int gameover=0;


public void setArray(string [] setOriginal)

{

original = setOriginal;

tempArray[0]="-";

tempArray[1]="-";

tempArray[2]="-";

tempArray[3]="-";

tempArray[4]="-";

tempArray[5]="-";

tempArray[6]="-";

tempArray[7]="-";

tempArray[8]="-";

tempArray[9]="-";

}


public string printCards()

{

string stemp="[";

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

{

stemp += " "+tempArray[i];

}

stemp +=" ] "+ "Tries : "+ turncount;

  

cout << stemp;

}


  

public showCard(int i)

{

turncount++;

int j=0;

// first card displayed

if(paircounter ==0)

{

tempArray[i] = original[i];

firstcard=i;

printCards();

}   

  

// second card displayed and rsult checked

else (if paircounter==1)

{

tempArray[i] = original[i];

// if cards match

if(original[i]==original[firstcard])

{

printCards();

for(j=0;j<10;j++)

{

if(tempArray[j]=="-")

checkcount=1;

break;

}   

// if all are completly matched , exit mainloop

if(checkcount==0)

{

cout << "Congratulations.. You have matched all cards in "+ turncount + " tries";

gameover=1;

}

else

{

cout << "You have made a match! Please Continue";

paircount=0;

firstcards=0;

}

}

// if cards dont match, reset the current two cards

else

{ printCards();   

cout << "No Match, Sorry";

tempArray[i]="-";

tempArray[firstcard]="-";

}

}

}

  

}