Question
Bulls and Cows is a guessing game where you try to guess a 'n' digit code. (Note: Each number in the code must be distinct.)
Bulls and Cows is a guessing game where you 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"!
Program Flow
- Get the number of digits for the code.
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: (Note: This will allow TAs/autograding to enter a code to make testing easier. Since this is for testing purposes, you do not have to check these for correct input.)
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 create a valid code. (i.e. number of digits was 3, 4 or 5)
Load vector with the appropriate number of random digits ensuring there are no duplicates.
- Regardless of the method you used to create the code (entered from keyboard or randomly generated), output the code to be guessed. (This is purely to help with auto-grading. Of course in a published game you would not output the code.)
Note: Be sure to match the output of the sample runs.
Coding Requirements
- The code and guess must each be stored in a vector of ints.
- Follow formatting as demonstrated in the "Sample Runs" below.
Sample Runs
This is not complete testing!
Enter number of digits in code (3, 4 or 5): 0 Enter code: 2894 Enter number of digits in code: 4 Number to guess: 2894 Enter number of digits in code (3, 4 or 5): 0 Enter code: 29 Enter number of digits in code: 3 Number to guess: 029 Enter number of digits in code (3, 4 or 5): 5 Number to guess: 48502
Enter number of digits in code (3, 4 or 5): 3 Number to guess: 072
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