-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand.cpp
More file actions
81 lines (78 loc) · 1.95 KB
/
hand.cpp
File metadata and controls
81 lines (78 loc) · 1.95 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
#include <iostream>
using namespace std;
#include "player.h"
//Constructor
Hand::Hand(){
curDeck = nullptr;
cards = new Card*[MAXHANDSIZE];
//Initialize all card pointers to nullptr
for(int i = 0; i < MAXHANDSIZE; i++){
cards[i] = nullptr;
}
numCards =0;
value =0;
busted = false;
dynamicAce = false;
}
//Clears the hand of cards
void Hand::clearHand(){
for(int i=0;i<numCards;i++){
cards[i]=nullptr;
}
numCards =0;
value =0;
busted = false;
dynamicAce = false;
}
//Actually adds the the card value to the hand. Includes ace logic and busted logic
void Hand::addCardVal(int cardVal) {
if (cardVal == 11) {
if (value + 11 > 21) {
value += 1;
} else {
value += 11;
dynamicAce =true; // sets this flag to true so that 11 can be compressed to 1 if need to
}
} else {
value += cardVal;
}
//Ace compression
if(dynamicAce && value > 21) {
value -= 10;
dynamicAce =false;//sets to false so that it doesnt happen when isnt suppsoe to
}
++numCards;
checkBusted();
}
//Checks to make sure that busted flag is set if the value is above 21
void Hand::checkBusted(){
if(value>21){
busted = true;
}
}
//Prints the first cards of the Hand and its value as the total
//For the dealer print
void Hand::printFirstCard(){
cout<<cards[0]->getSymbol()<<" H";
cout<<endl;
cout<<"Total: "<<cards[0]->getValue()<<endl;
}
//Prints the selected Hand
void Hand::printHand(){
for(int i=0;i<numCards;i++){
cout<<cards[i]->getSymbol()<<" ";
}
cout<<endl;
cout<<"Total: "<<value;
if(busted){
cout<<" Busted. ";
}
cout<<endl;
}
//Adds the card to the hand and adds it to the handvalue
void Hand::hit(){
Card *card = curDeck->removeCardFromDeck();
cards[numCards] = card;
//addCardVal handles the increment
addCardVal(card->getValue());
}