A 5-Card Hand

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是 `evaluate-hand` 函数的实现: ``` (defn evaluate-hand [hand community-cards] (let [all-cards (concat hand community-cards) sorted-cards (sort-by :rank all-cards) flush-suit (->> all-cards (group-by :suit) (sort-by #(count (val %))) last first :suit) straight (some #(apply = (map :rank %)) (partition 5 1 sorted-cards)) straight-flush (and flush-suit straight)] (cond->> {:hand all-cards} straight-flush (assoc :rank :straight-flush) flush-suit (assoc :rank :flush) straight (assoc :rank :straight) :else (let [by-rank (group-by :rank all-cards) counts (map count by-rank) max-count (apply max counts)] (cond->> {:rank :high-card} (= max-count 2) (assoc :rank :pair) (= max-count 3) (assoc :rank :three-of-a-kind) (= max-count 4) (assoc :rank :four-of-a-kind) (and (= max-count 2) (= (count (distinct counts)) 2)) (assoc :rank :two-pair) :else (let [sorted-counts (reverse (sort counts)) kickers (map first (filter #(= (second %) 1) (partition-all 2 sorted-counts)))] (cond (= max-count 1) (assoc :rank :high-card :kickers kickers) (= max-count 3) (assoc :rank :full-house :kickers kickers) :else (assoc :rank :three-of-a-kind :kickers kickers))))))) ``` 该函数接收两个参数,一个是表示手牌的列表 `hand`,一个是表示公共牌的列表 `community-cards`。在该函数中,首先将手牌和公共牌合并成一副牌,并将其按照牌面大小排序。 然后,通过一系列判断,可以判断出当前牌型的大小,并将其保存在一个字典中,其中包括了牌型和可能的“踢牌&rdquo;(如果有的话)。最终,这个字典会被返回。 这个函数并不是最优的实现方法,但是足以用于简单的德州扑克游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值