write a program to score five-card poker hands into one of the following
categories: nothing, one pair, two pairs, three of a kind, straight (in
order with no gaps), flush (all 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.
PROGRAM CODE:
//Header section
#include <iostream>
#include <string>
using namespace std;
//Define constant size
const int HAND_SIZE = 5;
// Function to score the poker hand and return the category
string scoreHand(const int values[], const char suits[])
{
// Check for a straight
bool isStraight = true;
for (int i = 1; i < HAND_SIZE; i++)
{
if (values[i] != values[i - 1] + 1)
{
isStraight = false;
break;
}
}
// Check for a flush
bool isFlush = true;
for (int i = 1; i < HAND_SIZE; i++)
{
if (suits[i] != suits[i - 1])
{
isFlush = false;
break;
}
}
// Check for different categories
if (isStraight && isFlush)
{
return "Straight Flush";
} else if (isFlush)
{
return "Flush";
} else if (isStraight)
{
return "Straight";
}
int pairCount = 0;
int threeCount = 0;
int fourCount = 0;
for (int i = 0; i < HAND_SIZE; i++)
{
int count = 0;
for (int j = 0; j < HAND_SIZE; j++)
{
if (values[j] == values[i])
{
count++;
}
}
if (count == 2)
{
pairCount++;
} else if (count == 3)
{
threeCount++;
} else if (count == 4)
{
fourCount++;
}
}
if (fourCount > 0)
{
return "Four of a Kind";
} else if (threeCount > 0 && pairCount > 0)
{
return "Full House";
} else if (threeCount > 0)
{
return "Three of a Kind";
} else if (pairCount == 2)
{
return "Two Pairs";
} else if (pairCount == 1)
{
return "One Pair";
}
return "Nothing";
}
//main program
int main()
{
// Arrays to hold card values and suits
int values[HAND_SIZE];
char suits[HAND_SIZE];
char choice;
do
{
// Get input for card values
cout << "Enter the values of the cards (2-9, T, J, Q, K, A): ";
for (int i = 0; i < HAND_SIZE; i++)
{
cin >> values[i];
}
// Get input for card suits
cout << "Enter the suits of the cards (H, D, C, S): ";
for (int i = 0; i < HAND_SIZE; i++)
{
cin >> suits[i];
}
// Score the poker hand
string category = scoreHand(values, suits);
// Output the result
cout << "Category: " << category << endl;
// Ask the user if they want to score another hand
cout << "Do you want to score another hand? (Y/N): ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
return 0;
}
OUTPUT OF THE PROGRAM CODE:
Enter the values of the cards (2-9, T, J, Q, K, A): A 2 3 4 5
Enter the suits of the cards (H, D, C, S): H H H H H
Category: Straight Flush
Do you want to score another hand? (Y/N): Y
Enter the values of the cards (2-9, T, J, Q, K, A): J J J 7 7
Enter the suits of the cards (H, D, C, S): H D C H C
Category: Full House
Do you want to score another hand? (Y/N): N