In: Computer Science
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. Ex: The following patterns yield a userScore of 4:
simonPattern: R, R, G, B, R, Y, Y, B, G, Y
userPattern: R, R, G, B, B, R, Y, B, G, Y
Screenshot of the code:
Sample Output:
Code to copy:
//Include the header files.
#include
#include
//Define the main() function.
int main(void)
{
//Declare and initialize the variables.
char simonPattern[50] = "";
char userPattern[50] = "";
int userScore = 0;
int i = 0;
//Initialize the variable.
userScore = 0;
//Copy the strings in the string variables.
strcpy(simonPattern, "RRGBRYYBGY");
strcpy(userPattern, "RRGBBRYBGY");
//Begin the for loop.
for(i=0; i < strlen(simonPattern); i++)
{
//Check the strings to be same.
if(simonPattern[i] == userPattern[i])
{
//Increase the userScore by 1.
userScore++;
}//End of if condition.
//Else part of if.
else
{
//Break the program.
break;
}//End of else part.
}//End of for loop.
//Display the userScores.
printf("userScore: %d\n", userScore);
//Return the value for the main()
//function.
return 0;
}//End of the main() function.