Question
answer in basic C++ this is for a intro class no adavanced stuff past iostream, strings,ctime,functions,arrays all the basics please

12:48 くCSC 2000 W19 Project 02 ビ Problem 0I: Memory Game (30 points) Children often play a memory game in which a deck of can
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:;

#include <iostream>
#include <stdlib.h>

using namespace std;
void displayArray(char array[][4]); //function to display array
bool gameFinish(char array[][4]); //check for game finish
int main()
{
   int array[4][4]; //array of size 4
   char arrayStar[4][4]; //array of size 4 for display *
   char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //convert int to char
   int i=0,j=0;
   for(i=0;i<4;i++){
for(j=0;j<4;j++){
           array[i][j]=0;
           arrayStar[i][j]='*';   //initialize both array
       }
}
   int row=0,col=0; //rand row
   bool insert = true; //rand col
   for(i=1;i<=8;i++){ //for 1 to 8

       insert= true; //initialize variable
       while(insert){

           row = rand()%4; //generate rand of 4
           col = rand()%4;
           if(array[row][col]==0){ //if data is zero at rand position put number there
               insert = false;
           }
       }
       array[row][col] = i; //putting number
       insert = true; //again same step for second pair
       while(insert){

           row = rand()%4;
           col = rand()%4;
           if(array[row][col]==0){
               insert = false;
           }
       }
       array[row][col] = i;

   }

   displayArray(arrayStar); //display * array
   int choiceRow1,choiceCol1,choiceRow2,choiceCol2;
   do{
       bool inputFlag= true;


       do{
           cout<<"Enter row(1 to 4) or column ( 1 to 4) position of the pair";
           cin>>choiceRow1>>choiceCol1;
           if(choiceRow1<1 || choiceRow1>4 || choiceCol1<1 || choiceCol1>4 ){
               cout<<"Invalid position."<<endl;
           }else if(arrayStar[choiceRow1-1][choiceCol1-1] !='*'){
       cout<<"Card at this position already faced up. Select position again."<<endl;
           }else{
               inputFlag = false;
           }
       }while(inputFlag); //validate until valid value


       arrayStar[choiceRow1-1][choiceCol1-1] = digits[array[choiceRow1-1][choiceCol1-1]]; //replace star array by array value that is generated randomly
       displayArray(arrayStar);
       inputFlag = true;
   do{
           cout<<"Enter row(1 to 4) or column ( 1 to 4) position of the pair";
           cin>>choiceRow2>>choiceCol2;
           if(choiceRow2<1 || choiceRow2>4 || choiceCol2<1 || choiceCol2>4 ){
               cout<<"Invalid position."<<endl;
           }else if(arrayStar[choiceRow2-1][choiceCol2-1] !='*'){
       cout<<"Card at this position already faced up. Select position again."<<endl;
           }else{
               inputFlag = false;
           }
       }while(inputFlag); //validate until valid value


   arrayStar[choiceRow2-1][choiceCol2-1] = digits[array[choiceRow2-1][choiceCol2-1]];//replace star array by array value that is generated randomly
   displayArray(arrayStar);


   if(array[choiceRow1-1][choiceCol1-1]!=array[choiceRow2-1][choiceCol2-1]){//if not match print and replace again to *
       cout<<"pair did not match! select again"<<endl;
       arrayStar[choiceRow1-1][choiceCol1-1] ='*';
       arrayStar[choiceRow2-1][choiceCol2-1] ='*';
           displayArray(arrayStar);
   }else{ //else pair match
       cout<<"pair match"<<endl;
           displayArray(arrayStar);
   }

   }while(!gameFinish(arrayStar));//do until game finish

   return 0;
}

void displayArray(char array[][4]){//display array
   int i,j;
   for(i=0;i<4;i++){
       for(j=0;j<4;j++){
           cout<<array[i][j]<<"\t";
       }
       cout<<endl;
   }
   cout<<endl;
}

bool gameFinish(char array[][4]){ //check for game finish
   int i,j;
   for(i=0;i<4;i++){
       for(j=0;j<4;j++){
       if(array[i][j]=='*'){
           return false;
       }
       }
       cout<<endl;
   }
   return true;
   cout<<endl;
}

output:

E Console CPP_Workspace.exe [C/C++ Application] C:\Users Mohammad Shahrukh\Desktop\c_language_workspaceleclipse c\CP Choi ceg2 Enter row (1 to 4) or column 1 to 4) position of the pair3 1 Card at this position already faced up. Select position again.

Add a comment
Know the answer?
Add Answer to:
Answer in basic C++ this is for a intro class no adavanced stuff past iostream, strings,ctime,fun...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT