C++ Final Project Help - Memory Game

Apr 19, 2020 at 6:01pm
All of my code works with what I have but I am struggling to find a way to make the matches made in the game stay visible to the user so they don't have to look back at their past inputs. I already tried to start figuring out my problem but I just don't know how to continue or if what I have is correct.

#include <windows.h>
#include<iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <ctime>
#include <bitset>

using namespace std;

//Class
class MemoryGameFinal
{
protected:

//Declare global variables
int points = 0, point = 0;
string firstName;
char difficuty;
int column, row, row1Value, column1Value, row2Value, column2Value, matrix[4][4];
int menu;
bool again = true;
bool matrix2[4][4];
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);

public:

void start()
{
while(again){
//Main Menu
SetConsoleTextAttribute(color, 9);
cout << "Welcome to Memory - The Card Game" << endl << endl;

// Select game
SetConsoleTextAttribute(color,7);
cout << "Please enter your first name" << endl;
cin >> firstName;
cout << "Welcome, " << firstName << "!" << endl;
cout << endl << endl;
cout << "Game Menu"<< endl;
cout << " ********************"<< endl;
cout << "Select 1 for a New Game and 2 to Exit Game" << endl;
cout << "1. New Game"<< endl;
cout << "2. Exit Game"<< endl;
cout << " ********************"<< endl;
cin >> menu;

//Option to exit game and reasks the user to play again
if (menu == 2){
cout << endl;
cout << "Would you like to play Memory - The Card Game again?" << endl;
cout << "Type '1' to play again and '0' to leave" << endl;
cin >> again;
cout << endl;
}
// Option to start game and difficulty selection
else if(menu==1){
// Game grid selection
cout << endl<< "Difficulty level selection"<< endl;
cout << "*******************************" << endl;
cout << "For an Easy Game type 'e' ( 4x4 grid )"<< endl;
cout << "For an Intermediate Game type 'i' ( 6x6 grid )"<< endl;
cout << "*******************************"<< endl;

cin >> difficuty;

//Switch statement which determines the grid setup for the chosen game
switch(difficuty)
{
//Case easy
case 'e':
srand((unsigned int)time(NULL));
//Dictates number of columns and rows
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
//Initialize a random matrix
matrix[row][column]=rand()%8+1;
}
cout << endl;
}
cout << "Easy Game" << endl;
cout << "Points: " << points << endl;
// Easy grid setup
cout << " 1 2 3 4" << endl;
cout << " ";

for (int i=0; i<=8; i++)
{
cout << "-";
}
cout << endl;

//More setup
for (int row=0; row<4; row++)
{
cout << row+1<<" | ";

for (int column=0; column<4; column++)
{
cout << "* ";
}
cout << endl;
}
cout << endl;

//Initalize the Easy game function after setup
easyLoop();

//End of case easy
break;

//Easy game function
void easyLoop()
{
//Prompt
cout << firstName << ", please enter a row value for first card: " << endl;
cin >> row1Value;

cout << firstName << ", please enter a column value for first card: " << endl;
cin >> column1Value;

cout << firstName << ", please enter a row value for second card: " << endl;
cin >> row2Value;

cout << firstName << ", please enter a column value for second card: " << endl;
cin >> column2Value;
cout << endl;

//Decrease the number value as the rows and columns go down
row1Value--;
column1Value--;
row2Value--;
column2Value--;

// Output game setup to show numbers in row
cout << " 1 2 3 4" << endl;
cout << " ";

// Output setup
for (int i=0; i<=8; i++)
{
cout << "-";
}
cout << endl;

// Output setup
for (int row=0; row<4; row++)
{
cout << row+1<<" | ";

// Matching
for (int column=0; column<4; column++)
{
if ((row==row1Value)&&(column==column1Value))
{
cout << matrix[row][column]<<" ";
}
else if ((row==row2Value)&&(column==column2Value))
{
cout << matrix[row][column]<<" ";
}
else
{
cout << "* ";
}
}
cout << endl;
}

if(matrix2[4][4] = true){



}
// Initializes the function for matching results
easyMatch();
}

//Match in easy game function
void easyMatch()
{
//Check condition
if (matrix[row1Value][column1Value]==matrix[row2Value][column2Value])
{ //If there is a match
matrix2 [4][4]= true;
points = points + 2;
SetConsoleTextAttribute(color, 4);
cout << "Congratulations!" << endl;
cout << "Your Two Cards Match!" << endl << endl;
SetConsoleTextAttribute(color, 7);
cout << "Points: " << points << endl << endl; //update points
if(points == 16){ //Exits game and end screen
system("cls");
cout << "Thank you for completing my game " << firstName << "!" << endl << endl;

cout << "Would you like to play Memory - The Card Game again?" << endl;
cout << "Type '1' to play again and '0' to leave" << endl;
cin >> again;
if (again){
start();
}
else{
cout << endl; // Exits the game
cout << "Thank you for visiting my game!" << endl;
}
}
else{ // If points aren't max then goes back to game board
easyLoop();
}
}
else
{
SetConsoleTextAttribute(color, 4);
cout << "Oh No!" << endl;
cout << "Your Two Cards Do Not Match!" << endl << endl;
SetConsoleTextAttribute(color, 7);
cout << "Points: " << points << endl << endl; //update points
// Go back to the grid for more matching
easyLoop();
}
}
};


// Executable code for the whole program
int main()
{

MemoryGameFinal Game;
Game.start();

};
Apr 19, 2020 at 6:14pm
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <windows.h>
#include<iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <ctime>
#include <bitset>

using namespace std;

//Class
class MemoryGameFinal
{
protected:

//Declare global variables
int points = 0, point = 0;
string firstName;
char difficuty;
int column, row, row1Value, column1Value, row2Value, column2Value, matrix[4][4];
int menu;
bool again = true;
bool matrix2[4][4];
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);

public:

void start()
{
while(again){
//Main Menu
SetConsoleTextAttribute(color, 9);
cout << "Welcome to Memory - The Card Game" << endl << endl;

// Select game
SetConsoleTextAttribute(color,7);
cout << "Please enter your first name" << endl;
cin >> firstName;
cout << "Welcome, " << firstName << "!" << endl;
cout << endl << endl;
cout << "Game Menu"<< endl;
cout << " ********************"<< endl;
cout << "Select 1 for a New Game and 2 to Exit Game" << endl;
cout << "1. New Game"<< endl;
cout << "2. Exit Game"<< endl;
cout << " ********************"<< endl;
cin >> menu;

//Option to exit game and reasks the user to play again
if (menu == 2){
cout << endl;
cout << "Would you like to play Memory - The Card Game again?" << endl;
cout << "Type '1' to play again and '0' to leave" << endl;
cin >> again;
cout << endl;
}
// Option to start game and difficulty selection
else if(menu==1){
// Game grid selection
cout << endl<< "Difficulty level selection"<< endl;
cout << "*******************************" << endl;
cout << "For an Easy Game type 'e' ( 4x4 grid )"<< endl;
cout << "For an Intermediate Game type 'i' ( 6x6 grid )"<< endl;
cout << "*******************************"<< endl;

cin >> difficuty;

//Switch statement which determines the grid setup for the chosen game
switch(difficuty)
{
//Case easy
case 'e':
srand((unsigned int)time(NULL));
//Dictates number of columns and rows
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
//Initialize a random matrix
matrix[row][column]=rand()%8+1;
}
cout << endl;
}
cout << "Easy Game" << endl;
cout << "Points: " << points << endl;
// Easy grid setup
cout << " 1 2 3 4" << endl;
cout << " ";

for (int i=0; i<=8; i++)
{
cout << "-";
}
cout << endl;

//More setup
for (int row=0; row<4; row++)
{
cout << row+1<<" | ";

for (int column=0; column<4; column++)
{
cout << "* ";
}
cout << endl;
}
cout << endl;

//Initalize the Easy game function after setup
easyLoop();

//End of case easy
break;

//Easy game function
void easyLoop()
{
//Prompt
cout << firstName << ", please enter a row value for first card: " << endl;
cin >> row1Value;

cout << firstName << ", please enter a column value for first card: " << endl;
cin >> column1Value;

cout << firstName << ", please enter a row value for second card: " << endl;
cin >> row2Value;

cout << firstName << ", please enter a column value for second card: " << endl;
cin >> column2Value;
cout << endl;

//Decrease the number value as the rows and columns go down
row1Value--;
column1Value--;
row2Value--;
column2Value--;

// Output game setup to show numbers in row
cout << " 1 2 3 4" << endl;
cout << " ";

// Output setup
for (int i=0; i<=8; i++)
{
cout << "-";
}
cout << endl;

// Output setup
for (int row=0; row<4; row++)
{
cout << row+1<<" | ";

// Matching
for (int column=0; column<4; column++)
{
if ((row==row1Value)&&(column==column1Value))
{
cout << matrix[row][column]<<" ";
}
else if ((row==row2Value)&&(column==column2Value))
{
cout << matrix[row][column]<<" ";
}
else
{
cout << "* ";
}
}
cout << endl;
}

if(matrix2[4][4] = true){



}
// Initializes the function for matching results
easyMatch();
}

//Match in easy game function
void easyMatch()
{
//Check condition
if (matrix[row1Value][column1Value]==matrix[row2Value][column2Value])
{ //If there is a match
matrix2 [4][4]= true;
points = points + 2;
SetConsoleTextAttribute(color, 4);
cout << "Congratulations!" << endl;
cout << "Your Two Cards Match!" << endl << endl;
SetConsoleTextAttribute(color, 7);
cout << "Points: " << points << endl << endl; //update points
if(points == 16){ //Exits game and end screen
system("cls");
cout << "Thank you for completing my game " << firstName << "!" << endl << endl;

cout << "Would you like to play Memory - The Card Game again?" << endl;
cout << "Type '1' to play again and '0' to leave" << endl;
cin >> again;
if (again){
start();
}
else{
cout << endl; // Exits the game
cout << "Thank you for visiting my game!" << endl;
}
}
else{ // If points aren't max then goes back to game board
easyLoop();
}
}
else
{
SetConsoleTextAttribute(color, 4);
cout << "Oh No!" << endl;
cout << "Your Two Cards Do Not Match!" << endl << endl;
SetConsoleTextAttribute(color, 7);
cout << "Points: " << points << endl << endl; //update points
// Go back to the grid for more matching
easyLoop();
}
}
};


// Executable code for the whole program
int main()
{

MemoryGameFinal Game;
Game.start();

};
Apr 19, 2020 at 8:02pm
All of my code works
Perhaps, but what you posted won't even compile. You seem to be missing four(!) closing braces after line 113. Thanks TheToaster for posting with code tags. It looks like you're missing a whole section for intermediate games.

I am struggling to find a way to make the matches made in the game stay visible to the user so they don't have to look back at their past inputs.
I think that is what matrix2 is for. Cells are true if the corresponding entries in matrix have been guessed correctly so far.

To simplify the code, I created a display() method that displays the board. It shows '*' if the position is hidden and the card value if the position has been exposed.

As you play, the code exposes the two cards that you guess. If the cards match, it leaves them exposed. Otherwise it hides them again.

I changed easyLoop() and easyMatch() to Loop() and Match(). These same functions should be used for the intermediate game. You should just add one member variable to the class that says how big the grid is (4 for easy, 6 for intermediate) and then the code should use that variable to initialize the board, display the pieces etc.

I also noticed that you call the various functions over and over rather than having loops. I changed that. I also moved some of the logic around slightly. Look at this version and compare it to your original code. I put a loop in easyLoop(). When you finish the game, I let the loop in start() print the menu again.
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <windows.h>
#include<iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <ctime>
#include <bitset>

using namespace std;

//Class
class MemoryGameFinal
{
  protected:

//Declare global variables
    int points = 0, point = 0;
    string firstName;
    char difficuty;
    int column, row, row1Value, column1Value, row2Value, column2Value, matrix[4][4];
    int menu;
    bool again = true;
    bool matrix2[4][4];
    HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE);

  public:

    // Display the cards
    void display()
    {
	// Easy grid setup
	cout << "    1 2 3 4" << endl;
	cout << "  ";

	for (int i = 0; i <= 8; i++) {
	    cout << "-";
	}
	cout << endl;

	//More setup
	for (int row = 0; row < 4; row++) {
	    cout << row + 1 << " | ";

	    for (int column = 0; column < 4; column++) {
		// Print the value or '*' depending on wether the card
		// is exposed.
		if (matrix2[row][column]) {
		    cout << matrix[row][column];
		} else {
		    cout << '*';
		}
		cout << ' ';
	    }
	    cout << endl;
	}
	cout << endl;
    }
    
    void start()
    {
	// The first few lines are only printed once.
	SetConsoleTextAttribute(color, 9);
	cout << "Welcome to Memory - The Card Game" << endl << endl;

	// Select game
	SetConsoleTextAttribute(color, 7);
	cout << "Please enter your first name" << endl;
	cin >> firstName;

	//Main Menu
	while (again) {
	    cout << "Welcome, " << firstName << "!" << endl;
	    cout << endl << endl;
	    cout << "Game Menu" << endl;
	    cout << " ********************" << endl;
	    cout << "Select 1 for a New Game and 2 to Exit Game" << endl;
	    cout << "1. New Game" << endl;
	    cout << "2. Exit Game" << endl;
	    cout << " ********************" << endl;
	    cin >> menu;

	    //Option to exit game and reasks the user to play again
	    if (menu == 2) {
		cout << endl;
		cout << "Would you like to play Memory - The Card Game again?" << endl;
		cout << "Type '1' to play again and '0' to leave" << endl;
		cin >> again;
		cout << endl;
	    }
	    // Option to start game and difficulty selection
	    else if (menu == 1) {
		// Game grid selection
		cout << endl << "Difficulty level selection" << endl;
		cout << "*******************************" << endl;
		cout << "For an Easy Game type 'e' ( 4x4 grid )" << endl;
		cout << "For an Intermediate Game type 'i' ( 6x6 grid )" << endl;
		cout << "*******************************" << endl;

		cin >> difficuty;

		//Switch statement which determines the grid setup for
		//the chosen game
		switch (difficuty) {
		    //Case easy
		case 'e':
		    srand((unsigned int) time(NULL));
		    //Dictates number of columns and rows
		    for (int row = 0; row < 4; row++) {
			for (int column = 0; column < 4; column++) {
			    //Initialize a random matrix
			    matrix[row][column] = rand() % 8 + 1;
			    matrix2[row][column] = false;
			}
			cout << endl;
		    }
		    cout << "Easy Game" << endl;
		    cout << "Points: " << points << endl;

		    // display the board
		    display();

		    //Initalize the  game function after setup
		    Loop();

		    //End of case easy
		    break;
		}
	    }
	}
    }

    // game function
    void Loop()
    {
	while (points != 16) {
	    //Prompt
	    cout << firstName << ", please enter a row value for first card: "
		 << endl;
	    cin >> row1Value;

	    cout << firstName <<
		", please enter a column value for first card: " << endl;
	    cin >> column1Value;

	    cout << firstName <<
		", please enter a row value for second card: " << endl;
	    cin >> row2Value;

	    cout << firstName <<
		", please enter a column value for second card: " << endl;
	    cin >> column2Value;
	    cout << endl;

	    //Decrease the number value as the rows and columns go down
	    row1Value--;
	    column1Value--;
	    row2Value--;
	    column2Value--;

	    // Temporarily expose the cards
	    matrix2[row1Value][column1Value] = true;
	    matrix2[row2Value][column2Value] = true;

	    // display the board
	    display();

	    // Initializes the function for matching results
	    Match();
	}

	// When you get here, points == 16
	system("cls");
	cout << "Thank you for completing my game " << firstName
	     << "!" << endl << endl;
    }

    //Match in  game function
    void Match()
    {
	//Check condition
	if (matrix[row1Value][column1Value] == matrix[row2Value][column2Value]) {	//If there is a match
	    // Leave them exposed
	    points = points + 2;
	    SetConsoleTextAttribute(color, 4);
	    cout << "Congratulations!" << endl;
	    cout << "Your Two Cards Match!" << endl << endl;
	    SetConsoleTextAttribute(color, 7);
	    cout << "Points: " << points << endl << endl;	//update points
	} else {
	    SetConsoleTextAttribute(color, 4);
	    cout << "Oh No!" << endl;
	    cout << "Your Two Cards Do Not Match!" << endl << endl;
	    SetConsoleTextAttribute(color, 7);
	    cout << "Points: " << points << endl << endl;	//update points

	    // Cover them up again
	    matrix2[row1Value][column1Value] = false;
	    matrix2[row2Value][column2Value] = false;
	}
    }
};

// Executable code for the whole program
int main()
{

    MemoryGameFinal Game;
    Game.start();

}


Topic archived. No new replies allowed.