This blackjack program was a summer project to refresh my C++ skills and testing my ability to adapt the concepts I’ve learnt in lectures into my own work independently and confidently. This program was made in Visual Studio and I focused on creating an OOP (object oriented programming) style to ensure that the code is as readable as I can make it. As the name implies, the program allows the user, and up to four other people, to locally play blackjack against the program, which acts as the dealer.
Blackjack Manager
The blackjack manager was an object that managed all of the operations for a game of blackjack; dealing cards, allowing for players to hit or stay on a hand, calculating the amount in a players hand and whether or not the players gone over twenty one as well as the rest. This is easily the most complex object in the program and for good reason.
bool Blackjack_Game_Manager::StartBlackjack(int NumOfPlayers)
{
srand(time(NULL));
int Card1, Card2, Extra;
UserWHighScore = { 0 };
for (int i = 1; i <= NumOfPlayers; i++)
{
std::cout << "\nPlayer " << i << " is up!\nLets see what you get!\n";
system("pause");
Card1 = SelectCard();
OverallScore = DrawCard(Card1, OverallScore);
/*std::cout << "\x1b[A";*/
std::cout << std::endl;
Card2 = SelectCard();
OverallScore = DrawCard(Card2, OverallScore);
std::cout << std::endl << "You have " << OverallScore << std::endl;
system("pause");
My favourite aspect of the blackjack manager was the method to choose a card for a deal as it uses a number of enumerators to assign a value from one to an ace, as well as assigning a suit for that card. The best part is that the code also detects if a suit and value is being selected again and will pick a different card, ensuring that each game is as fair as a real game of blackjack with a full 52 deck of cards. This system isn’t as essential for blackjack, besides the fairness factor, but for a card game such as poker, this method will be incredibly useful.
tempSuit = 1 + rand() % 5;
switch (tempSuit)
{
case HEARTS:
tempCard = 1 + rand() % 14;
return tempCard;
break;
case SPADES:
tempCard = 13 + rand() % 14;
return tempCard;
break;
case DIAMONDS:
tempCard = 26 + rand() % 14;
return tempCard;
break;
case CLUBS:
tempCard = 39 + rand() % 14;
return tempCard;
break;
}
ASCII Art
The program uses ASCII art to resemble each card in a deck of cards. This provides a more interesting experience for the user playing the program than a list of numbers. Additionally, the art will change slightly depending on if the card chosen is a heart, club, spade or diamond, which adds an extra level of depth to this feature.
if (CardNum > 0 && CardNum <= 13)
{
std::cout << "<-------------->\n";
std::cout << "| [--] |\n";
std::cout << "| \\/ |\n";
std::cout << "| |\n";
switch (CardNum)
{
case ACE_OF_HEART:
std::cout << "| /\\ |\n";
std::cout << "| /^^\\ |\n";
std::cout << "| |\n";
std::cout << "| |\n";
std::cout << "| [--] |\n";
std::cout << "| \\/ |\n";
std::cout << "<-------------->\n";
if (CurrentScore <= 10)
{
return CurrentScore + 11;
}
else
{
return CurrentScore + 1;
}
break;
case TWO_OF_HEART:
std::cout << "| __ |\n";
std::cout << "| __} |\n";
std::cout << "| {__ |\n";
std::cout << "| |\n";
std::cout << "| [--] |\n";
std::cout << "| \\/ |\n";
std::cout << "<-------------->\n";
return CurrentScore + 2;
break;
case THREE_OF_HEART:
std::cout << "| __ |\n";
std::cout << "| _} |\n";
std::cout << "| __} |\n";
std::cout << "| |\n";
std::cout << "| [--] |\n";
std::cout << "| \\/ |\n";
std::cout << "<-------------->\n";