Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Using C++

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

Requirements

Continuing from last week from outputting the code to be guessed,

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 number again.

Extract single digits from the single integer and put in a vector for the code. (You already did this last week.)

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 of the sample runs.

Do not use functions to find something that you could do by iterating through the vector yourself. E.g. Using .find() instead of iterating through each element in the vector and looking at the item there.

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 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! 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 guess: 89 2 bulls 0 cows Enter guess: 29 3 bulls - 029 is Correct! Enter number of digits in code (3, 4 or 5): 5 Number to guess: 48502 Enter guess: 49602 3 bulls 0 cows Enter guess: 48502 5 bulls - 48502 is Correct!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions