#include using namespace std; // A poker calculation on flushing enum suit { clubs, diamonds, hearts, spades }; // pips A, 2, ..., 10, J, Q, K, represented by integers // 1, 2, ..., 13, respectively. typedef int pips; struct card { suit s; pips p; }; // a deck has 52 cards: 13 clubs, 13 diamonds, 13 hearts // and 13 spades struct deck { card d[52]; }; // determine the pip from a card's integer representation pips assign_pips(int n) { return (n % 13 + 1); } // determine the suit from an integer suit assign_suit(int n) { return (static_cast(n / 13)); } // given an integer in [1, 52], assign the suit and pip // to the card that it represents void assign(int n, card& c) { c.s = assign_suit(n); c.p = assign_pips(n); } pips get_pip(card c) { return c.p; } suit get_suit(card c) { return c.s; } // print a card in the form of void print_card(card c) { cout << c.p; switch(c.s) { case clubs: cout << "C "; break; case diamonds: cout << "D "; break; case hearts: cout << "H "; break; case spades: cout << "S "; } } // print out all cards in a deck void print_deck(deck& dk) { for (int i = 0; i < 52; i++) print_card(dk.d[i]); } // shuffle a deck using the library-supplied pseudorandom // number generator rand() to exchange cards for every // deck position. void shuffle(deck& dk) { card t; for (int i = 0; i < 52; ++i) { int k = (rand() % (52 - i)); // rand() generates a random integer // swap two cards t = dk.d[i]; dk.d[i] = dk.d[k]; dk.d[k] = t; } } // the deal function takes cards in sequence from the deck // and arrange them into hands void deal(int n, int pos, card* hand, deck& dk) { for (int i = pos; i < pos + n; ++i) hand[i - pos] = dk.d[i]; } // initiate a deck, assigning pips and suits void init_deck(deck& dk) { for (int i = 0; i < 52; i++) assign(i, dk.d[i]); }