$29
In this assignment you will learn about loops and methods. This assignment requires you to write a class named CowsAndBulls. This class must be in a package named pa3.
Note that the description of a programming assignment is not a linear narrative and often
requires multiple readings before things start to click. You are encouraged to consult instruc- tors/Teaching Assistants for any questions/clarifications regarding programming assignments.
1 Cows and Bulls
You might have played the game of Cows and Bulls as a kid. This game consists of two players, lets call them “Alice” and “Bob”. The game starts by Alice picking a secret 4 digit number so that all digits of the number are distinct. For example, Alice can pick 1024 but is not allowed to pick
1231. Bob’s goal is to find the secret number that Alice has picked. The game proceeds in rounds. In each round Bob guesses a four digit number and Alice gives a clue about how close the guess is to the secret number. Bob’s guess must also have all distinct digits. Based on these clues, in the next round, Bob guesses another number. The game proceeds till Bob finds the secret number.
Alice gives clue by specifying how many “cows” and “bulls’ are present in Bob’s guess. This is best illustrated by an example. Suppose Alice’s secret number is 1723 and Bob’s first guess is 2893. The digit 2 is present in both the numbers. However, it is the second digit of Alice’s number and is the 4th digit of Bob’s number (we are counting from right to left). Thus 2 is considered a “cow”. The number 3 is also present in both the numbers and it is the first digit in both the numbers. So 3 is a “bull’. Now, Alice’s tells Bob that there is 1 cow and 1 bull in his guess. Based on this information, suppose Bob guessed 8923. This guess has 2 bulls (2 and 3) and 0 cows. So Alice tells Bob that there are 0 cows and 2 bulls in his guess. Suppose that Bob’s next guess is 7112. Since
7112 does not have distinct digits, Alice responds by telling that it is not a valid guess. Suppose Bob’s next guess is 7123. This guess has 2 cows ( 7 and 1) and 2 bulls ( 2 and 3). So Alice tells Bob that there are 2 cows and 2 bulls. Suppose Bob’s next guess is 1723. This guess has 4 bulls, and thus exactly matches the secret number, and the game is Over.
2 Your Task
Your task is to write a program to play this game. The program takes the role of “Alice” and the user takes the role of “Bob”. When you run the program, it will pick a secret 4-digit number (with distinct digits) and interacts with the user. For each guess the user types, the program prints the number of cows and bulls. The program ends, when the user finds the secret number.
Your program must behave as follows: At the beginning, it picks a random 4 digit number (with all distinct digits) and interacts with the user. It prompts the user to enter a valid guess. A guess is valid if all the digits of the guessed number are distinct. For each valid guess the user entered, the
program outputs the number of cows and bulls. If the guess is not valid, then the program outputs a message. The program repeatedly prompts the user until the user finds the secret number.
Hear is a sample run of the program: Boldface letters indicate user input:
Welcome to Cows and Bulls Game. I picked a random 4-digit number with distinct integers, try finding it. Type your guess, must be a 4-digit number with distinct digits.
1780
Cows: 0 Bulls: 2
Type your guess, must be a 4-digit number with distinct digits
1084
Cows: 1 Bulls: 1
Type your guess, must be a 4-digit number with distinct digits
2316
Cows: 2 Bulls: 0
Type your guess, must be a 4-digit number with distinct digits
2216
Invalid guess. Type your guess, must be a 4-digit number with distinct digits
1680
Cows: 0 Bulls: 3
Type your guess, must be a 4-digit number with distinct digits
6180
Cows: 2 Bulls: 1
Type your guess, must be a 4-digit number with distinct digits
1170
Invalid guess. Type your guess, must be a 4-digit number with distinct digits
1670
Cows: 0 Bulls: 3
Type your guess, must be a 4-digit number with distinct digits
1650
Congratulations, You found the number, you took 9 guesses
We index digits from right to left (not left to right) and indices start from 1 (not from 0). For example, the first digit of 123 is 3. The digit 1 appears at index 3 in 123.
You will write your program by writing several methods. Your class CowsAndBulls must have the following methods. You are NOT allowed to use any of the String methods in your code.
numDigits(int number). Takes a positive integer number as a parameter and returns the number of digits in number. You may assume that number is always a positive integer, and you do not have to write code to check this.
getDigit(int number, int i). Takes a positive integer number and an index i as parameters and returns the ith digit (from right) of number. If number does not have ith digit, then it returns
−1. This method must make call(s) to numDigits. It may make call(s) to other methods.You may
assume that number is always a positive integer, and you do not have to write code to check this.
hasDistinctDigits(int number). Takes a positive integer number as a parameter and re- turns true if all the digits of number are distinct, otherwise returns false. This method must make call(s) to getDigit. It may make call(s) to other methods. You may assume that number is always a positive integer, and you do not have to write code to check this.
indexOf(int number, int digit). Takes a positive integer number and a digit as parame- ters and returns the position (from right) at which digit appears in number, if number has distinct digits. If number does not have distinct digits, then this method returns −2. If number has distinct digits, and digit does not appear in number, then this method returns −1. This method must make calls to hasDistinctDigits, numDigits and getDigit. It may make call(s) to other meth- ods. You may assume that number is always a positive integer, and digit is a single digit number, and you do not have to write code to check this.
getCows(int first, int second). Takes two positive integers first and second and re- turns the number of cows between the two numbers. This method must make calls to numDigits, getDigit, and indexOf. It may make call(s) to other methods. You may assume that both first and second are positive integers, and both first and second have distinct digits. You do not have to write code to check this.
getBulls(int first, int second). Takes two positive integers first and second and re- turns the number of bulls between the two numbers. This method must make calls to numDigits, getDigit, and indexOf. It may make call(s) to other methods. You may assume that both first and second are positive integers, and both first and second have distinct digits. You do not have to write code to check this.
generateSecretNumber() This method returns a randomly chosen 4-digit number whose dig- its are all distinct. This method must make a call to hasDistinctDigits. It may make call(s) to other methods.
main. This is the main method. This method must do the following:
1. Print a welcome message (see the sample run).
2. Pick a secret number by calling the method generateSecretNumber
3. Repeatedly prompt the user to enter a valid guess until the guess matches with the secret number. For each guess, if the guess not valid (does not have distinct digits), then output a message saying so. If the guess is valid, then output the number of bulls (by calling the method getBulls) and number of cows (by calling the method getCows). Checking whether a guess is valid or not must be performed by making a call to hasDistinctDigits.
4. When user finds the secret number, display number of guesses that user took to find the number, and quit.
3 Generating Random Numbers
The method generateSecretNumber requires you to generate a random 4-digit number. You can use Random class of Java for this. Consider the following code fragment:
Random r = new Random();
int x = r.nextInt(50);
r.nextInt(50) generates a random integer between 0 and 49 (including 0 and 49). Similarly r.nextInt(100) generates a random integer in range [0, 99]. Note that this process may generate a number whose digits are not distinct.
4 Coding Conventions
You must follow good coding conventions. Variable names must be meaningful, variable names (and method names) must start with lower case letters, code must be properly indented. Your code must have appropriate comments. Failure to follow good coding conventions will cause you to lose points (even if your solution is correct).
5 Suggestions
For some of the methods that you will be writing (getDigit, numDigits) you will be using integer division and modulus operators. We strongly suggest you to understand these operators completely before you start coding. You will gain some practice on this in Recitation 7.
Do not attempt to write code for all the methods at once. Write one method at a time, test that the method works correctly then proceed to the next method.
Java comes with many in-built methods. You are NOT allowed to use any of the String methods. In addition, you are NOT allowed to use methods Math.pow, Integer.parseInt, Double.parseDouble. If you use any of these methods, you will lose points.
6 Specifications
You must follow the specifications exactly. Again, You are NOT allowed to use any String method in your code. If you use String methods, you will lose points. Your program must be named CowsAndBulls and should be in a package named pa3. Please note that Java is case-sensitive. The input-output behavior of your program must be exactly as described. Failure to follow the specifi- cations (even if your solution is correct) will cause you to lose points. Submit CowsAndBulls.java via blackboard. Do not submit CowsAndBulls.class.