Starting from:
$30

$24

Homework 3 & 4 Bulls & Cows Solution

Program




This homework will be done over two weeks. The parts below will be labeled with HW3 or HW4 indicating when it is due. HW3 will be due the first week, and HW4 will be added to HW3 to create the final program.




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




Program Flow




1.HW3: 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.




2.HW3: If 0 is input as digits to guess:




•ask for code and get as a single integer.




•ask for number of digits to guess.




•This will allow TAs to enter a code to make testing easier.

•Since this is for testing purposes, you do not have to check these for correct input.




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.




3.HW3: Ouput the code to be guessed. (This is purely to help with auto-grading, but would not be part of a real game program.)

4.HW4: Get guess as a single integer.




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




Coding Requirements




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




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




HW3: 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

HW4: 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




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




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

bulls



0 cows

Enter guess: 48502




5 bulls - 48502 is Correct!




Questions




Flowcharts for entire programs are not always useful. However, creating a flowchart for a portion of a program can be helpful.




1.HW3: Draw a flowchart for your algorithm to extract digits from an integer.




2.HW3: Draw a flowchart for your algorithm to create random code.




3.HW4: Draw a flowchart for how you will determine the number of bulls and cows.

More products