void sortHand( char * const);
void swap (int * const, int * const);
// DeckOfCards default constructor initializes deck
DeckOfCards::DeckOfCards()
{
// initialize suit and face arrays
char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
for ( int i = 0; i < 4; i++ )
suit[ i ] = suitValue[ i ];
for ( int i = 0; i < 13; i++ )
face[ i ] = faceValue[ i ];
// loop through rows of deck
for ( int row = 0; row <= 3; row++ )
{
// loop through columns of deck for current row
for ( int column = 0; column <= 12; column++ )
{
deck[ row ][ column ] = 0; // initialize slot of deck to 0
} // end inner for
} // end outer for
srand( time( 0 ) ); // seed random number generator
} // end DeckOfCards default constructor
// shuffle cards in deck
void DeckOfCards::shuffle()
{
int row; // represents suit value of card
int column; // represents face value of card
// for each of the 52 cards, choose a slot of the deck randomly
for ( int card = 1; card <= 52; card++ )
{
do // choose a new random location until unoccupied slot is found
{
row = rand() % 4; // randomly select the row
column = rand() % 13; // randomly select the column
}
while( deck[ row ][ column ] != 0 ); // end do...while
// place card number in chosen slot of deck
deck[ row ][ column ] = card;
} // end for
} // end function shuffle
// Deal a five card poker hand.
void DeckOfCards::deal()
{
int handPos = 0; // Hand Position
cout << "\nTHE PLAYER'S HAND:\n\n";
sortHand( faceValue ); // Sort the array
for ( int card = 1; card < 6; card++ )
{
for ( int row = 0; row <= 3; row++ )
{
for ( int column = 0; column <= 12; column++ )
{
if ( deck[ row ][ column ] == card )
{
hand[ handPos ][ 0 ] = row;
hand[ handPos ][ 1 ] = column;
cout << setw( 5 ) << face[ column ] << (" of ")
<< setw( 8 ) << left << suit[ row ] << endl;
handPos++;
} // end if statement
} // end for loop
} // end for loop
} // end for loop
} // end deal
// See if the deck of cards contains a sraight.
void DeckOfCards::straight()
{
for ( int i = 0; i < 4; i++ )
{
if ( hand[i][0] == 1 && hand[i][1] == 2 && hand[i][2] == 3 && hand[i][3] == 4 && hand[i][4] == 5 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 2 && hand[i][1] == 3 && hand[i][2] == 4 && hand[i][3] == 5 && hand[i][4] == 6 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 3 && hand[i][1] == 4 && hand[i][2] == 5 && hand[i][3] == 6 && hand[i][4] == 7 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 4 && hand[i][1] == 5 && hand[i][2] == 6 && hand[i][3] == 7 && hand[i][4] == 8 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 5 && hand[i][1] == 6 && hand[i][2] == 7 && hand[i][3] == 8 && hand[i][4] == 9 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 6 && hand[i][1] == 7 && hand[i][2] == 8 && hand[i][3] == 9 && hand[i][4] == 10 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 7 && hand[i][1] == 8 && hand[i][2] == 9 && hand[i][3] == 10 && hand[i][4] == 11 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 8 && hand[i][1] == 9 && hand[i][2] == 10 && hand[i][3] == 11 && hand[i][4] == 12 )
{
cout << ("\nThe hand contains a straight.\n");
}
else if ( hand[i][0] == 9 && hand[i][1] == 10 && hand[i][2] == 11 && hand[i][3] == 12 && hand[i][4] == 13 )
{
cout << ("\nThe hand contains a straight.\n");
}
}
}
void sortHand( char * const faceValue)
{
int smallest;
// loop over size - 1 elements
for ( int i = 0; i < 13; i++ );
{
smallest = i; // first index of remaining array
// loop to find index of smallest element
for ( int index = i + 1; index < 13; index++ )
if ( faceValue [index] < faceValue [smallest] )
smallest = index;
swap( &faceValue [ i ], &faceValue[ smallest ] );
}
}
void swap ( char * const element1Ptr, char * const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}