-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
475 lines (427 loc) · 16.4 KB
/
game.cpp
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//
// Created by iDarkDuck on 2018-10-19.
//
#include "game.h"
//#define TEST_GAME
Game::Game(int &sideCounter, int &nPlayers) : nRound(1), currentSide(Side::Top), sideCounter(sideCounter) {
cout << endl << "Welcome to Nevin's and Peter's Memory Card Game Fall 2018" << endl;
setMode();
createPlayers(nPlayers);
makeCardDeck(), makeRewardDeck();
reset();
ready = true;
sideCounter = nPlayers - 1;
}
void Game::setMode() {
cout << "Please choose your game version:" << endl;
cout << "Enter 1 for Base Mode and 2 for Expect Mode: ";
while (true) {
char input;
cin >> input;
mode = ((int) input - 48);
if (mode == 1 || mode == 2) break;
else cout << "Invalid input, please try again: ";
}
if (mode == 2) walrusBlockValue = "Z0";
}
void Game::createPlayers(int &nPlayers) {
cout << "Number of Players " << "(Minimum 2 - Maximum 4) : ";
while (true) {
char input;
cin >> input;
nPlayers = ((int) input - 48);
if ((nPlayers < 2 || nPlayers > 4)) cout << "Invalid input, please try again: " << endl;
else break;
}
vector<string> names(static_cast<unsigned long>(nPlayers));
for (int i = 0; i < nPlayers; i++) {
cout << "Enter name for Player number " << (i + 1) << ": ";
cin >> names[i];
Player player{names[i]};
player.setSide(sides[i]);
if (!containsPlayer(player)) addPlayer(player);
else cout << "Player already added to Game!" << endl, --i;
}
}
bool Game::containsPlayer(const Player &p) { return find(players.begin(), players.end(), p) != players.end(); }
void Game::makeCardDeck() {
CardDeck &deck = CardDeck::make_CardDeck();
int i = 1, j = 1;
while (!deck.isEmpty()) {
setCard(static_cast<Letter>(i), static_cast<Number>(j++), deck.getNext());
if (j == 5) { j = 0, i++; }
}
}
void Game::makeRewardDeck() {
RewardDeck &deck = RewardDeck::make_RewardDeck();
while (!deck.isEmpty()) rewardDeck.push_back(deck.getNext());
}
void Game::nextRound() {
++nRound;
awardActivePlayers();
if (nRound != 7) reset();
int nActive = 0;
for (const auto &player: players) if (player.isActive()) nActive++;
if (nActive == 1) walrusBlockValue = "Z0";
}
int Game::getRound() const { return nRound; }
void Game::addPlayer(const Player &player) {
players.push_back(player);
vector<const Card *> cardVector;
cards.push_back(cardVector);
}
void Game::setSide(Side side) { currentSide = side; }
Player &Game::getPlayer(Side side) {
for (auto &player: players) {
if (player.getSide() == side) {
setSide(side);
return player;
}
}
throw out_of_range("Invalid Player");
}
const Card *Game::getPreviousCard() const {
vector<const Card *> cardVector = cards[static_cast<int>(currentSide)];
if (cardVector.empty()) { throw new out_of_range("No previous card selected!"); }
const Card *previousCard = cardVector[0];
return previousCard;
}
const Card *Game::getCurrentCard() const {
vector<const Card *> cardVector = cards[static_cast<int>(currentSide)];
if (cardVector.size() <= 1) { throw new out_of_range("No current card selected!"); }
const Card *currentCard = cardVector[1];
return currentCard;
}
void Game::setCurrentCard(const Card *card) {
if (mode == 1) {
if (card != nullptr) {
vector<const Card *> &cardVector = cards[static_cast<int>(currentSide)];
cardVector.push_back(card);
} else if (card == nullptr && !cards[static_cast<int>(currentSide)].empty() &&
twoCardsSelected()) { clearSelectedCards(); }
}
}
bool Game::twoCardsSelected() const {
try { return (getPreviousCard() != nullptr && getCurrentCard() != nullptr); }
catch (...) { return false; }
}
void Game::clearSelectedCards() {
vector<const Card *> &cardVector = cards[static_cast<int>(currentSide)];
cardVector.pop_back();
cardVector.pop_back();
}
void Game::reset() {
setAllPlayersActive();
board.reset();
if (mode == 1 && nRound == 1) temporaryRevealThreeCards();
if (mode == 2) cardMap.clear();
cout << "Cards are hidden now" << endl;
board.reset();
}
void Game::setAllPlayersActive() { for (auto &player: players) { player.setActive(true); }}
void Game::temporaryRevealThreeCards() {
cout << "Three random cards are revealed temporary in front of the players" << endl;
for (const auto &player: players) {
switch (player.getSide()) {
case Side::Top:
for (int i = 0; i < 3; i++) board.turnFaceUp(A, Number(Two + i));
break;
case Side::Bottom :
for (int i = 0; i < 3; i++) board.turnFaceUp(E, Number(Two + i));
break;
case Side::Left:
for (int i = 0; i < 3; i++) board.turnFaceUp(Letter(B + i), One);
break;
default:
for (int i = 0; i < 3; i++) board.turnFaceUp(Letter(B + i), Five);
break;
}
}
cout << (*this) << endl;
}
void Game::setCard(const Letter &letter, const Number &number, Card *card) {
if (!ready) board.setCard(letter, number, card);
if (mode == 2) cardMap[convertToString(letter, number)] = card;
}
string Game::convertToString(const Letter &let, const Number &num) { return (LetterToChar(let) + to_string(num)); }
char Game::LetterToChar(const Letter &_letter) {
if (_letter == A) return 'A';
else if (_letter == B) return 'B';
else if (_letter == C) return 'C';
else if (_letter == D) return 'D';
else if (_letter == E) return 'E';
return 'Z';
}
Card *Game::getCard(const Letter &letter, const Number &number) {
if (mode == 1) {
Letter let = letter;
Number num = number;
getValidInput(&let, &num, Animals);
board.turnFaceUp(let, num);
return board.getCard(let, num);
} else { getExpertRulesInput(); }
return nullptr;
}
Card *Game::getExpertCard(Letter &let, Number &num, const FaceAnimal &animal) {
if (animal == Walrus || animal == Crab) getValidInput(&let, &num, Animals);
if (animal == Octopus) getValidInput(&let, &num, Octopus);
if (animal == Penguin) {
getValidInput(&let, &num, Octopus);
if (!board.isFaceUp(let, num)) board.turnFaceUp(let, num); else board.turnFaceDown(let, num);
}
return board.getCard(let, num);
}
void Game::getValidInput(Letter *letter, Number *number, const FaceAnimal &faceAnimal) {
while (true) {
string input;
cout << "Pick a card from (A to E) and from (1 to 5): ";
while (true) {
cin >> input;
if (input.length() == 2) break;
else cout << "Invalid input, please try again: ";
}
*letter = static_cast<Letter>(toEnum(input[0]));
*number = static_cast<Number>(toEnum(input[1]));
try {
if (faceAnimal != Octopus && !board.isFaceUp(*letter, *number)) break;
else if (mode == 2 && faceAnimal == Octopus && isValidCard(*letter, *number)) break;
else cout << "Card is already faced up!" << endl;
} catch (const exception &exc) {
cout << "Invalid Card Selected, please try again" << endl;
cerr << exc.what() << endl;
}
}
}
bool Game::isValidCard(const Letter &letter, const Number &number) {
int let = static_cast<Letter>(letter) - 1;
int num = static_cast<Number>(number) - 1;
if (let >= 0 && let <= 4 && num >= 0 && num <= 4) {
if (letter == 2 && number == 2) { throw out_of_range("Not allowed to pick treasure card! game"); }
else { return true; }
} else {
cout << let << num << endl;
throw out_of_range("Card is out of range");
}
}
void Game::awardActivePlayers() {
for (auto &p: players) if (p.isActive() && !rewardDeck.empty()) p.addReward(*rewardDeck.back()), rewardDeck.pop_back();
}
ostream &operator<<(ostream &os, const Game &game) {
if (game.mode == 1 && game.getRound() < 7) {
os << game.board << endl;
for (const auto &player: game.players) { os << player << endl; }
} else if (game.mode == 2) { game.expertModePrint(); }
else game.printLeastToMostRubiesAndWinner();
return os;
}
void Game::swapCards(const Letter &l1, const Number &n1, const Letter &l2, const Number &n2) {
board.turnFaceDown(l2, n2);
cout << l1 << n1 << " : " << l2 << n2 << endl;
board.setCard(l1, n1, nullptr);
board.setCard(l2, n2, nullptr);
}
void Game::printLeastToMostRubiesAndWinner() const {
vector<Player> playerS = players;
for (auto &player: playerS) player.setDisplayMode(true);
int winCounter = 1;
sort(playerS.begin(), playerS.end());
for (auto &player : playerS) {
if (winCounter++ == playerS.size()) { cout << "Winner : "; }
cout << player.getName() << " with Rubies: " << player.getNRubies() << endl;
player.setDisplayMode(false);
}
}
void Game::expertModePrint() const {
for (int i = 0; i < 3; i++) {
for (auto const&[key, val] : cardMap) { cout << (*val)(i) << " "; };
cout << endl;
}
for (auto const &card : cardMap) { cout << card.first << " "; };
cout << endl;
}
int Game::toEnum(const char &input) {
switch (input) {
case 'A':
case '1':
return 1;
case 'B':
case '2':
return 2;
case 'C':
case '3':
return 3;
case 'D':
case '4':
return 4;
case 'E':
case '5':
return 5;
default:
return -1;
}
}
Card *Game::getExpertCard(const Letter &letter, const Number &number) {
Letter let = letter;
Number num = number;
board.turnFaceUp(let, num);
return board.getCard(let, num);
}
void Game::setExpertCurrentCard(const Card *card) {
if (card != nullptr) {
vector<const Card *> &cardVector = cards[static_cast<int>(currentSide)];
cardVector.push_back(card);
} else if (card == nullptr && !cards[static_cast<int>(currentSide)].empty() &&
twoCardsSelected()) { clearSelectedCards(); }
}
void Game::getExpertRulesInput() {
Letter letter = Z;
Number number = Zero;
while (true) {
getExpertCard(letter, number, Walrus);
if (convertToString(letter, number) == walrusBlockValue) cout << "Selected Card blocked by walrus" << endl;
else break;
}
Card *selectedCard = getExpertCard(letter, number);
setCard(letter, number, selectedCard);
setExpertCurrentCard(selectedCard);
expertRules(selectedCard, letter, number, sideCounter);
}
void Game::expertRules(Card *card, Letter &letter, Number &number, int &sideCounter) {
switch (card->getAnimal()) {
case 'O':
expertOctopus(card, letter, number);
break;
case 'P':
expertPenguin(sideCounter);
break;
case 'C':
expertCrab(card, letter, number, sideCounter);
break;
case 'W':
expertWalrus();
break;
default:
expertTurtle();
break;
}
}
void Game::expertOctopus(Card *card, Letter &letter, Number &number) {
cout << "You have picked an Octopus!" << endl;
string pick = "Pick position with an adjacent card in the same (row or column) to swap: ";
cout << pick << endl;
Letter _letter = Z;
Number _number = Zero;
while (true) {
getExpertCard(_letter, _number, Octopus);
if (!(_letter == letter || number == _number)) cout << "Invalid Card! " + pick << endl; else break;
}
char cara = LetterToChar(_letter); //card to be swapped with second card
char cara2 = LetterToChar(letter); //current card
Card *selectedCard = getExpertCard(_letter, _number); //turns face up
map<string, Card *> &cardMap = getCardMap();
if (cardMap.count(cara + to_string(_number))) { //if the second card is already face up
cout << cara + to_string(_number) << endl;
Card *temp = card;
card = selectedCard;
selectedCard = temp;
cardMap.operator[](cara + to_string(_number)) = selectedCard; //swap hashes
cardMap.operator[](cara2 + to_string(number)) = card;
} else { //swapping with faced down card
board.turnFaceDown(_letter, _number); //hideCard(_letter, _number); //turn face down
auto it = (cardMap).find(cara2 + to_string(number));
cardMap.erase(it);
Card *temp = card;
card = selectedCard;
selectedCard = temp;
cardMap.operator[](cara + to_string(_number)) = selectedCard; //hashed to new location
swapCards(letter, number, _letter, _number); //Expert Octopus //_ underscore is face down// normal is face up
}
}
void Game::expertPenguin(int &sideCounter) {
cout << "You have picked a Penguin!" << endl;
cout << "Pick a card to change face up or face down: " << endl;
Letter someLetter = Z;
Number someNumber = Zero;
Card *selectedCard = getExpertCard(someLetter, someNumber, Penguin);
char cara = LetterToChar(someLetter);
if (cardMap.count(cara + to_string(someNumber))) {
cout << "Turning Face down :" << endl;
cardMap.erase(cara + to_string(someNumber));
board.turnFaceDown(someLetter, someNumber);//game.hideCard(someLetter, someNumber);
} else {
cout << "Turning Face Up:" << endl;
cardMap.operator[](cara + to_string(someNumber)) = selectedCard;
board.turnFaceUp(someLetter, someNumber);
//setExpertCurrentCard(selectedCard);
}
cout << *selectedCard << endl;
}
void Game::expertWalrus() {
cout << "You have picked a Walrus!" << endl;
cout << "Pick a card to block from being chosen in the next round:" << endl;
Letter someLetter = Z;
Number someNumber = Zero;
getExpertCard(someLetter, someNumber, Walrus);
walrusBlockValue = LetterToChar(someLetter) + to_string(someNumber);
}
void Game::expertCrab(Card *card, Letter &letter, Number &number, int &sideCounter) {
Player &player = getPlayer(sides[sideCounter]);
cout << "You have picked a Crab!" << endl;
cout << *card << endl;
cout << "Pick another card. If it doesn't match, you lose the round:" << endl;
Letter _letter = Z;
Number _number = Zero;
Card *selectedCard;
while (true) {
cout << *(selectedCard = getExpertCard(_letter, _number, Crab)) << endl;
if (selectedCard->getColor() == card->getColor() && selectedCard->getAnimal() == card->getAnimal()) {
cout << "This is the card you just picked. Choose another" << endl;
} else break;
}
cout << player.getName() + " picked:" << endl;
if (selectedCard->getColor() != card->getColor() && selectedCard->getAnimal() != card->getAnimal()) {
cout << "The cards didn't match :( " + player.getName() + " is out of the current round." << endl;
player.setActive(false);
} else {
cout << "The cards matched :) " + player.getName() + " is still in the current round." << endl;
char cara = LetterToChar(_letter);
cardMap.operator[](cara + to_string(_number)) = selectedCard;
board.turnFaceUp(_letter, _number);
}
}
void Game::expertTurtle() {
cout << "You have picked a Turtle!" << endl;
cout << "Skipping next player's turn." << endl;
sideCounter = static_cast<int>((sideCounter + 1) % players.size());
}
map<string, Card *> &Game::getCardMap() { return cardMap; }
#ifdef TEST_GAME
int main() {
cout << "TEST_GAME" << endl;
int sideCounter, nPlayers;
cout << "Testing Game Constructor" << endl;
Game game(sideCounter, nPlayers);
assert(sideCounter == nPlayers - 1);
cout << "Testing Game Players" << endl;
assert(nPlayers >= 2 && nPlayers <= 4);
Player *currentPlayer = &game.getPlayer(sides[(sideCounter = (sideCounter + 1) % nPlayers)]);
assert(currentPlayer != nullptr);
cout << "Testing Game methods" << endl;
assert(game.getRound() == 1);
cout << "Picking first card" << endl;
Card *cardPtr = game.getCard(Z, Zero);
assert(cardPtr != nullptr);
game.setCurrentCard(cardPtr);
cout << "Picking second card" << endl;
Card *cardPtr2 = game.getCard(Z, Zero);
assert(cardPtr2 != nullptr);
game.setCurrentCard(cardPtr2);
assert(game.getPreviousCard() != nullptr);
assert(game.getCurrentCard() != nullptr);
game.nextRound();
assert(game.getRound() == 2);
cout << "Testing Print Game" << endl;
cout << game << endl;
cout << "Testing Completed" << endl;
}
#endif