Wednesday 21 March 2012

Bulls and Cows

Click here to see the rules for 4numbers. Here i am giving the code for n number of char string hope it will be useful for many.....this is written using visual studio c++ so do the needed change if you try the code with other compilers.............


To learn UNIX basic commands click here

To learn Shell Script click here


// bullandcow.cpp : Defines the entry point for the console application.

//

#include"stdafx.h"
#include<string.h>
//#include<conio.h>
#include<stdlib.h>
int

_tmain(int argc, _TCHAR* argv[])
{

int n,ch=1,i,j,temp[10],t=0;
int cow=0,buff=0,count=0;
char word[10],check[10];
printf("Hi welcome to cow and bull game\n");
printf("Instructions for player 1:\n");
printf("Make sure player two is not when you r entering the word you think \n **note: no character should repet\n");
printf("\n\nInstructions for player 2:\n");
printf("You have 10 chance to find the word \n");
gets(word);
n=(int)strlen(word);
for(i=0;word[i]!='\0';i++)
{
for(j=i;word[j]!='\0';j++)
{
if(word[i]==word[j])
{
if(i!=j)
{
printf("Fatal error: The input word has char repeted start again");
getch();
exit(0);
}
}
}
}
//clrscr();
system("cls");
printf("\nWelcome Player - 2 you have 5 chances \n");
while(ch<=15)
{
printf("\nEnter the word gussed with %d char",n);
gets(check);
if(strlen(check)!=n)
{
printf("Error\n");
exit(0);
}
for(i=0;word[i]!='\0';i++)
{
for(j=0;check[j]!='\0';j++)
{
if(word[i]==check[j])
{
if(i==j)
{
cow=cow+1;
break;
}
else if(i!=j)
{
buff=buff+1;
break;
}
}
}
}
if(cow==n)
{
printf("Great job");
getch();
exit(0);
}
else
{
printf("cow:%d,buff:%d",cow,buff);
}
cow=0;buff=0;
ch++;
}
return 0;
}

No comments:

Post a Comment