Last Few Memory Game Questions

Thanks to this forum I'm getting there folks!

So now it's showing the elements from my array string, but it should be showing "PE" (for Periodic Element) in all slots first and then after the player selects their "cards" then they will be (and should stay) revealed.

Also, the elements aren't randomized at all, but they should be and I don't know why that's not happening.

Lastly (and admittedly I have no idea what I'm doing with this part) we are to include a timer. I tried to add one, but I have no idea if it's working or how to display it.

Any last help before this is due in 2 hours would be VERY appreciated.

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

using namespace std;

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

int grid = 6;
string easyTheme[16] = { "H", "He", "Li", "Be", "B", "C", "N", "O" };
string midTheme[36] = { "H", "He", "Li", "Be", "B", "C", "N", "O", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc" };
string hardTheme[64] = { "H", "He", "Li", "Be", "B", "C", "N", "O", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr" };

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,4);
	}
	
	if (grid ==2) {
		gridSize(6,6);
	}
	
	if (grid == 3) {
		gridSize(8,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));		
	}
	cout << "Please select a column (letter) and row (#)." << endl;
	cout << "Example: 1A or 2B. From left to right, top to bottom accordingly." << endl;
	system("Pause");
	return 0;
}
// Here is where the time should start to run. 
time_t start = time(0);

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

// Here is where my grid should display
void gridSize(int rows, int col)
{
	
   for (int p = 0; p < col; p++) 
   {
   
    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 << " " << hardTheme[p] << " " << setw(2) << left;
      if (loop == (rows)){
        cout << endl;
        loop = 0;    
      }
        loop++;

    }
  }
}
// Here is where my array should display and randomly pick based on the string array for the various boards

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

void randomizeTheme(string theme[])
{
	random_shuffle(theme, theme + 16);
}
string theme[col][rows]; We can't do this. An array's size must be known at compile time, unless you dynamically allocate a new array. We need to use "new" and "delete" to allocate a new array at run time.

The random shuffle won't work because you have a 2D array. You have to first shuffle the inner array, then shuffle the outer array (shuffle theme[][TheseHere] then shuffle theme[theseHere][]). Using 2D arrays is weird sometimes.
Unless std::random_shuffle is overloaded to take a 2D array (I don't know if it is), we need to shuffle it like I said above.

Personally, I hate timing in my code. Any reason why you have to time it?
Your instructor should tell you what you need to time (we could time anything we wanted, like how long it took to initialize a new variable, but that wouldn't be very useful, would it?).
Here are the complete instructions I've received and I've barely cracked the surface of them.
-----------------------------------------
Menu:
Start Option
New Game Option
Exit Game Option
Level of Play – Use selects at start of game
4 x 4 grid (Easy)
6 x 6 grid (Moderate)
8 X 8 grid (Difficult)
Speed of Play – At start of game, User selects time interval for clicked-on term-pair to display
1 seconds (Difficult)
3 seconds (Moderate)
5 seconds (Easy)
Populate Grid with Term
At start of game – program places the same face/theme term in all squares in the visible grid
If 4 x 4 grid, randomly pick 8 terms, place each image name twice in 2-Dim array.
If 6 x 6 grid, randomly pick 18 terns, place each image name twice in 2-Dim array.
If 8 x 8 grid, randomly pick 32 terms, place each image name twice in 2-Dim array.
The 2-Dim Array corresponds to grid on screen.
During the course of play, the face/theme term in the grid is replaced by a corresponding array term when user selects a grid square

Game Play
1) User selects a FIRST square, the theme/face term in the grid square is replace with correspond stored term, from the 2-dim array
2) User selects a SECOND square, the term theme/face in the second grid square is replace with the corresponding stored term, from the 2-dim array
3) The computer compares the terms for the two selected squares.
If they are the same, the terms remain on the screen and can no longer be selected.
If they are different, the term remains on the screen for 1, 3 or 5 seconds, depending on user selection at the beginning of the game. After that elapse time, those two grid terms are replaced with the face/theme term.

Timer (Extra credit) - Create/display a timer that keep track of the number of seconds it took to win a game.
Topic archived. No new replies allowed.