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

Blackjack game program

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

Problem

I'm learning C and decided to write my first major project in C. I was bored and wanted to play blackjack so I decided to create my own game. My program starts the user off with 500 credits and continues playing until the user runs out of credits or quits, at which point they can start again by typing play. The main input components are hit, stand, help, bet $, and quit.

I've also added a lot of code to handle special cases that I've encountered while playing the game (for example if the player was dealt a 10-value card and an Ace, it automatically gives the user a Blackjack instead of prompting the user to decide what value to get from the Ace card).

I'm certain that there are many things I could do to improve the structure and efficiency of my code.

```
#include
#include
#include
#define CARDS 52

int balance;
int pot;
int deck[52];
int sdeck[52];

void play();
void turn();
int hit(int *val);
char *cface(int v);
char *csuit(int v);
int cval(int v);
void bet();
void dealer();
void result();
void initdeck();
void shuffle();
void checkAce(int v1, int v2, int *a1);
void quit();

int main(){
printf("Welcome to my BlackJack Simulator! Below are the basic rules:\n- Beat the dealer's hand without going over 21.\n- Face cards are worth 10, Aces are worth 1 or 11, whichever makes a better hand.\n- Each player starts with two cards, and one of the dealer's cards is hidden until the end.\n- Type 'hit' to ask for another card. Type 'stand' to hold your total and end your turn.\n- If you go over 21 you bust, and the dealer wins regardless of his hand.\n- If you are dealt 21 from the start (Ace & 10), you got a blackjack. If you get a blackjack, you win 1.5 times the amount of your bet automatically, unless the dealer also gets a blackjack, in which case it is a push.\n- Remember: Type 'hit' to get another card, and 'stand' to hold. At the beginning of the round, type 'bet' followed by the quantity you want to bet (i.e. 'bet 50').\nType 'play' to begin. A

Solution

This is a really good effort for a first major project! It's very straightforward and easy to understand. Here are a few ways you could take it to the next level.
Avoid Global Variables

Right now you have 4 global variables:

int balance;
int pot;
int deck[52];
int sdeck[52];


These should be local variables inside the main() function and you should pass them to the other functions that need to access them. There are 2 reasons:

  • As it is now, it's difficult to find who changed them when they change



  • If you ever want to expand this (perhaps to be a server that serves games to multiple groups of people at the same time), having globals won't work.



Avoid Magic Numbers

You created a constant CARDS but then you only use it once. You should use it to declare your decks, too (and I'd rename it to NUM_CARDS):

int deck[NUM_CARDS];
int sdeck[NUM_CARDS];


While you're unlikely to ever need to change the number of cards in a deck, using a constant can clarify the code and save you from typos. (And actually, there are a few games that use fewer than 52 cards).
Use Arrays for Looking Things Up

In your csuit() and cface() functions, you have a bunch of case statements to convert between an int and a string. You could do this more easily with an array, like this:

const char* csuit(const int v)
{
    const char* kSuits[] = {
        "Hearts",
        "Clubs",
        "Diamonds",
        "Spades"
    };
    return kSuits [ v % 4 ];
}


You can do a similar thing with cface().
Simplify

Several of your functions have complicated nested while loops. I'd try to simplify them. I'd make the one in main() it more like this:

while (strcmp(input, "quit") != 0)
{
    if (strcmp(input, "play") == 0)
    {
        play();
    }
    else if (strcmp(input, "help") == 0)
    {
        printf("Type 'play' to begin the game.\n");
    }

    scanf("%s", input);
}


You can eliminate the call to quit() as your main() will just exit when the user enters "quit".

On the subject of simplifying, I'd probably also break turn() into smaller functions, and try to make the logic simpler. Perhaps use a state machine.

Code Snippets

int balance;
int pot;
int deck[52];
int sdeck[52];
int deck[NUM_CARDS];
int sdeck[NUM_CARDS];
const char* csuit(const int v)
{
    const char* kSuits[] = {
        "Hearts",
        "Clubs",
        "Diamonds",
        "Spades"
    };
    return kSuits [ v % 4 ];
}
while (strcmp(input, "quit") != 0)
{
    if (strcmp(input, "play") == 0)
    {
        play();
    }
    else if (strcmp(input, "help") == 0)
    {
        printf("Type 'play' to begin the game.\n");
    }

    scanf("%s", input);
}

Context

StackExchange Code Review Q#123818, answer score: 3

Revisions (0)

No revisions yet.