Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take the program written last weekend that creates a deck of cards, shuffles the

ID: 3653103 • 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)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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

- in PrintCard: tempface = deck[i][0] ----> tempface = deck[i][1] - in GetPlayValue: int face = deck[i][0]; ----> int face = deck[i][1]; - in GetPlayValue: if(face ==0) return 1; ----> if(face ==0) return 11; (it's the ace) and 1 blemish - in GetPlayValue: remove the line -- int suit = deck[i][0]; (suit doesn't matter for the value) ----------------- #define NCARDS_PP 5 pl1_points printf(" Player 2 wins "); else printf(" Tie, no one wins "); return 0; } the print order is different from the requirements Cards of player 1 Card 1 = face of suit - Value = xx Card 2 = face of suit - Value = xx Card 3 = face of suit - Value = xx Card 4 = face of suit - Value = xx Card 5 = face of suit - Value = xx Total points for player 1 = xx Cards of player 2 Card 1 = face of suit - Value = xx Card 2 = face of suit - Value = xx Card 3 = face of suit - Value = xx Card 4 = face of suit - Value = xx Card 5 = face of suit - Value = xx Total points for player 2 = xx Player 1 wins ..or.. Player 2 wins ..or.. Tie, no one wins edit correction of the function ShuffleDeck. it could be that src and dest is the same because dest is generated randomly. in this case a call of SwapCards makes no sense. void ShuffleDeck(int deck[NCARDS][NPROPS]) { int src, dest; for (src=0; src
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote