I am trying to design the game Bulls and Cows
we have a word to guess eg:"fame"(four lettered)
if i type abcd bulls=0 cows=0
efgh bulls=0 cows=1 (if the letter is at the same positon as the word we give bulls. If the letter is in the word but not at the same position we give cows.
one more thing
if i write seme i should get bull=2 cow=0
i mean if there is a bull for the letter cow will be 0 for that letter which is my problem.The cows is a problem
eg:fgeh should give bulls=1 cows=1
eg:mmmm should give bulls=1 cows=0
i hope u got the game logic
here is my code
import java.io.*;
class BullsandCows
{
public static void main(String args[])throws IOException
{int count=0;int count2=0;
String str="fame";int tries=0;
System.out.println("enter 4 lettered word");
if(count!=4)
{
do
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str2=br.readLine();
if(str2.length()==4)
{
for(int i=0;i<4;i++)
{
if(str.charAt(i)==str2.charAt(i))
count=count+1;
}
for(int i=0;i<4;i++)
{
for(int j=i+1;j<4;j++)
{
if(str.charAt(i)==str2.charAt(j))
count2=count2+1;
}
}
}
System.out.println("bulls= "+count);
System.out.println("cows= "+count2);
if(count==4)
{
System.out.println("congrats");
System.exit(0);
}
else
{
System.out.println("try again");
tries++;
}
count=0;
count2=0;
}
while(tries<=3);
if(tries>=3)
System.out.println("word is "+"fame");
}
}
}
thanks