Take the program written last weekend that creates a deck of cards, shuffles the
ID: 3653116 • 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.
Print out the winning player total points and then his cards.
Then print out the losing player total points and his cards.
(BELOW IS THE CODE I HAVE FOR LAST WEEKEND)
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NCARDS 52
#define NPROPS 2
#define NSUITS 4
#define NFACES 13
#define NHANDS 2
#define NCARDSHAND 5
// card text values using array of pointers to preinitialized constant text strings
char* suit[NSUITS]={"hearts","spades","clubs","diamonds"};
char* face[NFACES]={"ace","two","three","four","five","six","seven","eight","nine",
"ten","jack","queen","king"};
// function prototypes used for manipulating cards
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);
void dealcards(int n, int hands[][NCARDSHAND][NPROPS],int deck[][NPROPS]);
void getscores(int hands[][NCARDSHAND][NPROPS]);
int main()
{
//deck of cards
// face, suite, card value
int deck[NCARDS][NPROPS];
int hands[NHANDS][NCARDSHAND][NPROPS];
int i,j;
srand(time(NULL));
// init the deck
// loop on the chards
InitDeck(deck);
ShuffleDeck(deck);
// print the deck
puts("The shuffled deck is:");
for (i=0; i<NCARDS; i++)
{
PrintCard(deck,i);
}
dealcards(NHANDS,hands,deck);
getscores(hands);
system("PAUSE");
return 0;
}
void getscores(int hands[][NCARDSHAND][NPROPS])
{
int i,j,scores[NHANDS],winner,loser ;
for(j=0;j<NHANDS;j++)
{scores[j]=0;
for(i=0;i<NCARDSHAND;i++)
{
scores[j]+=GetPlayValue(hands[j],i);
}
}
if(scores[0]>scores[1])
{winner=0;
loser=1;
}
else
{winner=1;
loser=0;
}
printf("Winner ");
for(i=0;i<NCARDSHAND;i++)
{
PrintCard(hands[winner],i);
}
printf("Score: %d ",scores[winner]);
printf("Loser ");
for(i=0;i<NCARDSHAND;i++)
{
PrintCard(hands[loser],i);
}
printf("Score: %d ",scores[loser]);
}
void dealcards(int n,int hands[][NCARDSHAND][NPROPS],int deck[][NPROPS])
{int i,j,card=0;
for(i=0;i<NCARDSHAND;i++)
for(j=0;j<n;j++)
{hands[j][i][0]=deck[card][0];
hands[j][i][1]=deck[card][1];
card++;
}
return;
}
void InitDeck(int deck[NCARDS][NPROPS])
{
int row, suit, face;
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 i, dest, src;
for(i=0; i<NCARDS; i++)
{
dest = (NCARDS-1)-i;
src = rand()%NCARDS;
SwapCards(deck, src, dest);
}
}
void SwapCards(int deck[NCARDS][NPROPS], int src, int dest)
{
int tcardVal, i;
// printf("Swap src %d, dest %d ",src, dest);
for (i=0; i<NPROPS; i++)
{
tcardVal = deck[dest][i];
deck[dest][i] = deck[src][i];
deck[src][i] = tcardVal;
}
}
void PrintCard(int deck[NCARDS][NPROPS], int i)
{
int tface, tsuit, playval;
printf(" Card %d ",i);
tsuit = deck[i][0];
tface = deck[i][1];
playval = GetPlayValue(deck, i);
printf(" %s of %s Playvalue = %d ", face[tface], suit[tsuit], playval);
// printf(" %d of %d Playvalue = %d ", tface, tsuit, playval);
}
int GetPlayValue(int deck[NCARDS][NPROPS], int i)
{
int playval, tface;
/* assign face value */
tface = deck[i][1];
if(tface<10)
playval = tface+1; //Ace = 1
else
playval = 10; //tface = 1
if(tface==1)
playval+=10;
return playval;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.