Latest Tutorials| Questions and Answers|Ask Questions?|Site Map


View Questions and Answers by Category


Have Programming Question? Ask it here!

Bulls and Cows
1 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners

I need some help with my code for my bulls and cows game... the requirements for the program ar below

To start your gaming career, you have decided to write a math game. You want to create the following Menu:

Bulls and Cows
  1. Play new game - New Player
  2. Play new game â??? Same Player
  3. Sort by score
  4. Display the â??top scoreâ?? report
  5. Quit Game

Bulls and Cows Game is also known as MasterMind. Computer selects a four digit number 0 - 9, all four digits are different. In current implementation number may not begin with 0.

"Exist" column displays total number of digits you guessed right - "Cow", "Match" shows how many of those that exists was placed at the right spots - "Bull".

Play by entering your guess and then validate your entry.

Player: Rider Storm

Guess Exist Match

3412 3 1

3721 3 3

Keep track of the player and how many tries it takes to win In 2, single dim arrays, 1 string and 1 int. When the players are finished; append the â??best playerâ?? and â??scoreâ?? to the text file, â??winners.txtâ??.

The â??Display Scoresâ?? menu should display all the top scores already in the â??winners.txtâ?? file and also the ones added during the play. You will need to determine who has the best and worse score of the groups so you can display a message in the report like below. It should look something like:

Math Wizards: Guser Loop 76 â??? you have the worst score. It is time to Kick you out of the club.

heres the code I have so far for that:

import java.util.*;
public class bullGame {
    public static void main (String[] args) {
        int rnd = chooseRandomNumber();
        System.out.println("I have chosen a four-digit number. Can you guess what it is?");
        int guesses = 0;
        int yourGuess = 0;
        Scanner scan = new Scanner(;
        do {
            cows = 0;
            bulls = 0;
            System.out.print( "Guess " + ++guesses + ": ");
            yourGuess = scan.nextInt();
            calcCowsAndBulls( rnd, yourGuess);
            System.out.print( "Score: "+cows);
            System.out.print( cows==1 ? " cow" : " cows");
            System.out.print(" and "+bulls);
            System.out.println( bulls==1 ? " bull." : " bulls.");
            while (bulls<4 && guesses<25);
            if (bulls==4) {
                System.out.println("Congratulationsâ?¦ You got it in "+ guesses+ " guesses.");
                else {
                    System.out.println("Unlucky! The answer was "+ rnd);
                    public static void calcCowsAndBulls(int rnd, int yourGuess) {
                        String str1 = ""+ rnd; 
                        String str2 = ""+ yourGuess;
                        for (int i=0; i<4; i++) {
                            for (int j=0; j<4; j++) {
                                if (str1.charAt(i)==str2.charAt(j)) {
                                if (i==j) {
                                else {
                                public static int chooseRandomNumber() {
                                    Random ran = new Random();
                                    int r = 1000 + ran.nextInt(9000);
                                    return r;
                                    static int cows;
                                    static int bulls;

then after there a second part to the assignment:

Since you have written the bulls and cows game, your coding knowledge has come a long way and you want to update your system. Add the following options to your menu

Bulls and Cows
  1. Play new game - New Player
  2. Play new game â??? Same Player
  3. Sort by Score
  4. Sort by Name
  5. Display the â??top scoreâ?? report
  6. Quit Game

You now want to start your array with the existing data in the â??score.txtâ?? file. You now feel comfortable working with 2-dim arrays so you want to modify your existing program containing 2, single dims into one 2-dim array.

You will still keep track of the player and how many tries it takes to win in the new array. You also realize that your file may have saved one player multiple times.

You need to modify your program so that you are storing unique plays. You want to make sure your system keeps its modular design.

any help would be greatly appreciated...

View Answers

July 28, 2012 at 4:36 PM

Here is an example of Bulls and Cows game.

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(;
        String msg, ans, playNextGame;
        secretDigit=new int[4];
        arr=new int[4];
        System.out.println("Bulls and Cows");
        System.out.println("This is an ancient game with guessing numbers.");
        Random rand=new Random();
        Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
            String secret="";
            System.out.println("A secret number of 4-digit has been draw!");
                    System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
                    System.out.println("The secret number is: " + secret);
                            if(arr[i]==secretDigit[i]) bull++;
                        }catch(NumberFormatException nfe){}                     
                    if(cow==0 && bull==0){
                        msg="Neither a bull nor a cow!";
                            msg="4 bulls! You are a genius!";
                                    msg="1 bull only.";
                                    msg=bull + " bulls only.";
                            }else if(bull==0){
                                    msg="1 cow only.";
                                    msg=cow + " cows only.";
                                    msg="1 bull and ";
                                    msg=bull + " bulls and ";
                                    msg+="1 cow.";
                                    msg+=cow + " cows.";
            }while(bull!=4 && !ans.equals("quit"));
                    System.out.print("Do you want to play a new game (Y/N)?");
                }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!");

Related Tutorials/Questions & Answers:
Bulls and Cows
Bulls and Cows  I need some help with my code for my bulls and cows...: Bulls and Cows Play new game - New Player Play new game â??? Same... Bulls and Cows Game is also known as MasterMind. Computer selects a four digit
bulls and cows madness
bulls and cows madness  print("code sample");ok, so I have placed...]; System.out.println("Bulls and Cows"); System.out.println...) msg="4 bulls! You are a genius
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. The game bulls and cows is an ancient game...;)); System.out.println("Thank you for playing Bulls and Cows!"); } } Output
Java Program
roots, gives clues in the form of 'bulls' and 'cows'! Given a guess-word, it tells us the number of 'bulls' and 'cows' in the word. A 'bull' stands for a letter... is a bull and which one is a cow! Bulls are counted first and then cows
Minute to Win It
Minute to Win It   Frnds i urgently need the code for an online game similar to cows and bulls played by two opponents. The game can be played with 2digits, 3digits and also with 4digits at different levels respectively. Computer
The BCG Matrix
their location on the matrix grid. Cash cows: Business units falling under... phase of stars is the cash cows. Stars are the market leader due to their high growth rate. As soon as the growth slows stars become cash cows and can even become
Tourist Places of Nagaland North Eastern India
(Mithun), an endangered species of Asian Bulls. How to reach Nagaland