Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Blackjack Program for C Programming This is what have so far #include <stdio.h>

ID: 3738748 • Letter: B

Question

Blackjack Program for C Programming

This is what have so far

#include <stdio.h>

#include <stdlib.h>

#include <time.h> // Need for randome # generator

void sleep(int timeToSleep) {

int icurrentime = 0;

int iendtime = 0;

icurrentime = time(NULL); //Get the current time

while ((iendtime - icurrentime) < 3) { //Did the loop go for three seconds

iendtime = time(NULL);

}

}

int dealCard(int cards[]) {

int card;

card = rand() % 52;

while (cards[card] == 0) {

card = rand() % 52;

}

int nextCard = cards[card];

cards[card] = 0;

return nextCard;

}

int main() {

int wager, balance, card, playerWon = 0, computerWon = 0;

char keepPlaying = 'Y', gotAce = 'N';

int cards[52] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, //Array to initialize

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, //52 card

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, };//deck with 4 cards for each suit

srand(time(NULL)); // seed random # generator

printf("%s", "Welcome to CIS126 Blackjack");

sleep(3); //sleep for 3 seconds

system("cls"); // clear screen

// Ask user for how much money they are playing with

// (money >= 10 )

// Ask user for wager (must be >= 10 and <= money)

int playerHand = 0, computerHand = 0, cardValue = 0, card = 0;

while (keepPlaying == 'Y' && balance >= 10) {

// Deal Player Cards

card = dealCard(cards);

printf("%s", "Your cards are: ");

if (card <= 10) {

printf("%d", card);

cardValue = card;

}

else if (card == 11) {

printf("%c", 'J');

cardValue = 10;

}

else if (card == 12) {

printf("%c", 'Q');

cardValue = 10;

}

else if (card == 13) {

printf("%c", 'K');

cardValue = 10;

// ace

}

else {

printf("%c", 'A');

cardValue = 11;

gotAce = 'Y';

}

playerHand = playerHand + cardValue; // Make sure less than equal to 21

// Deal player hands till player busts (over 21) or stays

// Deal computer hand HIT <17 stand > 17 or over

// Compare PlayerHand to ComputerHand

// Ask user to play again if they have money

}

}

This project will simulate a blackjack program. It will put one player against the computer. The player will be able to wage money on the game. The game will continue until the player does not want to play anymore or he/she runs out of money.

This program will start with a screen that says “Welcome to CIS 126 Blackjack”. Show this message for three seconds, clear the screen, and then go to the following prompt:

            How much are you money are you playing with today?   1000

The user will then enter how much money they are playing with.

A sample execution of the program is as follows:

What is your wager? 10001

Sorry, you can’t wager 10001, you only have 1000 left

What is your wager? 2

Sorry, the minimum bet for this table is 10

What is your wager? 50

Your cards are 5,10

Your sum is 15. Would you like to hit(H) or stay(S)?   H

You got a J.

Your sum is 25. You lose this game. You have $950 left.

Score: Computer =1, Player 0

Would you like to play again? (Y/N?): Y

What is your wager? 950

Your cards are J,Q

Your sum is 20. Would you like to hit(H) or stay(S)?   S

Computer’s cards: 6, 7, 5, 3

Computer got: 21

You lose this game. You have $0 left.

Score: Computer =2, Player 0

You do not have anymore money to play. Thank you for playing. Good bye.

The computer's cards should have a sleep of one second between each card to give it some suspense.

If the dealer’s sum is less than 17, the dealer must take another card.

If the dealer’s sum is 17 or above, the dealer must stand

The deck is has 52 cards in it. Cards cannot be reused. Cards must be generated

using some random number. You will need to keep track of the number and type of cards played. If all 52 cards are played, then you will need to reshuffle the deck.

Assume Ace is always 11, unless you are over, then assume it is 1.

The program will always ask the user to play again, unless the user has no more money to play.

Blackjack, a total of 21 on your first two cards, pays 3:2

Advanced playing (such as doubling down, splitting pairs) IS NOT required.

Hints:

You will need to use an array to hold the deck. An array of 52 will work fine.

Since we are using characters, we will need to use a getchar() after each scanf to get rid of the newline.

To simulate sleeping for 3 seconds here’s the code.

int icrurrentime = 0;

int iendtime = 0;

            icurrenttime = time( NULL ); //Get the current time

            while( ( iendtime - icurrenttime ) < 3 ){ //Did the loop go for three seconds

                  iendtime = time( NULL );  

            }

Create the array like this:

         int   cards[52] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, //Array to initialize

                                      2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, //52 card

                                      2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,

                                      2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,};//deck with 4 cards for each suit

            In this deck, 11 is a J,12 is a Q, 13 is a K, and 14 is an Ace.

           

In order to deal a card

generate a random number between 0 and 51.

random = rand() …..;

See if the value of that array index is 0.

