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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around creating a memory game called "Simon Says," where the user must replicate a sequence of characters. The task involves writing a for loop in C++ to compare two strings character by character, incrementing a score for each match until a mismatch occurs, at which point the loop should exit using a break statement. Participants clarify that the comparison should start from index 0 and continue until a character differs or the end of the string is reached. A sample code snippet is provided, and there is a focus on using a for loop instead of a while loop, emphasizing that the length of the string can be determined using the size or length methods. The conversation also touches on the nuances of C++ strings, noting that they do not need to be null-terminated and can contain null characters within.
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].
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
2
Views
11K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
22
Views
3K
Replies
40
Views
3K
Replies
23
Views
2K
Back
Top