Java game bulls and cows


 

Java game bulls and cows

In this tutorial, you will learn how to implement bulls and cows game in java.

In this tutorial, you will learn how to implement bulls and cows game in java.

Java game bulls and cows

In this tutorial, you will learn how to implement bulls and cows game in java.

The game bulls and cows is an ancient game with guessing numbers. It draws a 4-digit number randomly and prompt the user to enter a four digit number or type 'quit' to exit. If the input is "quit", the user will come out from the game. If the user inputs 4 digit number, it displays the number of digit(s) that have correct position as bull and the number of digit(s) that appeared without exact position as cow. For example: The random number is 1234 and the user input 1355. Exact position: "1", appeared without exact position: "3". The system will display the following message:

1 bull 1 cow
If no digit found n the random number, the following message will be displayed:
Neither a bull nor a cow!
On the other hand, if the user guess the 4 digits correctly, it will display the following message:
4 bulls! You are a genius!

Example:

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class BullsAndCows{
public static void main(String args[]){
int i,j,bull=0,cow=0;
int[] secretDigit,arr;
Scanner scan=new Scanner(System.in);
String msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
Random rand=new Random();
Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
do{
String secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.nextInt(10);
secret+=secretDigit[i];
}
System.out.println("A secret number of 4-digit has been draw!");
do{
do{
System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
ans=scan.nextLine().trim();
System.out.println(ans);
}while(!regex.matcher(ans).find());
if(ans.equals("quit")){
System.out.println("The secret number is: " + secret);
}else{
cow=0;
bull=0;
for(i=0;i<4;i++)
try{
arr[i]=Integer.parseInt(ans.substring(i,i+1));
if(arr[i]==secretDigit[i]) bull++;
}catch(NumberFormatException nfe){} 
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}else{
if(bull==4)
msg="4 bulls! You are a genius!";
else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull + " bulls only.";
}else if(bull==0){
if(cow==1)
msg="1 cow only.";
else
msg=cow + " cows only.";
}else{
if(bull==1)
msg="1 bull and ";
else
msg=bull + " bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
System.out.println(msg);
}
}while(bull!=4 && !ans.equals("quit"));
if(!ans.equals("quit")){
do{
System.out.print("Do you want to play a new game (Y/N)?");
playNextGame=scan.nextLine().trim();
System.out.println(playNextGame);
}while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
}
} while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
System.out.println("Thank you for playing Bulls and Cows!");
}
}

Output:

A secret number of 4-digit has been draw!
Please enter a 4-digit number (or type 'quit' to exit):1234
1234
2 cows only.
Please enter a 4-digit number (or type 'quit' to exit):2345
2345
1 bull only.
Please enter a 4-digit number (or type 'quit' to exit):quit
quit
The secret number is: 2921
Thank you for playing Bulls and Cows!

Ads