top of page


// blackjack21.cpp : Defines the entry point for the console application.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

char CreateMainMenu();
int GetRandomCard();
void PlayGame();
void Initialize();
void PrintCard(int card);
void ShowCards();
int CalculatePoint(int cards[], int count);

char cardSuits[][15]={ {"Clubs"}, {"Diamond"}, {"Spades"}, {"Hearts"} } ;
const int MAXHAND=20;
int playerCards[MAXHAND];
int playerCardCount=0;
bool playerStand;
int playerPoint;
float playerMoney;
float bet;

int dealerCards[MAXHAND];
int dealerCardCount=0;
int dealerPoint;

int main(void)
{
    /* initialize random seed: */
    srand ( time(NULL) );
    char choice = CreateMainMenu();
    int suit, card;
    if (choice=='1')
    {
        PlayGame();
    }
    return 0;
}

char CreateMainMenu() {
    printf("=========== Blackjack ============\r\n");
    printf(" 1. Play Game: \r\n");
    printf(" 2. Exit Game: \r\n");
    printf(" Your Choice (1/2): ");
    char choice;
    scanf("%c",&choice);
    _flushall();
    return choice;
}

void Initialize() {
    dealerCardCount=0;
    playerCardCount=0;
    playerStand=false;
    playerPoint=0;
    dealerPoint=0;
    bet=0;
    for(int i=0; i<MAXHAND; i++) {
        playerCards[i]=0;
        dealerCards[i]=0;
    }
}

int GetRandomCard() {
    return (rand() % 52)+1;
}

void PlayGame()
{
    char choice;
    playerMoney=100.0;

    do
    {
        Initialize();
        printf("How much do you want to bet (max: $%.2f): $", playerMoney);
        scanf("%f", &bet);
        _flushall();

        /* Dealer gets two cards */
        dealerCards[dealerCardCount++]=GetRandomCard();
        dealerCards[dealerCardCount++]=GetRandomCard();


        /* Player gets two cards */
        playerCards[playerCardCount++]=GetRandomCard();
        playerCards[playerCardCount++]=GetRandomCard();

        ShowCards();
        choice=0;
        int newCard;
        while((playerPoint<21)&&(choice!='S')||(choice!='s'))
        {
            printf("Do you want to Hit or Stand?(H/S) ");
            scanf("%c", &choice);
            _flushall();
            if ((choice=='H')|| (choice=='h'))
            {
                playerCards[playerCardCount++]=GetRandomCard();
                playerCardCount++;
            printf("Your point is: %i\r\n",playerPoint);
            }
            else if((choice=='S')||(choice=='s'))
            {
                printf("You have chosen to stay. Wise!", playerPoint);
                playerStand=false;
            }
            else
            {
                printf("Invalid Entry!");
            }
            int playerScore=calculatePoint(int cards[], int count)

        }
        playerStand=true;

        if (playerScore<=21) {
            printf("Dealer's point is: %i\r\n", dealerPoint);
            while(dealerPoint<17) {
                printf("Dealer's point is: %i\r\n", dealerPoint);
            }
        }
        printf("\r\n\r\n");
        ShowCards();
        if (dealerPoint=21) {
            printf("Bummer, you lost the bet!");
            playerMoney-=bet;
        }
        else if(dealerPoint=playerScore) {
            printf("Its a draw, you will keep your bet.");
        }
        else if(playerScore=21) {
            printf("Congratulations! Your Awesome! You got a Blackjack and won 1.5x of your bet");
            playerMoney+= (bet*1.5);
        }
        else if(playerScore>21) {
            printf("Just not your day, you lost the best! Better luck next time!");
            playerMoney-=bet;
        }
        else {
            printf("You won your bet!");
            playerMoney+=bet;
        }
        printf("\r\n");
        printf("You have $%.2f\r\n", playerMoney);
        printf("Continue...?(Y/N) ");
        scanf("%c", &choice);
        _flushall();
    }
    while(choice!='N');

}

int CalculatePoint(int cards[], int count) {
    int points = 0;
    int aces=0;
    int faceValue;
    for(int i=0; i<count; i++) {
        faceValue = (cards[i] % 13) + 1;
        if (faceValue==1) {
            aces++;
        }
        else {
            if (faceValue>10) faceValue=10;
            points += faceValue;
        }
    }

    for(int i=0; i<aces; i++) {
        if (points+11<=21) {
            points+=11;
        }
        else {
            points++;
        }
    }

    return points;
}

void PrintCard(int card) {
    int value=(card%13)+1;
    if ((value>1)&&(value<=10)) {
     printf("Card: %i ", value);
    }
    else {
        printf("Card: ");
        switch(value) {
            case 1:
                printf("Ace ");
                break;
            case 11:
                printf("Jack ");
                break;
            case 12:
                printf("Queen ");
                break;
            case 13:
                printf("King ");
                break;
        };
    }
    printf("of %s\r\n", cardSuits[(int)floor(card/13.0)]);
}

void ShowCards() {
    if (playerStand) {
        printf("Dealer has following - \r\n");
        for(int i=0; i<dealerCardCount; i++) {
            PrintCard(dealerCards[i]);
        }
        dealerPoint = CalculatePoint(dealerCards, dealerCardCount);
        printf("Dealers total point is: %i\r\n", dealerPoint);
    }
    else {
        printf("Dealer has %i cards, facedown.\r\n", dealerCardCount);
    }
    printf("\r\n");
    printf("You have following - \r\n");
    for(int i=0; i<playerCardCount; i++) {
        PrintCard(playerCards[i]);
    }
    playerPoint = CalculatePoint(playerCards, playerCardCount);
    printf("Your total point is: %i\r\n", playerPoint);
}

C++ BLACKJACK GAME

#include <iostream>
using namespace std;
 
int main ()
 
{ // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; // output each array element's value for ( int i = 0; i < 6; i++ ) for ( int j = 0; j < 2; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout << a[i][j]<< endl; } return 0; }
C++ Multidimensional Array
bottom of page