Question: Write a program to score five-card poker hands into one of the following categories: nothing, one pair, two pair, three of a kind, straight (in
Write a program to score five-card poker hands into one of the following categories: nothing, one pair, two pair, three of a kind, straight (in order, with no gaps), flush (all of the same suit, for example, all spades)., full house (one pair and three of a kind), four of a kind, straight flush (both a straight and a flush),. Use two arrays, one to hold the value of the card and one to hold the suit. Include a loop that allows the user to continue to score more hands until the user says the program should end
. I have a program that can sort the hand into categories, but only for number cards with no suit. I'll post my code below, and I'm hoping somebody can add on pieces to test for face cards and suit. Here's my code currently
#include
using namespace std;
const int handSize = 5;
//This function instructs the user to enter their cards
void getHand(int hand[])
{
cout << "Enter five numeric cards, no face cards. Use 2 - 9" << endl;
for (int i = 0; i < handSize; i++) {
cout << "Card " << i + 1 << ": ";
cin >> hand[i];
}
}
//This function sorts the hand to test for a straight
void sortHand(int hand[])
{
int temp_card;
for (int i = 0; i < handSize - 1; i++)
for (int j = i + 1; j < handSize; j++)
if (hand[i] > hand[j]) {
temp_card = hand[j];
hand[j] = hand[i];
hand[i] = temp_card;
}
}
//This function tests for a straight
bool Straight(int hand[])
{
for (int i = 0; i < handSize - 1; i++)
if (hand[i] != hand[i + 1] - 1)
return false;
return true;
}
//This function tests for a single pair
bool Pair(int hand[])
{
for (int i = 0; i < handSize - 1; i++)
for (int j = i + 1; j < handSize; j++)
if (hand[i] == hand[j])
return true;
return false;
}
//This function tests for two pairs
bool TwoPair(int hand[])
{
bool pair1 = false;
bool pair2 = false;
for (int i = 0; i < handSize - 1; i++)
for (int j = i + 1; j < handSize; j++) {
if (hand[i] == hand[j]) {
if (!pair1)
pair1 = true;
else
pair2 = true;
break;
}
}
return pair1 && pair2;
}
//This function tests for three of a kind
bool ThreeOfaKind(int hand[])
{
int card_count = 1;
for (int i = 0; i < 3; i++, card_count = 1)
for (int j = i + 1; j < handSize; j++) {
if (hand[j] == hand[i])
card_count++;
if (card_count == 3)
return true;
}
return false;
}
//This function tests for four of a kind
bool FourOfaKind(int hand[])
{
int card_count = 1;
for (int i = 0; i < 2; i++, card_count = 1)
for (int j = i + 1; j < handSize; j++) {
if (hand[j] == hand[i])
card_count++;
if (card_count == 4)
return true;
}
return false;
}
//This function tests for a full house.
bool FullHouse(int hand[])
{
if (hand[0] == hand[1] && hand[2] == hand[3] && hand[3] == hand[4])
return true;
else if (hand[0] == hand[1] && hand[1] == hand[2] && hand[3] == hand[4])
return true;
return false;
}
int main()
{
int hand[handSize];
getHand(hand);
sortHand(hand);
if (FourOfaKind(hand))
cout << "Four of a kind!";
else if (FullHouse(hand))
cout << "Full House!";
else if (Straight(hand))
cout << "Straight!";
else if (ThreeOfaKind(hand))
cout << "Three of a kind!";
else if (TwoPair(hand))
cout << "Two Pair!";
else if (Pair(hand))
cout << "Pair!";
else
cout << "High Card!";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
