// Design a Blackjack game, selecting cards randomly both for the guest and the // banker. Print the value of both the hands and the winner. // Author:rahule@cse.iitk.ac.in #include #include void display(int r,char*s) ; int main() { int card_deck[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};//Array for keeping a count of each card drawn.This is to ensure that same card isnot drawn more than once int r,score,score_bank; char c[2],x; printf("\n Please input a random integer for random number generation"); scanf("%d",&r); srand(r);//Used for seeding the random generator function rand() so that it generated different set of random number at each execution of the program score=0; printf("\nStarting the game"); while(1) {r=rand()%13;//Optain a random number in the range 0-12.Now on consider 0 as card number 2,1 as card number 3......8 as card number 10,9 as JACK...and finally 12 as an ACE card_deck[r]++; if(card_deck[r]>4)//Checking whether the randomly generated card has already appeared 4 times.If it has then discard and generate a new card. {continue; } if(r>=0&&r<=7) {score=score+r+2; } if(r>=8&&r<=11) {score=score+10; } if(r==12&&card_deck[r]==1)//If the card is an ACE and it is appearing for the first time add 11 {score=score+11; if(score>21)//After adding 11 if the score goes above 21 consider the ace as 1. {score=score-10; } } if(r==12&&card_deck[r]==2)//If the card is an ACE and it is appearing for the second time consider it as 1 and subtract 10 to consider the previous ace as 1 {score=score-10+1; } if(r==12&&card_deck[r]>2)//If the card is an ACE and it is appearing for the third or fourth time consider it as 1 {score=score+1; } display(r,"You");//Function to display the card which you have drawn printf("\n Your score is %d",score); if(score>21)//checking whether you are busted or not.If yes then the bank wins { printf("\n You are busted \n Bank Wins");return 0; } printf("\n Do you want to draw another card?(y/n)"); scanf("%s",c); if(c[0]=='n'||c[0]=='N') break; } printf("\nBank is going to play"); score_bank=0; while(score_bank<16)//Bank will play till its score is above 16.The remaining logic of the game remains the same as that of the player. {r=rand()%13; card_deck[r]++; if(card_deck[r]>4) {continue; } if(r>=0&&r<=7) {score_bank=score_bank+r+2; } if(r>=8&&r<=11) {score_bank=score_bank+10; } if(r==12&&card_deck[r]==1) {score_bank=score_bank+11; if(score_bank>21) {score_bank=score_bank-10; } } if(r==12&&card_deck[r]==2) {score_bank=score_bank-10+1; } if(r==12&&card_deck[r]>2) {score_bank=score_bank+1; } display(r,"Bank"); printf("\n Bank's score is %d",score_bank); if(score_bank>21) { printf("\n Bank busted \n You Win");return 0; } if(score_bank>score) {printf("\nBank wins");return 0;} } if(score>score_bank) printf("\nYou win\n"); return 0; } void display(int r,char *s) { switch(r) { case 0: printf("\n %s drawn a %d",s,r+2); break; case 1: printf("\n %s drawn a %d",s,r+2); break; case 2: printf("\n %s drawn a %d",s,r+2); break; case 3: printf("\n %s drawn a %d",s,r+2); break; case 4: printf("\n %s drawn a %d",s,r+2); break; case 5: printf("\n %s drawn a %d",s,r+2); break; case 6: printf("\n %s drawn a %d",s,r+2); break; case 7: printf("\n %s drawn a %d",s,r+2); break; case 8: printf("\n %s drawn a %d",s,r+2); break; case 9: printf("\n %s drawn a JACK ",s); break; case 10: printf("\n %s drawn a QUEEN",s); break; case 11: printf("\n %s drawn a KING",s); break; case 12: printf("\n You have drawn an ACE",s); break; } return; }