HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Simple Blackjack/21 game in console

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
consolegamesimpleblackjack

Problem

I made a simple Blackjack/21 game in C++. It does not use any fancy graphics, just the console output. It is as simple as it can get.

#include 
#include 
#include 
#include 

void printData(int pTotal, int dCard) {

    std::cout = 21) {

            std::cout > choice;
            turn(pTotal, choice, stay);

        }

        stay = false;
        std::cout = 17)
                stay = true;
            else
                dTotal += getCard();

        }

        // Display winner
        std::cout << "\n\nThe player has " << pTotal << ".\nThe dealer has " << dTotal << ".\n\n";
        if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!\n";
        else std::cout << "The dealer wins!\n";

    }

}


  • Is this code efficient?



  • Is the code easy to read/understand and to follow?



  • Are variable names appropriate?

Solution

I want my money back!

Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s

It is now the dealer's turn!

The player has 15.
The dealer has 23.   <<=====

The dealer wins!     <<=====


Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard function can only return 11 different values (2 to 12).

This means that you can lose right off the bat:

$ ./a.out 
================================
Your total is 24       <<======     :-(
Dealer has a 9 showing.
[H]it or [S]tay?


In turn, if the player enters h and doesn't bust, you're not setting stay - this happens to work given the code flow, but it looks odd. Set it before returning.

If the user enters gibberish, you always interpret that as stay. That's not user-friendly.

In main:

bool stay;
char choice = NULL;


That's not a valid initializer for a char - chars are not pointers. And leaving a variable (stay) uninitialized is not a good idea. You should move stay inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)

Remove choice from main altogether. Only turn needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)

This shouldn't be inside the loop:

std::srand(std::time(0));


You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.

These lines are too long:

// Display winner
std::cout << "\n\nThe player has " << pTotal << ".\nThe dealer has " << dTotal << ".\n\n";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!\n";
else std::cout << "The dealer wins!\n";


Try something like:

// Display winner
std::cout << "\n\nThe player has " << pTotal
          << ".\nThe dealer has " << dTotal << ".\n\n";

if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
  std::cout << "The player wins!\n";
} else {
  std::cout << "The dealer wins!\n";
}


And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs since both scores will be at most 21.

Code Snippets

Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s


It is now the dealer's turn!


The player has 15.
The dealer has 23.   <<=====

The dealer wins!     <<=====
$ ./a.out 
================================
Your total is 24       <<======     :-(
Dealer has a 9 showing.
[H]it or [S]tay?
bool stay;
char choice = NULL;
std::srand(std::time(0));
// Display winner
std::cout << "\n\nThe player has " << pTotal << ".\nThe dealer has " << dTotal << ".\n\n";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!\n";
else std::cout << "The dealer wins!\n";

Context

StackExchange Code Review Q#119915, answer score: 5

Revisions (0)

No revisions yet.