Blackjack Game in C Lang!
This is the code for a blackjack game that deciacco and I worked on for a project for a class that I had. Documentation is in the code itself.
Update:
- By deciacco: I uploaded a zip file with the c file and a compiled exe so you can give it a try! You should be able to run the exe in Windows without a problem.
/*
Copyright (C) 2007 Bojan M.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/**********************************************************************************/
/** Bojan M. *****************************************************************/
/** blackjack.c *****************************************************************/
/** This will play a game of blackjack with the house, and 1 - 3 players for a **/
/** max of 10 hands. Since this game will not employ a wager, the scoring *******/
/** will be as follows: When the player or house get blackjack --> 2pts. ********/
/** If the player or house bust --> 0pts. If the player or house win --> 1pt. ***/
/** Pressing zero will exit the game. *******************************************/
/**********************************************************************************/
#define _CRT_SECURE_NO_DEPRECATE = 1 //Avoid Visual Studio warning
#include
#include
#include
#include
//Required for system()
#include //Required for rand() and srand()
//Value defenitions
#define TRUE 1
#define FALSE 0
#define BUFFSIZE 10
#define NOCARDS 52 //Will need to gen deck later
#define MAXPLAYERS 3
#define MAXHANDS 10
#define RANDSHUF 100
#define PLAYERSIZE 12
#define NEWCARDPOS 11
#define SCORE 0
#define SUM 1
//Function prototypes
void init_cards(int cards[]);
int get_card(int cards[], int *);
void shuf_cards(int cards[], int *);
void show_deck(int cards[]);
int random_int(int, int);
void show_p_hand(int player[][PLAYERSIZE], int);
void show_h_hand(int house[]);
void calc_p_sum(int player[][PLAYERSIZE], int);
void calc_h_sum(int house[]);
void summary(int house[], int player[][PLAYERSIZE], int);
void score(int house[], int player[][PLAYERSIZE], int);
/****************************** START main *********************************/
void main()
{
int i, j, no_players, no_hands, cur_hand = 0;
int cards[NOCARDS]; //Deck of cards
int player[MAXPLAYERS][PLAYERSIZE]; //Holds player stats during game
int house[PLAYERSIZE]; //Holds house stats during game
int play, ask, playhand; //Flags
int nextplayer; //Flag
int deckpos; //Keeps track of where to store next card
char yesno[BUFFSIZE]; //Input buffer
char hitstay[BUFFSIZE];
play = FALSE;
ask = playhand = TRUE;
house[SCORE] = 0; //Initializing the house total score
init_cards(cards); //Calling two functions to generate cards and shuffle them
shuf_cards(cards, &deckpos);
printf("BBBB LL AAA CCCCCC KK KK n");
printf("BB BB LL AA AA CC CC KK KK n");
printf("BB BB LL AA AA CC CC KK KK n");
printf("BB BB LL AA AA CC KK KK n");
printf("BB BB LL AA AA CC KK KK n");
printf("BB BB LL AA AA CC KK KK n");
printf("BB BB LL AA AA CC KKK n");
printf("BBBB LL AA AA CC KK KK n");
printf("BB BB LL AA AA CC KK KK n");
printf("BB BB LL AAAAAAAAA CC KK KK n");
printf("BB BB LL AAAAAAAAA CC KK KK n");
printf("BB BB LL AA AA CC KK KK n");
printf("BB BB LL AA AA CC CC KK KKn");
printf("BB BB LLLLLLL AA AA CC CC KK KKn");
printf("BBBB LLLLLLL AA AA CCCCCC KK KKnnn");
printf(" JJ AAA CCCCCC KK KKn");
printf(" JJ AA AA CC CC KK KKn");
printf(" JJ AA AA CC CC KK KKn");
printf(" JJ AA AA CC KK KKn");
printf(" JJ AA AA CC KK KKn");
printf(" JJ AA AA CC KK KKn");
printf(" JJ AA AA CC KKKn");
printf(" JJ AA AA CC KK KKn");
printf(" JJ AA AA CC KK KKn");
printf(" JJ AAAAAAAAA CC KK KKn");
printf(" JJ AAAAAAAAA CC KK KKn");
printf(" JJ JJ AA AA CC KK KKn");
printf(" JJ JJ AA AA CC CC KK KKn");
printf(" JJ JJ AA AA CC CC KK KKn");
printf(" JJJJJJJ AA AA CCCCCC KK KKnnnnn");
printf("Let's play some BlackJack!!!nn");
printf("Please enter '1' - '%d' for the number of players, or enter '0' to quit.n", MAXPLAYERS);
//If the user enters '0' ask will equal FALSE and will break out of loop
//Else it will take the input and go on to ask for how many hands
while(ask == TRUE)
{
printf("Enter choice: ");
scanf("%d", &no_players);
if(no_players == 0)
{
ask = FALSE;
}
else if(no_players >= 1 && no_players <= MAXPLAYERS)
{
play = TRUE;
ask = FALSE;
}
}
ask = TRUE;
//system("cls");
//If the user enters '0' ask will equal FALSE and will break out of loop
//Else it will take the input and go on to play the game
if(play == TRUE)
{
play = FALSE;
printf("How many hands do you want to play (max %d hands, '0' to quit.n", MAXHANDS);
while(ask == TRUE)
{
printf("Enter choice: ");
scanf("%d", &no_hands);
if(no_hands == 0)
{
ask = FALSE;
}
else if(no_hands >= 1 && no_hands <= MAXHANDS)
{
play = TRUE;
ask = FALSE;
}
}
//Initializing player score
for(i=0; i
{
player[i][SCORE] = 0;
}
//If the user entered 1 - 3 players play will equal to TRUE so the game starts
if(play == TRUE)
{
//system("cls");
printf("OK, let's play %d hands with %d players!!!nn", no_hands, no_players);
while(playhand == TRUE && cur_hand < no_hands)
{
//Give out current hand to each player
for(i = 0; i < no_players; i++)
{
for(j = 2; j < 4; j++)
{
player[i][j] = get_card(cards, &deckpos);
}
player[i][NEWCARDPOS] = 4; //Next available empty slot in player array
//must always be called after assigning empty slot
calc_p_sum(player, i);
}
//Take out two cards for house
for(j = 2; j < 4; j++)
{
house[j] = get_card(cards, &deckpos);
}
house[NEWCARDPOS] = 4;//Next available empty slot in house array
//must always be called after assigning empty slot
calc_h_sum(house);
//Go through every player for hit or stay
for(i = 0; i < no_players; i++)
{
nextplayer = FALSE;
while(nextplayer == FALSE)
{
printf("Player %dn", i+1);
show_p_hand(player,i); //Will show player X hand
if(player[i][SUM] == 21) //If it's 21, player will get
{ //blackjack and go on to next player
printf("Player %d got BLACKJACK!!!nn", i+1);
nextplayer = TRUE;
}
else
{
if(player[i][SUM] < 21)
{
//Must enter h or s to hit or stay, anything else
//will go to next playesr
printf("Player %d, hit or stay (h or s)?: ", i+1);
scanf("%s", hitstay);
if(hitstay[0] == 'h' || hitstay[0] == 'H')
{
player[i][player[i][NEWCARDPOS]] = get_card(cards, &deckpos);
player[i][NEWCARDPOS] += 1;
calc_p_sum(player, i);
}
else
nextplayer = TRUE;
}
else
{ //If a player get more then 21 it's a Bust and go to
//next player
printf("BUST!!!!nn");
nextplayer = TRUE;
}
}
}
}
//Process house and display it's hand
//if house has 17 or less, draw another card then calc sum
while(house[SUM] < 17)
{
house[house[NEWCARDPOS]] = get_card(cards, &deckpos);
house[NEWCARDPOS] += 1;
calc_h_sum(house);
}
show_h_hand(house); //Shows the house hand
summary(house, player, no_players); //Prints the current hand summary
//Next hand?
//If no, game quits
if (cur_hand < no_hands-1)
{
printf("Next hand? (y,n): ");
scanf("%s", yesno);
printf("n");
if(yesno[0] == 'y' || yesno[0] == 'Y')
{
playhand = TRUE;
shuf_cards(cards, &deckpos);
}
else
{
playhand = FALSE;
}
}
else
playhand = FALSE;
cur_hand += 1;//Increase the hand counter
}
}
}
score(house, player, no_players); //Score at the end of all hands
printf("nnThank you for playing BlackJack.n");
system("PAUSE");
}
/******************************** END main *********************************/
/**************************** START init_cards *****************************/
//Will fill the cards array (can change the number of decks to play
void init_cards(int cards[])
{
int base_cards[13];
int i,j;
//Fill base cards array
for(i = 0; i < 13; i++)
{
if (i <= 9 )
base_cards[i] = i+1;
else
base_cards[i] = 10;
}
//Fill deck with NOCARDS
i = 0;
for(j = 0; j < NOCARDS; j++)
{
cards[j] = base_cards[i];
if(i < 12)
i += 1;
else
i = 0;
}
}
/**************************** END init_cards *******************************/
/**************************** START show_deck ******************************/
//This will show the deck after the cards shuffled, can be used for cheating (kind of)
void show_deck(int cards[])
{
int i;
for(i = 0; i < NOCARDS; i++)
{
printf("%dn",cards[i]);
}
}
/***************************** END show_deck *******************************/
/**************************** START shuf_cards *****************************/
//Shuffles the cards 100 times in the cards array
//Basically it takes one card and swithes it with another in the array
void shuf_cards(int cards[], int *deckpos)
{
int i, tempvalue, randpos, x;
for(i = 0; i <= RANDSHUF; i++)
{
for(x = 0; x < NOCARDS; x++)
{
tempvalue = cards[x];
randpos = random_int(x,NOCARDS-1);
cards[x] = cards[randpos];
cards[randpos] = tempvalue;
}
}
*deckpos = 0; //Reset the card that we are on
//Keeps track of the current array position so all cards that have been
//taken out cannot be taken out anymore
}
/**************************** END shuf_cards *******************************/
/**************************** START random_int *****************************/
//Random function to pick random numbers to pick out of array and swap
int random_int(int min, int max)
{
int rand_int, i;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds); //Seed generator
for (i=0; i
rand_int = rand() % (max - min + 1) + min;
return rand_int;
}
/**************************** END random_int *******************************/
/**************************** START get_card *******************************/
//If user says hit, this function gets the card out of the shuffled array and increases
//the deckpos counter to the next slot in array
int get_card(int cards[], int *deckpos)
{
int cardval;
cardval = cards[*deckpos];
*deckpos += 1;
return cardval;
}
/***************************** END get_hand ********************************/
/*************************** START show_p_hand *****************************/
//Shows player hand
void show_p_hand(int player[][PLAYERSIZE], int p)
{
int i;
for(i = 2; i < player[p][NEWCARDPOS]; i++)
printf("%d ", player[p][i]);
printf(" = %dn", player[p][SUM]);
}
/**************************** END show_p_hand ******************************/
/*************************** START show_h_hand *****************************/
//Shows house hand
void show_h_hand(int house[])
{
int i;
printf("Housen");
for(i = 2; i < house[NEWCARDPOS]; i++)
printf("%d ", house[i]);
printf(" = %dnn", house[SUM]);
}
/**************************** END show_h_hand ******************************/
/*************************** START calc_p_sum ******************************/
//Calculates the player sum, determines if an Ace is 1 or 11
void calc_p_sum(int player[][PLAYERSIZE], int p)
{
int i;
int acepos = 1;
player[p][SUM] = 0; //Reset sum to calc
//Raw sum for player
for(i = 2; i < player[p][NEWCARDPOS]; i++)
{
player[p][SUM] += player[p][i];
}
//Lets check for 1 or 11
for(i = 2; i < player[p][NEWCARDPOS]; i++)
if(player[p][i] == 1)
acepos = i;
if(acepos > 1) //We have either have a 1 or an 11
{
if((player[p][SUM] - 1 + 11) <= 21){
player[p][acepos] = 11;
//New sum for player
player[p][SUM] = 0; //Reset sum to calc
for(i = 2; i < player[p][NEWCARDPOS]; i++)
{
player[p][SUM] += player[p][i];
}
}
}
}
/**************************** END calc_p_sum *******************************/
/************************** START calc_h_sum *******************************/
//Calculates the house sum, determines if an Ace is 1 or 11
void calc_h_sum(int house[])
{
int i;
int acepos = 1;
house[SUM] = 0; //Reset sum to calc
//Raw sum for player
for(i = 2; i < house[NEWCARDPOS]; i++)
{
house[SUM] += house[i];
}
//Lets check for 1 or 11
for(i = 2; i < house[NEWCARDPOS]; i++)
if(house[i] == 1)
acepos = i;
if(acepos > 1) //We have either a 1 or an 11
{
if((house[SUM] - 1 + 11) <= 21){
house[acepos] = 11;
//New sum for player
house[SUM] = 0; //Reset sum to calc
for(i = 2; i < house[NEWCARDPOS]; i++)
{
house[SUM] += house[i];
}
}
}
}
/**************************** END calc_h_sum *******************************/
/***************************** START summary *******************************/
//Goes through all scenarios to determine if it's a Bust, Blackjack, Win, Loss, Push/Tie
void summary(int house[], int player[][PLAYERSIZE], int no_players)
{
int i;
//Go through every player and check score for hand summary
printf("Current Hand summary:n");
for(i = 0; i < no_players; i++)
{
if(house[SUM] > 21 && player[i][SUM] < 21)
{
player[i][SCORE] += 1;
printf("Player %d - Winsn",i+1);
}
else if (player[i][SUM] == 21 && house[SUM] != 21)
{
player[i][SCORE] += 2;
printf("Player %d - BlackJack!n",i+1);
}
else if(player[i][SUM] < house[1] && house[SUM] <= 21)
{
house[SCORE] += 1;
printf("Player %d - Loosesn",i+1);
}
else if(player[i][SUM] > house[SUM] && player[i][SUM] <= 21)
{
player[i][SCORE] += 1;
printf("Player %d - Winsn",i+1);
}
else if(player[i][SUM] > 21)
{
printf("Player %d - Bustn",i+1);
}
else
{
printf("Player %d - Push/Tien",i+1);
}
}
}
/***************************** END summary *********************************/
/***************************** START score *********************************/
//Displays the final overall score for players and house
void score(int house[], int player[][PLAYERSIZE], int no_players)
{
int i;
//Go through every player for hit or stay
printf("nnOverall game scores:n");
for(i = 0; i < no_players; i++)
{
printf("Player %d score: %dn", i+1, player[i][SCORE]);
}
printf("House score: %dn", house[SCORE]);
}
/***************************** END score ************************************/
Download the source file and compiled binary here
If you want to compile it yourself, take a look at this page: MinGW