In: Computer Science
Use C++
Black Jack
Create a program that uses methods and allows the user to play the game of blackjack against the computer dealer. Rules of Blackjack to remember include:
1. You need one 52 card deck of cards with cards from 2-Ace (4 cards of each number).
2. Jacks, Queens and Kings count as 10 points.
3. An Ace can be used as either 1 or 11 depending on what the user decides during the hand.
4. Draw randomly two cards for the "dealer" and display one of them while keeping the other hidden. Also, randomly draw two cards for the player and display them in view.
5. Allow the user to hit (randomly draw a card) as many times as they wish. If the player "busts" or gets over 21, the dealer automatically wins the players wager.
6. After the user "stands" or is satisfied with his total, the dealer must take a card if his total is 16 or below, and cannot take a card if his total is 17 or above.
7. If the dealer "busts" or goes over 21, the player wins back his wager plus his wager again. (Example - if a player bets $3 he gets back his original $3 plus an additional $3).
8. If neither the dealer or the player "busts" or goes over 21, then the highest total wins. Ties go to the dealer.
9. Note - the player should start with $20 in the bank and cannot wager more than he currently has in the bank.
10. Note - before each round the user can bet in whole dollars how much to wager or how much of the total in the bank to risk.
11. Allow the user to quit at any time. Of course, the user must quit if he/she runs out of money.
12. Remember, somehow ensure that the same card (example - 6 of spades) cannot be drawn twice in a single hand.
PROGRAM:
#include <iostream>
#include <algorithm>
using namespace std;
string mainDeck[52];
string suits[4] = {"C","D","H","S"};
string cards[13] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
string Player[52], Dealer[52];
int playerInd=0, dealerInd=0,deckIndex=51;
int playerWager=20;
pair<string, int> Points[13];
void initAndShuffleDeck(){
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
mainDeck[13*i + j] = cards[j]+suits[i];
}
}
srand(time(0));
random_shuffle(&mainDeck[0],&mainDeck[52]);
}
void printDeck(){
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
cout << mainDeck[13*i + j] << " ";
}
cout << endl;
}
}
int askWager(){
int currBet;
cout << "How much do you want to bet? (max:" << playerWager << ") ";
cin >> currBet;
while(currBet>playerWager || currBet<0){
cout << "Invalid Bet amount" << endl;
cout << "How much do you want to bet? (max:" << playerWager << ") ";
cin >> currBet;
}
return currBet;
}
int cardToPoint(string card){
card = card.substr(0,card.length()-1);
for(int i=0;i<13;i++){
if(Points[i].first == card){
return Points[i].second;
}
}
}
int getPointsPlayer(){
int sum = 0;
for(int i=0;i<playerInd;i++){
sum += cardToPoint(Player[i]);
}
return sum;
}
int getPointsDealer(){
int sum = 0;
for(int i=0;i<dealerInd;i++){
sum += cardToPoint(Dealer[i]);
}
return sum;
}
void drawCardsFirstTime(){
Player[playerInd++] = mainDeck[deckIndex--];
Player[playerInd++] = mainDeck[deckIndex--];
cout << "Player Cards: " << Player[playerInd-1] << " " << Player[playerInd-2];
Dealer[dealerInd++] = mainDeck[deckIndex--];
cout << "\tDealer Cards: " << Dealer[dealerInd-1] << " |/|" << endl;
Dealer[dealerInd++] = mainDeck[deckIndex--];
}
bool endGame(){
cout << "Current Wager: " << playerWager << endl;
if(playerWager==0){return true;}
cout << "Enter 0 to Quit else 1: ";
char command;
cin >> command;
if(command=='0'){
return true;
}
return false;
}
char getAction(){
cout << "S - Stand" << endl;
cout << "H - Stand" << endl;
cout << "Enter Command: ";
char command;
cin >> command;
if(command=='H' || command=='S'){
return command;
}else{
cout << "Invalid command" << endl;
getAction();
}
}
char onHit(){
Player[playerInd++] = mainDeck[deckIndex--];
cout << "Card Drawn: " << Player[playerInd-1] << endl;
}
void restart(){
playerInd = 0;
dealerInd = 0;
deckIndex = 51;
initAndShuffleDeck();
}
void playGame(){
int currBet;
currBet = askWager();
drawCardsFirstTime();
if(getPointsPlayer()==21){
playerWager += currBet;
if(endGame()){
return;
}
}
while(getPointsPlayer()<=21){
char a = getAction();
if(a=='H'){
onHit();
}
if(a=='S'){
break;
}
}
if(getPointsPlayer()>21){
cout << "Player Bust" << endl;
playerWager -= currBet;
}else{
if(getPointsDealer()<=16){
Dealer[dealerInd++] = mainDeck[deckIndex--];
}
if(getPointsDealer()>21){
cout << "Dealer Bust" << endl;
playerWager += 2*currBet;
}else{
if(getPointsDealer()>=getPointsPlayer()){
cout << "Player Lost" << endl;
playerWager -= currBet;
}else{
cout << "Player Won" << endl;
playerWager += currBet;
}
}
}
if(endGame()){
return;
}else{
restart();
playGame();
}
}
void initPoints(){
Points[0] = make_pair("2",2);
Points[1] = make_pair("3",3);
Points[2] = make_pair("4",4);
Points[3] = make_pair("5",5);
Points[4] = make_pair("6",6);
Points[5] = make_pair("7",7);
Points[6] = make_pair("8",8);
Points[7] = make_pair("9",9);
Points[8] = make_pair("10",10);
Points[9] = make_pair("J",10);
Points[10] = make_pair("Q",10);
Points[11] = make_pair("K",10);
Points[12] = make_pair("A",11);
}
int main(){
initAndShuffleDeck();
initPoints();
playGame();
}
OUTPUT:
