I made this small Blackjack game in the past as a way to practice C++ basics and have fun at the same time. I stopped working on the game during the last two years of my computer science degree program and now in my free time I want to improve it. I'm looking for suggestions on how I can improve the game. Not just adding new features, but how I can improve the organization, eliminate unnecessary code, and stick to common (industry standard) programming practice. Feel free to make as many comments as you like! (I'm aware that I could add more comments)
Blackjack.cpp
#include "stdafx.h"
#include "Blackjack.h"
Blackjack::Blackjack()
{
srand(time(0));
dhandSize = 0;
phandSize = 0;
dhandSum = 0;
phandSum = 0;
playerDone = false;
dealerDone = false;
}
void Blackjack::playGame()
{
cout << "Welcome to Blackjack!\n";
// Start the player and dealer with two cards
addPlayerCard();
addPlayerCard();
addDealerCard();
addDealerCard();
sumHands();
printHand();
if (dhandSum == 21)
{
cout << "Dealer has blackjack. Dealer wins.\n";
return;
}
else if (phandSum == 21)
{
cout << "Player has blackjack. Player wins.\n";
return;
}
while (dealerDone == false || playerDone == false)
{
if (playerDone == false)
{
cout << "Would you like to hit? (1 - Yes, 2 - No)\n";
cin >> phit;
if (phit == 1)
{
addPlayerCard();
printHand();
sumHands();
if (phandSum > 21)
{
cout << "Player's hand exceeded 21. Player loses.\n";
return;
}
}
}
if (playerDone == false)
{
cout << "Would you like to stand? (1 - Yes, 2 - No)\n";
cin >> pstand;
}
if (pstand == 1)
{
playerDone = true;
}
if (dhandSum < 17 && dealerDone != true)
{
addDealerCard();
printHand();
sumHands();
if (dhandSum > 21)
{
cout << "Dealer hand exceeded 21. Dealer loses.\n";
return;
}
}
else if (dhandSum >= 17)
{
dealerDone = true;
}
if (phandSum == 21 && dhandSum == 21)
{
cout << "Push, player and dealer reached 21.\n";
return;
}
else if (phandSum == 21)
{
cout << "Player reached 21. Player wins.\n";
return;
}
else if (dhandSum == 21)
{
cout << "Dealer reached 21. Dealer wins.\n";
return;
}
if ((playerDone == true && dealerDone == true) || (phandSize == 5 && phandSize == 5))
{
if (dhandSum < phandSum)
{
cout << "Sum of your hand exceeds the dealer's sum of " << dhandSum << "! You win!";
return;
}
else if (phandSum == dhandSum)
{
cout << "Dealer sum of " << dhandSum << " is equal to the sum of your hand. Tie game.";
return;
}
else if (dhandSum > phandSum)
{
cout << "Sum of your hand is lower than the dealer's sum of " << dhandSum << ". You lose!";
return;
}
}
}
}
int dhand[5];
int phand[5];
int dhandSize;
int phandSize;
int dhandSum;
int phandSum;
int phit;
int pstand;
bool playerDone;
bool dealerDone;
void Blackjack::addPlayerCard()
{
if (phandSize <= 5)
{
phand[phandSize] = 1 + (rand() % 11);
phandSize++;
}
else
{
cout << "Sorry. You have reached the maximum number of cards (5)." << endl;
playerDone = true;
}
}
void Blackjack::addDealerCard()
{
if (dhandSize <= 5)
{
dhand[dhandSize] = 1 + (rand() % 11);
dhandSize++;
}
else
{
dealerDone = true;
}
}
void Blackjack::printHand()
{
cout << "Your current hand is...\n";
for (int i = 0; i < phandSize; i++)
{
cout << " -" << phand[i] << "- \n\n";
}
cout << "Dealer's current hand is...\n";
for (int j = 0; j < dhandSize; j++)
{
cout << " -" << dhand[j] << "- \n\n";
}
}
void Blackjack::sumHands()
{
dhandSum = 0;
phandSum = 0;
for (int i = 0; i < dhandSize; i++)
{
dhandSum += dhand[i];
}
for (int j = 0; j < phandSize; j++)
{
phandSum += phand[j];
}
cout << "Current player hand sum is: " << phandSum << endl;
}
main.cpp
#include "stdafx.h"
#include "Blackjack.h"
int main()
{
int exitGame = 1;
do
{
Blackjack casino_royale;
casino_royale.playGame();
cout << "\nWould you like to play again? (1 - Yes, 2 - No)\n";
cin >> exitGame;
}while (exitGame == 1);
cout << "\nThanks for playing!\n";
system("pause");
return 0;
}
Blackjack.h
#pragma once
#include "stdafx.h"
class Blackjack
{
public:
Blackjack();
void playGame();
private:
int dhand[5];
int phand[5];
int dhandSize;
int phandSize;
int dhandSum;
int phandSum;
int phit;
int pstand;
bool playerDone;
bool dealerDone;
void addPlayerCard();
void addDealerCard();
void printHand();
void sumHands();
};