Simon Says game

Pages: 12
Hi, I am trying to create a program for a class that gives the user a letter, then erases the letter, then asks the user to type in the letter. If the letter is correct the program repeats but adds an additional letter to the string. If the user input is wrong then the program ends. Currently I am having issues with the program showing the string and then erasing it. What I want the program to do is show me a letter for a couple seconds, and then erase the letter. But instead all I am getting is the erased letter. And for strings with more than one letter only the last letter is getting erased. How do I fix that? Here is my current 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
// HW6 Simon Program
// Author - Tyler Humphries
// 10/5/11

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main () {
// variable declaration, initialization
string seq;
string userseq;
char color[4];
color[0]='R';
color[1]='G';
color[2]='B';
color[3]='Y';
int round;
round=1;

// loop of 15 rounds
while (round<16){

// Displays and erases sequence
seq +=color[rand()%4];
cout << seq;
sleep (1);
cout << "\010." << flush <<'\n';



//user input of sequence
cin >> userseq;
if (userseq != seq) goto lose;
//if match continue with next round otherwise quit the loop
if (round == 15) goto win;
round++;
}

lose:
cout << "Sorry, you lose. The correct sequence was: " << seq;
goto end;

win:
cout << "Congratulations! you win!";
goto end;


end:
return 0;
}
closed account (10oTURfi)
Well you should consider adding
srand(time(NULL));
at begining of main, since rand() %4 gives you same number over and over again without it.
Also batch-like coding with alot of goto is ugly.
You should put the content of lose and win into the body of if statement.
Like this:
if(/*condition*/){/*statement sequence*/}
Also why would you want program to "sleep" for 1 milisecond? Funny.
Last edited on
Gee, thanks for critiquing my programming style but not actually fixing my problem. I'm completely new to c++, I'm not going to have perfect code just yet, that is why I am taking a class on it. It's 3:30 AM where I live, and this assignment has to be turned in tomorrow. I would really appreciate if you would at least help me with the problem that is preventing my code from functioning properly

