#include #include #include using namespace std; const int Decksize = 52; const int HandSize = 5; // function prototypes void createDeck(int*); void shuffle(int deck[]); void swap(int& a, int& b); void deal(int[], int hand[], int numberOfCards = HandSize); int value(int card); int suit(int card); void sort(int a[], int size); bool isAStraight(int hand[]); bool isAFlush(int hand[]); // overloaded functions void print(int); // print a card void print(const int [], int numberOfCard = Decksize); // print the deck string getSumOfFrequencyDistributionsForHand(int hand[]); int main() { int deck[Decksize]; int hand[5]; createDeck(deck); //print(deck); cout << "--------------------------" << endl; shuffle(deck); print(deck, 15); cout << "==========================" << endl; deal(deck,hand); print(hand,HandSize); cout << "==========================" << endl; deal(deck,hand); print(hand,HandSize); cout << "You have " << getSumOfFrequencyDistributionsForHand(hand) << endl; cout << "==========================" << endl; int fullhouse[] = {7,20,33,10,23}; print(fullhouse,HandSize); cout << "You have " << getSumOfFrequencyDistributionsForHand(fullhouse) << endl; cout << "==========================" << endl; int straight[] = {17,31,19,15,42}; print(straight,HandSize); cout << (isAStraight(straight) ? "is a straight" : "is not a straight") << endl; cout << "==========================" << endl; int almostastraight[] = {17,31,18,15,42}; print(almostastraight,HandSize); cout << (isAStraight(almostastraight) ? "is a straight" : "is not a straight") << endl; cout << "==========================" << endl; int flush[] = {23,3,7,15,31}; print(flush,HandSize); cout << (isAFlush(flush) ? "is a flush" : "is not a flush") << endl; cout << "==========================" << endl; int almostaflush[] = {23,3,17,15,31}; print(almostaflush,HandSize); cout << (isAFlush(almostaflush) ? "is a flush" : "is not a flush") << endl; return 0; } void createDeck(int* d) { for (int i = 0; i < Decksize; i++) d[i] = i; } void print(int card) { string cardValues[] = {"two","three","four","five","six","seven", "eight","nine","ten","jack","queen","king","ace" }; string cardSuits[] = {"clubs","diamonds","hearts","spades"}; cout << cardValues[card%13] << " of " << cardSuits[card%4] << endl; } void print(const int somecards[], int numberOfCards) { for (int i = 0 ; i < numberOfCards; i++) print(somecards[i]); } void shuffle(int deck[]) { int randomCard; for (int i = 0 ; i < Decksize; i++) { randomCard = rand() % Decksize; swap(deck[i],deck[randomCard]); } } void swap(int& a, int& b) { int temp = a; a = b; b = temp; } void deal(int deck[], int hand[], int numberOfCards) { static int topOfDeck = 0; for (int i = 0; i < numberOfCards; ++i) { hand[i] = deck[topOfDeck]; topOfDeck++; } } string getSumOfFrequencyDistributionsForHand(int hand[]) { int sum = 0; int target; // this a a card for (int i= 0; i < HandSize; i++) { target = hand[i]; for (int j = 0; j < HandSize; j++) { if (value(target) == value(hand[j])) sum++; } } cout << " *** " << sum << " ***" << endl; switch (sum) { case 7: return "a pair"; case 9: return "two pair"; case 11: return "three of a kind"; case 13: return "full house"; case 17: return "four of a kind"; default: ; } return "nothing"; } int value(int card) { return card%13; } int suit(int card) { return card%4; } void sort(int a[], int size) { int minIndex; for (int i = 0; i < size - 1; i++) { minIndex = i; for (int j = i+1; j < size; j++) { if (value(a[j]) < value(a[minIndex])) { minIndex = j; } } if(minIndex != i) { swap(a[i],a[minIndex]); } } } bool isAStraight(int hand[]) { sort(hand,HandSize); for (int i = 0; i < HandSize-1; i++) { if (value(hand[i]) + 1 != value(hand[i+1])) return false; } return true; } bool isAFlush(int hand[]) { int firstcardsuit = suit(hand[0]); for (int i = 1; i < HandSize; i++) { if (firstcardsuit != suit(hand[i])) return false; } return true; }