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

Problem 2. The famous Match&Hit; game is playey a computer and a human player as

ID: 3722163 • Letter: P

Question

Problem 2. The famous Match&Hit; game is playey a computer and a human player as follows. First, the com- puter selects a random 4-digit number N- a 103+b102 +c 10+d, where a, b, c, d are distinct non- zero digits -that is, a, b,c, d are distinct elements of the set (1,2,3,4,5,6,7,8,9). Let us call num- bers like this valid. The human player then tries to deduce N from a sequence of queries to the com- puter. Each query consists of a valid 4-digit number M103y 102 +z.10+ w. The computer responds to each query with the number of matches and the number of hits. A match is a digit of N that appears in M at the same position (thus each of x-a, y-b, z-c, or w d counts as one match). A hit is a digit of N that appears in M, but not at the same position. For example, if N = 5167, then the queries 2934, 1687, 7165, 5167 will result in the following numbers of matches and hits: 5167 2934no matches and no hits 1687- one match and two hits 7165 5167 two matches and two hits --* four matches and no hits where matches are denoted by and hits are denoted by o. The play continues until the human player either wins or loses the game, as follows: Human player wins if he submits a query with 4 matches (that is, M = N). Human player loses if he submits.12 queries, none of them with 4 matches

Explanation / Answer

#include<string.h>

#include<string.h>

#include<time.h>

#include<stdlib.h>

#include<stdio.h>

void main()

{

int i=0;

printf("***Welcome to the MATCH and HIT game**** ");

int n=choosen(),m;

printf("the computer has choosen 4-digit number %d try to deduce it in 12 queries ",n);

for(i=0;i<12;i++)

{

int match=0,hit=0;

printf("Round# %d ",i+1);

char arr[4];

printf("Please enter your query (4 digits ): ");

scanf(" %c %c %c %c",&arr[0],&arr[1],&arr[2],&arr[3]);

if(!(arr[0]>='1'&&arr[0]<='9'||arr[1]>='1'&&arr[1]<='9'||arr[2]>='1'&&arr[2]<='9'||arr[3]>='1'&&arr[3]<='9'))

printf("invalid query, input again!!! ");

else

{

m=1000*((char)arr[0]-'0')+100*((char)arr[1]-'0')+10*((char)arr[2]-'0')+((char)arr[3]-'0');

}

if(!isvalid(m))

printf("invalid query, input again!!! ");

else

{

match=matches(n,m);

hit=hits(n,m)-match;

printf("------> %d MATCHES %d HITS ",match,hit);

}

if(match==4)

break;

}

if(i==12)

printf("********************** sorry no more queries!!! you lost this game ********************** ");

else

{

printf("********************** congo!! you have won this game ******************** ");

}

}

int notsame(int *a)

{

int i,k;

for(i=0;i<4;i++)

{

for(k=i+1;k<4;k++)

{

if(a[i]==a[k])

break;

}

if(k!=4)

break;

}

if(i==4)

return 1;

else

return 0;

}

int matches(int n,int m)

{

int match=0;

int n1[4]={0};

int m1[4]={0};

int n2=n,i=0;

int m2=m;

while(n2>0)

{

n1[i]=n2%10;

n2=n2/10;

i++;

}

i=0;

while(m2>0)

{

m1[i]=m2%10;

m2=m2/10;

i++;

}

for(i=0;i<4;i++)

{

if(n1[i]==m1[i])

{

match++;

}

}

return match;

}

int hits(int n,int m)

{

int hit=0;

int n1[4]={0};

int m1[4]={0};

int n2=n,i=0,k=0;

int m2=m;

while(n2>0)

{

n1[i]=n2%10;

n2=n2/10;

i++;

}

i=0;

while(m2>0)

{

m1[i]=m2%10;

m2=m2/10;

i++;

}

for(i=0;i<4;i++)

{

for(k=0;k<4;k++)

{

if(n1[i]==m1[k])

hit++;

}

}

return hit;

}

int isvalid(int n)

{

int a[4];

int num=n;

int i=0,k;

if(n<=0)

{

return 0;

}

else

{

while(num>0)

{

a[i]=num%10;

num=num/10;

i++;

}

for(k=0;k<4;k++)

{

if(a[k]>=1&&a[k]<=9)

{

continue;

}

else   

break;

}

if(k==4&&notsame(a))

return 1;

else   

return 0;

}

}

int choosen()

{

srand(time(0));

int flag=1,n;

while(flag)

{

n=rand()%9999+1000;

if(isvalid(n))

{

flag=0;

}

}

return n;

}

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