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

Discussion Overview

The discussion revolves around implementing a C++ solution for the "Simon Says" memory game, focusing specifically on using a for loop to compare two strings representing the patterns. Participants are exploring how to correctly structure the loop to calculate a score based on character matches.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes the game mechanics and the need for a for loop to compare the two strings character by character, suggesting that a score is incremented for each match.
  • Another participant emphasizes the need for an if-else statement within the loop to handle matches and mismatches, indicating that the score is determined by the loop's final index.
  • A different participant proposes a rephrased problem statement, suggesting a method to find the first index where the two strings differ, and mentions that a while loop could be used instead of a for loop.
  • One participant notes that C++ strings do not require null termination and discusses the appropriateness of using a for loop for a fixed number of iterations, referencing the string length methods.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of comparing the two strings and the need for a loop structure, but there is some disagreement on the necessity of using a break statement and the appropriateness of different loop constructs.

Contextual Notes

Some participants mention the potential confusion between C strings and C++ strings, highlighting that C++ strings can contain null characters within them. There is also a discussion about the implications of using a for loop versus a while loop in this context.

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 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
3K
Replies
89
Views
6K