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?