Sunday, July 14, 2013

BULLS and COWS... A guessing number game

Amazon Coding Contest:-
Bulls and Cows
In the classic guessing game of Bulls and Cows (A.K.A. Mastermind), the computer picks a random number N digit number with all digits unique. The contender is expected to make a guess (also a number with N unique digits). The contender is provided with feedback about how close the guess is in the form of “You had X Bulls and Y Cows”. X bulls implies, you had X digits in your number in the correct place, and Y cows implies, you had Y digits in your number that appear in the original number, but they appear at a position different from your guess.
Accept a number between 4-6 (indicating N), the number of digits followed by N digits (indicating the computer’s random pick) and N additional digits of the guess. Compute and display the number of Bulls and Cows
Input => 5 12345 13579
Output => 1 Bull and 2 Cows
Reason => 1 is in the correct place, hence 1 bull. 3 and 5 are in the guess, but appear at a different location in the original number. Hence 2 cows.
Bonus points, can you convert this to a number guessing game that others can play?

//Here is my solution For GAME.. You can play

#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
 
void Mastermind(int D,char N1[],char N2[])
{
        int arr[20]={0};
        int i,count=0,B=0,BC=0,temp1=0,temp2=0;
 
        int m=strlen(N1),n=strlen(N2);
        if(m!=D&&n!=D||m>D||n>D)
        {
                printf("Bad Input");
            return ;
        }
        i=0;
        int j=0,k=0;
        i=D;
        while(i)
        {
                if(m==D)
                {
                temp1=N1[j]-48;
                
                arr[temp1]+=1;
                j++;
              //  printf("temp1= %d   %d\n",N1[j],temp1);
                }
                else
                {
                    temp1=0;
                    arr[j]+=1;
                    m++;
                }
                
                if(n==D)
                {
                temp2=N2[k]-48;
                arr[temp2+10]+=1;
                k++;
                //printf("temp2= %d   %d\n",N1[k],temp2);
              }
              
               else
                {   temp2=0;
                    arr[k+10]+=1;
                    n++;
                }
            if(temp1>9||temp1<0||temp2>9||temp2<0)
            {
            printf("Bad Input\n");return ;
            }
            if(arr[temp1]>1||arr[temp2+10]>1)
            {
                printf("Bad input");
                return ;
            }
            
            if(temp1==temp2)
            B++;
        i--;
        }
        BC=0;
        for(i=0;i<10;i++)
       if(arr[i]==1&&arr[10+i]==1)
            BC++;
        printf("%d BULLS and %d COWS\n\n",B,BC-B);
}
//Driver program for the above function
int main(void)
{
int D;char choice;
char N1[10],N2[10];     
do
{
printf("Enter the digits in the no, and two guessed no.\n");
scanf("%d%s%s",&D,&N1,&N2);
getchar();
Mastermind(D,N1,N2);
while(1)
{
printf("Do you want to play again...Press y/n for yes or No\n");
scanf("%c",&choice);
//choice='n';
if(choice!='y'&& choice!='n')
{printf("Invalid entry,Try again");
continue;
}
else
 break;
}
 
}while(choice!='n');
 
return 0;       
}
//Happy Coding...

No comments:

Post a Comment