Question: bulls and cows madness

Ads

 
 
 

Share on Google+Share on Google+

phil
bulls and cows madness
0 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners

print("code sample");ok, so I have placed a menu onto the program, but I have no clue how to make each option i.e. new game to start a new game, or sort by score to bring up a list of scores. Any help is appreciated, thank you in advance. heres my code:

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class Main {
    public void display_menu(); 
    System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
    System.out.print( "Selection: " );
    }
    public class Main() {
        Scanner in = new Scanner ( System.in );
        display_menu();
        switch ( in.nextInt()) {
            case 1:
            System.out.println ( "Start New game - New Player" );
            break;
            case 2:
            System.out.println ( "Start new Game - Same player" );
            break;
            case 3:
            System.out.println ( "Here are the scores, highest to lowest: " );
            break;
            case 4:
            System.err.println ( "Here's who has played the game!! " );
            break;
            case 5:
            System.out.println ( "Here are the Top Scores " );
            break;
            case 6:
            System.out.println ( "thanks for paying " );
            break;

            }}
            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 ";
                                                                        else
                                                                        msg=cow + " cows ";
                                                                        }
                                                                        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!");
                                                                                }}
Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
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
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
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
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
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
could use the most help. Please Help. Thank You Confibulatory Madness
Tourist Places of Nagaland North Eastern India
(Mithun), an endangered species of Asian Bulls. How to reach Nagaland
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
see that I am commenting on it. Thank You Confibulatory Madness   
JbuilderTutorials
;  Java Madness For the purposes of this course
Ajax Framework
to the madness of JavaScript development
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads