Solve C++ "Simon Says" Memory Game with a for Loop

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++
In summary, the program compares the two strings character by character and if the corresponding characters are equal, the loop continues. If the characters are not equal, the break statement is used to exit the loop.
  • #1
ineedhelpnow
651
0
"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
Sample program:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
   string simonPattern;
   string userPattern;
   int userScore = 0;
   int i = 0;
   
   userScore = 0;
   simonPattern = "RRGBRYYBGY";
   userPattern  = "RRGBBRYBGY";

   <STUDENT CODE>

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

   return 0;
}

having trouble understanding this activity (Wasntme) can someone like clarify and help me out with it?
 
Technology news on Phys.org
  • #2
You want to compare the two pattern strings character by character (a looping construct would be appropriate with $i$ as the counter or index), and then if the corresponding characters in the two strings are equal, you want to continue, otherwise you want to quit. Then the score will be calculated from the value of $i$ when the loop finished. So, it sounds like an if-else statement will be needed inside the loop.

Can you make an attempt based on this?
 
  • #3
Hi,
Rephrase your problem. Given two strings s1 and s2 of the same length l, find the first index i with s1 unequal to s2 or if no such i exists, set i=l. Since C++ strings "start" at index 0 and s1[l]==s2[l]=='\0', this solves your problem.

Student code without a break in the program:

Code:
while(i<10 && simonPattern[i]==userPattern[i]) {
   i++;
}

Now modify the while loop with an if statement in the body that uses break. You'll also want to change the "boolean" control expression of the while loop.

This is an example showing that break is never necessary, but break is merely a programmer convenience.

Oops. Sorry, I didn't see that the problem asks to use a for loop; also I didn't notice the use of the string class, but was thinking of C strings. My comment about break was really the reason I replied in the first place.
 
Last edited:
  • #4
johng said:
Since C++ strings "start" at index 0 and s1[l]==s2[l]=='\0', this solves your problem.
C++ strings (objects of class [m]string[/m]) don't have to be null-terminated and may contain '\0' in the middle.

johng said:
Student code without a break in the program:

Code:
while(i<10 && simonPattern[i]==userPattern[i]) {
   i++;
}
The problem asks to write a [m]for[/m] statement, which is more appropriate for loops with a fixed number of iterations. The length of the simonPattern string can be found as [m]simonPattern.size()[/m] or [m]simonPattern.length()[/m].
 
  • #5


Sure, I can help clarify and guide you through this activity.

Firstly, the goal of this activity is to create a C++ program that simulates the "Simon Says" memory game. In this game, the user will be given a pattern (sequence of characters) by "Simon" and they must repeat the pattern correctly to earn points. The pattern can only contain the characters R, G, B, or Y.

To start, we need to declare and initialize some variables. The string variables "simonPattern" and "userPattern" will store the patterns given by "Simon" and the user, respectively. The integer variable "userScore" will keep track of the user's score. We also initialize the variable "i" to 0, which will be used as the index for our for loop.

Next, we need to assign values to the "simonPattern" and "userPattern" variables. In this example, we will use the patterns "RRGBRYYBGY" for "Simon" and "RRGBBRYBGY" for the user.

Now, we can start writing our for loop. The for loop will iterate through both patterns, starting from index 0. This means that we will compare the first character of both patterns, then the second character, and so on.

Inside the for loop, we will use an if statement to check if the characters at the current index in both patterns match. If they do, we will add 1 to the "userScore" variable. If they do not match, we will use a break statement to exit the for loop. This is because in the "Simon Says" game, if the user makes a mistake, the game ends and they do not earn any more points.

Once the for loop is completed, we will print out the final value of "userScore" to show the user how many points they earned.

I hope this helps clarify the activity for you. Let me know if you have any further questions or need more assistance. Good luck!
 

FAQ: Solve C++ "Simon Says" Memory Game with a for Loop

1. How does the "Simon Says" memory game work?

The "Simon Says" memory game is a popular game in which a series of flashing lights and sounds are played in a specific sequence. The player's goal is to remember the sequence and repeat it back correctly. If the player makes a mistake, the game is over.

2. What is the purpose of using a for loop in the game?

A for loop is used in the "Simon Says" memory game to repeat a specific set of instructions or actions for a certain number of times. In this game, the for loop is used to generate and display the sequence of flashing lights and sounds for the player to memorize.

3. How do I create a for loop in C++ for the "Simon Says" game?

To create a for loop in C++, you will need to specify the starting and ending points for the loop, as well as the number of iterations or repetitions. In this game, the for loop would start at 0, end at the length of the sequence, and increment by 1 each time.

4. Can the length of the sequence be changed in the game?

Yes, the length of the sequence can be changed in the game by modifying the value of a variable that represents the length. This variable can be adjusted to make the game easier or harder for the player.

5. How can I improve the "Simon Says" game using a for loop?

One way to improve the game using a for loop is to add more levels with increasing difficulty. This can be done by increasing the length of the sequence and adding more complex patterns for the player to memorize. Additionally, the game can be made more challenging by shortening the time between each iteration of the for loop, making the sequence play faster.

Similar threads

Replies
2
Views
10K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
10
Views
2K
Replies
22
Views
3K
Replies
118
Views
8K
Replies
40
Views
3K
Replies
23
Views
2K
Replies
29
Views
9K
Back
Top