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



Home Answers Viewqa Java-Beginners bulls and cows madness

Have Programming Question? Ask it here!
 
 
 


phil
bulls and cows madness
0 Answer(s)      2 years 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!");
                                                                                }}

Advertisement
View Answers

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
 

 

 

DMCA.com