I thing you all know about the cows and bulls game here is the code for that game ?
import java.util.Random;
import java.util.Scanner;
public class cab
{
public static void main(String args[])
{/*
System.out.println("Do you want to play a game?(enter 1(true)/0ther(false))?");
Scanner ch=new Scanner(System.in);
int num=ch.nextInt();
while(num==1)
{
game();
System.out.println("Do you want to play a game again?(enter 1(true)/Other(false))?");
num=ch.nextInt();
}
ch.close();
System.out.println("Thanks for visiting..");*/
// while(true)
{
game();
}
}
public static void game()
{
gamerule();
Random num=new Random();
Scanner scan=new Scanner(System.in);
int[] list=new int[4];//answer random
int i,j,k;
list[0]=1+num.nextInt(9);
for(i=1;i<4;i++)
{
list[i]=num.nextInt(10);
for(j=0;j<i;j++)
{
if(list[i]==list[j])
{
list[i]=num.nextInt(10);
j=-1;
}
}
}
/*for(i=0;i<4;i++)
{
System.out.print(list[i]);
}*/
int[] gn=new int[6];//guessing number
int[] hoc=new int[6];//history of cows
int[] hob=new int[6];//history of bulls
int [] temp=new int[4];//to check bulls and cows
int temp1;
for(i=0;i<6;i++)
{
gn[i]=scan.nextInt();
if((gn[i]>9999))
{
System.out.println("Sorry.. You entered more than 4 numbers.. ");
i--;
}
else if((gn[i]<1000))
{
System.out.println("Sorry.. You entered less than 4 numbers.. ");
i--;
}
else
{
temp1=gn[i];
j=3;
while(temp1!=0)
{
temp[j--]=temp1%10;
temp1/=10;
}
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
if(j==k)
{
if(temp[j]==list[k])
hoc[i]++;
}
else
{
if(temp[j]==list[k])
hob[i]++;
}
}
}
if(hoc[i]==4)
{
System.out.println("Congratulations.. You won!!");
break;
}
else
{
//System.out.prinln("Nice try but failed.. keep trying on..");
System.out.println(i+1+". Cows:"+hoc[i]+"\tBulls:"+hob[i]);
}
}
}
if(i==6)
{
System.out.println("Sorry.. You lose.. tries over..");
System.out.print("Answer is:");
for(i=0;i<4;i++)
{
System.out.print(list[i]);
}
System.out.println("");
}
scan.close();
}
public static void gamerule()
{
System.out.println("*********************************************************");
System.out.println("1.\tYou have to guess a 4 digit number which will be randomly generated..");
System.out.println("2.\tYou will be given by 6 chances..");
System.out.println("3.\t**HINT**: number wont have repeatitions and not start with 0..");
System.out.println("Your Game Starts now..");
}
}
And here is the output of this code
import java.util.Random;
import java.util.Scanner;
public class cab
{
public static void main(String args[])
{/*
System.out.println("Do you want to play a game?(enter 1(true)/0ther(false))?");
Scanner ch=new Scanner(System.in);
int num=ch.nextInt();
while(num==1)
{
game();
System.out.println("Do you want to play a game again?(enter 1(true)/Other(false))?");
num=ch.nextInt();
}
ch.close();
System.out.println("Thanks for visiting..");*/
// while(true)
{
game();
}
}
public static void game()
{
gamerule();
Random num=new Random();
Scanner scan=new Scanner(System.in);
int[] list=new int[4];//answer random
int i,j,k;
list[0]=1+num.nextInt(9);
for(i=1;i<4;i++)
{
list[i]=num.nextInt(10);
for(j=0;j<i;j++)
{
if(list[i]==list[j])
{
list[i]=num.nextInt(10);
j=-1;
}
}
}
/*for(i=0;i<4;i++)
{
System.out.print(list[i]);
}*/
int[] gn=new int[6];//guessing number
int[] hoc=new int[6];//history of cows
int[] hob=new int[6];//history of bulls
int [] temp=new int[4];//to check bulls and cows
int temp1;
for(i=0;i<6;i++)
{
gn[i]=scan.nextInt();
if((gn[i]>9999))
{
System.out.println("Sorry.. You entered more than 4 numbers.. ");
i--;
}
else if((gn[i]<1000))
{
System.out.println("Sorry.. You entered less than 4 numbers.. ");
i--;
}
else
{
temp1=gn[i];
j=3;
while(temp1!=0)
{
temp[j--]=temp1%10;
temp1/=10;
}
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
if(j==k)
{
if(temp[j]==list[k])
hoc[i]++;
}
else
{
if(temp[j]==list[k])
hob[i]++;
}
}
}
if(hoc[i]==4)
{
System.out.println("Congratulations.. You won!!");
break;
}
else
{
//System.out.prinln("Nice try but failed.. keep trying on..");
System.out.println(i+1+". Cows:"+hoc[i]+"\tBulls:"+hob[i]);
}
}
}
if(i==6)
{
System.out.println("Sorry.. You lose.. tries over..");
System.out.print("Answer is:");
for(i=0;i<4;i++)
{
System.out.print(list[i]);
}
System.out.println("");
}
scan.close();
}
public static void gamerule()
{
System.out.println("*********************************************************");
System.out.println("1.\tYou have to guess a 4 digit number which will be randomly generated..");
System.out.println("2.\tYou will be given by 6 chances..");
System.out.println("3.\t**HINT**: number wont have repeatitions and not start with 0..");
System.out.println("Your Game Starts now..");
}
}
And here is the output of this code
Comments
Post a Comment