CS 102
Assignment 9
due Tuesday 5 April at 01:00 AM
In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit, all face cards (jack, queen, and king) are counted as 10, and an ace is counted either as a 1 or an 11, depending on the total count of all the cards in a player's hand.
The ace is counted as 11 only if the total value of all cards in a player's hand does not exceed 21, else it is counted as a 1.
Using this information, write a C program that uses a random number generator to select three cards (a 1 initially corresponding to an ace, a 2 corresponding to a face card of two, and so on), calculate the total value of the hand appropriately, and display the value of the three cards with a printed message.
Welcome to Blackjack! You drew the: Three of Spades Jack of Clubs Ace of Hearts Your score is: 14
Table of Contents
1 From class on 3/31
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int deal_a_card();
int rank_to_score(int rank);
int main()
{
int c1, score;
srand(time(NULL));
c1 = deal_a_card();
score = rank_to_score (c1);
printf("(score was %d)\n", score);
return 0;
}
int deal_a_card()
{
int r;
int s;
r= rand();
int rank =(r%13)+1;
s= rand();
int suit =(r%4)+1;
switch (rank)
{
case 1:
printf("Ace");
break;
case 11:
printf("Jack");
break;
case 12:
printf("Queen");
break;
case 13:
printf("King");
break;
default:
printf("%d", rank);
break;
}
switch (suit)
{
case 1:
printf(" Of Hearts");
break;
case 2:
printf(" Of Spades");
break;
case 3:
printf(" Of Clubs");
break;
case 4:
printf(" Of Diamonds");
break;
}
return rank;
}
int rank_to_score (int rank)
{
switch (rank)
{
case 11:
case 12:
case 13:
return 10;
break;
default:
return rank;
}
}