-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
187 lines (159 loc) · 4.44 KB
/
Game.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
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
#include <iostream>
#include "Game.h"
Game::Game() {
_deck.clear();
pHand.clear();
dHand.clear();
_deck = PopulateDeck();
srand(time(0));
ag = ArtGenerator();
}
int Game::CalcHandTotal(char who) {
std::vector<Card> tempDeck;
if(who == player)
tempDeck = pHand;
else if(who == dealer)
tempDeck = dHand;
int sum = 0;
for(int i = 0; i < tempDeck.size(); i++) {
sum += tempDeck.at(i).GetValue();
}
return sum;
}
void Game::ShowHand(char who) {
std::vector<Card> tempDeck;
std::string name = "";
if(who == player) {
tempDeck = pHand;
name = "Player";
}
else if(who == dealer) {
tempDeck = dHand;
name = "Dealer";
}
std::cout << "=" << name << "'s Cards=====================\n";
ag.DrawCardArray(tempDeck);
std::cout << "====================================\n";
if(!HasAceCard(tempDeck))
std::cout << name << "\'s cards are worth " << CalcHandTotal(who) << std::endl;
else if(HasAceCard(tempDeck) > 0) {
int tempSum = CalcHandTotal(who);
std::cout << name << "\'s cards are worth " << tempSum << " or with ace high " << (tempSum + 10) << std::endl;
}
}
int Game::Dealer() {
//Dealer uses the traditional Bicycle logic to try and beat the player
DrawCard(dealer);
if(CalcHandTotal(dealer) > 16) {
return Stand(dealer);
}
else if(CalcHandTotal(dealer) < 17) {
int acehigh = ( CalcHandTotal(dealer) + 10 );
if(HasAceCard(dHand) && acehigh >= 17 && acehigh < 22 ) {
return Stand(dealer);
}
else {
return Dealer();
}
}
return Dealer();
}
bool Game::DidYouBust(char who) {
if(CalcHandTotal(who) > 21)
return true;
else
return false;
}
int Game::Stand(char who) {
std::vector<Card> tempDeck;
if(who == player)
tempDeck = pHand;
else if(who == dealer)
tempDeck = dHand;
int total = CalcHandTotal(who);
if(HasAceCard(tempDeck)){
char tempChoice;
if(who == 'p') {
std::cout << "Are you playing ace low or high (H/L)";
std::cin >> tempChoice;
}
if(toupper(tempChoice) == 'H' || who == 'd') {
return ( total + 10 );
}
else if(toupper(tempChoice) == 'L') {
return total;
}
else {
Stand(who);
}
}
return total;
}
bool Game::HasAceCard(std::vector<Card> v) {
for(int i = 0; i < v.size(); i++) {
if(v.at(i).GetType() == 'A') {
return true;
}
}
return false;
}
void Game::DrawCard(char who) {
int deckDraw = rand() % _deck.size();
if(who == player)
pHand.push_back(_deck.at(deckDraw));
else if(who == dealer)
dHand.push_back(_deck.at(deckDraw));
_deck.erase(_deck.begin() + deckDraw);
}
void Game::DrawCard(char who, int amt) {
for(int i = 0; i < amt; i++)
DrawCard(who);
}
//Some coupled logic to the card class
std::vector<Card> Game::PopulateDeck() {
std::vector<Card> tempDeck;
char cSuit;
for (int suit = 1; suit < 5; suit++) {
for (int i = 1; i < 14; i++) {
switch(suit) {
case 1:
cSuit = 'D';
break;
case 2:
cSuit = 'C';
break;
case 3:
cSuit = 'S';
break;
case 4:
cSuit = 'H';
break;
}
Card tempCard = Card(i, cSuit);
tempDeck.push_back(tempCard);
}
}
return tempDeck;
}
char Game::DecideWinner() {
int pTotal = Stand(player);
//Play the Dealer's Turn
int dTotal = Dealer();
//Decide the winner
if( ( dTotal > pTotal && dTotal <= 21 ) || ( dTotal < pTotal && pTotal > 21 ) ) {
std::cout << "\nThe dealer wins! and you had " << pTotal << std::endl;
ShowHand(dealer);
return dealer;
}
else if( ( pTotal > dTotal && pTotal <= 21 ) || ( pTotal < dTotal && dTotal > 21 ) ) {
std::cout << "\nYou win! and the dealer had " << dTotal << std::endl;
ShowHand(player);
return player;
}
else {
std::cout << "\nStalemate, no winner this round." << std::endl;
ShowHand(player);
ShowHand(dealer);
return ' ';
}
}