-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround.cpp
118 lines (103 loc) · 3.26 KB
/
round.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
#include <iostream>
#include "round.h"
#include <cstdarg>
Round::Round() = default;
void Round::bet(Player* player, double blues) {
player->deductBlues(blues);
double newBet = bets.at(player) + blues;
bets.insert_or_assign(player, newBet);
}
double Round::bust(Player* player) {
double bluesToBePaid = bets.at(player);
bets.insert_or_assign(player, -1);
return bluesToBePaid;
}
double Round::get(Player* player) {
return bets.at(player);
}
void Round::hitPlayers() {
auto deck = dealer->getDeck();
for (auto& [player, _] : bets) {
player->hit(deck->getACard(), true);
}
}
void Round::joinPlayers(int num, ...) {
va_list valist;
va_start(valist, num);
for (int i = 0; i < num; i++) {
bets.insert_or_assign(va_arg(valist, Player*), 0);
}
va_end(valist);
}
void Round::joinDealer(Dealer* givenDealer) {
dealer = givenDealer;
}
void Round::go() {
// Deal initial hands
dealer->hit();
dealer->hit();
hitPlayers();
hitPlayers();
// Get everyone's bets
for (auto& [player, _] : bets) {
std::cout << "Player " << player->getName() << " - Balance " << player->getBlues() << std::endl;
std::cout << "Please insert your bet > ";
double playerBet;
std::cin >> playerBet;
bet(player, playerBet);
}
// Go until each player busts or stands
for (auto& [player, _] : bets) {
bool quit = false;
while (!quit) {
switch (player->chooseAction()) {
case Hit:
player->hit(dealer->getDeck()->getACard(), true);
if (player->sumOfHand(true) > 21) {
bust(player);
std::cout << "Player " << player->getName() << " has bust!" << std::endl;
quit = true;
}
break;
case Stand:
quit = true;
break;
case Split:
break;
}
}
}
// Go until the dealer busts or stands
bool quit = false;
while (!quit) {
switch(dealer->chooseAction()) {
case Hit:
dealer->hit();
break;
case Stand:
quit = true;
break;
case Split:
break;
}
}
// Show the game results and distribute winnings!
for (auto& [player, bet] : bets) {
if (bet == -1) continue;
auto playerHand = player->sumOfHand(true);
auto dealerHand = dealer->sumOfHand();
if (dealerHand > 21) {
std::cout << "Dealer busted! Player " << player->getName() << " gets double dollars!" << std::endl;
player->giveBlues(bet * 2.0);
} else if (playerHand == dealerHand) {
std::cout << "Player " << player->getName() << " pushed!" << std::endl;
player->giveBlues(bet);
} else if (playerHand > dealerHand) {
std::cout << "Player " << player->getName() << " won! Double dollars!" << std::endl;
player->giveBlues(bet * 2.0);
} else {
std::cout << "Player " << player->getName() << " lost! Nothing for you!" << std::endl;
}
}
}
Round::~Round() = default;