Skip to content

Instantly share code, notes, and snippets.

@knowsuchagency
Created September 13, 2016 05:19
Show Gist options
  • Save knowsuchagency/1aa895f397df4d444dce6e2354d2a777 to your computer and use it in GitHub Desktop.
Save knowsuchagency/1aa895f397df4d444dce6e2354d2a777 to your computer and use it in GitHub Desktop.
poker hands
#include <iostream>
#include <array>
#include <map>
using namespace std;
// prototype swapArrays
void swapArrays(array<int, 5>& stdArray, int oldArray[5]);
bool containsPair(int hand[]) {
// Return a true if hand contains a pair else false
// create std array and map
array<int, 5> handArray;
swapArrays(handArray, hand);
map<int, int> cardMap;
// fill map
for (int i: handArray) {
cardMap[i] += 1;
}
// determine if there is a pair of cards in the hand
for (auto const &pair: cardMap) {
if (pair.second == 2) {return true;}
}
return false;
}
bool containsTwoPair(int hand[]) {
// Return a true if hand contains a pair else false
// create std array, map, and flags
array<int, 5> handArray;
swapArrays(handArray, hand);
map<int, int> cardMap;
bool firstPair = false;
// fill map
for (int i: handArray) {
cardMap[i] += 1;
}
// determine if there are two pairs in the hand
for (auto const &pair: cardMap) {
if (pair.second == 2 && firstPair) {return true;}
else if (pair.second == 2) {firstPair = true;}
}
return false;
}
bool containsThreeOfaKind(int hand[]) {
// Return a true if hand contains three of a kind else false
// create std array and map
array<int, 5> handArray;
swapArrays(handArray, hand);
map<int, int> cardMap;
// fill map
for (int i: handArray) {
cardMap[i] += 1;
}
// determine if there are three of any card in the hand
for (auto const &pair: cardMap) {
if (pair.second == 3) {return true;}
}
return false;
}
bool containsStraight(int hand[]) {
// Return true if hand has a straight else false
// create array
array<int, 5> handArray;
swapArrays(handArray, hand);
// sort array
sort(handArray.begin(), handArray.end());
// determine if it's a straight
for (int i=1; i<5; i++) {
if (handArray[i] != handArray[i-1] + 1) {
return false;
}
}
return true;
}
bool containsFullHouse(int hand[]) {
// Return true if hand has full house else false
// create std array, map, and flags
array<int, 5> handArray;
swapArrays(handArray, hand);
map<int, int> cardMap;
bool handContainsPair = false;
bool handContainsThreeOfaKind = false;
// fill map
for (int i: handArray) {
cardMap[i] += 1;
}
// find if hand has full house
for (auto const &pair : cardMap) {
if (pair.second == 2) {handContainsPair = true;}
if (pair.second == 3) {handContainsThreeOfaKind = true;}
if (handContainsPair && handContainsThreeOfaKind) {return true;}
}
return false;
}
bool containsFourOfaKind(int hand[]) {
// Return a true if hand contains four of a kind else false
// create std array and map
array<int, 5> handArray;
swapArrays(handArray, hand);
map<int, int> cardMap;
// fill map
for (int i: handArray) {
cardMap[i] += 1;
}
// determine if there are four of any card in the hand
for (auto const &pair: cardMap) {
if (pair.second == 4) {return true;}
}
return false;
}
void swapArrays(array<int, 5>& stdArray, int oldArray[5]) {
// Fill an std array with that of another array
for (int i=0; i < 5; i++) {
stdArray[i] = oldArray[i];
}
}
void determineHand(int hand[]) {
// print the highest hand for the given cards
if (containsFourOfaKind(hand)) {
cout << "Four of a kind!";
return;
} else if (containsFullHouse(hand)) {
cout << "Full House!";
return;
} else if (containsStraight(hand)) {
cout << "Straight!";
return;
} else if (containsThreeOfaKind(hand)) {
cout << "Three of a kind!";
return;
} else if (containsTwoPair(hand)) {
cout << "Two Pair!";
return;
} else if (containsPair(hand)) {
cout << "Pair!";
return;
}
cout << "High Card!";
}
void getUserInput(int hand[5]) {
// Populate hand with input from user
cout << "Enter five numeric cards. No face cards. Use 2-9" << endl;
string sUserInput;
int iUserInput;
int count = 0;
while (count < 5) {
cout << "Card " << count + 1 << ": ";
cin >> sUserInput;
iUserInput = atoi(sUserInput.c_str());
if (! ( (iUserInput > 1) && (iUserInput < 10) ) ) {
cout << "Number must be between 2-9. Try again." << endl;
continue;
}
hand[count] = iUserInput;
count++;
}
}
int main() {
int hand[5];
getUserInput(hand);
determineHand(hand);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment