-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardHand.h
224 lines (166 loc) · 3.04 KB
/
CardHand.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/** StandardPlayingCard.h
*
* A class representing a standard playing card.
*
*
* @author Emilie Sharkey, Sophia Wajdie, Parth Patel
* @date 2015-12-10
* @info Used for building the card hands.
*
*/
#ifndef _STD_CARD_HAND
#define _STD_CARD_HAND
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>
#include "StandardPlayingCard.h"
#include "MyInputValidation14.h"
#include "StandardDeck.h"
using namespace std;
using namespace myValidation14;
// Container class
class CardHand
{
private:
vector<StandardPlayingCard> myHand;
int myPoints = 0; // the size of the c-array
StandardDeck cards;
public:
CardHand(); // constructor
~CardHand()
{
myHand.clear();
myHand.shrink_to_fit();
} // destructor
int GetPoints() const; // mySize accessor
void Initialize();
void AddToHand();
void Clear();
void CalculatePoints();
string ShowCards() const;
string ShowCards(bool) const;
static const int DEFAULT_HAND_SIZE;
};
const int CardHand::DEFAULT_HAND_SIZE = 2;
CardHand::CardHand()
{
Initialize();
}
int CardHand::GetPoints() const
{
//this->CalculatePoints();
return myPoints;
}
// to initialize or re-initialize the object
void CardHand::Initialize()
{
const int EMPTY = 0 ;
if(myHand.size() != EMPTY)
myHand.clear();
for(int counter = 0; counter < DEFAULT_HAND_SIZE; counter++)
{
myHand.push_back(cards.DrawRandomCard());
}
this->CalculatePoints();
}
void CardHand::AddToHand()
{
myHand.push_back(cards.DrawRandomCard());
this->CalculatePoints();
}
void CardHand::Clear()
{
myHand.clear();
}
void CardHand::CalculatePoints()
{
int temp = 0;
for(int i = 0; i < myHand.size();i++)
{
string hold = myHand[i].getRankString();
if (hold == "Two")
{
temp+=2;
}
else if( hold == "Three")
{
temp+=3;
}
else if( hold == "Four")
{
temp+=4;
}
else if( hold == "Five")
{
temp+=5;
}
else if( hold == "Six")
{
temp+=6;
}
else if( hold == "Seven")
{
temp+=7;
}
else if( hold == "Eight")
{
temp+=8;
}
else if( hold == "Nine")
{
temp+=9;
}
else if( hold == "Ten" || hold == "Jack" || hold == "Queen" || hold == "King")
{
temp+=10;
}
else if( hold == "Ace")
{
if (temp <= 10){
temp+=11;
} else {
temp+=1;
}
}
}
myPoints = temp;
}
string CardHand::ShowCards() const
{
stringstream output;
output;
for(int counter = 0; counter < myHand.size();counter++)
{
output<<(string)myHand[counter] <<endl;
}
int point = GetPoints();
output<<"Points: "<<point<<endl;
return output.str();
}
string CardHand::ShowCards(bool flag) const
{
stringstream output;
int counter;
output <<endl<<endl;
if (flag == false)
{
counter = 1;
output << "****** "<< endl;
}
else
{
counter = 0;
}
for(; counter < myHand.size();counter++)
{
output<< (string)myHand[counter] <<endl;
}
if(flag == true)
{
int point = GetPoints();
output<<"Points: "<<point<<endl;
}
return output.str();
}
#endif