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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion focuses on implementing a "Simon Says" memory game in C++ using a for loop to compare two character sequences. Participants clarify that the goal is to iterate through the strings character by character, incrementing a score for each match until a mismatch occurs. The correct implementation involves using a for loop with an index variable to track the position in the strings, and a break statement is suggested for exiting the loop upon a mismatch. The example provided demonstrates how to initialize the strings and calculate the user score based on the number of matching characters.

PREREQUISITES
  • Understanding of C++ string manipulation
  • Familiarity with for loops and control statements in C++
  • Knowledge of character comparison in strings
  • Basic programming concepts such as variables and data types
NEXT STEPS
  • Implement a for loop in C++ to compare two strings character by character
  • Explore the use of break statements within loops for control flow
  • Learn about the C++ string class and its methods like size() and length()
  • Study error handling in C++ to manage mismatches in string comparisons
USEFUL FOR

Beginner and intermediate C++ programmers, game developers looking to implement memory games, and educators teaching string manipulation and control structures in programming.

ineedhelpnow
Messages
649
Reaction score
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
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?
 
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:
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].
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
89
Views
6K