-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.cpp
129 lines (110 loc) · 2.95 KB
/
blackjack.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
/**
* @file blackjack.cpp
* @author DSN
* @brief Defines the member functions of \c Blackjack
* @version 0.4
* @date 2023-08-19
*
*/
#include "blackjack.h"
#include "user.h"
#include "dealer.h"
#include "outcome.h"
#include <algorithm>
/**
* @details
* - Each player in \c m_players is reset.
* - \c active_players is cleared and re-initialized with the reset players.
*
*/
void Blackjack::resetPlayers()
{
active_players.clear();
for (auto& player : m_players)
{
player->reset();
active_players.push_back(player);
}
}
/**
* @details
* - The game is not over if
* - there is more than one player who isn't busted, AND
* - there's is atleast one person who isn't standing.
*
*/
bool Blackjack::gameNotOver()
{
bool someoneCanHit{ false };
for (auto& player : active_players)
if (not player->isStand())
{
player->chance(m_deck);
someoneCanHit = true;
}
// Use a lambda expression to remove if player->isBusted()
active_players.erase(
std::remove_if(active_players.begin(), active_players.end(),
[](const auto& player) { return player->isBusted(); }),
active_players.end());
return active_players.size() > 1 and someoneCanHit;
}
/**
* @details
* - Compare only the scores of players not busted, which is conveniently stored in \c active_players
*/
int Blackjack::getMaxScore()
{
// Use a lambda expression to compare the players by their scores.
auto winner = std::max_element(active_players.begin(), active_players.end(),
[](const auto& a, const auto& b) { return a->getScore() < b->getScore(); });
return (*winner)->getScore();
}
/**
* @details
* - A player is a winner if and only if his score matches the one calculated by \c maxScore()
*/
void Blackjack::computePlayerOutcomes()
{
std::cout << "\n----- SCORES ------\n";
int maxScore{ getMaxScore() };
for (auto& player : m_players)
{
std::cout << player->getName() << "'s score: " << player->getScore() << '\n';
player->setOutcome((player->getScore() == maxScore) ? WIN : LOSE);
}
}
void Blackjack::displayResult()
{
std::cout << "\n----- RESULTS ------\n";
for (auto& player : m_players)
{
std::cout << player->getName();
switch (player->getOutcome())
{
case WIN:
std::cout << ": WON!\n";
break;
default:
std::cout << ": lost.\n";
break;
}
}
}
/**
* @details
* - Each \c User is first \c hit twice.
* - You play while \c gameNotOver
* - Once it's over, you \c computePlayerOutcomes
*/
void Blackjack::play()
{
m_deck.shuffle();
for (std::size_t i{ 1 }; i < m_numPlayers; ++i)
{
m_players.at(i)->hit(m_deck);
m_players.at(i)->hit(m_deck);
}
while (gameNotOver());
computePlayerOutcomes();
}