Thanks everyone. It seems to me the best way for me to learn is
to ask for feedback from the actual code, too. I want to thank all of
you for this assistance. This is great.
I think the program does what the exercise ask for.
// C How to Program by Dietel and Dietel
// Exercise 7.13
//
//
// gdotone
/* This program comes from the exercises found in Dietel and Dietel
* C How to program. The execise calls for the modification of program
* found in fig. 7.24. The card dealing function is to deal two
* poker hands. Evaluate each hand and determine which hand is better.
*
* In this program Aces are not high. Dealers choice.

*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBEROFPLAYERS 2 /* changing this value does not add players */
#define NUMBEROFCARDSINHAND 5
/* prototypes */
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13], const char *wFace[],
const char *wSuit[], int aHand[][2], int otherHand[][2] );
int whichHandWins( int myHand[][2], int otherHand[][2] );
int isThere_A_Pair_In ( const int aHand[][2], int *kind );
int isThere_Two_Pair_In ( const int aHand[][2], int *kind );
int isThere_Three_Of_A_Kind_In ( const int aHand[][2], int *kind );
int isThere_Four_Of_A_Kind_In ( const int aHand[][2], int *kind );
int isThere_A_Flush_In ( const int aHand[][2] );
void bubbleSort( int aHand[][2] );
int isThere_A_Straight_In ( int aHand[][2]);
void printHand( const int aHand[][2], const char *wSuit[],
const char *Face[], int );
/***************************** 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" };
/* declare and initialize deck array */
int deck[ 4 ] [13 ] = { 0 };
/* declare and intialize hand */
int playerOnesHand[5][2] = {0};
int playerTwosHand[5][2] = {0};
int playerOne = 1;
int playerTwo = 2;
/* declare and initialize pot */
int takeThePot = 0;
srand( (unsigned int) time( 0 ) ); /* seed random-number generator */
shuffle( deck ); /* shuffle the deck */
/* deal the Cards */
deal( deck, face, suit, playerOnesHand, playerTwosHand );
/* determine which hand wins the pot */
takeThePot = whichHandWins( playerOnesHand, playerTwosHand );
/* print winning hand and message */
if ( takeThePot == playerOne )
printHand (playerOnesHand, suit, face, playerOne);
else if ( takeThePot == playerTwo )
printHand ( playerTwosHand, suit, face, playerTwo );
else
printf("\n %s \n" ,
"Money stays in pot. Keep all hands above the table");
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 aHand[][2], int anotherHand[][2] )
{
int card; /* card counter */
int row; /* row counter */
int column; /* column counter */
int t = 0; /* t used as row index for myHand */
int u = 0; /* u used as row index for otherHand */
/* deal cards, poker hand */
for ( card = 1; card <= NUMBEROFCARDSINHAND * NUMBEROFPLAYERS; 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)
{
if ( card % 2 )
{
aHand[t][0] = column;
aHand[t][1] = row;
printf( "%5s of %-8s\t", wFace[ column ], wSuit[ row ] );
t++;
} /* end if ( card % 2 ) */
else
{
anotherHand
[0] = column;
anotherHand[1] = row;
printf( "%5s of %-8s\n", wFace[ column ], wSuit[ row ] );
u++;
} /* end else ( card % 2 ) */
}
}/* end for ( column = 0 ...) */
} /* end for ( row = 0 ...) */
} /* end for ( card = 1 ...) */
printf("\n");
} /* end void deal(... ) */
/********************* isThere_A_Pain_In () ******************************/
int isThere_A_Pair_In (const int aHand[][2], int *kind )
{
int strenghtOfHand;
int col = 0;
for ( int i = 0; i < NUMBEROFCARDSINHAND; i++ )
for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )
if ( aHand[col] == aHand[j][col] )
{
*kind = aHand[col];
/*
* printf( "Found a pair\n");
* printf( " %d\n", *kind );
*/
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 ( const int aHand[][2], int *kind )
{
int col = 0;
int numberOfPairs = 0; /* number of pairs found in hand */
int strenghtOfHand;
int firstPair, secondPair;
/* look for first pair */
for (int i = 0; i < NUMBEROFCARDSINHAND; i++ )
for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )
if ( aHand[col] == aHand[j][col] )
{
numberOfPairs++;
firstPair = aHand[col];
/* looking for the next pair */
for ( int k = i + 1; k < NUMBEROFCARDSINHAND; k++ )
for ( int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )
if( ( aHand[k][col] == aHand[m][col])
&& ( aHand[k][col] != aHand[j][col]))
{
numberOfPairs++;
secondPair = aHand[k][col];
/* printf( "This hand has two of a kind" ); */
if (firstPair > secondPair)
*kind = firstPair;
else if (firstPair < secondPair)
*kind = secondPair;
else
*kind = firstPair; /* or secondPair, both are equal */
return strenghtOfHand = 2;
}
} /* end if ( ahand[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 ( const int aHand[][2], int *kind )
{
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 ( aHand[col] == aHand[j][col] )
{
/* find next card of same kind */
for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)
{
if ( aHand[col] == aHand[k][col]) /* found third kind match */
{
*kind = aHand[col];
/* printf( "\nThere are three of a kind in hand\n"); */
return strenghtOfHand = 3;
} /* end if ( aHand[col] ... ) */
} /* end for ( int k = j + 1; ... */
} /* end if ( aHand[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 ( const int aHand[][2], int *kind )
{
int strenghtOfHand;
int col = 0;
for ( int i = 0; i <= NUMBEROFCARDSINHAND; i++ )
for ( int j = i + 1; j < NUMBEROFCARDSINHAND; j++ )
if ( aHand[col] == aHand[j][col] )
{
/* find next card of same kind */
for (int k = j + 1; k < NUMBEROFCARDSINHAND; k++)
{
if ( aHand[col] == aHand[k][col]) /* found third kind */
{
/* find next card of same kind */
for (int m = k + 1; m < NUMBEROFCARDSINHAND; m++ )
if ( aHand[col] == aHand[m][col] )
{
*kind = aHand[col];
/* printf( "This hand has four of a kind"); */
return strenghtOfHand = 4;
}
} /* end if ( aHand[col] ... ) */
} /* end for (int k = j + 1 ... ) */
} /* end if ( aHand[col] ... ) */
return strenghtOfHand = 0; /* four of a kind not found in hand */
} /* end isThere_Four_Of_A_Kind_In () */
/************************** isThere_A_Flush_In () ****************************/
int isThere_A_Flush_In ( const int aHand[][2] )
{
int strenghtOfHand;
int col = 1;
for ( int j = 2; j < NUMBEROFCARDSINHAND ; j++ )
{
if ( aHand[0][col] != aHand[j][col] ) /* each kind must be the same */
return strenghtOfHand = 0;
/* kind = hearts, spades, etc. */
} /* end for */
/* printf( "\nI've got a flush\n"); */
return strenghtOfHand = 5;
} /* end isThere_A_Flush_In() */
/************************* isThere_A_Straight () *****************************/
/* A flush must exist, all cards of the same suit,
* if a flush exist, put hand in face value order
* Aces low (dealers choice)
*/
int isThere_A_Straight_In ( int aHand[][2] )
{
int col_1 = 1; /* is used to check kind between cards */
int col_2 = 0; /* is used to check face value */
int strenghtOfHand;
int difference = 0;
for ( int j = 1; j < NUMBEROFCARDSINHAND ; j++ )
{
if ( aHand[0][col_1] != aHand[j][col_1] ) /* each kind must be the same */
return strenghtOfHand = 0;
} /* end for ( j = 1; ... ) */
bubbleSort( aHand );
for (int i = 0; i < NUMBEROFCARDSINHAND - 1; i++)
{
difference = aHand[i+1][col_2] - aHand[col_2];
if (difference != 1 )
{
return strenghtOfHand = 0;
} /* end if (difference ... ) */
} /* for ( int i = 0; ...) */
/*
* printf ( "%s\n%s\n", "just put your money on in my Pocket",
" no need to rest it on the table");
*/
return strenghtOfHand = 6;
} /* end isThere_A_Straight () */
/************************* bubblesort() *************************************/
/* This bubble sort is taken from Dietel and Dietel fig 6.16 and modified
* to handle a 2-D array cosisting of two columns.
*/
void bubbleSort( int aHand[][2] )
{
int pass; /* pass counter */
int j; /* comparison counter */
int hold; /* temporary location used to swap elements */
int col = 0;
/* loop to control number of passes */
for ( pass = 1; pass < NUMBEROFCARDSINHAND; pass++ )
{
/* loop to control number of comparisons per pass */
for ( j = 0; j < NUMBEROFCARDSINHAND - 1; j++ )
{
/* swap element if out of order */
if ( aHand[j][col] > aHand[ j + 1 ][col] )
{
hold = aHand[ j ][col];
aHand[ j ][col] = aHand[ j + 1 ][col];
aHand[ j + 1][col] = hold;
} /* end if */
} /* end inner for */
} /* end outer for */
} /* end function bubbleSort */
/***************************** printHand() ***********************************/
void printHand( const int aHand[][2], const char *wSuit[],
const char *wFace[], int player )
{
int col = 0; /* column relates to suit of card */
printf ("\n\n %s\n\n","This hand wins the pot.");
for (int row = 0; row < NUMBEROFCARDSINHAND; row++)
printf (" %-6s of %-8s\n", wFace[ aHand[row][col] ],
wSuit[ aHand[row][col + 1] ]);
printf("\n\n%s",
(player==1)? " Player One Wins": " Player Two Wins");
printf("\n");
}
/******************************** whichHandWins() ****************************/
int whichHandWins( int aHand[][2], int anotherHand[][2] )
{
int evaluateTheHand( int aHand[][2], int *kind );
int aHandsValue;
int anotherHandsValue;
int aHandsKind;
int anotherHandsKind;
/* evaluate hand */
aHandsValue = evaluateTheHand(aHand, &aHandsKind);
anotherHandsValue = evaluateTheHand(anotherHand, &anotherHandsKind);
/* determine which hand wins and return winning hand */
if ( anotherHandsValue > aHandsValue )
return 2; /* anotherHand wins, correponds to second player's hand */
else if ( anotherHandsValue < aHandsValue )
return 1; /* aHand wins, correponds to first player's hand */
else if ( (anotherHandsValue == aHandsValue) && (anotherHandsValue != 0) )
{
/* look at the value of the matching cards */
if ( anotherHandsKind > aHandsKind )
return 2; /* anotherHand wins, correponds to second player's hand */
else if ( anotherHandsKind < aHandsKind )
return 1; /* aHand wins, correponds to first player's hand */
else
return 0; /* otherwise both, ...Kind,kinds are equal */
/* this return not neccessary */
} /* end else if ( anotherHandsValue ... ) */
else
return 0;
}
/****************************** evaluateTheHand () ****************************/
int evaluateTheHand ( int aHand[][2], int *kind )
{
int value = 0;
if ( (value = isThere_A_Straight_In(aHand)) )
{
return value;
}
else if ( (value = isThere_A_Flush_In(aHand)) )
{
return value;
}
else if ( (value = isThere_Four_Of_A_Kind_In(aHand, kind)) )
{
return value;
}
else if ( (value = isThere_Three_Of_A_Kind_In(aHand, kind)) )
{
return value;
}
else if ( (value = isThere_Two_Pair_In(aHand, kind)) )
{
return value;
}
else if( (value = isThere_A_Pair_In(aHand, kind)) )
{
return value;
}
return value;
} /* end int evaluateTheHand ( ... ) */