-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_game.cpp
More file actions
225 lines (225 loc) · 6.56 KB
/
Copy pathmemory_game.cpp
File metadata and controls
225 lines (225 loc) · 6.56 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
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <limits>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
const string Str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
class Board {
private:
vector<vector<char>> Grid;
vector<vector<char>> Ans;
int Row;
int Col;
int GameOver;
int Run;//Steps
string Name;//Used char
public:
Board() {}
Board(int choice)//Mode 1/2/3
{
switch (choice) {
case 1: Row=Col=2;
break;
case 2: Row=Col=4;
break;
case 3: Row=Col=6;
break;
}
for(int i=0;i<Row;++i)
{
vector<char> temp;
for(int j=0;j<Col;++j)
{
temp.push_back(' ');
}
Grid.push_back(temp);
}
}
Board(int row,int col)//Mode 4
{
Row=row;Col=col;
for(int i=0;i<Row;++i){
vector<char> temp;
for(int j=0;j<Col;++j)
{
temp.push_back(' ');
}
Grid.push_back(temp);
}
}
void Show()//Show the board
{
for(int i=0;i<Row;++i){
for(int j=0;j<Col;++j){
cout<<Grid[i][j]<<"|";
}
cout<<endl;
}
}
void Menu()//Choose the difficulty
{
cout << "Welcome to the Memory Challenge! Please choose your difficulty:" << endl;
cout << "1 -> Beginner(2x2)" << endl;
cout << "2 -> Easy(4x4)" << endl;
cout << "3 -> Hard(6x6)" << endl;
cout << "4 -> DIY the size" << endl;
}
void Play(Board& board)//Play the game
{
while(GameOver)
{
int index=-1;
int temp=-1;
bool flag=false;//Note if card is already opened
while(1)
{
board.Show();
cout<<"Enter the index of card(index start from 1~"<<Row*Col<<")"<<endl;
cout<<"Too hard?Enter 0 to see the answer"<<endl;
if((cin>>index)&&((index>0&&index<=Row*Col&&Grid[(index-1)/Col][(index-1)%Col]==' ')||(index==0)))
{
if(index==0)
{
for(int i=0;i<Row;++i){
for(int j=0;j<Col;++j){
cout<<Ans[i][j]<<"|";
}
cout<<endl;
}
system("pause"); system("cls");
continue;
}
index--;Run++;
if(flag==false)//Open one card
{
Grid[index/Col][index%Col]=Ans[index/Col][index%Col];
temp=index;
flag=true;
system("cls");
}else{//Open another card
Grid[index/Col][index%Col]=Ans[index/Col][index%Col];
system("cls");
board.Show();
if(Grid[temp/Col][temp%Col]==Grid[index/Col][index%Col])
{
GameOver--;//Match
}else{
Grid[temp/Col][temp%Col]=Grid[index/Col][index%Col]=' ';
}
flag=false;
system("pause");system("cls");
break;
}
}else{
cin.clear();cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout<<"Invalid input!Try again."<<endl;
system("pause");system("cls");
continue;
}
}
}
}
void InitSet()//Set the board
{
Name=Str.substr(0,Row*Col/2);
Run=0;
GameOver=Row*Col/2;
Ans=Grid;
RandomSet();
}
void RandomSet()//Set the answer
{
vector<char> temp;
for(int i=0;i<Col*Row/2;++i)
{
temp.push_back(Name[i]);
temp.push_back(Name[i]);
}
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(temp.begin(),temp.end(),gen);
for(int i=0;i<Row;++i){
for(int j=0;j<Col;++j){
Ans[i][j]=temp[i*Col+j];
}
}
}
void ShowResult()//Show the result
{
cout<<"Congratulations!You have finished the game!"<<endl;
cout<<"Your score is "<<Row*Col*(int)(100.0*(double)(Row*Col)/Run)<<" and you used "<<Run<<" steps."<<endl;
}
};
int main()
{
char choice;
bool Init=false;
Board board;
start:
board.Menu();
while(!Init&&cin>>choice)
{
cin.clear();cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choice)
{
case '1':
board=Board(1);cout<<"Board has been created!"<<endl;Init=true;
break;
case '2':
board=Board(2);cout<<"Board has been created!"<<endl;Init=true;
break;
case '3':
board=Board(3);cout<<"Board has been created!"<<endl;Init=true;
break;
case '4':
cout<<"Input the row and column for the board"<<endl;
int R,C;
while(cin>>R&&cin>>C)
{
if(R>0&&C>0&&R*C%2==0&&C*R<=124)
{
board=Board(R,C);
cout<<"Board has been created!"<<endl;Init=true;
break;
}else{
cout<<"Board failed!Try to input again"<<endl;
cin.clear();cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
break;
default:
cout<<"Board failed!Try to input again"<<endl;
system("pause");system("cls");
board.Menu();
}
}
cin.clear();cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(Init)
{
board.InitSet();
system("cls");
board.Play(board);
}
if(!Init){
cout<<"Fail to start,want to try again?(Enter 'Y')"<<endl;
}else{
board.ShowResult();
cout<<"Want to play again?(Enter 'Y')"<<endl;
}
string finish;
cin>>finish;
if(finish=="y"||finish=="Y"||finish=="yes"||finish=="Yes")
{
system("cls");Init=false;
goto start;
}else{
cout<<"Done!Enter to quit."<<endl;
}
return 0;
}