G

#### gdotone

allocating any memory, malloc, calloc , I don't believe there is a memory

leak, but as I'm learning...

I find if I run the program over and over again it compiles find but it does not

print as fast as it does over the first few runs. The hand dealt, when finally

present not have any matching face cards.

Also, up to this point in the book structures, or unions have not been discussed.

//

// main.c

// fig07_24.c

//

// C How to Program by Dietel and Dietel

// with modification by gdotone

/* This program comes from the exercises found in Dietel and Dietel

* C How to program. The execise calls for the modification of a program

* found in fig. 7.24, of the book. The card dealing function is to deal a 5 card

* poker hand. The sub-exercises ask for the hand to be evaluated, to

* find a pair, find two of a kind, three of a kind, four of a kind,

* straight or flush.

*

* This program impliments the first four objectives so far.

*

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define NUMBEROFCARDSINHAND 5

/* prototypes */

void shuffle( int wDeck[][ 13 ] );

void deal( const int wDeck[][ 13], const char *wFace[],

const char *wSuit[], int myHand[5][2] );

int isThere_A_Pair_In ( int myHand[][2]);

int isThere_Two_Pair_In ( int myHand[][2] );

int isThere_Three_Of_A_Kind_In ( int myHand[][2] );

int isThere_Four_Of_A_Kind_In ( int myHand[][2] );

// void printHand( int myHand[5][2], const char *wSuit[], const char *Face[] );

/***************************** main () ***************************************/

int main(int argc, const char * argv[])

{

/* initialize suit array */

const char *suit[ 4 ] = { "Hearts", "Dimonds", "Clubs", "Spades" };

/* initialize face array */

const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",

"Five", "Six", "Seven", "Eight",

"Nine", "Ten", "Jack", "Queen", "King" };

/* initialize deck array */

int deck[ 4 ] [13 ] = { 0 };

/* intialize hand */

int myHand[5][2] = {0};

srand( (unsigned int) time( 0 ) ); /* seed random-number generator */

shuffle( deck ); /* shuffle the deck */

/* deal the deck */

deal( deck, face, suit, myHand );

/* check poker hand */

isThere_A_Pair_In ( myHand );

isThere_Two_Pair_In ( myHand );

isThere_Three_Of_A_Kind_In ( myHand );

isThere_Four_Of_A_Kind_In ( myHand );

// printHand( myHand, suit, face ); /* use as test to see if hand is dealt */

return 0;

}

/******************************** shuffle () *********************************/

/* suffle cards in deck */

void shuffle( int wDeck[][ 13 ] )

{

int row; /* row */

int column; /* colum number */

int card; /* counter */

/* for each of the 52 cards, choose slot of deck randomly */

for ( card = 1; card <= 52; card++ )

{

/* choose new random location until unoccupide slot found */

do {

row = rand() % 4;

column = rand() % 13;

} while ( wDeck[row][column] != 0 ); /* end do ... while */

/* place card number in chosen slot of deck */

wDeck[ row ][ column ] = card;

} /* end for ( card = 1 ... ) */

} /* end shuffle */

/********************************* deal() ************************************/

/* deal cards in deck */

void deal( const int wDeck[][ 13 ], const char *wFace[],

const char *wSuit[], int myHand[][2] )

{

int card; /* card counter */

int row; /* row counter */

int column; /* column counter */

int t = 0; /* t used as row index for myHand */

/* deal 5 cards, poker hand */

for ( card = 1; card <= NUMBEROFCARDSINHAND; card++ )

{

/* loop through rows of wDeck */

for ( row = 0; row <= 3; row++ )

{

/* loop through columns of wDeck for current row */

for ( column = 0; column <= 12; column++ )

{

/* if slot contains current card, display card */

if ( wDeck[row][ column] == card)

{

myHand[t][0] = column;

myHand[t][1] = row;

printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ],

card % 2 == 0 ? '\n' : '\t' );

t++;

}

}/* end for ( column = 0 ...) */

} /* end for ( row = 0 ...) */

} /* end for ( card = 1 ...) */

} /* end void deal(... ) */

/********************* isThere_A_Pain_In () ******************************/

int isThere_A_Pair_In ( int myHand[][2] )

{

int strenghtOfHand;

int col = 0;

for ( int i = 0; i < NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHand

*[col] == myHand[j][col] )*

{

printf( "\n\nFound a pair\n");

return strenghtOfHand = 1;

} /* found a pair */

return strenghtOfHand = 0; /* no pair found */

} /* end isThere_A_Pair_In () */

/********************* isThere_Two_Pair_In () ******************************/

int isThere_Two_Pair_In ( int myHand[][2] )

