-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.cpp
More file actions
166 lines (148 loc) · 4.38 KB
/
Copy pathblackjack.cpp
File metadata and controls
166 lines (148 loc) · 4.38 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
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
/* Author: Farrah Lee
Program: Playing Blackjack */
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
//Function prototypes
int deal(vector<int> & deck);
int player_plays(vector<int> & player_hand, vector<int> & deck);
int dealer_plays(vector<int> & dealer_hand, vector<int> & deck);
void initialize(vector<int> & deck);
void play_game();
int value(vector<int> & hand);
void print_hand(string who, vector<int> & hand);
void print_rank(int c);
int main(){
//Initializes the game
play_game();
char answer;
//Asks the player if they would like to play again
cout << "Would you like to take a challenge and play Blackjack again? Y or N? ";
cin >> answer;
while (answer == 'Y' || answer == 'y'){
play_game();
cout << "Play again? ";
cin >> answer;
}
cout << "Thank you for playing!! Good-bye!!!!" << endl;
return 0;
}
void play_game(){
//Initialization of variables
vector<int> deck;
initialize(deck);
vector<int> player_hand;
vector<int> dealer_hand;
//Deals cards to the player and the dealer
for(int i=0;i<2;i++){
player_hand.push_back(deal(deck));
}
dealer_hand.push_back(deal(deck));
//This prints out the Player and Dealer's current hand
print_hand("Player", player_hand);
print_hand("Dealer", dealer_hand);
//This gets the value of the player and dealer's hand
int player_value, dealer_value;
player_value = player_plays(player_hand, deck);
dealer_value = dealer_plays(dealer_hand, deck);
cout << "**********************\n" << "Game Over!\n"<< "**********************" << endl;
print_hand("Player", player_hand);
print_hand("Dealer", dealer_hand);
//This if statement declares who wins the match
if (player_value > 21 && dealer_value > 21){
cout << "**********************\n" << "Bust for both Players! No one wins!\n" << "**********************" << endl;
}
else if (dealer_value > 21 || ((player_value > dealer_value) && player_value <= 21)){
cout << "**********************\n" << "Congratulations Player wins!!\n" <<"**********************" << endl;
}
else if (player_value > 21){
cout << "**********************\n" << "Dealer wins!\n" << "**********************" << endl;
}
else if (player_value == dealer_value){
cout << "**********************\n" << "Push! No one wins, try again!\n"<< "**********************" << endl;
}
else{
cout << "**********************\n" << "Dealer wins!\n"<< "**********************" << endl;
}
}
//This function deals a card from the deck
int deal(vector<int> & deck){
int r;
r = rand() % 13;
while (deck[r] == 0){
r = rand() % 13;
}
deck[r] -= 1;
return r;
}
//This function initializes the deck
void initialize(vector<int> & deck){
for(int i=0;i<12;i++){
deck.push_back(4);
}
}
//This function is where the player is playing Blackjack
int player_plays(vector<int> & player_hand, vector<int> & deck){
char answer;
while (value(player_hand) < 21){
cout << "Would you like to hit or stop? Type H or S: ";
cin >> answer;
if (answer == 'h' || answer == 'H'){
player_hand.push_back(deal(deck));
print_hand("Player", player_hand);
}
else{
return value(player_hand);
}
}
return value(player_hand);
}
//This function is where the Dealer makes its move
int dealer_plays(vector<int> & dealer_hand, vector<int> & deck){
cout << "**********************\n" << "Dealer's Turn\n" << "**********************" << endl;
while (value(dealer_hand) < 17){
dealer_hand.push_back(deal(deck));
print_hand("Dealer", dealer_hand);
}
return value(dealer_hand);
}
int value(vector<int> & hand){
int val = 0;
for (int i = 0; i < hand.size(); i ++){
int c_val = hand[i] + 1;
if ( c_val > 10 ){
c_val = 10;
}
val = val + c_val;
}
for (int i = 0; i < hand.size(); i ++){
if (val <= 11 && hand[i] == 0 ){
val = val + 10;
}
}
return val;
}
//This function displays the player's hand
void print_hand(string who, vector<int> & hand){
cout << who << "'s hand: " ;
for(int i = 0; i < hand.size(); i ++){
print_rank(hand[i]);
}
cout << endl;
cout << "Value: " << value(hand) << endl;
}
void print_rank(int c){
c = c + 1;
if ( c >= 2 && c <= 10 ){
cout << c << " ";
}
else switch(c){
case 1: cout << "A "; break;
case 11: cout << "J "; break;
case 12: cout << "Q "; break;
case 13: cout << "K "; break;
}
}