Using random number generation, and multi-dimensional array/structure concepts:
ID: 3847365 • Letter: U
Question
Using random number generation, and multi-dimensional array/structure concepts: deal 52 cards into 6 different hands (M, N, O, P, Q, R), 8 cards each hand. 4 Cards should be left out randomly: The 4 colors are Spades, Hearts, Diamonds and Clubs. 13 cards: A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2. Your software should eliminate any duplication of the cards. (a) Ace has a value of 13; King 12, and so on; Spades value of: 4, Hearts 3, Diamonds 2, and Clubs 1. (b) After each hand is dealt, calculate the total value each hand has in numerical value. (c) Highest score wins. P hand is the dealer and should win at least 3 out of 6 times [50% of the time]. (d) Extend the game to 8 players (M, N, O, P, Q, R, S, T) with 6 cards each and 4 cards remaining at random. Dealer hand P should win at least 3 out of 8 times [37.5% of the time] (e) Clearly explain in your report, how you are accomplishing these tasks.Explanation / Answer
#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);
void dealCards(int deck [NCARDS][NPROPS], int i);
void PrintCardHands(int deck[NCARDS][NPROPS], int i);
int main()
{
int deck[NCARDS][NPROPS];
int i;
srand(time(NULL));
InitDeck(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;
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,i,dest);
}
}
void SwapCards(int deck[NCARDS][NPROPS], int src, int dest)
{
int temp;
temp = deck[dest][0];
deck[dest][0] = deck[src][0];
deck[src][0] = temp;
temp = deck[dest][1];
deck[dest][1] = deck[src][1];
deck[src][1] = temp;
}
void PrintCard(int deck[NCARDS][NPROPS], int i)
{
int tsuit, tface;
tsuit = deck[i][0];
tface = deck[i][1];
printf ("Card Number %d: %s of %s ",i+1, face[tface],suit[tsuit]);
}
int GetPlayValue(int deck[NCARDS][NPROPS], int i)
{ int playval, tface;
tface= deck [i][1];
if (tface==12)
{
playval = 11;
}
else
{
if (tface<10)
{
playval = tface+1;
}
else
{
playval = 10;
}
}
return playval;
}
void dealCards(int deck [NCARDS][NPROPS], int i)
{
int HAND1 [5][2];
int HAND2 [5][2];
int row;
row = 0;
for(i=0;i<5;i++)
{
HAND1 = deck[row];
row++;
HAND2 = deck[row];
row++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.