{

int col = 0;

int numberOfPairs = 0; /* number of pairs found in hand */

int strenghtOfHand;

/* look for first pair */

for (int i = 0; i < NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHand

{

printf( "\n\nFound a pair\n");

return strenghtOfHand = 1;

} /* found a pair */

return strenghtOfHand = 0; /* no pair found */

} /* end isThere_A_Pair_In () */

/********************* isThere_Two_Pair_In () ******************************/

int isThere_Two_Pair_In ( int myHand[][2] )

{

int col = 0;

int numberOfPairs = 0; /* number of pairs found in hand */

int strenghtOfHand;

/* look for first pair */

for (int i = 0; i < NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHand

*[col] == myHand[j][col] )*

{

numberOfPairs++;

/* looking for the next pair */

for ( int k = j + 1; k < NUMBEROFCARDSINHAND; k++ )

for ( int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )

if( ( myHand[k][col] == myHand[m][col])

&& ( myHand[k][col] != myHand[j][col]))

{

numberOfPairs++;

printf( "This hand has two of a kind" );

return strenghtOfHand = 2;

}

} /* end if ( myhand{

numberOfPairs++;

/* looking for the next pair */

for ( int k = j + 1; k < NUMBEROFCARDSINHAND; k++ )

for ( int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )

if( ( myHand[k][col] == myHand[m][col])

&& ( myHand[k][col] != myHand[j][col]))

{

numberOfPairs++;

printf( "This hand has two of a kind" );

return strenghtOfHand = 2;

}

} /* end if ( myhand

*[0] ... ) */*

return strenghtOfHand = 0; /* two pair not found */

} /* end isThere_Two_Pair_In() */

/********************* isThere_Three_Of_A_Kind_In () ***********************/

int isThere_Three_Of_A_Kind_In ( int myHand[][2] )

{

int strenghtOfHand;

int col = 0; /* need not change, only looking at the face of the card */

for ( int i = 0; i <= NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHandreturn strenghtOfHand = 0; /* two pair not found */

} /* end isThere_Two_Pair_In() */

/********************* isThere_Three_Of_A_Kind_In () ***********************/

int isThere_Three_Of_A_Kind_In ( int myHand[][2] )

{

int strenghtOfHand;

int col = 0; /* need not change, only looking at the face of the card */

for ( int i = 0; i <= NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHand

*[col] == myHand[j][col] )*

{

/* find next card of same kind */

for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)

{

if ( myHand{

/* find next card of same kind */

for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)

{

if ( myHand

*[col] == myHand[k][col]) /* found third kind */*

{

printf( "\nThere are three of a kind in hand\n");

return strenghtOfHand = 3;

} /* end if ( myHand{

printf( "\nThere are three of a kind in hand\n");

return strenghtOfHand = 3;

} /* end if ( myHand

*[col] ... ) */*

} /* end for ( int k = j + 1; ... */

} /* end if ( myHand} /* end for ( int k = j + 1; ... */

} /* end if ( myHand

*[col]...) */*

return strenghtOfHand = 0; /* no three of a kind */

} /* end isThere_Three_Of_A_Kind_In () */

/********************* isThere_Four_Of_A_Kind_In () ************************/

int isThere_Four_Of_A_Kind_In ( int myHand[][2] )

{

int strenghtOfHand;

int col = 0;

for ( int i = 0; i <= NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHandreturn strenghtOfHand = 0; /* no three of a kind */

} /* end isThere_Three_Of_A_Kind_In () */

/********************* isThere_Four_Of_A_Kind_In () ************************/

int isThere_Four_Of_A_Kind_In ( int myHand[][2] )

{

int strenghtOfHand;

int col = 0;

for ( int i = 0; i <= NUMBEROFCARDSINHAND; i++ )

for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )

if ( myHand

*[col] == myHand[j][col] )*

{

/* find next card of same kind */

for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)

{

if ( myHand{

/* find next card of same kind */

for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)

{

if ( myHand

*[col] == myHand[k][col]) /* found third kind */*

{

/* find next card of same kind */

for (int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )

if ( myHand{

/* find next card of same kind */

for (int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )

if ( myHand

*[col] == myHand[m][col] )*

{

printf( "This hand has four of a kind");

return strenghtOfHand = 4;

}

} /* end if ( myHand{

printf( "This hand has four of a kind");

return strenghtOfHand = 4;

}

} /* end if ( myHand

*[col] ... ) */*

} /* end for (int k = j + 1 ... ) */

} /* end if ( myHand} /* end for (int k = j + 1 ... ) */

} /* end if ( myHand

*[col] ... ) */*

return strenghtOfHand = 0; /* four of a kind not found in hand */

} /* end isThere_Four_Of_A_Kind_In () */

/***************************** printHand() ***********************************/

/* used to test hand, check what is dealt */

void printHand( int myHand[5][2], const char *wSuit[], const char *wFace[] )

{

int col = 0; /* column relates to suit of card */

printf ("\n\n");

for (int row = 0; row < NUMBEROFCARDSINHAND; row++)

printf (" %-6s of %-8s\n", wFace[ myHand[row][col] ],

wSuit[ myHand[row][col + 1] ]);

printf("\n");

}return strenghtOfHand = 0; /* four of a kind not found in hand */

} /* end isThere_Four_Of_A_Kind_In () */

/***************************** printHand() ***********************************/

/* used to test hand, check what is dealt */

void printHand( int myHand[5][2], const char *wSuit[], const char *wFace[] )

{

int col = 0; /* column relates to suit of card */

printf ("\n\n");

for (int row = 0; row < NUMBEROFCARDSINHAND; row++)

printf (" %-6s of %-8s\n", wFace[ myHand[row][col] ],

wSuit[ myHand[row][col + 1] ]);

printf("\n");

}