0

I need some help with my code for my bulls and cows game... the requirements for the program are 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.

Stevo Sky 21

Stinky Sulivan 5 – you are now the president of the math world.

You want to make sure your system has a modular design

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.

I am very new to java... and am having a hard time figuring out how to do the menu system for the game... below is the code that I have so far...

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class bullGame{
    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!");
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by NormR1
0

how to do the menu system for the game

Many people print out the user's options, read in the user's response and use a switch statement to control what the program does.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.