Question
can someone solve this program using c++ (visual studio)?

You have a grid of 4x4 cells which is filled by numbers from 1 to 8. Each number appears twice in the grid. The purpose of th
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstdlib>

using namespace std;
int p1=0,p2=0,count=0;
int turn=0;
void disp(int cards[4][4])
{
   //display board
cout<<" 1 2 3 4\n";
cout<<" ";
for (int i=0; i<=8; i++)
{
cout<<"-";
}
cout<<endl;
for (int r=0; r<4; r++)
{
cout<<r+1<<" | ";
for (int c=0; c<4; c++)
{
cout<<"* ";
}
cout<<"\n";
}
}

void reveal(int cards[4][4],int r1,int c1,int r2, int c2)
{
   //reveal
cout<<" 1 2 3 4\n";
cout<<" ";
for (int i=0; i<=8; i++)
{
cout<<"-";
}
cout<<endl;
for (int r=0; r<4; r++)
{
cout<<r+1<<" | ";
for (int c=0; c<4; c++)
{
if ((r==r1)&&(c==c1))
{
cout<<cards[r][c]<<" ";
}
else if((r==r2)&&(c==c2))
{
cout<<cards[r][c]<<" ";
}
else
{
cout<<"* ";
}
   }
   cout<<"\n";
   }
  
  
}

void game(int cards[4][4],int n)
{
   char comma,any;
   int r1, c1, r2, c2;
   turn=n;
   cout<<"Press any character to continue\n";
   cin>>any;
   system("cls");
   disp(cards);
  
   //selection
     
   while(true){
   if(turn==1)
   cout<<"Player 1 turn\n";
   else if(turn==2)
   cout<<"Player 2 turn\n";
   cout<<"Please insert the first card row and column seperated by a comma.\n";
cin>>r1>>comma>>c1;
cout<<"Please insert the second card row and column seperated by a comma.\n";
cin>>r2>>comma>>c2;
  
   //checking
if(cards[r1-1][c1-1]==0 || cards[r2-1][c2-1]==0)
cout<<"Card already checked. Enter new position\n";
else if(r1>4 || c1>4 || r2>4 || c2>4)
cout<<"Position out of range. Enter again \n";
else if(r1==r2 && c1==c2)
cout<<"Same position enter again\n";
else{
       //fix
   r1--;
   c1--;
   r2--;
   c2--;
   break;
   }
   }
   reveal(cards,r1,c1,r2,c2);
  
   if(cards[r1][c1]==cards[r2][c2])
   {
       cout<<"There is a match \n";
       cards[r1][c1]=0;
       cards[r2][c2]=0;
       count++;
       if(count==8)
       {
           if(p1>p2)
           {
               cout<<"Player 1 wins. Score "<<p1<<"\n";
               exit(0);
           }
           else if(p2>p1)
           {
               cout<<"Player 2 wins. Score "<<p2<<"\n";
               exit(0);
           }
           else if(p1==p2)
           {
               cout<<"Its a tie!!!\n";
               exit(0);
           }
       }
       if(turn==1)
       {
           p1++;
           cout<<"Player 1 Score: "<<p1<<"\n";
           cout<<"Player 2 Score: "<<p2<<"\n";
           game(cards,1);
          
       }
       else if(turn==2)
       {
           p2++;
           cout<<"Player 1 Score: "<<p1<<"\n";
           cout<<"Player 2 Score: "<<p2<<"\n";
           game(cards,2);
          
       }
      
      
   }
   else
   {
       cout<"No match. Changing Player";
       if(turn==1)
       game(cards,2);
       else if(turn==2)
       game(cards,1);
   }
  
  
  
  
}
int gen(int a[8])
{
   int k;
   while(true)
   {
       k=rand()%8;
       if(k<8 && a[k]!=2 )
       {
           a[k]++;
           return k+1;
       }
   }
}
int main()
{
  
int cards[4][4];
int a[]={0,0,0,0,0,0,0,0};
//fill board
for (int r=0; r<4; r++)
{
for (int c=0; c<4; c++)
{
  
           cards[r][c]=gen(a);
cout<<cards[r][c];
}
cout<<endl;
}
  
    game(cards,1);
  
   return 0;
   }
  

  

Add a comment
Know the answer?
Add Answer to:
can someone solve this program using c++ (visual studio)? You have a grid of 4x4 cells...
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
ADVERTISEMENT