-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryGame.cpp
More file actions
189 lines (179 loc) · 5.94 KB
/
MemoryGame.cpp
File metadata and controls
189 lines (179 loc) · 5.94 KB
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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void printCards(int** gameBoard, int rows, int cols);
void shuffle(int** gameBoard, int rows, int cols);
int main()
{
cout << endl << "*************** MEMORY GAME ***************";
cout << endl << endl << "Welcome to Memory Game!";
newGame:
char A;
int rows, cols;
int hasWon = 0;
cout << endl << endl << "Enter size of rows: ";
cin >> rows;
cout << "Enter size of columns: ";
cin >> cols;
cout << endl;
if ((rows * cols) % 2 != 0)
{
cout << endl << "Invalid size. The total number of cards must be even. Please try again." << endl;
cout << endl << endl << "Enter size of rows: ";
cin >> rows;
cout << "Enter size of columns: ";
cin >> cols;
cout << endl;
}
int** gameBoard = new int* [rows];
for (int i = 0; i < rows; ++i)
{
gameBoard[i] = new int[cols];
}
printCards(gameBoard, rows, cols);
int** taken = new int* [rows];
for (int i = 0; i < rows; ++i)
{
taken[i] = new int[cols];
for (int j = 0; j < cols; j++)
taken[i][j] = 0;
}
shuffle(taken, rows, cols);
////Fonsiyonun çalýþtýðýný görmek için
//for (int i = 0; i < rows; i++) {
// for (int j = 0; j < cols; j++) {
// cout << taken[i][j] << " ";
// }
// cout << endl;
//}
gameGo:
int row1, col1, row2, col2;
cout << "\n\nFlip two cards!";
cout << "\nInput row and column for the first card: ";
cin >> row1 >> col1;
cout << "\nInput row and column for the second card: ";
cin >> row2 >> col2;
do {
cout << endl;
if (gameBoard[row1 - 1][col1 - 1] == gameBoard[row2 - 1][col2 - 1] && (row1 != row2 || col1 != col2)) {
gameBoard[row1 - 1][col1 - 1] = taken[row1 - 1][col1 - 1];
gameBoard[row2 - 1][col2 - 1] = taken[row2 - 1][col2 - 1];
hasWon++;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << gameBoard[i][j] << " ";
}
std::cout << std::endl;
}
cout << "\nMatch!\n";
cout << "Do you want to quite? Please enter Y for quite, N for new game or C for continue." << endl;
cin >> A;
if (A == 'Y' || A == 'y') {
cout << endl << endl << "******************** END ********************";
}
if (A == 'N' || A == 'n') {
goto newGame;
}
else {
cout << endl << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << gameBoard[i][j] << " ";
}
std::cout << std::endl;
}
goto gameGo;
}
}
else {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << gameBoard[i][j] << " ";
}
std::cout << std::endl;
}
cout << "\nNo match!\n";
cout << "\nCard-1 is " << gameBoard[row1-1][col1-1] <<endl<< "Card-2 is " << gameBoard[row2-1][col2-1]<<endl;
cout << "Do you want to quite? Please enter Y for quite, N for new game or C for continue." << endl;
cin >> A;
if (A == 'Y' || A == 'y') {
cout << endl << endl << "******************** END ********************";
}
if (A == 'N' || A == 'n') {
goto newGame;
}
else {
cout << endl << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << gameBoard[i][j] << " ";
}
std::cout << std::endl;
}
goto gameGo;
}
}
if (row1 == row2 && col1 == col2) {
cout << "\nYou have not choosen two different cards!" << endl;
cout << "Do you want to quite? Please enter Y for quite, N for new game or C for continue." << endl;
cin >> A;
if (A == 'Y' || A == 'y') {
cout << endl << endl << "******************** END ********************";
}
if (A == 'N' || A == 'n') {
goto newGame;
}
else {
cout << endl << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << gameBoard[i][j] << " ";
}
std::cout << std::endl;
}
goto gameGo;
}
}
} while (hasWon != (rows*cols)/2);
if (hasWon == (rows * cols) / 2)
cout << "You have won!" << endl;
for (int i = 0; i < cols; i++) {
delete[] taken[i];
}
delete[] taken;
for (int i = 0; i < cols; i++) {
delete[] gameBoard[i];
}
delete[] gameBoard;
return 0;
}
void shuffle(int** taken, int rows, int cols)
{
srand(static_cast<unsigned int>(time(NULL)));
for (int i = 0; i < (rows * cols) / 2; i++) {
int num = rand() % 100;
int row1, col1, row2, col2;
do {
row1 = rand() % rows;
col1 = rand() % cols;
} while (taken[row1][col1] != 0);
do {
row2 = rand() % rows;
col2 = rand() % cols;
} while (taken[row2][col2] != 0 || (row1 == row2 && col1 == col2));
taken[row1][col1] = num;
taken[row2][col2] = num;
}
}
void printCards(int** gameBoard, int rows, int cols)
{
cout << " " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
gameBoard[i][j] = 0;
cout << gameBoard[i][j] << " ";
}
cout << endl;
}
}