Does cards[random] == 0?

      If it is, generate another random number until

           cards[random] doesn’t equal zero

Show the card, and then set

cards[random] == 0

Once all the cards have been dealt, then reset the array

to the original state.

Explanation / Answer

here is the working code for the given problem

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CARDS 52

int bal;
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 shufle();
void checkAce(int v1, int v2, int *a1);
void quit();

int main(){
    printf("Welcome to my BlackJack Simulator! Below are the basic rules: - Beat the dealer's hand without going over 21. - Face cards are worth 10, Aces are worth 1 or 11, whichever makes a better hand. - Each player starts with two cards, and one of the dealer's cards is hidden until the end. - Type 'hit' to ask for another card. Type 'stand' to hold your total and end your turn. - If you go over 21 you bust, and the dealer wins regardless of his hand. - 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. - 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'). Type 'play' to begin. At any time, you may type 'help' to get a list of valid commands. ");
    char input[6];
    scanf("%5s", input);
    while(strcmp(input, "quit") != 0){
        while(strcmp(input, "play") != 0){
            if(strcmp(input, "help") == 0)
                printf("Type 'play' to begin the game. ");
            else if(strcmp(input, "quit")==0){
                quit();
                printf("Enter 'play' to start the game. ");
            }else
                printf("Invalid command, type 'play' to begin. ");
            scanf("%s", input);
        }     
        play();
        scanf("%5s", input);
    }
}

void play(){
    printf("Lets start you off with 500 credits. ");
    bal = 500;
    while(bal>0){
        bet();
        initdeck();
        shufle();
        turn();
    }
    printf("Sorry, it looks like you lost! Type 'play' to play another game. ");
}

void turn(){
    int inc=0;

    int d1 = hit(&inc);
    char *cn1 = csuit(d1);
    char *cf1 = cface(d1);
    int cv1 = cval(d1);

    printf("You got a %s of %s worth %d, and ", cf1, cn1, cv1);

    int d2 = hit(&inc);
    char *cn2 = csuit(d2);
    char *cf2 = cface(d2);
    int cv2 = cval(d2);

    printf("a %s of %s worth %d. ", cf2, cn2, cv2);

    int d3 = hit(&inc);
    char *cn3 = csuit(d3);
    char *cf3 = cface(d3);
    int cv3 = cval(d3);

    printf("The dealer's face up card is a %s of %s worth %d. ", cf3, cn3, cv3);

    int d4 = hit(&inc);
    char *cn4 = csuit(d4);
    char *cf4 = cface(d4);
    int cv4 = cval(d4);

    int ptotal = cv1 + cv2;
    int dtotal = cv3;

    checkAce(cv1, cv2, &ptotal);

    printf("You have a total of %d points, and the dealer has %d. ", ptotal, dtotal);

    if(ptotal == 21){
        printf("Congrats! You got a blackjack!! Your payout is 2-to-1, %d. ", (int)(pot*2));
        bal += (pot*2);
    }else{
        char input[6];
        scanf("%5s", input);
        while(strcmp(input,"stand")!=0){
            if(strcmp(input,"hit") == 0){
                int val = hit(&inc);
                char *ns = csuit(val);
                char *nf = cface(val);
                int nv = cval(val);
                ptotal += nv;
                printf("You got a %s of %s worth %d. ", nf, ns, nv);
                if(ptotal < 21){
                    if(nv==1){
                        if(ptotal+10 == 21){
                            ptotal += 10;
                            printf("Congrats! You got 21! Payout is 2-to-1. ");
                            bal += (pot*2);                       
                            break;
                        }else{
                            int input;
                            printf("You've got an ace. Choose whether you want to make it count as 1 or 11. ");
                            scanf("%d",&input);
                            if(input == 11)
                                ptotal += 10;
                        }
                    }
                }else if(ptotal == 21){
                    printf("Congrats! You got 21! ");
                    break;
                }else{
                    printf("Oh no, you've busted with %d. Try Again! ", ptotal);
                    break;
                }
            }else if(strcmp(input,"help")==0)
                printf("Type 'hit' to be dealt another card. Type 'stand' to hold. ");
            else if(strcmp(input, "quit")==0){
                quit();
                printf("Enter 'hit' or 'stand'. ");
            }else
                printf("Invalid command, try again. ");
            printf("Your new total is %d. ", ptotal);
            scanf("%5s", input);
        }
    }

    if(ptotal < 21){

        printf("The dealer's flips a %s of %s worth %d. ", cf4, cn4, cv4);
        dtotal += cv4;
        if(cv4 == 1){
            if(dtotal+11 < 21)
                dtotal += 10;
        }

        if(dtotal >= 16){
            printf("The dealer stands with %d. ", dtotal);
        }

        while(dtotal < 16){
            int val = hit(&inc);
            char *ns = csuit(val);
            char *nf = cface(val);
            int nv = cval(val);
            dtotal += nv;
            printf("The dealer got a %s of %s worth %d. ", nf, ns, nv);
            if(dtotal < 16){
                if(nv==1){
                    if(dtotal+11<21)
                        dtotal += 10;
                }
            }else if(dtotal == 21){
                printf("The dealer just got 21. ");
                break;
            }else if(dtotal > 21){
                printf("The dealer busted with %d! You win! ", dtotal);
                break;
            }else{
                printf("The dealer stands with %d. ", dtotal);
                break;
            }
            printf("The dealer's new total is %d. ", dtotal);
        }
        if(dtotal<ptotal){
            printf("You beat the dealer! Your payout is %d. ", (int)(pot*1.5));
            bal += (pot*1.5);
        }else if(dtotal==ptotal){
            printf("Its a tie! Push pot, 1-to-1 payout of %d. ", pot);
            bal += pot;
        }else if(dtotal>ptotal && dtotal <= 21){
            printf("Oh no! Looks like the dealer won. Try again! ");
        }else{
            printf("You beat the dealer! Your payout is %d. ", (int)(pot*1.5));
            bal += (pot*1.5);
        }
    }
    if(bal > 0)
        printf("Your new bal is %d. ", bal);
}

