Write a program to play the game of Blackjack where the user of the program is t
ID: 3626458 • Letter: W
Question
Write a program to play the game of Blackjack where the user of the program is the player, and the program acts as the dealer. The program begins by dealing two cards each to the player and the dealer. The values of player's cards are shown. The value of one of the dealer's card is shown, with the other value hidden until the hand is over. The hidden dealer card is called the "hole" card. Once the player's hand is dealt, the player inputs the following one-character commands: "h" for hit (take another card), "s" for stand (stand pat on hand, proceed to dealer play), and q for "quit hand". If the player uses the 'q' command, the player loses immediately. The dealer does not play, and the hole card is not revealed. This is an idea of what it should output:Player's Hand: 3S, JC
Dealer's Hand: 5H, X
Enter command: h
Player's Hand: 3S, JC, 9H
Dealer's Hand: 5H, 2D
Player busts.
Enter 'q' to quit, anything else to play another hand: c
Player's Hand: KS, 8D
Dealer's Hand: 9C, X
Enter command: s
Player's Hand: KS, 8D
Dealer's Hand: 9C, 3H, 10H
Dealer busts.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
void deal(int,int,int*,int*,int[][2]);
void deal2(int,int,int*,int*,int*,int[][2]);
void filldeck(int[][2]);
void shuffle(int[][2]);
void getcard(int,int*,int*,int[][2]);
int whoWins(int,int);
int hid1,hid2;
int main()
{
int totdealer,totplayer,deck[52][2];
int money,bet=5,aces,i,win;
char *person[]={"Dealer","player"};
char *face[] = { "Ace", "Deuce", "Three", "Four", "Five","Six","Seven", "Eight", "Nine", "Ten",
"Jack", "Queen","King"};
char* suit[]={"hearts","diamonds","spades","clubs"};
char hit='H';
char yesno='Y';
int again,upto=0;
srand(time(0));
while(yesno=='Y')
{totdealer=0;
aces=0;
filldeck(deck);
shuffle(deck);
deal2(0,2,&aces,&totdealer,&upto,deck);
totplayer=0;
deal2(1,2,&aces,&totplayer,&upto,deck);
again=0;
while(totdealer<21&&totplayer<21&&again==0)
{
printf(" HIT or STAND or Quit? ");
scanf("%c",&hit);
while (getchar() != ' ' );
if(toupper(hit)=='H')
{deal2(1,1,&aces,&totplayer,&upto,deck);
}
else if(toupper(hit)=='Q')
again=2;
else
again=1;
}
if(again!=2)
{
printf("Dealer total: %d ",totdealer);
win=whoWin(totdealer,totplayer);
printf(" Game over %s wins ",person[win]);
}
printf("Dealers hidden card: %s of %s ",face[hid1],suit[hid2]);
printf(" Play again (Y/N)?");
scanf("%c",&yesno);
while (getchar() != ' ' );
yesno=toupper(yesno);
}
return 0;
}
int whoWin(int totdealer,int totplayer)
{if(totdealer>totplayer||totplayer>21)
return 0;
else if(totdealer<totplayer&&totplayer<=21)
return 1;
}
void getcard(int n,int *f,int*s,int deck[][2])
{int i,j,card=0;
*s=deck[n][0];
*f=deck[n][1];
}
void deal2(int p,int num,int* aces,int *tot,int* upto,int deck[][2])
{int i,n,s;
char *person[]={"Dealer","player"};
char *face[] = { "Ace", "Deuce", "Three", "Four", "Five","Six","Seven", "Eight", "Nine", "Ten",
"Jack", "Queen","King"};
char* suit[]={"hearts","diamonds","spades","clubs"};
printf("%s draws ",person[p]);
for(i=0;i<num;i++)
{getcard(*upto,&n,&s,deck);
if(p==0&&i!=0||p!=0)
printf("%s of %s, ",face[n],suit[s]);
else
{hid1=n;
hid2=s;
}
n++;
if(n==1)
{*aces=*aces+1;
*tot=*tot+11;
}
else if(n>=10)
*tot=*tot+10;
else
*tot=*tot+n;
*upto=*upto+1;
}
if(*tot>21)
if(*aces==0)
return;
else
{*aces=*aces-1;
*tot=*tot-10;
}
printf(" ");
}
void shuffle(int deck[][2])
{
int i,num1,type1,num2,type2,temp;
for(i=0;i<100;i++)
{num1=rand()%13;
num2=rand()%13;
type1=rand()%4;
type2=rand()%4;
temp=deck[type1][0];
deck[type1][0]=deck[type2][0];
deck[type2][0]=temp;
temp=deck[num1][1];
deck[num1][1]=deck[num2][1];
deck[num2][1]=temp;
}
}
void filldeck(int deck[][2])
{int cards[4][13];
int i,j,num,type;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
cards[i][j]=0;
for(j=0;j<52;j++)
{do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
deck[j][0]=type;
deck[j][1]=num;
cards[type][num]=1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.