In: Computer Science
(C++) "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Assume simonPattern and userPattern are always the same length. Ex: The following patterns yield a userScore of 4:
simonPattern: RRGBRYYBGY
userPattern: RRGBBRYBGY
Output
//This code assumes simon to be a computer and thus generates a
random text for simon
#include
#include
#include
#include
using namespace std;
int main()
{
//this is done so that we get new output every time the program is
executed
srand(time(0));
char arr[4]={'R','G','B','Y'};
string simon="",user;
int score=0;
//Assigning string to simon
for(int i=0;i<10;++i)
{
simon+=arr[rand()%4];
}
cout<<"simonPattern: "<
cin>>user;
//Checking simon pattern with user pattern
for(int i=0;i<10;++i)
{
if(simon[i]==user[i])
score++;
//The loop breaks if the character isnt same
else
break;
}
//The program outputs the score at the end
cout<<"userScore: "<
}