In: Computer Science
C program simple version of blackjack following this design.
1. The basic rules of game
A deck of poker cards are used. For simplicity, we have
unlimited number of cards, so we can generate a random card without
considering which cards have already dealt. The game here is to
play as a player against the computer (the dealer). The aim of the
game is to accumulate a higher total of points than the dealer’s,
but without going over 21. The cards 2 to 10 have their face values
as points. J, Q, and K are10 points each, and the Ace is either 1
point or 11 points (player's choice). To simplify the matter, we
consider that the Ace is 11 points and we don’t have card J, Q, or
K unless you like to implement the option anyway.
a) Betting
The player first places a bet. Let’s assume the minimum bet is $10
and maximum = is $1000.
b) Each play will result in one of the following events for the player
c) The start of the game
At the start, the player and the dealer receive two cards each. The
player’s cards are normally dealt face up (displayed), while the
dealer has one face down (called the hole card) and one face up.
The best possible blackjack hand is an opening deal of an Ace with
any of the ten-point cards. This is called a "blackjack", or a
natural 21, and the player holding this automatically wins unless
the dealer also has a blackjack. If a player and the dealer each
have a blackjack, the result is a push.
d) The player’s turn
The player can keep his hand as it is (stand) or take more cards
from the deck (hit), one at a time, until either the player judges
that the hand is strong enough to go up against the dealer’s hand
and stands, or until it goes over 21, in which case the player
immediately loses (busted).
e) The dealer’s turn
The dealer turns over the hidden hole card. The dealer hits (takes more cards) or stands depending on the value of the hand. The dealer must hit if the value of the hand is lower than 17, otherwise the dealer stands.
If the dealer is busted, the player wins. Otherwise the player wins
if s/he has a higher score, loses if s/he has a lower score, or
pushes if s/he has the same score as the dealer.
Blackjack consideration is not required, unless you like to implement the option anyway. By the way, a blackjack hand beats any other hand, also those with a total value of 21 but with more cards (which is not a natural blackjack).
f) The program towards the
end
If the player won or lost, s/he must decide whether to quit or to
play another game unless the player runs out of money. Your program
should give the player an initial betting amount of $1000.00.
2. The specific design of this project
a) The main() program and its variables
You will need to decide on appropriate variables in which to store
the player's bankroll (in order to keep track of how much money or
how many points the player has), the bet at a game, and other
information. Let’s use an integer array gamerecord[] to store how
many times the player won, lost, hit a blackjack, and got busted.
(Again, blackjack is optional).
The bankroll, bet, and gamerecord[] should be kept up to date on
the player's current status. (The program calls playing() to play a
game, as discussed below. )
After each game, the program must report the result of the game:
the amount of money won or lost, the current value of the bankroll,
how many times the player won and lost, and how many times the
player hit a blackjack and got busted. (You may want to record and
report how many times the dealer got busted as well, as an
option.)
After each game (by calling playing()), the program should allow the player to continue playing until s/he chooses to quit, or until s/he runs out of money. This central program control may be done within main(), in a do-while loop: 1) call playing() to play a game; 2) check whether to play again. We will add some more components later.
b) “Dealing” the card: the dealing() function
A separate dealing() function will be used to
generate a card number. You may want to implement and double check
this function first. You will use a random number generator. The
random number generator needs to be seeded with the current time at
the beginning of the main program. The possible random values
generated are 1 to 10 (or 13 if J, Q, and K are considered),
representing the cards’ face values. This function will return the
number generated. The return value 1 represent the Ace’s face value
(and the return value 11, 12, and 13 are J, Q, and K’s face value,
respectively.) A large random number n can be converted to
a value between 1 to 13 by: (1 + n%13).
c) “Playing” the game: playing() function
A second function playing() will be used to play a
single game until the player either wins or loses a bet, based upon
the rules given above. This function should get a bet, modify the
current amount of the player's bank roll according to the game
result, modify the gamerecord array values of the player won or
lost, and the player hit the blackjack or got
busted. These values are returned through function
parameters by address passing in playing().
Within the function, the player is asked to place a bet (10 to 1000
within the bankroll amount), so the corresponding value is read
from the keyboard. The system (dealer) then "deals the cards"
(simulated by calling the function dealing(), one card at a time).
After each dealing, this function should report the card values,
except the dealer’s hole card. The function should have two
variables to store the player and the dealer’s scores. Remember
face value 1 represents score 11 (or 1 if you want to be more
complete as an option, and 11, 12, or 13 represents score 10).
The player can keep his hand as it is (stand) or take more cards
from the deck (hit), one at a time, until either the player judges
that the hand is strong enough to go up against the dealer's hand
and stands, or until it goes over 21, in which case the player
immediately loses the bet.
The dealer turns over his hidden hole card by displaying the hold
card face value, and starts the game process automatically until
the dealer wins or loses.
d) "Ending" and "Beginning" of the game
This part is implemented after you have done your programming as
described above already.
You need a separate function ending() to do the following: you should report the current value of the bank roll, how many times the player won, lost, hit a blackjack, and went busted. You need to save the above information into a text file as well.
You need a separate function beginning() to do the
following at the beginning of your program in main(): the function
will open the text file you used to save the game information for
reading if it exists, so that your game can continue from previous
played results. If the file does not exist or the bank roll has a
balance below the minimum bet, you start the game from scratch as
usual, and report “new game” or “continual game”.
So, main() includes 1) beginning(); 2) a loop: playing(); 3) ending();
Code:-
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define spade 06
#define club 05
#define diamond 04
#define heart 03
#define RESULTS "Blackjack.txt"
//Global Variables
int k;
int l;
int d;
int won;
int loss;
int cash = 500;
int bet;
int random_card;
int player_total=0;
int dealer_total;
int clubcard();
int diamondcard();
int heartcard();
int spadecard();
int randcard();
int betting();
void asktitle();
void rules();
void play();
void dealer();
void stay();
void cash_test();
void askover();
void fileresults();
//Main Function
int main(void)
{
int choice1;
asktitle();
printf("\n");
printf("\n");
system("pause");
return(0);
} //end program
void asktitle() // Function for asking player if they want to
continue
{
char choice1;
int choice2;
printf("\n Are You Ready?");
printf("\n (Y/N)\n ");
scanf("\n%c",&choice1);
while((choice1!='Y') && (choice1!='y') &&
(choice1!='N') && (choice1!='n'))
{
printf("\n");
printf("Incorrect Choice. Please Enter Y for Yes or N for
No.\n");
scanf("%c",&choice1);
}
if((choice1 == 'Y') || (choice1 == 'y')) // If yes, continue.
Prints menu.
{
system("cls");
printf("\nEnter 1 to Begin the game.");
printf("\nEnter 2 to Exit Game. ");
printf("\nChoice: ");
scanf("%d", &choice2); // Prompts user for choice
if((choice2<1) || (choice2>3)) // If invalid choice
entered
{
printf("\nIncorrect Choice. Please enter 1, 2 or 3\n");
scanf("%d", &choice2);
}
switch(choice2)
{
case 1: // Case to begin game
system("cls");
play();
break;
case 2: // Case to exit game
system("pause");
exit(0);
break;
default:
printf("\nInvalid Input");
}
}
else if((choice1 == 'N') || (choice1 == 'n')) // If no, exit
program
{
system("pause");
exit(0);
}
return;
} // End function
int clubcard() //Displays Club Card Image
{
srand((unsigned) time(NULL));
k=rand()%13+1;
if(k<=9) //If random number is 9 or less, print card with
that number
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| %d |\n", k);
printf("| %c|\n", club);
printf("-------\n");
}
if(k==10) //If random number is 10, print card with J (Jack) on
face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| J |\n");
printf("| %c|\n", club);
printf("-------\n");
}
if(k==11) //If random number is 11, print card with A (Ace) on
face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| A |\n");
printf("| %c|\n", club);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to
11 or 1 depending on dealer total
{
k=11;
}
else
{
k=1;
}
}
if(k==12) //If random number is 12, print card with Q (Queen) on
face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| Q |\n");
printf("| %c|\n", club);
printf("-------\n");
k=10; //Set card value to 10
}
if(k==13) //If random number is 13, print card with K (King) on
face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| K |\n");
printf("| %c|\n", club);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
}// End function
int diamondcard() //Displays Diamond Card Image
{
srand((unsigned) time(NULL)); //Generates random seed for rand()
function
k=rand()%13+1;
if(k<=9) //If random number is 9 or less, print card with
that number
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| %d |\n", k);
printf("| %c|\n", diamond);
printf("-------\n");
}
if(k==10) //If random number is 10, print card with J (Jack) on
face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| J |\n");
printf("| %c|\n", diamond);
printf("-------\n");
}
if(k==11) //If random number is 11, print card with A (Ace) on
face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| A |\n");
printf("| %c|\n", diamond);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to
11 or 1 depending on dealer total
{
k=11;
}
else
{
k=1;
}
}
if(k==12) //If random number is 12, print card with Q (Queen) on
face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| Q |\n");
printf("| %c|\n", diamond);
printf("-------\n");
k=10; //Set card value to 10
}
if(k==13) //If random number is 13, print card with K (King) on
face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| K |\n");
printf("| %c|\n", diamond);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
}// End function
int heartcard() //Displays Heart Card Image
{
srand((unsigned) time(NULL));
k=rand()%13+1;
if(k<=9)
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| %d |\n", k);
printf("| %c|\n", heart);
printf("-------\n");
}
if(k==10) //If random number is 10, print card with J (Jack) on
face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| J |\n");
printf("| %c|\n", heart);
printf("-------\n");
}
if(k==11) //If random number is 11, print card with A (Ace) on
face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| A |\n");
printf("| %c|\n", heart);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to
11 or 1 depending on dealer total
{
k=11;
}
else
{
k=1;
}
}
if(k==12) //If random number is 12, print card with Q (Queen) on
face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| Q |\n");
printf("| %c|\n", heart);
printf("-------\n");
k=10; //Set card value to 10
}
if(k==13) //If random number is 13, print card with K (King) on
face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| K |\n");
printf("| %c|\n", heart);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
} // End Function
int spadecard() //Displays Spade Card Image
{
srand((unsigned) time(NULL)); //Generates random seed for rand()
function
k=rand()%13+1;
if(k<=9) //If random number is 9 or less, print card with
that number
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| %d |\n", k);
printf("| %c|\n", spade);
printf("-------\n");
}
if(k==10) //If random number is 10, print card with J (Jack) on
face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| J |\n");
printf("| %c|\n", spade);
printf("-------\n");
}
if(k==11) //If random number is 11, print card with A (Ace) on
face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| A |\n");
printf("| %c|\n", spade);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to
11 or 1 depending on dealer total
{
k=11;
}
else
{
k=1;
}
}
if(k==12)
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| Q |\n");
printf("| %c|\n", spade);
printf("-------\n");
k=10; //Set card value to 10
}
if(k==13) //If random number is 13, print card with K (King) on
face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| K |\n");
printf("| %c|\n", spade);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
} // End Function
int randcard() //Generates random card
{
srand((unsigned) time(NULL)); //Generates random seed for rand()
function
random_card = rand()%4+1;
if(random_card==1)
{
clubcard();
l=k;
}
if(random_card==2)
{
diamondcard();
l=k;
}
if(random_card==3)
{
heartcard();
l=k;
}
if(random_card==4)
{
spadecard();
l=k;
}
return l;
} // End Function
void play() //Plays game
{
int p=0; // holds value of player_total
int i=1; // counter for asking user to hold or stay (aka game
turns)
char choice3;
cash = cash;
cash_test();
printf("\nCash: $%d\n",cash); //Prints amount of cash user
has
randcard(); //Generates random card
player_total = p + l; //Computes player total
p = player_total;
printf("\nYour Total is %d\n", p); //Prints player total
dealer(); //Computes and prints dealer total
betting(); //Prompts user to enter bet amount
while(i<=21) //While loop used to keep asking user to hit or
stay at most twenty-one times
// because there is a chance user can generate twenty-one
consecutive 1's
{
if(p==21) //If user total is 21, win
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
if(p>21) //If player total is over 21, loss
{
printf("\nWoah Buddy, You Went WAY over.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
if(p<=21) //If player total is less than 21, ask to hit or
stay
{
printf("\n\nWould You Like to Hit or Stay?");
scanf("%c", &choice3);
while((choice3!='H') && (choice3!='h') &&
(choice3!='S') && (choice3!='s')) // If invalid choice
entered
{
printf("\n");
printf("Please Enter H to Hit or S to Stay.\n");
scanf("%c",&choice3);
}
if((choice3=='H') || (choice3=='h')) // If Hit, continues
{
randcard();
player_total = p + l;
p = player_total;
printf("\nYour Total is %d\n", p);
dealer();
if(dealer_total==21) //Is dealer total is 21, loss
{
printf("\nDealer Has the Better Hand. You Lose.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
if(dealer_total>21) //If dealer total is over 21, win
{
printf("\nDealer Has Went Over!. You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
}
if((choice3=='S') || (choice3=='s')) // If Stay, does not
continue
{
printf("\nYou Have Chosen to Stay at %d.!\n", player_total);
stay();
}
}
i++; //While player total and dealer total are less than 21, re-do
while loop
} // End While Loop
} // End Function
void dealer() //Function to play for dealer AI
{
int z;
if(dealer_total<17)
{
srand((unsigned) time(NULL) + 1);
z=rand()%13+1;
if(z<=10)
{
d=z;
}
if(z>11)
{
d=10;
}
if(z==11) //If random number is 11(Ace), change value to 11 or 1
depending on dealer total
{
if(dealer_total<=10)
{
d=11;
}
else
{
d=1;
}
}
dealer_total = dealer_total + d;
}
printf("\nThe Dealer Has a Total of %d", dealer_total); //Prints
dealer total
} // End Function
void stay()
{
dealer();
if(dealer_total>=17)
{
if(player_total>=dealer_total)
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
if(player_total<dealer_total) //If player's total is less than
dealer's total, loss
{
printf("\nDealer Has the Better Hand. You Lose.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
if(dealer_total>21) //If dealer's total is more than 21,
win
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won,
loss);
dealer_total=0;
askover();
}
}
else
{
stay();
}
} // End Function
void cash_test()
{
if (cash <= 0)
{
printf("You Are Bankrupt. Game Over");
cash = 500;
askover();
}
} // End Function
int betting() //Asks user amount to bet
{
printf("\n\nEnter Bet: $");
scanf("%d", &bet);
if (bet > cash) //If player tries to bet more money than
player has
{
printf("\nYou cannot bet more money than you have.");
printf("\nEnter Bet: ");
scanf("%d", &bet);
return bet;
}
else return bet;
} // End Function
void askover()
char choice1;
{
printf("\nWould You Like To Play Again?");
printf("\nPlease Enter Y for Yes or N for No\n");
scanf("\n%c",&choice1);
while((choice1!='Y') && (choice1!='y') &&
(choice1!='N') && (choice1!='n')) // If invalid choice
entered
{
printf("\n");
printf("Incorrect Choice. Please Enter Y for Yes or N for
No.\n");
scanf("%c",&choice1);
}
if((choice1 == 'Y') || (choice1 == 'y')) // If yes, continue.
{
system("cls");
play();
}
else if((choice1 == 'N') || (choice1 == 'n')) // If no, exit
program
{
fileresults();
printf("\nBYE!!!!\n\n");
system("pause");
exit(0);
}
return;
} // End function
void fileresults() y
{
FILE *fpresults;
fpresults = fopen(RESULTS, "w");
if(fpresults == NULL)
printf("\nError: File Missing\n");
system("pause");
exit(1);
}
else
{
fprintf(fpresults,"\n\t RESULTS");
fprintf(fpresults,"\n\t---------\n");
fprintf(fpresults,"\nYou Have Won %d Times\n", won);
fprintf(fpresults,"\nYou Have Lost %d Times\n", loss);
fprintf(fpresults,"\nKeep Playing !");
}
fclose(fpresults);
return;
} // End Function
Output:-
Are You Ready?
(Y/N)
Y
Enter 1 to Begin the game.
Enter 2 to Exit Game.
Choice: 1
Cash: $500
-------
| |
| Q |
| |
-------
Your Total is 10
The Dealer Has a Total of 10
Enter Bet: $40
Would You Like to Hit or Stay?
Please Enter H to Hit or S to Stay.
S
You Have Chosen to Stay at 10.
The Dealer Has a Total of 20
Dealer Has the Better Hand. You Lose.
You have 0 Wins and 1 Losses. Awesome!
Would You Like To Play Again?
Please Enter Y for Yes or N for No
N
Please UPVOTE thank you...!!!