Question
Bulls and Cows is a guessing game where we try to guess a 'n' digit code. (Note: Each number in the code must be distinct.)
Bulls and Cows is a guessing game where we try to guess a 'n' digit code. (Note: Each number in the code must be distinct.) When a number is guessed and a digit is in the correct location, then that is a bull. When a digit is in the number, but in the incorrect location, then that is a cow. So if a four digit code is 2894, and the user guesses 4698. Then that is "1 bull and 2 cows" because '9' is in the correct location and '4' and '8' are in the code but are currently in the wrong location. The guessing continues until the code is guessed in the correct order... "4 bulls
- Only accept inputs of 0, 3, 4, or 5. (Note: 0 is a cheat code)
- Repeat until a valid choice is input.
- Load vector with code digits
- If 0 is input as number of digits for the code:
- Get code as a single integer (int).
- Get the number of digits for the code. (Note: You need this so you can add a leading zero if needed, e.g. they type in 29 and you need to create 029.)
- Extract individual digits from the single integer and put into the vector for the code. Consider using integer division and modulus.
- Else randomly valid code. (i.e. number of digits was 3, 4 or 5)
- Load vector with the required number of random digits ensuring there are no duplicate.
- Get guess as a single integer.
- Extract single digits from the single integer and put in a vector for the code.
- If not enough digits, assume the missing digits to the left are zero.
- For example: For a 3 digit code, 35 is the same as 0-3-5 where zero is the first digit. For a 4 digit code, 35 is the same as 0-0-3-5.
- Ensure the guess is a valid guess!
- If it is invalid, report why the guess is not good and get it again.
- Invalid
- More digits than the number of digits in the code.
- If both problems occur, only report that the code has too many digit.
- Output results of guess.
- Bulls and cows on separate lines.
- When correct number of bulls and echo the guess back. Be sure to include a leading zero if it has one. E.g. 0-2-3 and not 2-3.
Sample run:
Enter number of digits in code (3, 4 or 5): 0
Enter code: 2894
Enter number of digits in code: 4
Number to guess: 2-8-9-4
Enter Guess: 5555
Each number must be different.
Enter Guess: 59
Each number must be different.
Enter Guess: 12345
You can only enter 4 digits.
Enter Guess: 112233
You can only enter 4 digits.
Enter Guess: 4698
1 bulls
2 cows
Enter Guess: 9687
0 bulls
2 cows
Enter Guess: 2894
4 bulls... 2-8-9-4 is correct!
Enter number of digits in code (3, 4 or 5): 5
Number to guess: 4-6-9-7-3
Enter Guess: 49763
2 bulls
3 cows
Enter Guess: 46973
5 bulls... 4-6-9-7-3 is correct!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started