C++ Your Simon game must have the equivalent of three \"buttons. We recommend us
ID: 3883006 • Letter: C
Question
C++
Your Simon game must have the equivalent of three "buttons. We recommend using integers (e.g., 0, 1, 2) for this so that each round involves printing a sequence of numbers and having the user repeat this sequence, one number at a time, and then having the user type an additional number, but if you prefer to use characters (e.g., 'r', 'g', 'b'), you may use them instead. To make your button equivalences more clear to a user, please have your program start with a message similar to the following: "Welcome to the Simon game. Sequences will be made up of numbers 0, 1, and 2." The first round of your game must present only one number (or character) for the user to repeat. The user should be asked to repeat this 'sequence' and to add an additional number. Each subsequent round must present the same sequence to the user as that in the prior round with two additional numbers (or characters) at the end: (1) the number/character that the user added to the end of the sequence in the prior round and (2) a randomly generated new number/character. Note that at the beginning of round n, the sequence that will be presented to the user should be of length 2n - 1 and at the end of the round (when the user types a sequence back), the sequence length should be 2n (as the user has added a number to the sequence). The game must end as soon as a number (or character) in the sequence is incorrect. For example, if the user needs to repeat a sequence of length 5, but enters an incorrect number in the middle of the sequence, the game should end at that point rather than waiting until the user enters all numbers. The game must also end if the user selects an ineligible number to be added to the end of the sequence (i.e., trying to repeat the last number or adding a number that doesn't exist in the game). Your Simon game must be able to last for at least 6 rounds. After 6 rounds, it is okay to directly say that the user wins rather than requiring the game to continue until the user fails. Use this to your advantage in designing your program (e.g., by declaring an initial maximum-length array before the rounds begin). Each time you run the program, the user should be presented with a different sequence (so you will need to be able to randomly generate the numbers to be filled into the computer-generated portions of the sequence)Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
//Main function definition
int main ()
{
//To store computer sequence
string comSeq = "";
//To store user sequence
string userSeq = "";
//Value array with size 3
char data[3];
//Assigns character to the array
data[0]='R';
data[1]='G';
data[2]='B';
//Counter for number of rounds
int counterRound = 1;
//For random
srand(time(NULL));
//Loop for 10 rounds
while (counterRound <= 10)
{
//Generates random number max is 3
comSeq += data[rand() % 3];
//Pause
system("pause");
cout << "." << flush <<' ';
//Displays user input sequence character
cout << "Please enter " << counterRound << " characters to match:";
//Accept user sequence character
cin >> userSeq;
//if user sequence match with computer sequence continue with next round otherwise quit the loop
if (userSeq != comSeq)
{
//Displays the message and correct computer sequence
cout << "Sorry, you lose. The correct sequence was: " << comSeq;
//Come out of the loop
break;
}//End of if
//Checks if the round counter is 10 display the success message
if (counterRound == 10)
{
//Display the success message
cout << "Congratulations! you win!";
//Come out of the loop
break;
}//End of if
//Increase the counter by one
counterRound++;
}//End of loop
return 0;
}//End of main function
Sample Run 1:
Press any key to continue . . .
.
Please enter 1 characters to match:Y
Sorry, you lose. The correct sequence was: R
Sample Run 2:
Press any key to continue . . .
.
Please enter 1 characters to match:R
Press any key to continue . . .
.
Please enter 2 characters to match:RG
Press any key to continue . . .
.
Please enter 3 characters to match:RGR
Press any key to continue . . .
.
Please enter 4 characters to match:RGRB
Press any key to continue . . .
.
Please enter 5 characters to match:RGRBR
Sorry, you lose. The correct sequence was: RGRBG
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.