void checkAce(int v1, int v2, int *a1){
    if(v1 == 1 || v2 == 1){
        int input;
        if(v1 == 1 && v2 == 1){
            v1 = 11;
            v2 = 1;
            *a1 = 12;
            printf("Since you got 2 Aces, we set one to be worth 11 and the other to be worth 1. ");
        }else if(v1 == 1 || v2 == 1){
            if(v1+10+v2 == 21)
                *a1 == 21;
            else{
                printf("You've got an ace. Choose whether you want to make it count as 1 or 11. ");
                scanf("%d",&input);
                if(input == 11)
                    *a1 += 10;
            }
        }
    }
}

int cval(int v){
    int a;
    if(((v%13)+1)<10)
        a = v%13+1;
    else
        a = 10;
    return a;
}

char *csuit(int v){
    v = v%4;
    switch(v){
        case 0 :
            return "Hearts";
        case 1 :
            return "Clubs";
        case 2 :
            return "Diamonds";
        case 3 :
            return "Spades";
    }
}

char *cface(int v){
    v = v%13+1;
    char *output;
    switch(v){
        case 1 :
            output = "Ace";
            break;
        case 2 :
            output = "Two";
            break;
        case 3 :
            output = "Three";
            break;
        case 4 :
            output = "Four";
            break;
        case 5 :
            output = "Five";
            break;
        case 6 :
            output = "Six";
            break;
        case 7 :
            output = "Seven";
            break;
        case 8 :
            output = "Eight";
            break;
        case 9 :
            output = "Nine";
            break;
        case 10 :
            output = "Ten";
            break;
        case 11 :
            output = "Jack";
            break;
        case 12 :
            output = "Queen";
            break;
        case 13 :
            output = "King";
    }
    return output;
}

void quit(){
    printf("Are you sure you want to quit? Type 'y' or 'n'. ");
    char input[6];
    scanf("%s",input);
    if(strcmp(input,"y")==0)
        exit(0);
    else
        printf("Quit cancelled. ");
}

int hit(int *i){
    int a = sdeck[*i];
    *i = *i + 1;
    return a;
}

void bet(){
    char input[6];
    int val;
    printf("Enter an amount you would like to bet. ");
    scanf("%5s", input);
    while(strcmp(input, "bet") != 0){
        if(strcmp(input,"help") == 0)
            printf("Type 'bet' followed by a bet amount. For example, you could type 'bet 50'. ");
        else if(strcmp(input,"quit")==0){
            quit();
            printf("Enter a bet amount. ");
        }else
            printf("You've entered an invalid command. Type 'help' for a list of valid commands. ");
        scanf("%5s", input);
    }
    scanf("%d", &val);
    while(val > bal || (val < 10 && val > 0) || val <=0){
        if(val > bal)
            printf("You cannot bet more than your bal. Your bal is: %d. ", bal);
        else if(val <= 0)
            printf("You must make a bet. Enter a valid bet amount. ");
        else
            printf("Invalid entry. Try again. ");
        scanf("%d", &val);
    }
    bal -= val;
    printf("You've made a bet of %d. Good luck! ", val);
    pot = val;    
}

void initdeck(){
    int i=0;
    for(i;i<52;i++){
        deck[i] = i+1;
    }
}

void shufle(){
    srand(time(NULL));
    int i=0;
    for(i;i<52;i++){
        sdeck[i] = deck[i];
    }
    i=0;
    for(i=CARDS-1;i>0;i--){
        int j = rand()%(i+1);
        int n = sdeck[i];
        sdeck[i] = sdeck[j];
        sdeck[j] = n;
    }
}