7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the ca
ID: 3913025 • Letter: 7
Question
7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing
function deals a five-card poker hand. Then write the following additional functions:
a) Determine whether the hand contains a pair.
b) Determine whether the hand contains two pairs.
320 Chapter 7 C Pointers
c) Determine whether the hand contains three of a kind (e.g., three jacks).
d) Determine whether the hand contains four of a kind (e.g., four aces).
e) Determine whether the hand contains a flush (i.e., all five cards of the same suit).
f) Determine whether the hand contains a straight (i.e., five cards of consecutive face values).
7.13 (Project: Card Shuffling and Dealing) Use the functions developed in Exercise 7.12 to
write a program that deals two five-card poker hands, evaluates each, and determines which is the
better hand.
I need the 7.13 please. Not 7.12 .
Thank you
// Fig. 7.24: fig07_24.c
// Card shuffling and dealing.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
// prototypes
void shuffle(unsigned int wDeck[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[]); // dealing doesn't modify the arrays
int main(void)
{
// initialize deck array
unsigned int deck[SUITS][FACES] = {0};
srand(time(NULL)); // seed random-number generator
shuffle(deck); // shuffle the deck
// initialize suit array
const char *suit[SUITS] =
{"Hearts", "Diamonds", "Clubs", "Spades"};
// initialize face array
const char *face[FACES] =
{"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
deal(deck, face, suit); // deal the deck
}
// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES])
{
// for each of the cards, choose slot of deck randomly
for (size_t card = 1; card <= CARDS; ++card) {
size_t row; // row number
size_t column; // column number
// choose new random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
} while(wDeck[row][column] != 0);
// place card number in chosen slot of deck
wDeck[row][column] = card;
}
}
// deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[])
{
// deal each of the cards
for (size_t card = 1; card <= CARDS; ++card) {
// loop through rows of wDeck
for (size_t row = 0; row < SUITS; ++row) {
// loop through columns of wDeck for current row
for (size_t column = 0; column < FACES; ++column) {
// if slot contains current card, display card
if (wDeck[row][column] == card) {
printf("%5s of %-8s%c", wFace[column], wSuit[row],
card % 2 == 0 ? ' ' : ' '); // 2-column format
}
}
}
}
}
Explanation / Answer
include<stdio.h>
#include<stdlib.h>
#include<time.h>
//main functions
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wface[], const char *wSuit[], char *wfSuit[], char *wfFace[] );
//operation functions
void pair( const char *wfSuit[], const char *wfFace[]);
void twoPair( const char *wfSuit[], const char *wfFace[] );
void threeKind( const char *wfSuit[], const char *wfFace[] );
void fourKind( const char *wfSuit[], const char *wfFace[] );
void flush( const char *wfSuit[], const char *wfFace[] );
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] );
//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] );
int main(void){
//initilize suit array
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
//initialize face array
const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
//initialize suit and face array for storing five-hand card
char *fSuit[ 5 ] ;// Hearts
char *fFace[ 5 ] ;// Seven, Ace, Ace, Four, Five
//initialize deck array
int deck[ 4 ][ 13 ] = { 0 };
//seed random number generator
srand( time( 0 ) );
shuffle( deck );//shuffle the deck
deal( deck, face, suit, fSuit, fFace );//deal five-hand card
pair( fSuit, fFace );//compute for pair
twoPair( fSuit, fFace );//compute for two-pair
threeKind( fSuit, fFace );//compute for three of a kind
fourKind( fSuit, fFace );//compute for four of the kind
flush( fSuit, fFace );//compute for flush
straight( fSuit, fFace, face );//compute for straight
system("pause");
return 0;
}
void shuffle( int wDeck[][ 13 ] ){
int row, column, 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[], char *wfSuit[], char *wfFace[] ){
int card, row, column, i;
//deal 5-card poker hand
for(card = 1, i = 0; card <= 5; card++, i++){
for(row = 0; row < 4; row++){
for(column = 0; column < 13; column++){
if(wDeck[ row ][ column ] == card){
wfSuit[ i ] = wSuit[ row ];
wfFace[ i ] = wFace[ column ];
}
}
}
}
//display 5-card poker hand
printf("Your five-card poker hand... ");
for(i = 0; i < 5; i++){
printf("%10s of %-8s ", wfFace[ i ], wfSuit[ i ]);
}
}
void pair(const char *wfSuit[], const char *wfFace[]){
int card, i, j = 0, k;
int pair[ 5 ] = { 0 };
//compute whether the five-hand card contains a pair
printf(" Pair... ");
for(card = 0; card < 5; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
j++;
pair[ card ] = i;
}
}
}
//display result
if(j == 1){
for(card = 0; card < 5; card++){
if(pair[ card ] != 0){
k = pair[ card ];
printf(" %10s of %-8s and %5s of %-8s ", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
}
}
}
else{
printf(" Your five-hand card doesn't contains any pair. ");
}
}
void twoPair( const char *wfSuit[], const char *wfFace[] ){
int card, i, j = 0, k;
int pair[ 5 ] = { 0 };
//compute whether the five-hand card contains two pair
printf(" Two-pair... ");
for(card = 0; card < 5; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
j++;
pair[ card ] = i;
}
}
}
//display result
if(j == 2){
for(card = 0; card < 5; card++){
if(pair[ card ] != 0){
k = pair[ card ];
printf(" %10s of %-8s and %5s of %-8s ", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
}
}
}
else{
printf(" Your five-hand card doesn't contains two pair. ");
}
}
void threeKind( const char *wfSuit[], const char *wfFace[] ){
int card, i, j = 0, k, x;
int pair[ 3 ] = { 0 };
//compute whether the five-hand card contains three of a kind
printf(" Three of a kind... ");
for(card = 0; card < 3; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
for(x = i + 1; x < 5; x++){
if(wfFace[ i ] == wfFace[ x ]){
j++;
pair[ 0 ] = card;
pair[ 1 ] = i;
pair[ 2 ] = x;
}
}
}
}
}
//display result
if(j == 1){
for(card = 0; card < 3; card++){
k = pair[ card ];
printf(" %10s of %-8s ", wfFace[ k ], wfSuit[ k ] );
}
}
else{
printf(" Your five-hand card doesn't contains three of a kind. ");
}
}
void fourKind( const char *wfSuit[], const char *wfFace[] ){
int card, i, indi = 0, pair[ 5 ] = { 0 };
//compute whether your five-hand card contains four-of-a-kind
printf(" Four of a kind... ");
for(card = 0; card < 2; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
indi++;
if(indi == 1)
pair[ i - 1 ] = 1;
pair[ i ] = 1;
}
}
if(indi == 3)
break;
else if(indi > 3)
break;
else{
indi = 0;
for(i = 0; i < 5; i++)
pair[ i ] = 0;
}
}
//display result
if(indi == 3){
for(i = 0; i < 5; i++){
if( pair[ i ] == 1)
printf(" %5s of %-9s ", wfFace[ i ], wfSuit[ i ] );
}
printf(" ");
}else{
printf(" Your five-hand card doesn't contains four-of-a-kind. ");
}
}
void flush ( const char *wfSuit[], const char *wfFace[] ){
int i, j = 0;
//compute whether your five-hand card contains a flush
printf(" Flush... ");
for(i = 1; i < 5; i++){
if(wfSuit[ 0 ] == wfSuit[ i ] )
j++;
}
//display result
result( j, 0, wfFace, wfSuit, " Not flush! " );
}
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] ){
int i, j, k, pass, count, hold, check = 1;
int faceValue[ 5 ] = { 0 };
printf(" Straight... ");
//locate face value and store in an array
for(i = 0 ; i < 5; i++){
for(j = 0 ; j < 13; j++){
if(wfFace[ i ] == wFace[ j ]){
faceValue[ i ] = j;
}
}
}
//sort face value in ascending order using bubble sort
for(pass = 0; pass < 4; pass++){
for(count = 0; count < 4; count++){
if(faceValue[ count ] > faceValue[ count + 1 ]){
//swap
hold = faceValue[ count ];
faceValue[ count ] = faceValue[ count + 1 ];
faceValue[ count + 1 ] = hold;
}
}
}
//check if the hand contains a straight
for(i = 0; i < 4; i++){
if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
check = 0;
}
}
//display result
result( check, 1, wfFace, wfSuit, " Not a straight hand. " );
printf(" ");
}
//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] ){
int i;
if(compare == compareTo){
for(i = 0; i < 5; i++){
printf(" %5s of %-8s ", wfFace[ i ], wfSuit[ i ] );
}
}
else{
printf("%s", statement );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.