Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Use CLion Software or C++ Programming This game will require the player to determine the code that cracks open a safe. The safe is

Please Use CLion Software or C++ Programming

This game will require the player to determine the code that cracks open a safe. The safe is opened by entering 4 single digit numbers. Numbers can be repeated, and any number 0 - 9 is allowed. If the 4 numbers entered by the player are the correct ones and in the correct order, then and only then is the safe opened. In this game, the numbers will be chosen randomly and the player only gets 12 guesses.

Two things are given to the player to help him or her find the correct sequence of numbers. First, the player is told at the beginning of the game the sum of the four numbers. Second, after every turn, the player is told how many numbers were correctly guessed and 1) placed in the correct location or 2) placed in the wrong location. An example game play for a secret code of '9 3 0 0' is below, with the player's input in blue, bold italics and the computer's output in normal font:

#include

#include

#include

#include

#include

using namespace std;

The sum of the numbers is 12.

Please enter your guesses as 4 numbers separated by spaces.

Guess 1. 1 2 3 4

Right #'s: Right Loc. - 0, Wrong Loc. - 1

Guess 2. 5 6 7 8

Right #'s: Right Loc. - 0, Wrong Loc. - 0

Guess 3. 9 0 1 2

Right #'s: Right Loc. - 1, Wrong Loc. - 1

Guess 4. 9 4 4 9

Right #'s: Right Loc. - 1, Wrong Loc. - 0

Guess 5. 3 0 8 7

Right #'s: Right Loc. - 0, Wrong Loc. - 2

Guess 6. 9 2 0 3

Right #'s: Right Loc. - 2, Wrong Loc. - 1

Guess 7. 9 3 0 0

Right #'s: Right Loc. - 4, Wrong Loc. - 0

You cracked the safe! You win!

This game will be coding the logic that determines how many numbers are correct. When a guess is entered, the code should determine how many numbers from the guess match numbers in the secret code and are in 1) the correct location or 2) the wrong location. This logic is complicated to get correct because there can be repeated digits in the guess and/or secret code. For example, in the game shown above, the 4th guess contains two 9's. The first 9 is the match for right #/right loc. The second 9the last number on the rightdoes not get counted as a match because the 9 in the secret code has already been accounted for. I highly recommend writing pseudocode for the logic before you start coding!

Using arrays and functions make it much easier to develop a program and to write it in a way that is interpretable by humans. Please store the secret code as a 4-element array and store the user's guess each time as a second 4-element array. Please implement a function to operate the logic that determines how many numbers in the guess are right number/right location or right number/wrong location. This function will have as input 2 arraysthe current guess and the secret codeand 2 variables by referencethe number of correct numbers in the right location and the number of correct numbers in the wrong location. The reason for using variables by reference here is so that two values can be determined inside this function and "output" to variables in main ().

In this game, develop codes that allow a user to play this game against the computer. Specific functionality expected from your code is:

- Generate and store a secret code consisting of an array of 4 random single digit numbers. Output the sum of the numbers to the screen.

- Ask the player to enter in a guess for the four numbers. The player's guess should be stored as an array of 4 integers.

- Send the following values to a function: the array containing the player's guess, the array containing the secret code, a variable by reference that holds the number of right number/right locations in the current guess, and a variable by reference that holds the number of right number/wrong locations in the current guess. The function changes the values of the two variables by reference according to the guess and code arrays. It doesn't need to return a value, so can be of type void.

- Repeat the previous two steps to give the player up to 12 tries. Based on the value of the right number/right location variable, we can know if the correct code has just been guessed. If the correct code is guessed in any of those tries, the player is congratulated and the game stops. If an incorrect guess is made on the 12th try, then the player is told of their loss and the correct code is revealed. The computer can be a sore loser and/or winner if you want. The program should then exit or, if you want, it can ask the user if they want to play again and either exit the program or loop back around to the start of the game as appropriate.

Consider working first on the user interface. Write in main(): the creation of the secret code array, the display of the sum, and the collection of up to 12 guesses. Once the user interface is how you want it, put that program aside temporarily. Now, start working on the logic that determines how many digits are right #/right location and how many are right #/wrong location using a separate program. In this program, main() asks the user to input both the answer and a "guess" and then determines and outputs to the screen the right #/right location and right #/wrong location values. Doing this as a separate program will greatly speed the time to debug this difficult logic. Debug first with simpler cases like code = '1 2 3 4' and guesses of '1 2 3 5' or '0 2 3 4' or '4 3 2 1'. If those work, continue debugging with more difficult cases like code = '0 1 1 0' and guesses of '0 1 2 3' or '0 0 1 1'. Once the logic is fully functional and de-bugged, turn that code into a function and use it in the original program.

As you write the logic, one great way to make sure you don't double count repeated values in the code or guess array is to use a 4-element bool array that stores whether each spot in the secret code has already been found as a match to one of the numbers in the guess. Start with a 4-digit array that is all false/0s, and each time you match a guess, change the false/0 to a true/1 in the bool array. In the if statement, check BOTH the number and that the bool array is false. If you set the bool array up for both the guess array and the secret code array, then you can avoid measuring double and other errors.

Put another way:

First have the function look one-at-a-time through each number in the guess to compare with the corresponding number in the secret code and see if it is a match for both number and location. If so, set the corresponding bool array value to true and increase by one the right number/right location count. Next, again work one-at-a-time through each number in the guess and compare with each number in the secret code to see if it is a match for right number/wrong location. If it is and the corresponding value in the bool array is false, then this is a unique match and we should both increase by one the right number/wrong location count and set the bool array value to true.

An example:

Guess 9900

Code is 0903

Bool Guess starts 0000

Bool Guess after 'right number, right place' 0110

Bool Guess after 'right number, wrong place' 0111

Bool Code starts 0000

Bool Code after 'right number, right place' 0110

Bool Code after 'right number, wrong place' 1110

After the 'right number, right place' measurement, if both bools are 1111, the guess should perfectly match the code!

Please add a short description (with an appendix for the code and screenshots). Include an introduction, description of your work including text description of what the code does and pseudocode description of how the code works, results showing that your code if fully functional (screen shots), and a conclusion. Transcribe the commented code in a neatly formatted manner into an appendix. The entire description should have text that neatly flows from beginning to end. Do not assume that the person reading the description remembers anything about what you have been tasked to perform. In other words, the report should be readable on its own, without someone having read this document.

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

What is our total expenditure with them?

Answered: 1 week ago