Your Simon game must have the equivalent of three \"buttons.\" We recommend usin
ID: 3883504 • Letter: Y
Question
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 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. 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>
#include <ctime>
#include <cerrno>
using namespace std;
/**
* Custom sleep funtion for linux.
* @param msec Number of milliseconds to sleep.
*/
void my_sleep(unsigned msec)
{
struct timespec req, rem;
int err;
req.tv_sec = msec / 1000;
req.tv_nsec = (msec % 1000) * 1000000;
while ((req.tv_sec != 0) || (req.tv_nsec != 0)) {
if (nanosleep(&req, &rem) == 0)
break;
err = errno;
// Interrupted; continue
if (err == EINTR) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
}
// Unhandleable error (EFAULT (bad pointer), EINVAL (bad timeval in tv_nsec), or ENOSYS (function not supported))
break;
}
}
int main ()
{
// variable declarations
string my_seq = "";
string user_seq = "";
char colors[4];
int round_now;
// Initialization
round_now = 1;
srand(time(NULL));
colors[0]='R';
colors[1]='G';
colors[2]='B';
colors[3]='Y';
// loop of 15 round_nows
while (round_now < 16) {
// Displays and erases my_sequence
my_seq += colors[rand()%4];
cout << my_seq << flush;
my_sleep (5);
cout << "." << flush <<' ';
// user input of my_sequence
cout << "Please enter " << round_now << " characters to match:";
cin >> user_seq;
// if match continue with next round_now otherwise quit the loop
if (user_seq != my_seq) {
cout << "Sorry, you lose. The correct my_sequence was: " << my_seq
<< endl;
break;
}
if (round_now == 15){
cout << "Congratulations! you win!" << endl;
break;
}
round_now++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.