The language of code is Java. Write a program to play the Bulls and Cows word ga
ID: 3699368 • Letter: T
Question
The language of code is Java.
Write a program to play the Bulls and Cows word game. The purpose of the game is for the player to guess a 4-letter hidden word. After each guess, the program gives the player the number of bulls and the number of cows as a hint. Bull a correct letter guessed in the correct position of the hidden word Cow - a correct letter guessed in the wrong position of the hidden word For example: . Hidden word loop Player's guess boat 1 bull and 0 cows Hidden word loop Player's guess logo 2 bulls and 1 cow Hidden word loop Player's guess fall 0 bulls and 1 cow The hidden word should be randomly chosen from the array of strings: (Place this line in your program) Stringl words "code", "loop", "edit", "long", "bugs", "byte", "data", "hack", "file', "user" After the program determines the hidden word, it asks the player to guess it. After each guess, the program tells the user the number of bulls and cows, and asks the player to guess again. The game continues until the player guesses the word correctly.Explanation / Answer
Hi, here is the required program you need. This will conduct the bulls and cows game exactly as specified. I have defined a couple of methods in this program to assist the game play. Everything is explained in comments. Drop a comment if you have any doubts. Thanks.
// BullsAndCows.java
import java.util.Random;
import java.util.Scanner;
public class BullsAndCows {
static Scanner scanner = new Scanner(System.in);
static String[] words = { "code", "loop", "edit", "long", "bugs", "byte",
"data", "hack", "file", "user" };
static Random random = new Random();
public static void main(String[] args) {
String mysteryWord = randomWord();
String userInput = "";
/**
* Looping until user guesses the word correctly
*/
while (!userInput.equalsIgnoreCase(mysteryWord)) {
System.out.println("Enter your guess: ");
userInput = scanner.nextLine();
System.out.println(bullsAndCows(mysteryWord, userInput));
if (userInput.equalsIgnoreCase(mysteryWord)) {
System.out.println("Right guess! You win!");
}
}
}
/**
* method to pick and return a random word from the words array
*/
static String randomWord() {
int randIndex = random.nextInt(words.length);
return words[randIndex];
}
/**
* method to analyze a user input and determine how many bulls and cows are
* there
*
* @return - a string containing bulls and cows info
*/
static String bullsAndCows(String mysteryWord, String userInput) {
int bulls = 0, cows = 0;
/**
* Converting both strings to lowercase, for easy comparison
*/
mysteryWord = mysteryWord.toLowerCase();
userInput = userInput.toLowerCase();
/**
* looping through each characters in user input
*/
for (int i = 0; i < userInput.length(); i++) {
// getting the current character
char c1 = userInput.charAt(i);
// checking if current index is a valid index in mystery word
if (i >= 0 && i < mysteryWord.length()) {
// getting current character in mystery word
char c2 = mysteryWord.charAt(i);
// checking if both are equal (bull)
if (c1 == c2) {
bulls++;
} else if (mysteryWord.contains("" + c1)) {
/**
* the character exist somewhere else in the mystery word
* (cow)
*/
cows++;
}
}
}
return bulls + " bull(s) and " + cows + " cow(s)";
}
}
/*OUTPUT*/
Enter your guess:
ball
1 bull(s) and 0 cow(s)
Enter your guess:
full
1 bull(s) and 0 cow(s)
Enter your guess:
file
0 bull(s) and 0 cow(s)
Enter your guess:
code
bull(s) and 0 cow(s)
Enter your guess:
loop
0 bull(s) and 0 cow(s)
Enter your guess:
user
0 bull(s) and 2 cow(s)
Enter your guess:
bug
3 bull(s) and 0 cow(s)
Enter your guess:
buggy
3 bull(s) and 1 cow(s)
Enter your guess:
bugs
4 bull(s) and 0 cow(s)
Right guess! You win!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.