forked from ozo98/CardMatchingGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardGameSource.cpp
More file actions
356 lines (335 loc) · 8.97 KB
/
Copy pathCardGameSource.cpp
File metadata and controls
356 lines (335 loc) · 8.97 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "CardGameHeader.h"
char map[23][41]{
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,1 },
{ 1,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,1 },
{ 1,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,1 },
{ 1,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,2,0,2,0,0,0,1 },
{ 1,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};
char Alphabet::GetAlphabet()const { return _alphabet; }
void Alphabet::SetAlphabet(char alphabet) { this->_alphabet = alphabet; }
bool Alphabet::GetUsed()const { return _used; }
void Alphabet::SetUsed(bool what) { this->_used = what; }
Alphabet::Alphabet() : _alphabet(NULL), _used(false) {};
void Card::SetAlphabet(char a) { _alphabet.SetAlphabet(a); }
CARDSTATE Card::GetState()const { return _state; }
void Card::SetState(CARDSTATE a) { _state = a; }
int Card::GetXPos()const { return _posx; }
void Card::SetXPos(int x) { _posx = x; }
int Card::GetYPos()const { return _posy; }
void Card::SetYPos(int y) { _posy = y; }
bool Card::GetUsed()const { return _used; }
void Card::SetUsed(bool isUsed) { _used = isUsed; }
Alphabet Card::GetAlphabet()const { return _alphabet; }
void Card::OpenCard()
{
this->SetState(OPEN_CASE);
}
void Card::MatchCard()
{
}
void Card::CloseCard()
{
this->SetState(CLOSE_CASE);
}
void GameManager::PlayGame()
{
curXPos = 5;
curYPos = 5;
int n;
while (true)
{
if (_kbhit())
{
n = _getch();
n = toupper(n);
system("cls");
switch (n)
{
case 'S':
//cout << "S pressed" << endl;
if (curYPos > 0)
{
map[curYPos][curXPos] = 0;
curYPos += 4;
CHECK(curXPos, curYPos);
map[curYPos][curXPos] = 3;
}
//GetCurrentCard(0).CloseCard(cursorPosition.X,cursorPosition.Y);
cout << map[curYPos][curXPos] << endl;
Render();
break;
case 'A':
//cout << "A pressed" << endl;
if (curXPos > 0)
{
map[curYPos][curXPos] = 0;
curXPos -= 6;
CHECK(curXPos, curYPos);
map[curYPos][curXPos] = 3;
}
//GetCurrentCard(0).CloseCard(cursorPosition.X, cursorPosition.Y);
cout << map[curYPos][curXPos] << endl;
Render();
break;
case 'D':
//cout << "D pressed" << endl;
if (curXPos > 0)
{
map[curYPos][curXPos] = 0;
curXPos += 6;
CHECK(curXPos, curYPos);
map[curYPos][curXPos] = 3;
}
//GetCurrentCard(0).CloseCard(cursorPosition.X, cursorPosition.Y);
cout << map[curYPos][curXPos] << endl;
Render();
break;
case 'W':
//cout << "W pressed" << endl;
if (curYPos > 0)
{
map[curYPos][curXPos] = 0;
curYPos -= 4;
CHECK(curXPos, curYPos);
map[curYPos][curXPos] = 3;
}
//GetCurrentCard(0).CloseCard(cursorPosition.X, cursorPosition.Y);
cout << map[curYPos][curXPos] << endl;
Render();
break;
case 'F':
for (int i = 0; i < 30; i++)
{
int x = GetCurrentCard(i).GetXPos();
int y = GetCurrentCard(i).GetYPos();
if (x == curXPos && y + 2 == curYPos)
{
Check(GetCurrentCard(i));
}
}
//이게 답은 아님,, 하지만 이런 방향으로 해보세요
//이함수에서 가진건 카드 좌표밖에 없으니. 좌표값과 동일한 위치에 있는
// 실제 데이터가 있는 배열의 index = [x][y-2]
//카드를 가져와서 OpenCard()해주시면 됩니다. Check라는 함수 원형이랑 대충 틀만들어 놨으니
//함수안에 구현하시고 여기는 Check(ref) 입력하면 자동으로 실행되게 하시면 됩니다.
break;
default:
break;
}
}
}
}
void GameManager::Check(Card&ref)
{
_check_list[_checkCount] = ref;
_check_list[_checkCount].OpenCard();
_checkCount++;
if (_checkCount >= 2)
{
if (!IsSame(_check_list))
{
_check_list[0].CloseCard();
_check_list[1].CloseCard();
}
_checkCount = 0;
}
return;
}
bool GameManager::IsSame(Card* ref)
{
char alphabet1 = ref[0].GetAlphabet().GetAlphabet();
char alphabet2 = ref[1].GetAlphabet().GetAlphabet();
if (alphabet1 == alphabet2)
{
return true;
}
return false;
}
int GameManager::GetXPos()const { return curXPos; }
int GameManager::GetYPos()const { return curYPos; }
void GameManager::SetXPos(int x) { curXPos = x; }
void GameManager::SetYPos(int y) { curYPos = y; }
GAMESTATE GameManager::GetStatus()const
{
return _status;
}
Card& GameManager::GetCurrentCard(int idx)
{
return _card_list[idx];
}
void GameManager::Render()
{
system("cls");
int x[30], y[30];
char alphabet[30];
int idx = 0;
for (idx = 0; idx < 30; idx++)
{
x[idx] = GetCurrentCard(idx).GetXPos();
y[idx] = GetCurrentCard(idx).GetYPos();
map[y[idx]][x[idx]] = GetCurrentCard(idx).GetAlphabet().GetAlphabet();
alphabet[idx] = GetCurrentCard(idx).GetAlphabet().GetAlphabet();
}
idx = 0;
for (int i = 0; i < 23; i++)
{
cout << " ";
for (int j = 0; j < 41; j++)
{
if (map[i][j] == 0)
cout << ' ';
else if (map[i][j] == 1)
cout << '#';
else if (map[i][j] == 2)
cout << '.';
else if (map[i][j] == 3)
{
cout << '^';
}
else
{
if (GetCurrentCard(idx).GetState() == OPEN_CASE)
{
map[i][j] = alphabet[idx];
cout << map[i][j];
}
else
{
cout << ' ';
}
idx++;
}
}
cout << endl;
}
}
void GameManager::OpenCard()
{
system("cls");
for (int i = 0; i < 30; i++)
{
GetCurrentCard(i).OpenCard();
}
Render();
Sleep(5000);
system("cls");
for (int i = 0; i < 30; i++)
{
GetCurrentCard(i).CloseCard();
}
Render();
Sleep(2000);
_status = RUNNING;
}
void GameManager::MainMenu()
{
system("cls");
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos{ 26,10 };
int key;
MENU;
while (true)
{
if (_kbhit())
{
system("cls");
MENU;
key = _getch();
switch (key)
{
case 's': case 'S':
pos.Y = 11;
break;
case 'w': case 'W':
pos.Y = 10;
break;
case 'f': case 'F':
if (pos.Y == 10)
{
_status = OPENCARD;
return;
}
else
{
cout << "게임 종료" << endl;
exit(0);
}
default:
break;
}
}
SetConsoleCursorPosition(handle, pos);
cout << ">";
}
}
void GameManager::InitGame()
{
point = 0;
int CardIndex, AlphaIndex;
// Note: 알파벳 문자 배열을 초기화
for (int i = 0; i < 26; i++)
{
_alphabet_list[i].SetUsed(false);
_alphabet_list[i].SetAlphabet('A' + i);
}
for (int i = 0; i < 30; i++)
_card_list[i].SetUsed(false);
// Note: 카드 배열을 초기화
srand(time(NULL));
for (int i = 0; i < 15; i++)
{
while (true)
{
// Note: 임의의 알파벳 문자 인덱
AlphaIndex = rand() % 26;
if (!_alphabet_list[AlphaIndex].GetUsed())
{
_alphabet_list[AlphaIndex].SetUsed(true);
break;
}
}
// Note: 카드 배열
for (int j = 0; j < 2; j++)
{
Sleep(10);
while (1)
{
// Note: 임의의 카드 인덱스
CardIndex = rand() % 30;
if (!_card_list[CardIndex].GetUsed())
{
_card_list[CardIndex].SetUsed(true);
_card_list[CardIndex].SetAlphabet(_alphabet_list[AlphaIndex].GetAlphabet());
break;
}
}
}
}
for (int i = 0; i < 30; i++)
{
_card_list[i].SetXPos((i % 6) * 6 + 5);
_card_list[i].SetYPos((i / 6) * 4 + 3);
_card_list[i].SetState(CLOSE_CASE);
_state_list[i] = GetCurrentCard(i).GetState();
}
_status = READY;
}