Help with C programming: Write a program to let the user play a game called Blac
ID: 3667680 • Letter: H
Question
Help with C programming:
Write a program to let the user play a game called Blackjack Dice against the dealer. The game is played with two, 11-
sided dice, where each die has faces representing the numbers 1 through 11. The objective of the game is to roll 21.
If the user or the dealer goes over 21, it is considered a bust and a loss. In general, the player’s score must beat the
dealer’s or the player loses.
How the game is played:
The player begins by rolling 2 dice and the players total is the sum rolled on the two dice. If the player’s total is 21 on
the first roll that is considered blackjack and the player automatically wins. If the player does not roll 21 on the first
roll, they may choose to roll a single die as many times as they wish, adding the value of the resulting roll to their
total. If the player’s total exceeds 21, they bust and lose the game.
After the player is done rolling and if the player did not get blackjack, the dealer begins by rolling the 2 dice and the
dealer’s total is the sum rolled on the two dice. If the dealer gets a total of 21 on the first roll, that is blackjack and
the dealer wins. If the total is greater than 16, the dealer stops rolling. Otherwise, the dealer continues to roll a single
die with the value of each roll added to total until his total is greater than 16. If the dealer busts by going over 21, the
player wins. The dealer wins all ties.
Your program must consist of “at least four” functions: main( ), rollDie( ), playersTurn( ), and dealersTurn( ). The
function rollDie( ) simulates the rolling of an 11-sided die. The function playersTurn( ) implements the players turn.
The function dealersTurn( ) implements the dealer’s turn.
Sample output from a program that satisfies the requirements of this exercise is given below.
Example 1:
****** PLAYER'S TURN ******
Roll: 10 11
Total: 21
Blackjack!
****** GAME SUMMARY ******
Player wins!
Example 2:
****** PLAYER'S TURN ******
Roll: 10 8
Total: 18
Enter R to Roll or S to Stay: S
****** DEALER'S TURN ******
Roll: 7 6
Total: 13
Roll: 5
Total: 18
****** GAME SUMMARY ******
Dealer wins!
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int rand_num()
{
return ( 1 + (rand()%11) );
}
int playersTurn(){
printf("****** PLAYER'S TURN ****** ");
int D1 = rand_num();
int D2 = rand_num();
int total = D1+D2;
int sRoll;
printf("Roll: %d, %d ",D1,D2);
printf("Total: %d ",total);
if(total==21){
printf("Blackjack! ");
return 1;
}
char c = 'R';
while(1){
printf("Enter R to Roll or S to Stay: ");
scanf("%s",&c);
if(c=='R'){
sRoll = rand_num();
printf("Roll: %d ",sRoll);
total = total + sRoll;
printf("Total: %d ",total);
if(total > 21){
return 0;
}
else if(total == 21){
printf("Blackjack! ");
return 1;
}
}
return 2;
}
}
int dealersTurn(){
printf("****** DEALER'S TURN ****** ");
int D1 = rand_num();
int D2 = rand_num();
int total = D1+D2;
int sRoll;
printf("Roll: %d, %d ",D1,D2);
printf("Total: %d ",total);
if(total==21){
printf("Blackjack! ");
return 1;
}
else if(total > 16)
return 2;
while(1){
sRoll = rand_num();
printf("Roll: %d ",sRoll);
total = total + sRoll;
printf("Total: %d ",total);
if(total > 21){
return 0;
}
else if((total >16) && (total <= 21)){
printf("Blackjack! ");
return 1;
}
}
}
int main(){
srand ( time( NULL ) );
int user = playersTurn();
if(user == 1){
printf("****** GAME SUMMARY ****** ");
printf("Player wins! ");
return 0; // player won
}
if(user ==0){
printf("****** GAME SUMMARY ****** ");
printf("Dealer wins! ");
return 0; // dealer won
}
int dealer = dealersTurn();
if(dealer == 1){
printf("****** GAME SUMMARY ****** ");
printf("Dealer wins! ");
return 0; // dealer won
}
if(dealer ==0){
printf("****** GAME SUMMARY ****** ");
printf("Player wins! ");
return 0; // dealer won
}
//Tie case
printf("****** GAME SUMMARY ****** ");
printf("Dealer wins! "); // dealer won
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.