Problem 4: A 5-Card Hand
Write a C++ program to generate a random hand of FIVE poker cards from a deck of 52 poker
cards, and determine the type of poker hand it is. The input and output of your program as
are follows:
Input:
• An integer
x
that you would use as input parameter to
srand()
for initializing the
random number generator (RNG).
Output:
• the first line displays the five cards in the hand. (Note that there is a space at the
end of the first output line.)
• the second line tells the type of the 5-card hand, in the following categories:
◦
four of a kind (e.g., A
♠
A
♥
A
♣
A
♦
2
♣
)
◦
full house (e.g., 8
♠
8
♥
8
♦
4
♥
4
♣
)
◦
flush (e.g., K
♠
10
♠
Q
♠
5
♠
3
♠
)
◦
three of a kind (e.g., 10
♥
10
♦
10
♣
5
♥
J
♠
)
◦
two pair (e.g., 9
♦
9
♣
2
♥
2
♣
3
♥
)
◦
one pair (e.g., 7
♠
7
♦
A
♠
Q
♠
6
♠
)
◦
others
(Check the wiki page "
list of poker hands
" for the detailed definition of
each type.)
Requirement
:
• You are provided with a template program
4.cpp
. Read the code in the template
carefully to see what have been provided for you.
• Represent each card of the deck by an integer from 0 to 51 in the following ways:
◦
A
♠
, 2
♠
, 3
♠
, ..., 10
♠
, J
♠
, Q
♠
, K
♠
are represented by 0, 1, 2, ..., 12,
respectively
◦
A
♥
, 2
♥
, 3
♥
, ..., 10
♥
, J
♥
, Q
♥
, K
♥
are represented by 13, 14, 15, ..., 25,
respectively
◦
A
♣
, 2
♣
, 3
♣
, ..., 10
♣
, J
♣
, Q
♣
, K
♣
are represented by 26, 27, 28, ..., 38,
respectively
◦
A
♦
, 2
♦
, 3
♦
, ..., 10
♦
, J
♦
, Q
♦
, K
♦
are represented by 39, 40, 41, ..., 51,
respectively
• The array
cards
of 5 integers defined in the main function is used for storing a
5-card hand.
• Use the
rand()
function in
<cstdlib>
to generate random numbers. However,
the RNG ONLY guarantees to generate the same sequence of random numbers
given the same seed on the same OS & platform only. The sample test cases are
generated on the CS academy* servers, so
you have to run the program there
in order to generate the same results
.
• You may assume that the first 5 random numbers generated by
rand()
are all
different.
• You can ONLY use the simple data types
char
,
bool
,
int
,
double
,
string
and arrays. In other words, you are not allowed to use other data types or data
structures such as STL containers (e.g., vectors), etc.
•
You program MUST contain the following 8 functions:
void
DealHand
(
int
cards[])
• The function
DealHand()
should read from user input an integer
x
and use it as
seed to the RNG for generating five random numbers, each representing a card in
a 52-card deck. The first five numbers generated by the RNG should be stored in
the array
cards[]
in order.
void
PrintHand
(
int
cards[])
• The function
PrintHand()
should print a hand stored in
cards[]
. For example,
if
cards[0] == 0
,
cards[1] == 25
,
cards[2] == 14
,
cards[3] == 50
,
cards[4] == 36
, the line
A♠ K♥ 2♥ Q♦ J♣
should be output to the screen.
• You may refer to the following program on how to display the suit symbols in
UTF-8 encoding (on Linux/MacOS):
#
include
<iostream>
// include the following 4 lines in your program
// these define the UTF-8 encoding of the suit symbols
#
define
SPADE
"\xE2\x99\xA0"
#
define
CLUB
"\xE2\x99\xA3"
#
define
HEART
"\xE2\x99\xA5"
#
define
DIAMOND
"\xE2\x99\xA6"
using namespace
std;
int
main
()
{
cout
<<
SPADE
<<
CLUB
<<
HEART
<<
DIAMOND
<<
endl;
return
0
;
}
The other 6 functions that you must have in your program are:
bool
IsFourOfAKind
(
int
cards[])
// return if the hand is a four of a kind
bool
IsFullHouse
(
int
cards[])
// return if the hand is a full house
bool
IsFlush
(
int
cards[])
// return if the hand is a flush
bool
IsThreeOfAKind
(
int
cards[])
// return if the hand is a three of a kind
bool
IsTwoPair
(
int
cards[])
// return if the hand is a two pair
bool
IsOnePair
(
int
cards[])
// return if the hand is a one pair
These 6 functions should take a 5-card hand as input and return whether the hand is of a
certain type. Note that these functions do not take the array size as an input parameter as we
assume a hand always contain 5 cards in this problem (and a constant is defined in the code
template).
Sample Test Cases
User inputs are shown in
blue
.
4_1
0
A♦ 10♥ Q♣ 5♦ 2♠
others
4_2
3
7♠ 9♥ 10♦ 4♥ 7♦
one pair
4_3
540
10♣ 6♠ 10♥ 3♠ 10♦
three of a kind
4_4
13
8♦ 8♣ 9♦ J♥ J♣
two pair