-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.cpp
More file actions
110 lines (101 loc) · 2.85 KB
/
Hand.cpp
File metadata and controls
110 lines (101 loc) · 2.85 KB
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include "Card.h"
#include "Deck.h"
#include "Hand.h"
using namespace std;
// You don't need to store the actual card values in a hand
// object.
// The only thing that matters is what the value of all the
// cards in the hand is and whether or not the hand is hard
// or soft. This is why there is no array member variable
// Custom constructor for Hand
Hand::Hand() {
discard_all();
}
// Discards "cards" in hand and resets count to 0 and
// initialized the hand to a hard hand
void Hand::discard_all() {
value = 0;
soft = false;
}
// Adding a card to the hand and determining whether
// a hand is soft or hard when an Ace is added
// Ace is the tricky case
void Hand::add_card(Card c) {
int RANK_VALUES_SOFT[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10,
10, 10, 10, 11};
int RANK_VALUES_HARD[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10,
10, 10, 10, 1};
for(int rank = 0; rank < 13; ++rank) {
// Check the rank of the card passed in
if (static_cast<Card::Rank>(rank) == c.get_rank()) {
// If the card is an Ace
if(rank == 12) {
// If the Ace can bust the hand
if (value + 11 > 21) {
// If the Ace can bust the hand, but there is
// also another Ace in the hand
if(soft) {
// Change the value of the Ace in
// the hand to 1
value = value - 10;
// Add a soft value of the current Ace
value = value + RANK_VALUES_SOFT[rank];
// If a soft value of the current Ace can
// bust the hand
if(value > 21) {
// Take back the soft value and add
// the current Ace as hard, hand is hard
value = value - 11;
value = value + RANK_VALUES_HARD[rank];
soft = false;
}
else {
soft = true;
}
}
// If there is no other Ace in the hand
else {
value = value + RANK_VALUES_HARD[rank];
soft = false;
}
}
// If the current Ace cannot bust the hand, add it
// as a soft value
else {
value = value + RANK_VALUES_SOFT[rank];
soft = true;
}
}
// If the current card is not an Ace, but it can still bust
// the hand and the hand contains an Ace
else if (value + RANK_VALUES_HARD[rank] > 21 && soft == true) {
// Turn the value of the Ace in the
// hand to a hard value, hand is hard.
value = value - 10;
soft = false;
value = value + RANK_VALUES_HARD[rank];
}
// For all other cases, might bust the hand
else {
value = value + RANK_VALUES_HARD[rank];
}
}
}
}
// Getter function for value
int Hand::hand_value() const {
return value;
}
// Getter function for soft
bool Hand::hand_is_soft() const{
if (soft) {
return true;
}
return false;
}