and also the program sleeps for one second, not one millisecond.
Last edited on
closed account (10oTURfi)
Sleep wont work on my compiler :(


Buut back on topic:
Since I have no damn idea how to make flush work you can do
\b\b\b ( backspaces )
And rewrite the seq with something?
make it do 15 backspaces (wont affect lines other than one where it is) and then ask for input?
Last edited on
closed account (10oTURfi)
Gee, thanks for critiquing my programming style but not actually fixing my problem. I'm completely new to c++, I'm not going to have perfect code just yet, that is why I am taking a class on it.

Its not good to get bad habbits.
closed account (10oTURfi)
Try this. My compiler wont compile it so I am not sure will it do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while (true){

seq +=color[rand()%4];
cout << seq;
sleep(5);
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "Your input:       ";


cin >> userseq;
if (userseq != seq){ 
    cout << "Sorry, you lose. The correct sequence was: " << seq;
    break;
}
if (round == 15) {
    cout << "Congratulations! you win!";
    break;
}
round++;
}
}
Last edited on
You don't have to have a go at Krofna. He maybe just doesn't know the answer to your problem (I know I don't) and most people normally appreciate whatever help others give them. The fact that you decided to leave your homework until the last possible minute is hardly Krofna, mine on anyone else's concern.
you could probably use system("cls") to clear the whole console window. It's considered bad practise, but you don't seem very concerned about that.
You don't have to have a go at Krofna. He maybe just doesn't know the answer to your problem (I know I don't) and most people normally appreciate whatever help others give them. The fact that you decided to leave your homework until the last possible minute is hardly Krofna, mine on anyone else's concern.


I'm not blaming anyone for working on my homework this late, there is no need to put words in my mouth. If I had posted this topic asking people to clean up my programming style then I would appreciate what he had to offer, but all in all his first post was completely off topic, thus my annoyance. However his additional posts have been aimed at my issue and I appreciate his assistance.
closed account (10oTURfi)
@TheMeerkat
Haha I was trying to avoid system("cls") mainly beacuse I was trying to teach him good habbits.
But that is indeed easiest way to do it.
It will make your program run considerably slower, but It will work.

Also main problem in helping you is both indent of code and your style. That must be worst code I have tried to read in while. What do you use for coding? Notepad? Get Notepad++ at least? :/

He also couldve done this, but his teacher would obviously say he cheated:
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
void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
I'm not here to cheat on my homework. Look I'm sorry if I upset either of you guys, but currently my instructor is not really worried about style too much. I was going to fix my indents and white space after I got the program operational. I figured that goto was probably a little messy, but like I said, I tend to worry about those things after my program is actually performing as I wish. I won't post another topic asking for help unless my program is actually in proper style, because it's obvious to me there is no room in a beginner forum for slightly messy code.

As far as my coding I have been coding in chameleon. It is what my instructor gave me to code so I would have access to the university's linux servers.

something must be wrong with the sleep function. It is the function my instructor gave us to use, but she has made these mistakes before. your /b command worked as far as eliminating all the variables, but the variables still did not appear, the program waited 5 seconds and then took 15 backspaces without ever outputting the seq variable.

Look I'm sorry if I upset you guys. I'm just a beginner trying to get his program working. I'll go find a different forum for help, it's apparent to me that I'm not welcome here.
Last edited on
Don't get emo on us. Krofna was trying to help you, just in a long term way rather than a short term way.

Anyway, if you had used a debugger, you would have soon noticed that "seq" is never assigned a value, until something is ADDED to it. This means that 'seq' now contains "TOTALNONSENSEcolour1colour2colour3colour4...".

First, initialize your string, even if it's by string seq = "";. THEN add things to it.

Anyway, your biggest worry is still those goto statements. Do not use them. You're avoiding proper loop-control and it's going to bite you in the ass sooner or later.
Ah sorry, didn't mean to come off as so unwelcoming. You just see a lot of people on forums who seem to expect someone else to do a lot of their work for them and are incredibly ungrateful. Krofna's first post wasn't what you were hoping for, but it's still better for you to have read it that to have not. Also him helping you with style is relevant because when you post your code to forums if it is easier to understand then your more likely to get an answer from someone.

Anyway, good luck in your assignment and I hope you don't disappear off thinking I'm a complete jerk.
Ah sorry, didn't mean to come off as so unwelcoming. You just see a lot of people on forums who seem to expect someone else to do a lot of their work for them and are incredibly ungrateful. Krofna's first post wasn't what you were hoping for, but it's still better for you to have read it that to have not. Also him helping you with style is relevant because when you post your code to forums if it is easier to understand then your more likely to get an answer from someone.

Anyway, good luck in your assignment and I hope you don't disappear off thinking I'm a complete jerk.



I'm willing to let bygones be bygones, I just ask that when a beginner comes with a question that you understand the frustration of it all. This is learning a new language plain and simple. While I'm sure this program is incredibly simple to the two of you, I've been writing it for hours. When I came here with a question, it was as if a french man was asking someone the meaning of a word in English, yet, at the beginning at least, instead of actually telling him the meaning of the word, he was corrected on his grammar. So now he is still confused, and has even more to fix now. I'm not upset, and the style pointers are very good, but it just seems to me that style should come secondary to actually having the program working.

Your pointers have fixed some things, the delay is still broken, but I appreciate what you have done.
a) Do you actually need the delay? I don't really see a point to it.
b) Is it broken (i.e. giving compiler errors/crashing), or simply unnoticable?
Don't get emo on us.


there is no need for name calling, I made a mistake and I did my best to make amends

Anyway, if you had used a debugger, you would have soon noticed that "seq" is never assigned a value,

I am using a debugger, but it said no such thing, thank you for the correction.

Anyway, your biggest worry is still those goto statements. Do not use them. You're avoiding proper loop-control and it's going to bite you in the ass sooner or later.


No use in beating a dead horse, I have fixed the goto statements, but amongst all this style correction the delay still does not work. The program simply sits a blank screen for 5 seconds and seq is never displayed. I believe that would be my biggest problem
a) Do you actually need the delay? I don't really see a point to it.
b) Is it broken (i.e. giving compiler errors/crashing), or simply unnoticable?


a) Yes the delay is essential, basically the program should output a string of letters, which the player has a certain amount of time to memorize (the delay), erase them, and then ask for the characters again. That is game.

b) no I am not receiving compiler errors, I just can not see the characters that I have to enter, the delay happens, but I never see seq output on the screen. Therefore the game is unplayable.
Post your most recent code in it's entirety.
Can you posted the updated code?
Line 29 should read:

cout << seq << flush;

P.S. Your coding style's terrible.
(Only joking ;)

Regards, keineahnung

*Edit*
Actually that still leaves the problem that your output isn't cleared after pressing [Enter], so it's only the last letter in the sequence that's 'hidden' every iteration. This is one of those things that's just a pain in the neck to do in a console but would be easy in a GUI/game screen setup.
Last edited on
Pages: 12