Skip to main content
2 of 4
edited tags

A very basic black jack program, which just lets you play one round of black jack without any balance or betting

I just wanted to make a fun project for practise and as far as I know the program runs just fine without any flaws, but I would like to add an option to it, if a round ends to ask the user if he wants to play another round. Maybe I should put the whole thing in a while loop or something but everything I tried failed. And I should reset the first array int values_w_p[][2], but I dont know what would be the best method for that. Another thing would be of course the betting and balance, but for that I have atleast some ideas which I will try. Tips are welcome too! Here is the code:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> 
#include <stdbool.h>

int *actValue(int values[][2]){     // function to create the array based on probability

int len = 0;
for (int i = 0; i<10;i++){
    len = len + values[i][1];
}
int *arr = (int*)malloc(len*(sizeof(int)));
int count=0;
for (int q = 0; q<10; q++){
    for (int w = 0; w < values[q][1]; w++){
        arr[count]=values[q][0];
        count++;
    }
}
return arr;

}

int lenOfArr(int values[][2]){  //based on the probability array returns the length of it (values_w_p)

int len = 0;
for (int i = 0; i<10;i++){
    len = len + values[i][1];
}
return len;
}

int drawacard(int *valuenow, int len, int values_w_p[][2]){      // picks a random value from the array generated with actValue and takes one from the probability 

int yourcard = valuenow[rand() % (len - 0) + 0];
int sec=1;
values_w_p[yourcard-2][sec] -= 1;
return yourcard;

}
void name(int card) {       //prints the name of the card baed on the value

const char *color [] = {"Clubs", "Diamonds", "Hearts", "Spades" };
const char *nev [] = {"2","3","4","5","6","7","8","9","10","Jack", "Queen", "King","Ace" };
if(card < 10){
    printf("%s of %s\n", nev[card-2], color[rand()%4]);
}
else if (card == 10){
    printf("%s of %s\n", nev[rand() % (11 + 1 - 8) + 8], color[rand()%4]);
}
else if(card == 11){
    printf("%s of %s\n", nev[12], color[rand()%4]);
}
}

int total(int total[]){         //it will calc the total value of a the cards based on the array(player or dealer) 

int sum = 0;
for (int j = 0; j<16;j++){
    sum = sum + total[j];
}
return sum;
}

int main(){

srand((unsigned int)time(0));

int values_w_p [][2] = {  
    {2,8},{3,8},{4,8},{5,8},{6,8},{7,8},{8,8},{9,8},{10,32},{11,8}
};
int *ptotal = (int*)calloc(16,sizeof(int));
int *dtotal = (int*)calloc(16,sizeof(int));
ptotal[0]= drawacard(actValue(values_w_p), lenOfArr(values_w_p), values_w_p);
ptotal[1]= drawacard(actValue(values_w_p), lenOfArr(values_w_p), values_w_p);
dtotal[0]= drawacard(actValue(values_w_p), lenOfArr(values_w_p), values_w_p);
printf("\nyour card: ");
name(ptotal[0]);
printf("\nyour card: ");
name(ptotal[1]);
printf("\ndealers card: ");
name(dtotal[0]);
printf("\n------------------------------------------------\n");
int inp=0;
int count=0;
while(inp==0){
    if (total(ptotal)==21){ 
        printf("\nYou have a blackjack, lets see the dealers hand");
        dtotal[count] = drawacard(actValue(values_w_p), lenOfArr(values_w_p), values_w_p);
        printf("\ndealers card: ");
        name(dtotal[count]);
        if (total(dtotal)!=21){ 
            printf("\nCongrats! You win!");
            break;
        }
        else{
            printf("\nSadly its a TIE");
            break;
        }
    }
    printf("Hit or stand?(0 for hit, 1 for stand): ");
    scanf("%d", &inp);
    if (inp == 1){
        while(total(dtotal)<17){
            count++;
            dtotal[count] = drawacard(actValue(values_w_p), lenOfArr(values_w_p),values_w_p);
            printf("\ndealers card: ");
            name(dtotal[count]);
            if (total(dtotal)==21){
                printf("\nThe dealer has a blackjack, you lost");
                break;
            }
            else if(total(dtotal)>21 && total(ptotal)<22){
                printf("\nYou win");
                break;
            }
            else if(total(dtotal)>21 && total(ptotal)>21){
                printf("\n Its a TIE");
                break;
            }
            else if(total(dtotal)>16 && total(dtotal)>total(ptotal)){
                printf("You lost");
                break;
            } 
            else if(total(dtotal)==total(ptotal) && total(dtotal)>16){
                printf("its a TIE");
                break;
            }
            else if(total(dtotal)<total(ptotal) && total(dtotal)>16){
                printf("You win");
                break;
            }
        }
    }
    else{
        count++;
        ptotal[count+1] = drawacard(actValue(values_w_p), lenOfArr(values_w_p), values_w_p);
        printf("\nyour card: ");
        name(ptotal[count+1]);
        if (total(ptotal)>21){
            printf("\nYou lost");
            break;
        }
    }
}
return 0;
}