Question: Using C++ and microsoft visual studios Bulls and Cows is a guessing game where you try to guess a 'n' digit code. (Note: Each number

Using C++ and microsoft visual studios

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"!

This website will give you a feel for the game.http://www.mathsisfun.com/games/bulls-and-cows.html

To emulate our version, click on "Options" and select any 0-9 choice.

Requirements

Coding Requirements

The code and guess must each be stored in a vector of ints.

Follow formatting as demonstrated in the "Sample Runs" below.

Program Flow

Get the number of digits to guess as input.

Only accept inputs of 0, 3, 4, or 5.

Repeat until a valid choice is input.

If 0 is input as digits to guess: (Note: This will allow us to enter a code to make testing easier. Since this is for testing purposes, you do not have to check these for correct input.)

ask for code and get as a single integer.

ask for number of digits in the code. This is needed since the number 026 would be just a 26.

Extract individual digits from the single integer and put into a vector for the code. Consider using integer division and modulus.

Else randomly create a valid code.

number of digits matching the number of digits to guess. Note: 0 (zero) is valid for the leftmost digit. However, it will look like it has less digits if output as an integer. For example: 0471 will look like 471.

each digit is distinct/unique.

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.)

Get guess as a single integer. (Not necessarily in this order.)

If more digits than the number in the code, get number again.

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 035 where zero is the first digit. For a 4 digit code, 35 is the same as 0035.

If any digits are repeated, get guess number again.

Extract single digits from the single integer and put in a vector for the code.

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. 023 and not 23.

Match the output formatting of the sample runs.

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 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: 4698 1 bulls 2 cows Enter guess: 9687 0 bulls 2 cows Enter guess: 2894 4 bulls - 2894 is Correct!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!