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();
}
|