Question: Java please Cows & Bulls is a two-player pen and paper code breaking game. One player (known as the host) comes up with a 4-letter
Java please
Cows & Bulls is a two-player pen and paper code breaking game. One player (known as the host) comes up with a 4-letter word (or 4-digit number) that contains no repeating letters (or digits) and the other player (known as the guesser) must guess this word or number. The host will give the user clues based on 2 factors: Bulls: Matching letters (or digits) guessed are in the correct position Cows: Matching letters (or digits) are not in the correct position Our version of the game will be played with 4-letter words. Remember that these words cannot contain repeating letters (i.e. rely is acceptable, but rally is not). The computer will be the host and the user will be the guesser. The user will enter a word with the same number of non-repeating letters. The computer will respond with the number of cows and bulls. The game continues until there are 4 bulls. For example, if the secret word is ARMY and the user guesses: SIZE: This would yield 0 cows and 0 bulls because none of the letters match MANY: This would yield 0 cows and 1 bull because the Y is in the correct position ARYL: This would yield 1 cow and 2 bulls because the A and R are in the correct position, but the Y is not 1a Counting Bulls Write a method, countBulls, that has 2 parameters, both arrays of type char. One will be for the letters in the hosts word, the other for the letters in the guessers word. This method returns the number of bulls found. This method will need to loop through the arrays and count the number of bulls. My suggestion here is that you overwrite matches so that you know what already counts as a bull. Loop through each element in both arrays and if the letters at the same index are the same, overwrite it with an X in one array and an O in the other to denote that youve already counted those characters i.e. if guesserLetters[i] is the same as hostLetters[i]: increment the number of bulls
assign the value X to hostLetters[i]
assign the value O to guesserLetters[i]
1b Counting Cows Write a method, countCows, that has 2 parameters, both arrays of type char. One will be for the letters in the hosts word, the other for the letters in the guessers word. This method returns the number of cows found. This method will need to use a set of nested loops. That is, for each letter in hostLetters, check each letter of userLetters to see if they are the same. For example: if hostLetters is A, R, M, Y and userLetters is M, A, N, Y Starting with A in hostLetters, you will check each letter in userLetters. That is: Check the M against the A Check the A against the A Check the N against the A Check the Y against the A Move on to the next letter in ARMY (M), and check all the characters in MANY starting with its M and ending with its Y If the two characters match: increment the number of cows assign the value X to hostLetters[i] assign O to guesserLetters[j]
1c main method Here is where you will allow the user to interact with your application to play the game. 1. You will need to generate a random number and get input from the user, so import the necessary packages and create the necessary objects from both the Scanner and Random classes 3. Using the Random object you created in step 1, pick one of the words from the String array and assign it to a String variable this is the hosts word 4. Remember that the game should repeat until the number of bulls is 4, so the remaining steps should be inside of an appropriate loop
Retrieve a word from the user this will be the guessers word Convert both the hosts word (step 3) and the guessers word (step 4) to an array of chars ( If the number of bulls is 4, congratulate the user and end the game. Otherwise, display the number of cows and bulls and have the user try again Compile and run your application.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
