You will program a modified game of Blackjack, in which a single user can play a
ID: 3554844 • Letter: Y
Question
You will program a modified game of Blackjack, in which a single user can play against the dealer (the computer). Your program must make use of functions in addition to main. Your program should play the game in the following sequence: Shuffle: Fill an array of 52 elements with random integers ranging from 1 to 11. This array will serve as your deck of cards. Deal: Select one card for the dealer and two cards to the player from the array. Cards must be picked, in order, from the end of the array. Print out the value of all these cards to the screen. Ask the Player for Choices: The player can choose to hit or stand. Hit means that the player would like to receive another card. Stand means that the player would not like to receive any more cards. Execute the player's choices until the player chooses to stand or he/she busts. Execute the Dealer's Choices: if the player has busted, the dealer has won. Otherwise, upon completion of the player's turn, the dealer hits until it gets closer to 21 than the player, or there's a bust. Special Notes For the purposes of to ensure that there is one card of each suit. multiple cards with may create none, one, or 1 to 11). For the purposes of this project, face cards can be referred to by their numerical value. You will only need to generate "cards" ranging from 1 - 11, no need for Ace, Jack, Queen, or King. For the purposes of this project, you do not need to have a betting structure, as in real Blackjack. There are no bet or payout calculations required.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cards[52],dealer,player;
static int ptr = 51;
void shuffleCards(){
srand(time(NULL));
dealer = player = 0;
int i;
for(i=0;i<52;i++){
cards[i] = 1 + rand()%11;
}
}
void deal(){
printf("Dealer got: %d ",cards[ptr]);
dealer+=cards[ptr--];
printf("Player got: %d and ",cards[ptr]);
player+=cards[ptr--];
printf("%d ", cards[ptr]);
player+=cards[ptr--];
}
void askChoice(int *ch){
printf("1. Hit 2. Stand Enter your choice: ");
scanf("%d",ch);
printf("%d ", *ch);
if(*ch == 1 || *ch == 2){
return;
}else{
printf(" Wrong choice. Enter again.. ");
askChoice(ch);
}
}
int main(int argc, char const *argv[])
{
int ch;
shuffleCards();
deal();
while(1){
askChoice(&ch);
if(ch==1){
printf("Player got: %d ", cards[ptr]);
player+=cards[ptr--];
if(player>21 && dealer<=21){
printf("Player busted!!! Dealer's total: %d ", dealer);
}else if(player<21 && dealer>21){
printf("Dealer busted!!! Player's total: %d ", player);
}if(player<21 && dealer<21 && player>dealer){
printf("Dealer busted!!! Player's total: %d ", player);
}
}else if(ch==2){
printf("Dealer got: %d ", cards[ptr]);
dealer+=cards[ptr--];
if(dealer>21 && player<=21){
printf("Dealer busted!!! Player's total: %d ", player);
}else if(dealer<21 && player>21){
printf("Player busted!!! Dealer's total: %d ", player);
}else if(player<21 && dealer<21 && dealer>player){
printf("Player busted!!! Dealer's total: %d ", player);
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.