C Program: Shuffle a deck, deal, and determine winners. Problem 2: Card Shufflin
ID: 3705817 • Letter: C
Question
C Program: Shuffle a deck, deal, and determine winners.
Problem 2: Card Shuffling: Entertainment Industry: Using random number generation, and multi-dimensional array/structure concepts: deal 52 cards into 4 different hands (P, Q, R, and S), 13 cards each hand. The 4 colors are Spades, Hearts, Diamonds and Clubs. 13 cards are: 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 4 times [75% of the time] (d) Extend the game to 8 players (P,Q,R, S, T, U, V, W) 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) Clearly explain in your report, how you are accomplishing these tasks. (e)Explanation / Answer
SourceCode:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void shuffle( int wdeck[][13]);
void deal( const int wdeck[][13], const char *wface[],
const char *wsuit[]);
void pair( int a[]);
int main()
{
const char *suit[4] = {"H", "D", "C", "S"};
const char *face[13] = {"A", "2", "3", "3", "5",
"6", "7", "8", "9", "10", "J", "Q", "K"};
int deck[4][13] ={0};
srand( time(0));
shuffle(deck);
deal(deck, face, suit);
return 0;
}
void shuffle(int wdeck[][13])
{
int row;
int column;
int card;
for(card = 1; card <= 52; card++) {
do {
row = rand() % 4;
column = rand() % 13;
}while(wdeck [row] [column] !=0);
wdeck [row] [column] = card;
}
}
void deal(const int wdeck[][13], const char *wface[],
const char *wsuit[])
{
int card;
int row;
int column;
for ( card = 1; card <= 52; card++ ) {
for ( row = 0; row <= 3; row++ ){
for ( column = 0; column <=12; column++ ){
if ( wdeck[row][column] == card ) {
printf("%5s%-8s% ", wface[column], wsuit[row],
card % 52 == 0 );
}
}
}
}_getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.