cs Java coding. please help Write a program that plays the game Bulls And Cows.
ID: 3813530 • Letter: C
Question
cs Java coding. please help
Write a program that plays the game Bulls And Cows. A sample run might look like this: Welcome to Bulls and Cows. You have 12 guesses to discover the random four digit number that has been chosen. All digits are unique. A bull means that you have the correct digit in the correct place, a cow that you have the correct digit in the wrong place. Enter a guess: 3703 Digits must be unique Enter a guess: 37 There must be four digits Enter a guess 31298 There must be four digits Enter a guess: 3701 1 bulls 2 cows Enter a guess: 3702 1 bulls 1 cows Enter a guess: 3714 2 bulls 2 cows Enter a guess: 3174 1 bulls 3 cows Enter a guess: 1734 0 bulls 4 cows Enter a guess: 3741 1 bulls 3 cows Enter a guess: 3417 4 bulls 0 cows took you 7 guesses! You can design the game any way you want, but should use methods to perform the various tasks, rather than putting all the code in the main method, so that your program is modular. Print instructions for the user at the start of the program (which you might want to store in a separate method). If users guess the number, indicate how many turns it took them, if they don't tell them what the number was.Explanation / Answer
ANSWER:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Game {
public static boolean hasUniqueDigits(int num) {
boolean[] digs = new boolean[10];
while (num > 0) {
if (digs[num % 10])
return true;
digs[num % 10] = true;
num /= 10;
}
return false;
}
public static int getNmberOfGuesses(String codeStr) {
int guesses = 0;
boolean guessed = false;
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
do {
if(guesses < 12) {
int bulls = 0;
int cows = 0;
System.out.print("Enter a guess: ");
int guess;
try {
guess = input.nextInt();
String temp = Integer.toString(guess);
if(temp.length() != 4) {
System.out.println("There must be four digits");
continue;
}
if (hasUniqueDigits(guess) || guess < 1000) {
System.out.println("Digits must be unique");
continue;
}
} catch (InputMismatchException e) {
continue;
}
guesses++;
String guessStr = guess + "";
for (int i = 0; i < 4; i++) {
if (guessStr.charAt(i) == codeStr.charAt(i)) {
bulls++;
} else if (codeStr.contains(guessStr.charAt(i) + "")) {
cows++;
}
}
if (bulls == 4) {
guessed = true;
} else {
System.out.println(bulls + " bulls " + cows + " cows");
}
}
else return -1;
} while (!guessed);
return guesses;
}
public static void main(String[] args) {
int code = 0;
while (hasUniqueDigits(code = (int) (Math.random() * 9000 + 1000)));
String codeStr = code + "";
int guesses = getNmberOfGuesses(codeStr);
if(guesses != -1)
System.out.println("It took you " + guesses + " guesses!");
else
System.out.println("Play again. Thank you");
}
}
Working fine.
Let me know any concerns. Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.