58.1k views
3 votes
"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

User Dazzafact
by
6.6k points

2 Answers

1 vote

int main() {

string simon_Pattern;

string user_Pattern;

int userScore;

int i;

user_Score = 0;

simon_Pattern = "RRGBRYYBGY";

user_Pattern = "RRGBBRYBGY";

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

if (simon_Pattern[i] == user_Pattern[i]) {

user_Score = user_Score + 1;

} else {

break;

}

}

cout << "userScore: " << user_Score << endl;

return 0;

}

Here it uses two string variable to store “simson’s pattern and user’s pattern”. Then a “for loop” is executed till the end of the string. Inside the for loop both the strings are compared character by character and when found the score is added. If not for loop is exited and finally the score is displayed.

User Annachiara
by
5.8k points
2 votes

Answer:

The code is given below in Java

Step-by-step explanation:

import java.util.Scanner;

public class SimonSays

{

public static void main(String[] args)

{

String simonPattern = "";

String userPattern = "";

int userScore = 0;

int i = 0;

userScore = 0;

simonPattern = "RRGBRYYBGY";

userPattern = "RRGBBRYBGY";

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

{

if (simonPattern.charAt(i) == userPattern.charAt(i))

{

userScore = userScore + 1;

}

else

{

break;

}

}

System.out.println("userScore: " + userScore);

return;

}

}

User Andon Mitev
by
6.3k points