In: Computer Science
Design a c++ program to simulate the BlackJack game. Rules:
1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values).
2) display BOTH cards of the players, and ONE card of the computer.
3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below.
5) Computer must stay if 17 or above.
6) Ace can be 1 or 11 in value -Can not bust if you go over 21 if you are using an Ace as 11 (example: 8+11+6 =/= 25, it would convert to 8+1+6 == 15) .
For example ACE ACE ACE ACE 8.
Can't Equal 11+1+1+1+8 = 22(it's a bust! no good!) ^--that 11 would drop to 1
much better: 1+1+1+1+8 = 12,
for example, A, 2, 3, A. So for the best result, the user will choose 11 for the first A, 1 for the second A, or vise versa.
Requirements: 1. Repeat the game until the player doesn't want to play. Use a new deck for each round.
2. program must be well-documented
3. must-have functions defined (not just the main method) in the program
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
Code:
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include<time.h>
using namespace std;
void intro ();
bool playAgain();
bool decide(char ans);
void checkWin(int, int);
string deck[52]=
{"AC","2C","3C","4C","5C","6C","7C","8C","9C","0C","JC","QC",
"KC","AD","2D","3D","4D","5D","6D","7D","8D","9D","0D","JD","QD","KD","AS","2S","3S","4S","5S",
"6S","7S","8S","9S","0S","JS","QS","KS","AH","2H","3H","4H","5H","6H","7H","8H","9H","0H","JH","QH","KH"};
string drawCard(string deck[], vector<string> hand,
vector<string> dealer);
string determineCardType(string card);
string determineSuiteType(string card);
bool aceCheck(vector<string> card);
int determineCardValue(string card, vector<string> hand);
int main(){
bool play = true;
int playerTotal;
int dealerTotal;
srand(time(0));
intro();
while (play == true){
//Initial deal.
playerTotal=0;
dealerTotal=0;
vector<string>
playerHand;
vector<string>
dealerHand;
string playerCard1 = drawCard(deck,
playerHand, dealerHand);
playerTotal =
determineCardValue(playerCard1, playerHand);
playerHand.push_back(playerCard1);
cout <<"Player Card 1
:"<< determineCardType(playerCard1) << " of " <<
determineSuiteType(playerCard1) << endl;
string playerCard2 = drawCard(deck,
playerHand, dealerHand);
playerTotal +=
determineCardValue(playerCard2, playerHand);
playerHand.push_back(playerCard2);
cout <<"Player Card 2
:"<< determineCardType(playerCard2) << " of " <<
determineSuiteType(playerCard2) << endl;
string dealerCard1 = drawCard(deck,
playerHand, dealerHand);
dealerHand.push_back(dealerCard1);
dealerTotal =
determineCardValue(dealerCard1, dealerHand);
cout <<"Dealer Card 1
:"<< determineCardType(dealerCard1) << " of " <<
determineSuiteType(dealerCard1) << endl;
string dealerCard2 = drawCard(deck,
playerHand, dealerHand);
//Decide to hit.
cout << "Dealer showing: "
<< dealerTotal << endl;
char hit;
do{
cout <<
"You have: " << playerTotal << endl;
cout <<
"Do you want to hit (y/n)?" << endl;
cin >>
hit;
if(hit=='y'){
string playerCard = drawCard(deck, playerHand,
dealerHand);
playerTotal += determineCardValue(playerCard,
playerHand);
playerHand.push_back(playerCard);
cout << determineCardType(playerCard)
<< " of " << determineSuiteType(playerCard) <<
endl;
}else{
break;
}
} while (playerTotal <=
21);
if(playerTotal>21){
cout <<
"You have: " << playerTotal << endl;
cout<<"You
got busted.\n House wins.\n";
}
else{
//Dealer's play
cout << "Dealer's 2nd card: "
<<
determineCardType(dealerCard2) << " of "
<< determineSuiteType(dealerCard2) << endl;
dealerHand.push_back(dealerCard2);
dealerTotal +=
determineCardValue(dealerCard2, dealerHand);
while(playerTotal < 21
&& dealerTotal <= 17){
string
dealerCard = drawCard(deck, playerHand, dealerHand);
cout
<<"Dealer's next card: "
<<determineCardType(dealerCard) << " of " <<
determineSuiteType(dealerCard) << endl;
dealerHand.push_back(dealerCard);
dealerTotal +=
determineCardValue(dealerCard, dealerHand);
}
//Check who won -- finish this
part
cout << "You have: " <<
playerTotal << endl;
cout << "Dealer has: "
<< dealerTotal << endl;
checkWin(playerTotal,dealerTotal);
}
play = playAgain();
}
cout << "You are no longer playing\n";
//debug
system("pause");
return 0;
}
// finish this part ***
void checkWin(int playerTotal, int dealerTotal){
if(playerTotal == dealerTotal){
cout << "Push." <<
endl;
} else if(playerTotal == 21){
cout << "You got BlackJack!"
<< endl;
cout << "You win!" <<
endl;
} else if(dealerTotal == 21){
cout << "Dealer has
BlackJack." << endl;
cout << "House wins."
<< endl;
} else if(playerTotal > dealerTotal){
cout << "You win!";
} else if(dealerTotal>21){
cout << "Dealer got Busted!"
<< endl;
cout << "You win!" <<
endl;
}
}
bool aceCheck(vector<string> hand){
int size = hand.size();
for(int i = 0; i < size; i++){
string card = hand[i];
return card[0] == 'A';
}
return false;
}
int determineCardValue(string card, vector<string>
hand){
switch(card[0]){
case '0':
return 10;
case 'K':
return 10;
case 'Q':
return 10;
case 'J':
return 10;
case 'A':
if(aceCheck(hand)){
return 1;
} else {
return 11;
}
default:
return
(card[0]-'0');
}
}
string determineCardType(string card){
string type;
switch(card[0]){
case 'A':
type =
"Ace";
return
type;
case 'K':
type =
"King";
return
type;
case 'Q':
type =
"Queen";
return
type;
case 'J':
type =
"Jack";
return
type;
case '0':
type =
"10";
return
type;
default:
type =
card[0];
return
type;
}
}
string determineSuiteType(string card){
string suite;
switch(card[1]){
case 'H':
suite =
"Hearts";
return
suite;
case 'D':
suite =
"Diamonds";
return
suite;
case 'S':
suite =
"Spades";
return
suite;
case 'C':
suite =
"Clubs";
return
suite;
default:
return "Suite
undetermined.";
}
}
void intro(){
cout << "Welcome to BlackJack!" <<
endl;
}
bool decide(char ans){
while (ans != 'y' && ans != 'n'){
cout << "You must answer with
a 'y' or 'n'" << endl;
cout << "Would you like to
play again (y/n)?" << endl;
cin >> ans;
}
if(ans == 'y'){
return true;
} else {
return false;
}
}
bool playAgain(){
cout << "Would you like to play again (y/n)?"
<< endl;
char ans;
cin >> ans;
return decide(ans);
}
bool checkDuplicates(string card, vector<string>
hand){
int size = hand.size();
for (int i = 0; i < size; i++){
if(card == hand[i])
return
true;
}
return false;
}
string drawCard(string deck[], vector<string> hand,
vector<string> dealer){
int r = rand() % 52;
string card;
if((checkDuplicates(card, hand) || checkDuplicates(card, dealer))
&& r != 52){
card = deck[r + 1];
} else if ((checkDuplicates(card, hand) || checkDuplicates(card,
dealer)) && r == 52){
int j = 0;
while(checkDuplicates(card, hand) ||
checkDuplicates(card, dealer)){
card = deck[j];
j++;
}
} else {
card = deck[r];
}
return card;
}
Output: