Simon Game

I'm trying to work on arrays, for loops, etc. by building a Simon-style game. The basic idea is that a character (R,G,B,Y) will show up and then disappear, leaving the user to remember it. As the game progresses through all 15 rounds, a new character gets added each time. I realize my code is a mess at this point, as I have changed a lot trying to get it to work. Any assistance would be greatly appreciated, as I am lost.

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
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int main () {

cout<<"Welcome to Simon! Please press Enter to begin the game!" << endl;

cin. get();
int val,range;
char index, ans[15];

char min = 'A', max = 'Z';
   		char randomSet[15]={'R','G','B','Y'};
			srand ( time(0) );
 			range = (int) max - (int) min + 1;
			randomSet [index];



	for	(val =0; val < 16; val++){

   		

  		index = rand() % 4;
  		cout << "Simon Says " << randomSet; 
			sleep (1); 
			cout<< '\010'<<"*" << endl;
			cin >> ans;
				if (strcmp (ans,randomSet)) {
						val++;		}	 	
					else 
						{cout << "You have failed.  Please try again." << endl;

							}


}
return 0;

}
Gaze in awe at this wonderful program

main.cpp:

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
130
131
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <conio.h>

#include "concor.h"
using namespace std;

void genColors(int colors[256],int round)
{
    for(int s = 0;s < round;s++)
    {
        switch(colors[s])
        {
            case 1:
                setcolor(red,red);
                break;
            case 2:
                setcolor(blue,blue);
                break;
            case 3:
                setcolor(green,green);
                break;
            case 4:
                setcolor(yellow,yellow);
                break;
            default:
                break;
        }
        system("CLS");

        Sleep(300);
        setcolor(7,black);
        system("CLS");
        Sleep(300);
    }
    setcolor(7,black);
}
int main(int nNumberofArgs,char* pszArgs[])
{
    Restart:
    int round = 1;
    int colors[256];
    char input;
    int input2;
    char input3;
    cout << "Welcome to the Simon color game!" << endl;
    system("PAUSE");
    system("CLS");

    for(;;round++)
    {
        Select:
        srand((unsigned)time(0));
        cout << "Welcome to round " << round << "!" << endl;
        cout << "R = Red, B = Blue, G = Green, Y = Yellow." << endl;
        system("PAUSE");

        for(int c = 0;c < round;c++)
        {
            colors[c] = 1 + rand() % (4);
        }
        genColors(colors,round);

        for(int c2 = 0;c2 < round;c2++)
        {
            input = getch();
            if(input == '/')
            {
                cout << "Level select: ";
                cin >> round;
                system("CLS");
                goto Select;
            }
            switch(input)
            {
                case 'r':
                case 'R':
                    input2 = 1;
                    setcolor(red,red);
                    cout << "                                                                            " << endl;
                    break;
                case 'b':
                case 'B':
                    input2 = 2;
                    setcolor(blue,blue);
                   cout << "                                                                            " << endl;
                    break;
                case 'g':
                case 'G':
                    input2 = 3;
                    setcolor(green,green);
                    cout << "                                                                            " << endl;
                    break;
                case 'y':
                case 'Y':
                    input2 = 4;
                    setcolor(yellow,yellow);
                    cout << "                                                                            " << endl;
                    break;
                default:
                    cout << "Please enter R,B,G, or Y." << endl;
            }

            setcolor(7,black);
            if(input2 != colors[c2])
            {
                cout << "You have gotten the sequence incorrect!" << endl;
                cout << "Thank you for playing!" << endl;
                cout << "Would you like to try again?(Y/N): ";
                cin >> input3;
                if(input3 == 'y' || input3 == 'Y')
                {
                    input3 = 'n';
                    system("CLS");
                    goto Restart;
                }

                system("PAUSE");
                return 0;
            }
        }
        cout << "You have won round " << round << "!" << endl;
        cout << "Now for round " << round + 1 << "!" << endl;
        system("PAUSE");
        system("CLS");
    }
}



concor.h: //How to make colors

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
#ifndef _EKU_CONCOL
#define _EKU_CONCOL

#ifndef _INC_WINDOWS
#include <windows.h>
#endif /*_INC_WINDOWS*/

bool textcolorprotect=true;
/*doesn't let textcolor be the same as backgroung color if true*/

enum concol
{
	black=0,
	dark_blue=1,
	dark_green=2,
	dark_aqua,dark_cyan=3,
	dark_red=4,
	dark_purple=5,dark_pink=5,dark_magenta=5,
	dark_yellow=6,
	dark_white=7,
	gray=8,
	blue=9,
	green=10,
	aqua=11,cyan=11,
	red=12,
	purple=13,pink=13,magenta=13,
	yellow=14,
	white=15
};

inline void setcolor(concol textcolor,concol backcolor);
inline void setcolor(int textcolor,int backcolor);
int textcolor();/*returns current text color*/
int backcolor();/*returns current background color*/

#define std_con_out GetStdHandle(STD_OUTPUT_HANDLE)

//-----------------------------------------------------------------------------

int textcolor()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(std_con_out,&csbi);
	int a=csbi.wAttributes;
	return a%16;
}

int backcolor()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(std_con_out,&csbi);
	int a=csbi.wAttributes;
	return (a/16)%16;
}

inline void setcolor(concol textcol,concol backcol)
{setcolor(int(textcol),int(backcol));}

inline void setcolor(int textcol,int backcol)
{
	if(textcolorprotect)
	{if((textcol%16)==(backcol%16))textcol++;}
	textcol%=16;backcol%=16;
	unsigned short wAttributes= ((unsigned)backcol<<4)|(unsigned)textcol;
	SetConsoleTextAttribute(std_con_out, wAttributes);
}

#if defined(_INC_OSTREAM)||defined(_IOSTREAM_)
ostream& operator<<(ostream& os,concol c)
{os.flush();setcolor(c,backcolor());return os;}
#endif

#if defined(_INC_ISTREAM)||defined(_IOSTREAM_)
istream& operator>>(istream& is,concol c)
{cout.flush();setcolor(c,backcolor());return is;}
#endif

#endif /*_EKU_CONCOL*/




Last edited on
Topic archived. No new replies allowed.