Memory Concentration Game Issues

I'm attempting to create a 4x4, 6x6 and 8x8 grid for my concentration game, but whenever the game compiles and runs it closes after making the initial selection of the difficulty level. I'm feeling a little panicked as I'm supposed to also include a timer (1, 2, 3 seconds for each choice) and I can't even get my grids to appear.

Any assistance would be greatly appreciated. I've only been learning C++ for about 10 weeks and the instructor has been less than helpful in terms of guidance for this project.

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
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;

void gridSize(int rows);
void gridSmall(void);
void gridMedium (void);
void gridHard (void);
void randomizeTheme(string theme[]);

int main()
{
	string theme[16] = { "H", "He", "Li", "Be", "B", "C", "N", "O" };
	
	int grid;
	bool firstRun = true;

	cout << "How good is your Memory? Please select a difficulty level." << endl;
	cout << "Please choose your grid: " << endl << endl;
	cout << "1. 4 x 4 grid (Easy)" << endl;
	cout << "2. 6 x 6 grid (Medium)" << endl;
	cout << "3. 8 x 8 grid (Hard)" << endl << endl;

	cin >> grid;

	if (grid == 1)
	{
		gridSize(4);
	}

	else if (grid == 2)
	{
		gridSize(6);
	}

	else if (grid == 3)
	{
		gridSize(8);
	}

	else
	{
		do
		{
			cout << "Please choose your grid: " << endl << endl;
			cout << "1. 4 x 4 grid (Easy)" << endl;
			cout << "2. 6 x 6 grid (Medium)" << endl;
			cout << "3. 8 x 8 grid (Hard)" << endl << endl;

			if (!firstRun)
			{
				system("CLS");
				cout << "Not a correct response, please try again." << endl;
				cin.clear();
				cin.ignore(INT_MAX, '\n');
			}
			firstRun = false;
		} while (!(cin >> grid));


		gridEasy();
		srand(time(0));
		randomizeTheme(theme);
		for (int i = 0; i < 16; i++)
		{
			cout << theme[i] << endl;
		}
		system("Pause");
		return 0;
	}
}

//time_t start = time(0);

//double seconds_since_start = difftime( time(0), start);


void gridSize(int rows)
{
    int element = 7;
    int loop = 0;
   for(int p = 0; p < rows; p++) {
      cout << "--- ";
      if (loop == (rows-1)){
        cout << endl;
        loop = 0;    
      }
        loop++;
   }
   for (int p = 0; p < rows; p++) {
        cout << "|" << "PE" << "| ";
      if (loop == (rows)){
        cout << endl;
        loop = 0;    
      }
        loop++;

    }
}




void randomizeTheme(string theme[])
{
	random_shuffle(theme, theme + 16);
}


Edited as I continue to make changes, but I'm still having the same issue.
Last edited on
It looks like the problem is in the if-else chain. If you enter valid input, it calls gridSize(), but then doesn't go into the else. It looks like the System("pause") is inside that else. So the code skips the else, hits the end of the main function, and ends.

Try seeing what happens if you enter invalid input the first time.

P.S. The do while loop for valid input ends if they enter an int, not checking if it is a valid one (1,2, or 3)

P.P.S. You can get rid of the if-else chain. Replace it with the do-while you already have, but make the while condition be "input != 1 && input != 2 && input != 3" and then call gridSize() after the loop (when you know the option is valid).
Last edited on
Thank you! Making those changes now and will report back.
OK it's starting to look better, but now I have two new questions.

1) It's only displaying the first row of the grid. How do I get it to display the various sizes 4x4, etc.? I thought my code towards the end would display whatever grid I wanted based on the selection made by the player, but it's not doing what I want.

2) After I select the difficulty, but before the grid displays we're supposed to have the player select their speed. However I haven't the faintest idea how to do the three different intervals (1 second - Hard, 2 seconds - Medium, 3 seconds - Easy) and since we don't have to keep a score or anything I don't even understand why the time is necessary except to make the program more complex (and stress me out since we never covered this in class).

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
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;

void gridSize(int rows);
void randomizeTheme(string theme[]);
void populateArray(string theme[]);

int grid = 9;
string theme[16] = { "H", "He", "Li", "Be", "B", "C", "N", "O" };
string cardValue[16] = {"0"};

int main()
{
	
	
	bool firstRun = true;

	cout << "How good is your Memory? Please select a difficulty level." << endl;
	cout << "Please choose your grid: " << endl << endl;
	cout << "1. 4 x 4 grid (Easy)" << endl;
	cout << "2. 6 x 6 grid (Medium)" << endl;
	cout << "3. 8 x 8 grid (Hard)" << endl << endl;

	cin >> grid;
	
	if (grid == 1) {
		gridSize(4);
	}
	
	if (grid ==2) {
		gridSize(6);
	}
	
	if (grid == 3) {
		gridSize(8);
	}

	else
	{
		do
		{
			cout << "Please choose your speed: " << endl << endl;
			cout << "1. 3 Second Intervals (Easy)" << endl;
			cout << "2. 2 Second Intervals (Medium)" << endl;
			cout << "3. 1 Second Intervals (Hard)" << endl << endl;
			
			cin >> grid;

			if (!firstRun)
			{
				system("CLS");
				cout << "Not a correct response, please try again." << endl;
				cin.clear();
				cin.ignore(INT_MAX, '\n');
			}
			firstRun = false;
		} while ((grid < 1) && (grid > 3));		
	}
	
	system("Pause");
	return 0;
}

//time_t start = time(0);

//double seconds_since_start = difftime( time(0), start);


void gridSize(int rows)
{
    int element = 7;
    int loop = 0;
   for(int p = 0; p < rows; p++) {
      cout << "--- ";
      if (loop == (rows-1)){
        cout << endl;
        loop = 0;    
      }
        loop++;
   }
   for (int p = 0; p < rows; p++) {
        cout << "|" << "PE" << "| ";
      if (loop == (rows)){
        cout << endl;
        loop = 0;    
      }
        loop++;

    }
}


void populateArray(string theme[]) {
     srand(time(0));
		randomizeTheme(theme);
		for (int i = 0; i < 16; i++)
		{
			cout << theme[i] << endl;
		}     
}

void randomizeTheme(string theme[])
{
	random_shuffle(theme, theme + 16);
}
Last edited on
Topic archived. No new replies allowed.