-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
76 lines (62 loc) · 1.72 KB
/
Player.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
#include <iostream>
#include "Player.h"
Player::Player() {
money = 0;
playing = true;
busted = false;
}
void Player::hit(Deck *deck) {
Card card = deck->draw();
// Keep track of aces so they can be adjusted later
if (card.is_facecard && card.name.char_name == 'A')
aces.push_back(hand.size());
hand.push_back(card);
if (card.is_facecard) {
std::cout << name << " draws " << card.name.char_name << card.suit << std::endl;
} else {
std::cout << name << " draws " << card.name.int_name << card.suit << std::endl;
}
hand_value = calc_hand_value();
if (hand_value > 21) {
std::cout << name << " BUSTS with " << hand_value << "!!!" << std::endl;
busted = true;
playing = false;
}
}
int Player::calc_hand_value() {
int value = 0;
for (int i = 0; i < hand.size(); i++)
value += hand[i].value;
while (value > 21 && aces.size() > 0) {
adjust_aces();
value -= 10;
}
return value;
}
void Player::print_hand_value() {
std::cout << name << "'s current hand is worth " << hand_value << std::endl;
}
void Player::adjust_aces() {
hand[aces.back()].value = 1;
aces.pop_back();
}
void Player::stay() {
playing = false;
std::cout << name << " stays with " << hand_value << std::endl;
}
void Player::lose(int bet) {
money -= bet;
if (money < 0)
money = 0;
std::cout << name << " loses $" << bet << " (Money left: $"
<< money << ")" << std::endl;
}
void Player::win(int bet) {
money += bet;
std::cout << name << " wins $" << bet << " (New Total: $"
<< money << ")" << std::endl;
}
void Player::discard_hand() {
hand.clear();
aces.clear();
}