Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Mastermind game

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mastermind game

    I am confused on how to write a Boolean that will end the game if the guess is correct if anyone can help I would greatly appreciate it
    My teacher calls it baseball lol so I made a baseball class which contains all of my methods and arraylist of words to guess from
    <import java.util.*;
    import java.io.*;
     
    public class Baseball {
     
        private ArrayList<String> numbers;
        private String secret;
        private int count, strike, ball;
        private StringBuffer hideNumber = new StringBuffer();
        private StringBuffer disguise = new StringBuffer();
     
        public Baseball(){
     
            numbers = new ArrayList<String>();
            numbers.add("012");
            numbers.add("123");
            numbers.add("456");
            numbers.add("789");
            numbers.add("098");
            numbers.add("765");
            numbers.add("432");
            numbers.add("109");
            numbers.add("876");
            numbers.add("543");
            numbers.add("210");
            numbers.add("987");
            numbers.add("654");
            numbers.add("321");
            numbers.add("135");
            numbers.add("579");
            numbers.add("024");
            numbers.add("468");
            numbers.add("420");
            numbers.add("864");
            numbers.add("531");
            numbers.add("975");
            numbers.add("079");
            numbers.add("035");
            numbers.add("046");
     
            secret = numbers.get((int)Math.floor(Math.random()*numbers.size()));
        }
     
        public StringBuffer choseWord(String s){
            hideNumber.append(s);
            return hideNumber;
        }
     
        public void noBalls(String guess){
           count = 0;
           for(int i = 0; i < hideNumber.length(); i++){
               for(int j = 0; j < hideNumber.length(); j++){
                   if(i != j && guess.charAt(i) == hideNumber.charAt(j)){
                       count++;
                       ball = count;
                   }
               }
           }
        }
     
        public void noStrikes(String guess){
            count = 0;
            for(int i = 0; i < hideNumber.length(); i++){
                if(guess.charAt(i) == hideNumber.charAt(i)){
                    count++;
                    strike = count;
                }
            }
        }
     
        public Boolean gameOver(){
     
                return true;
        }
     
        @Override
        public String toString(){
            return "Strikes: " + strike + " " + "Balls: " + ball;
        }
     
        public ArrayList<String> getNumbers() {
            return numbers;
        }
     
        public String getSecret() {
            return secret;
        }
     
        public StringBuffer getHideNumber() {
            return hideNumber;
        }
     
        public StringBuffer getDisguise() {
            return disguise;
        }
     
         public Integer getBall() {
            return ball;
        }
     
        public Integer getStrike() {
            return strike;
        }
    }>

    This is my main class that will execute the game
    <import java.util.*;
    import java.io.*;
     
    public class Homework2 {
     
        public static void main(String[] args){
     
            Scanner input = new Scanner(System.in);
            boolean repeat = false;
            boolean valid = false;
            String guess, secret, answer;
            StringBuffer guesses, number;
            int tries = 0;
     
            while(!repeat){
                Baseball play = new Baseball();
                secret = play.getSecret();
                number = play.choseWord(secret);
                System.out.println("*****************************");
                System.out.println("*****WELCOME TO BASEBALL*****");
                System.out.println("**  (c) Devin Johnson      **");
                System.out.println("**      Have Fun!!!!       **");
                System.out.println("*****************************" +"\n");
                System.out.println("Key for debugging: " + number);
     
                while(play.gameOver()){
                    System.out.print("Guess three numbers: ");
                    guess = input.nextLine();
                    tries++;
                    Character a = guess.charAt(0);
                    Character b = guess.charAt(1);
                    Character c = guess.charAt(2);
                    if(guess.length() < 0 || guess.length() > 3 || Character.isAlphabetic(a)
                            || Character.isAlphabetic(b) || Character.isAlphabetic(c))
                    {
                        System.out.println("Invalid input, Please try again!");
                        continue;
                    }
                    if(a == b || a == c || b == c || c == a || b == a ){
                        System.out.println("Invalid input, Please try again!");
                        continue;
                    }
                    else{
     
                           play.noBalls(guess);
                           play.noStrikes(guess);
                           System.out.println(play); 
     
                    }
                    if(!play.gameOver()){
                        System.out.println("Congrats!    You are correct!    It took you "+ tries + " tries.");
     
                    } 
                }
                System.out.print("Would you like to play again, yes/no:");
                answer = input.next();
                repeat = answer.equalsIgnoreCase("no");
            }
        }
     
    }>


    --- Update ---

    The code is all mine also just confused how I should do a Boolean or if I even should do one?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mastermind game

    how to write a Boolean that will end the game
    A boolean variable can have two values: true and false.

    You can use an if statement to test its value and do something that will "end the game" if it is true.

    Define the boolean as a class variable.
    Set it true when you want other parts of the class to know about it.
    Test its value when you want to exit the game if true.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind game

    That is true thank you very much i didnt think about that

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Mastermind game

    What are all those numbers for?

    numbers = new ArrayList<String>();
            numbers.add("012");

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind game

    the numbers were for creating my own arraylist that contained a bank of number strings for the computer to randomly generate one as the secret number to have the user guess from

Similar Threads

  1. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  2. Mastermind Game, Function problem?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: February 26th, 2013, 03:36 PM
  3. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  4. Mastermind commenting help needed! :)
    By Mahela in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 03:55 PM
  5. How to implement the logic of Mastermind game into the GUI?
    By coccoster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2009, 05:18 AM