Take the program written last weekend that creates a deck of cards, shuffles the
ID: 3653544 • Letter: T
Question
Take the program written last weekend that creates a deck of cards, shuffles them, and then lists them out.
Modify the program to 'deal' two hands of cards (two players). Each player will be dealt 5 cards. Deal one to the first
player, then one to second player. Repeat until both players have 5 cards in their hand.
Then compute the total points in each player's hand and determine the winner with the most points. If a player has an ace,
then count that as 11 points.
#include
#include
#include
#define NCARDS 52
#define NPROPS 2
#define NSUITS 4
#define NFACES 13
char* suit[NSUITS]={"hearts","spades","clubs","diamonds"};
char* face[NFACES]={"ace","two","three","four","five","six","seven","eight","nine", "ten","jack","queen","king"};
void PrintCard(int deck[NCARDS][NPROPS], int i);
void InitDeck(int deck[NCARDS][NPROPS]);
void SwapCards(int deck[NCARDS][NPROPS], int src, int dest);
void ShuffleDeck(int deck[NCARDS][NPROPS]);
int GetPlayValue(int deck[NCARDS][NPROPS], int i);
int main()
{
int deck[NCARDS][NPROPS];
int src;
int dest;
int i;
srand(time(NULL));
printf(" Initializing the deck... ");
InitDeck(deck);
printf(" Shuffling the deck... ");
ShuffleDeck(deck);
for (i=0; i<NCARDS; i++)
{
PrintCard(deck,i);
}
return 0;
}
void InitDeck(int deck[NCARDS][NPROPS])
{
int suit;
int face;
int row = 0;
for (suit=0; suit<4; suit++)
for (face=0; face<13; face++)
{
deck[row][0]= suit;
deck[row][1]= face;
row++;
}
}
void ShuffleDeck(int deck[NCARDS][NPROPS])
{
int src, dest, i;
for (i=0; i<NCARDS; i++)
{
src = i;
dest = rand() % NCARDS;
SwapCards(deck, src, dest);
}
}
void SwapCards(int deck[NCARDS][NPROPS], int src, int dest)
{
int temp;
temp = deck[src][0];
deck[src][0] = deck[dest][0];
deck[dest][0] = temp;
temp = deck[src][1];
deck[src][1] = deck[dest][1];
deck[dest][1] = temp;
}
void PrintCard(int deck[NCARDS][NPROPS], int i)
{
int tempsuit;
int tempface;
tempsuit = deck[i][0];
tempface = deck[i][0];
printf(" Card %d = %s of %s - Value = %d", i+1, face[tempface], suit[tempsuit], GetPlayValue(deck,i));
}
int GetPlayValue(int deck[NCARDS][NPROPS], int i)
{
int suit = deck[i][0];
int face = deck[i][0];
int value;
if(face ==0)
return 1;
else if( (face > 0) && (face < 10))
return face+1;
else
return 10;
}
Explanation / Answer
int GetPlayValue(int deck[NCARDS][NPROPS], int i) { int playval, tface; /* assign face value */ tface = deck[i][1]; if(tface==0) playval = 11; //Ace = 11 else if(tfaceRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.