forked from ProgrammingCCC/blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.cpp
95 lines (77 loc) · 2.18 KB
/
logic.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
#include "logic.hpp"
void Deck::print()
{
std::string s = "";
for (auto card : cards) {
s += card.to_string() + '\n';
}
std::cout << s << std::endl;
}
Card Deck::take_next()
{
Card tmp = cards.back();
cards.pop_back();
return tmp;
}
Card Blackjack::take_random()
{
size_t n = rand() % GAME_SIZE;
if (decks[n].empty()) { return take_random(); }
return decks[n].take_next();
}
void Blackjack::hit(std::vector<Card> &hand)
{
Card card = Blackjack::take_random();
if (card.get_face() == 'A' && Blackjack::get_sum(hand) + 11 > 21) { card.set_value(1); }
hand.push_back(card);
}
OutcomeType Blackjack::play()
{
hit(dealer);
hit(player);
hit(player);
std::cout << "INIT PLAYER\t";
Blackjack::print_hand(Blackjack::player);
std::cout << "INIT DEALER\t";
Blackjack::print_hand(Blackjack::dealer);
if (Blackjack::get_sum(Blackjack::player) == 21) {
std::cout << "NATURAL BLACKJACK\n";
return OutcomeType::WIN;
}
while (Blackjack::get_sum(Blackjack::player) < Blackjack::threshold) {
round++;
std::cout << Blackjack::round << ": PLAYER\t";
Blackjack::hit(Blackjack::player);
Blackjack::print_hand(Blackjack::player);
}
while(Blackjack::get_sum(Blackjack::dealer) < 17) {
round++;
std::cout << Blackjack::round << ": DEALER\t";
Blackjack::hit(Blackjack::dealer);
Blackjack::print_hand(Blackjack::dealer);
}
int dealer_total = Blackjack::get_sum(Blackjack::dealer);
int player_total = Blackjack::get_sum(Blackjack::player);
if (dealer_total == player_total) {
return OutcomeType::DRAW;
} else if (dealer_total > 21) {
return OutcomeType::WIN;
} else if (player_total > 21 || 21 - dealer_total < 21 - player_total) {
return OutcomeType::LOSE;
}
return OutcomeType::WIN;
}
int Blackjack::get_sum(std::vector<Card> &hand)
{
int sum = 0;
for (auto card : hand) {
sum += card.get_value();
}
return sum;
}
void Blackjack::print_hand(std::vector<Card> &hand)
{
for (auto card : hand) {
std::cout << card.to_string() << "\t";
} std::cout << "\n";
}