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:
Ex: The following patterns yield a userScore of 9:
simonPattern: RRGBRYYBGY
userPattern: RRGBBRYBGY
Result: Can't get test 2 to occur when userScore is 9
Testing: RRGBRYYBGY/RRGBBRYBGY
Your value: 4
Testing: RRRRRRRRRR/RRRRRRRRRY
Expected value: 9
Your value: 4
Tests aborted.

Respuesta :

Answer:

In C++:

#include <iostream>

using namespace std;

int main(){

   int userScore = 0;

   string simonPattern, userPattern;

   cout<<"Simon Pattern: ";    cin>>simonPattern;

   cout<<"User Pattern: ";    cin>>userPattern;

   for (int i =0; i < simonPattern.length();i++){

       if(simonPattern[i]== userPattern[i]){

           userScore++;        }

       else{            break;        }

   }

   cout<<"Your value: "<<userScore;

   return 0;

}

Explanation:

This initializes user score to 0

   int userScore = 0;

This declares simonPattern and userPattern as string

   string simonPattern, userPattern;

This gets input for simonPattern

   cout<<"Simon Pattern: ";    cin>>simonPattern;

This gets input for userPattern

   cout<<"User Pattern: ";    cin>>userPattern;

This iterates through each string

   for (int i =0; i < simonPattern.length();i++){

This checks for matching characters

       if(simonPattern[i]== userPattern[i]){

           userScore++;        }

This breaks the loop, if the characters mismatch

       else{            break;        }

   }

This prints the number of consecutive matches

   cout<<"Your value: "<<userScore;