problem

hi im working on simon says. the game shows you a letter(For example: A)and you have to put that letter in order to continue pleying. the game also has 3 types of difficulties(begginer, intermediate, advanced) and the only thing that it varies is the speed.
Also, the game has a score to show, if you enter the sequence well you plus 1 point.
well, the problem is that i don`t know on how to continue the sequence,i mean, the program generates ONLY 1 ramdom letter,could be A, N,V,R, if you enter the right letter, the program has to show you the first letter and also a new one.
meanwhile, i need help to do the score(plus 1 if you have the sequece fine(does not matter the quantity of letters))
thanks!
this the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 #include <cstdio>   // #include <stdio.h>
#include <iostream>
#include <cstdlib>  // #include <stdlib.h>
#include <ctime>    // #include <time.h>
//#include <windows.h>
#include <string>
using namespace std;

struct player {
  string name;
  int points;
  char difficulty;
};

int main()  
{
  int num;
  srand(time(NULL));
  string sec_user;
  string sequence = "";
  int round = 1;
  player j1;   

  cout << ".::welcome to SIMON ver. 1.0.::." << endl;
  cout << " your name (fin=end of the game):" << endl;
  cin >> j1.name;
  if (j1.name == "fin") {
    return 0;
  }
  //system("cls");
  cout << ".::select level::.\n\n(b)Beginner\n\n(i)Intermediate\n\n(a)Advanced\n";
  cin >> j1.difficulty;
  //system("pause");
  //system("cls");
  //Sleep(500);


  num = rand() % 4;

  switch (num) {

  case 0:
    cout << "A"<<endl;
    //system("color 1F");
    //Beep(700, 400);
    if (j1.difficulty == 'b') {
      //Sleep(1000);
    }
    if (j1.difficulty == 'i') {
      //Sleep(500);
    }
    if (j1.difficulty == 'a') {
      //Sleep(100);
    };
    //system("cls");
    break;
  case 1:
    cout << "N"<<endl;
    //system("color 6F");
    //Beep(400, 500);
    if (j1.difficulty == 'b') {
      //Sleep(1000);
    }
    if (j1.difficulty == 'i') {
      //Sleep(500);
    }
    if (j1.difficulty == 'a') {
      //Sleep(100);
    }
    //system("cls");
    break;

  case 2:
    cout << "V"<<endl;
    //system("color 2F");
    //Beep(900, 500);
    if (j1.difficulty == 'b') {
      //Sleep(1000);
    }
    if (j1.difficulty == 'i') {
      //Sleep(500);
    }
    if (j1.difficulty == 'a') {
      //Sleep(100);
    }
    //system("cls");
    break;

  case 3:
    cout << "R"<<endl;
    //system("color CF");
    //Beep(200, 500);
    if (j1.difficulty == 'b') {
      //Sleep(1000);
    }
    if (j1.difficulty == 'i') {
      //Sleep(500);
    }
    if (j1.difficulty == 'a') {
      //Sleep(100);
    }
    //system("cls");
    break;

  }

  if (num == 0) {
    sequence = sequence + 'a';
  }
  if (num == 1) {
    sequence = sequence + 'n';
  }
  if (num == 2) {
    sequence = sequence + 'v';
  }
  if (num == 3) {
    sequence = sequence + 'r';
  }

  //system("color 0F");
  cout << "enter the sequence: " << endl;
  cin >> sec_user;
  if (sec_user == sequence)
    cout << "correct" << endl;
  else
    cout << "error" << endl;

  //system("pause");
}
Last edited on
To repeatedly run a section of code, simply use a loop. A for loop is for running it a certain number of times and a while loop is to run until an expression is false.

You can learn more about them here: http://www.cplusplus.com/doc/tutorial/control/
@kingjames6

I shortened up the code a bit, added in keeping track of points and letting a user play another game after losing.

(Entering in the sec_user, MUST be in uppercase, else the two won't match up.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdio>   // #include <stdio.h>
#include <iostream>
#include <cstdlib>  // #include <stdlib.h>
#include <ctime>    // #include <time.h>
#include <windows.h>
#include <string>

using namespace std;

struct player 
{
	string name;
	int points;
	char difficulty;
};

int main()  
{
	int num;
	srand((unsigned)time(0));
	string sec_user;
	string sequence;
	int points;

	char AddSequence[4]={'A','N','V','R'};
	player j1;   
	do
	{
		sequence = "";
		points = 0;
		// Initialize the variables being used
		cout << ".::welcome to SIMON ver. 1.0.::." << endl;
		cout << " your name (Enter 'fin' to end of the game):" << endl;
		cin >> j1.name;
		if (j1.name != "fin")
		{
			cout << ".::select level::.\n\n(b)Beginner\n\n(i)Intermediate\n\n(a)Advanced\n";
			cin >> j1.difficulty;

			do
			{
				num = rand() % 4;
				sequence += AddSequence[num];
				cout << "You have " << points << " so far.." << endl << endl;
				cout << "Sequence = " << sequence << endl;
				switch (num)
				{
				case 0:
					{
						Beep(700, 400);
					}
					break;
				case 1:
					{
						Beep(400, 500);
					}
					break;

				case 2:
					{
						Beep(900, 500);
					}
					break;

				case 3:
					{
						Beep(200, 500);
					}
					break;

					//system("color 0F");
				}
				if (j1.difficulty == 'b')
				{
					Sleep(2000);
				}
				if (j1.difficulty == 'i')
				{
					Sleep(1000);
				}
				if (j1.difficulty == 'a')
				{
					Sleep(500);
				}
				system("cls");
				cout << "enter the sequence: " << endl;
				sec_user = "";
				cin >> sec_user;
				if (sec_user == sequence)
				{
					cout << "correct" << endl;
					points++;
				}
				else
				{
					cout << "error" << endl;
					cout << "The sequence was '" << sequence << "',
 and you entered '" << sec_user << "'." << endl << endl;
				}
			}while(sec_user == sequence);
		}
	}while(j1.name != "fin");
}
Last edited on
Hello kingjames6,

I have been trying to figure out your program all day. The instructions are not complete enough to help and the code helps even less.

I am having a hard time figuring out what the delay is for and where to put it.

The more I try to figure out how to fix what you have the more I come to the conclusion that it would be best to start over. I will do that tomorrow and see what I can come up with.

If you have not already done so it would be best to start with paper and pencil to write down your idea of what the program needs to do. Back when I was in school a flow chart was emphasized more than pseudo code, but either one will work to express your ideas.

Some ideas I have:

I do not know if a struct is required, but for one player you can just as easily define the variables in "main". If some day you intend to expand to two or more players the struct would have more use.

You have a series if if statements that are repeated three times. This is a good candidate for a function. But there are ways around that too.

The variables names are good for the most part. "num" could have a better name that is more descriptive of what it is. Although you can do with out it "j1" gives no idea what it is. "player" or "player1" would be better.

Your "srand" works, but it is not the best way to write it: srand(static_cast<size_t>(time(nullptr)));. When you look up "srand" http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand you will find that the parameter needs to be an "unsidned int", but "time" does not return an "unsigned int", so it needs to be type cast. For "time()" although zero (0) or "NULL" work "nullptr" is the better choice. http://www.cplusplus.com/reference/ctime/time/

Starting with line 107 you have four if statements that contain sequence = sequence + 'a'; with the letter changing in the three following if statements. The "=" will replace anything that may be in "sequence" with a single letter. What you want is sequence += 'a';. This will add tot he string nor replace. And the same for the other if statements.

Another thing I was thing about is the "level of difficulty". When dealing with the number for the delay you might consider setting a variable to limit how many letters are used for each level.

I will think on this tonight and work on a new idea for the program tomorrow.

Hope that helps,

Andy
Topic archived. No new replies allowed.