Bulls and Cows

Bulls and Cows

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(System.in);
        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) {
                                bulls++;
                                } 
                                else {
                                cows++;
                                }
                                }
                                }}}
                                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(System.in);
        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$");
        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!");
    }
}









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
Advertisements
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:ADS
ModuleNotFoundError: No module named 'cows'
ModuleNotFoundError: No module named 'cows'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'cows' How to remove the ModuleNotFoundError: No module named 'cows' error
ModuleNotFoundError: No module named 'cows'
ModuleNotFoundError: No module named 'cows'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'cows' How to remove the ModuleNotFoundError: No module named 'cows' error
ModuleNotFoundError: No module named 'cows'
ModuleNotFoundError: No module named 'cows'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'cows' How to remove the ModuleNotFoundError: No module named 'cows' error
ModuleNotFoundError: No module named 'cows'
ModuleNotFoundError: No module named 'cows'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'cows' How to remove the ModuleNotFoundError: No module named 'cows' error
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
to the busines units depending upon their location on the matrix grid. Cash cows... 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 dogs due to low
Tourist Places of Nagaland North Eastern India
where you can find Bos Frontalis (Mithun), an endangered species of Asian Bulls

Ads