Code:
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::ios;
using std::setw;
using std::setiosflags;
#include <cstdlib>
#include <ctime>
struct card {
int face;
int suit;
};
const int suit_len = 4;
const int face_len = 13;
const int deck_len = suit_len * face_len;
const int hand_len = 5;
void initialise( card deck[] );
void showcards( card cards[], int num_cards,
const char *suit_names[], const char *face_names[] );
void shuffle( card deck[] );
void deal( card deck[], card hand[] );
void handTester ( card hand[],
const char *suit_names[], const char *face_names[] );
int main()
{
const char *suit[suit_len] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
const char *face[face_len] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
card deck[deck_len];
card hand[hand_len];
srand( time( 0 ) );
initialise( deck );
showcards( deck, 10, suit, face );
shuffle( deck );
showcards( deck, 10, suit, face );
deal( deck, hand );
showcards( hand, 5, suit, face );
handTester( hand, suit, face );
return 0;
}
// puts cards into order in a deck
void initialise ( card deck[] ) {
int index = 0;
for ( int suit = 0 ; suit < suit_len ; suit++ ) {
for ( int face = 0 ; face < face_len ; face++ ) {
deck[index].face = face;
deck[index].suit = suit;
index++;
}
}
}
// just a debug function to show what's going on
void showcards ( card cards[], int num_cards,
const char *suit_names[], const char *face_names[] ) {
for ( int i = 0 ; i < num_cards ; i++ ) {
cout << "card " << i << " is ";
cout << suit_names[cards[i].suit] << ",";
cout << face_names[cards[i].face] << endl;
}
cout << endl;
}
// takes pairs of cards, and swaps them over
void shuffle( card deck[] ) {
for ( int i = 0 ; i < deck_len ; i++ ) {
int pos1 = rand() % deck_len;
int pos2 = rand() % deck_len;
if ( pos1 != pos2 ) {
// swap them over (one small shuffle)
card temp = deck[pos1];
deck[pos1] = deck[pos2];
deck[pos2] = temp;
}
}
}
// simple deal from the start of the deck
// to deal more hands, you would need to remember the last card dealt
void deal( card deck[], card hand[] ) {
for ( int i = 0 ; i < hand_len ; i++ ) {
hand[i] = deck[i];
}
}
// sort cards into face order
int sortface ( const void *a, const void *b ) {
const card *pa = (const card *)a;
const card *pb = (const card *)b;
if ( pa->face < pb->face ) return -1;
if ( pa->face > pb->face ) return 1;
return 0;
}
// sort cards into suit order
int sortsuit ( const void *a, const void *b ) {
const card *pa = (const card *)a;
const card *pb = (const card *)b;
if ( pa->suit < pb->suit ) return -1;
if ( pa->suit > pb->suit ) return 1;
return 0;
}
// I suggest you write a separate function for each possible rule
// like
// bool handHasPair ( card hand[] );
// bool handHasTwoPair ( card hand[] );
// that way, you can focus on each problem in turn, and bring them all
// together in handTester
void handTester ( card hand[],
const char *suit_names[], const char *face_names[] )
{
// sorting into face order will make pairs adjacent to one another
qsort( hand, hand_len, sizeof(card), sortface );
showcards( hand, 5, suit_names, face_names );
for ( int i = 0 ; i < hand_len-1 ; i++ ) {
if ( hand[i].face == hand[i+1].face ) {
cout << "A pair" << endl;
}
}
}