BlackJack C++
Engin 30 is an intro to c++ course I have taken at Chaffey College.
Final Project
The purpose of this code is to recreate the game blackjack using many of the techniques I learned throughout this class. The game asks the player to get as close as they can to 21 without going over to win. Basic blackjack rules apply. Made by Marlene Pimienta Herrera for ENGIN 30.
This code is a combination of if statements, for loops, and while loops that come together to create the game. I also created a function that creates the entire deck of 52 cards and puts it into an array. Afterwards I used random_shuffle to mix the cards randomly every time the code is run for the first time, this is accomplished by setting srand(time(0)). Then the code displays a card to the user, asking for their input to do the next step. It will either add another card or check the value to determine if the player has won If the player has not won or lost the dealer gets a turn to win, lose, or tie with the player. The outcome determines if the player wins, loses, or keeps their bet.
References:
http://www.cplusplus.com/forum/beginner/26611/
I used this forum to learn more about how to shuffle an array randomly. This is how I learned about the integrated function in the algorithm library: random_shuffle(&array[0],&array[max]).
https://bicyclecards.com/how-to-play/blackjack/
I used this to find the basic rules of the game Blackjack. I also disregarded some rules like the dealer must always take an ace as 11, only because that is not how I play it.
https://www.blackjack.org/blackjack/how-to-play/
I used this website to find the values of each card in the game and then accordingly code the createdeck function to make the cards.
Code
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
void createdeck(int[], int);
int main()
{
int cash, bet, player, dealer, count, deck, ace, dace;
char play;
string action;
int cards[52];
cash = 1000;
player = 0;
dealer = 0;
bet = 0;
count = 0;
ace = 0;
dace = 0;
deck = 1;
cout << "Welcome to the casino! Let's begin with a game of black jack!" << endl;
cout << "You will begin with $1,000 in cash, don't lose it all!" << endl;
cout << "You can place any size bet as long as it does not exceed the amount you have." << endl;
cout << "If you lose the house takes all. If you win you double your bet!\n" << endl;
cout << "-----------------------------------------------------------------\n\n";
createdeck(cards, deck);
random_shuffle(&cards[0], &cards[51]);
cout << "Enter 'Y' for yes to begin round: ";
cin >> play;
while (play == 'Y' || play == 'y')
{
cout << "Place your bet! ";
cin >> bet;
while (cash < bet)
{
cout << "Oops! You can't bet more than you have. Try again." << endl;
cin >> bet;
}
while (bet < 1)
{
cout << "Oops! You need to bet more than $0. Try again." << endl;
cin >> bet;
}
if (cash >= bet)
cash = cash - bet;
cout << "You bet: $" << bet << ". Your remaining balance is: $" << cash << "." << endl;
player = player + cards[count];
if (cards[count] == 1 && player + 11 <= 21)
{
player = player + 10;
ace++;
}
cout << "Your first card is: " << cards[count] << ". Your total is: " << player << ".\n";
cout << "Would you like to hit or stand? ";
cin >> action;
for (int i = 0; i < 1; i++)
{
if (count > 51)
count = 0;
while (action == "hit")
{
count++;
if (cards[count] == 1 && player + 11 <= 21)
{
player = player + 10;
ace++;
}
if (ace > 0 && player + cards[count] > 21)
{
player = player - 10;
}
player = player + cards[count];
if (player < 21)
{
cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl;
cout << "Would you like to hit or stand ? ";
cin >> action;
if (cards[count] == 1 && player + 11 <= 21)
{
player = player + 10;
ace++;
}
if (ace > 0 && player + cards[count] > 21)
{
player = player - 10;
ace--;
}
}
if (player == 21)
{
cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl;
cout << "Congratulations! You won $" << bet * 2 << "!" << endl;
cash = cash + bet * 2;
break;
}
if (player > 21)
{
cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl;
cout << "Bust! You lost $" << bet << "!" << endl;
break;
}
}
if (action == "stand")
{
count++;
cout << "\nDealer's turn!" << endl;
dealer = dealer + cards[count];
if (cards[count] == 1 && dealer + 11 <= 21)
{
dealer = dealer + 10;
dace++;
}
if (dace > 0 && dealer + cards[count] > 21)
{
dealer = dealer - 10;
dace--;
}
cout << "Dealer's first card is: " << dealer << "." << endl;
while (dealer < 17)
{
if (dealer <= player)
{
count++;
if (cards[count] == 1 && dealer + 11 <= 21)
{
dealer = dealer + 10;
dace++;
}
if (dace > 0 && dealer + cards[count] > 21)
{
dealer = dealer - 10;
ace--;
}
dealer = dealer + cards[count];
cout << "Dealer's next card is: " << cards[count] << ". Their total is now: " << dealer << "." << endl;
}
if (dealer > player)
break;
}
if (dealer > player && dealer > 21)
{
cout << "Player total: " << player << " Dealer total: " << dealer << endl;
cout << "Dealer busts! Player wins $" << bet * 2 << "!" << endl;
cash = cash + bet * 2;
}
if (dealer > player && dealer <= 21)
{
cout << "Player total: " << player << " Dealer total: " << dealer << endl;
cout << "Dealer wins! You lost $" << bet << "." << endl;
}
if (dealer == player)
{
cout << "Player total: " << player << " Dealer total: " << dealer << endl;
cout << "You tied! Your bet of $" << bet << " was returned." << endl;
cash = cash + bet;
}
if (dealer < player)
{
cout << "Player total: " << player << " Dealer total: " << dealer << endl;
cout << "Dealer loses! Player wins $" << bet * 2 << "!" << endl;
cash = cash + bet * 2;
}
}
player = 0;
dealer = 0;
bet = 0;
ace = 0;
dace = 0;
if (count > 52)
count = 0;
cout << "\nYour total balance is currently: $" << cash << "." << endl;
if (cash == 0)
{
cout << "\nThe casino wins, you have no more money left!\n";
return 0;
}
cout << "\n-----------------------------------------------------\n";
cout << "\nEnter 'Y' to play again! ";
cin >> play;
}
}
}
void createdeck(int cards[], int deck)
{
srand(time(0));
for (int i = 0; i < 52; i++)
{
for (int j = 0; j < 4; j++)
{
cards[i] = deck;
i++;
}
i--;
deck++;
if (deck == 11)
deck--;
}
if (play != 'Y' || play != 'y')
cout << "\nYour season earnings were: $" << cash << ". Thanks for playing!\n";
}
Results
This image displays the beginning of the code where a brief explanation appears at the top. The player chooses to play, bets an amount and then begins the game. The player then wins twice their bet above.
This image shows what happens when the player chooses not to play again. Total cash is displayed.
This image shows the special case of the ace card in blackjack working. In the beginning when the player receives the ace card it is counted as 11. However when the total is meant to exceed 21 because of the 10 card, the ace card turns back into a 1 in order to prevent a bust, similar to how real black jac is played.
This image on the right displays the player losing due to going over 21. It also shows what happens when the player runs out of money after gambling it all. The program ends.