Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I'mma just wrote a poker hand conversion routine.

52 views
Skip to first unread message

Mr Flibble

unread,
Nov 6, 2017, 2:32:58 PM11/6/17
to
template <typename GameTraits>
inline poker_hand to_poker_hand(const basic_hand<GameTraits>& aHand)
{
typedef basic_card<GameTraits> card_type;
std::map<typename card_type::value, uint32_t, std::greater<typename
card_type::value>, boost::fast_pool_allocator<std::pair<typename
card_type::value, uint32_t>>> valueMap;
for (uint32_t i = 0; i < GameTraits::hand_size; ++i)
++valueMap[aHand.card_at(i)];
std::multiset<uint32_t, std::greater<uint32_t>,
boost::fast_pool_allocator<uint32_t>> values;
for (auto& vme : valueMap)
values.insert(vme.second);
boost::optional<poker_hand> result;
bool possibleStraight = (valueMap.size() == GameTraits::hand_size);
for (auto i = std::next(valueMap.begin()); possibleStraight && i !=
valueMap.end(); ++i)
if (static_cast<uint32_t>(std::prev(i)->first) -
static_cast<uint32_t>(i->first) != 1)
possibleStraight = false;
if (possibleStraight)
result = poker_hand::Straight;
if (result == boost::none)
{
static const std::vector<std::pair<std::vector<uint32_t>,
poker_hand>> sValueTable
{
{ { 4 }, poker_hand::FourOfAKind },
{ { 3, 2 }, poker_hand::FullHouse },
{ { 3 }, poker_hand::ThreeOfAKind },
{ { 2, 2 }, poker_hand::TwoPair },
{ { 2 }, poker_hand::Pair },
{ { 1 }, poker_hand::HighCard }
};
for (auto vte = sValueTable.begin(); vte != sValueTable.end(); ++vte)
if (vte->first.size() <= values.size() &&
std::equal(vte->first.begin(), vte->first.end(), values.begin()))
{
result = vte->second;
break;
}
}
if (*result < poker_hand::ThreeOfAKind || *result == poker_hand::Straight)
{
bool possibleFlush = true;
for (uint32_t i = 1; possibleFlush && i < GameTraits::hand_size; ++i)
if (static_cast<card_type::suit>(aHand.card_at(i - 1)) !=
static_cast<card_type::suit>(aHand.card_at(i)))
possibleFlush = false;
if (possibleFlush)
{
if (*result == poker_hand::Straight)
{
if (valueMap.begin()->first == card_type::value::Ace)
result = poker_hand::RoyalFlush;
else
result = poker_hand::StraightFlush;
}
else
result = poker_hand::Flush;
}
}
return *result;
}

No sausages I'm afraid.

/Flibble

Mr Flibble

unread,
Nov 6, 2017, 3:11:07 PM11/6/17
to
The first person to reply with the (harmless) mistake in this code wins
a cookie. Also explain why it is harmless.

/Flibble

red floyd

unread,
Nov 6, 2017, 3:45:34 PM11/6/17
to
On 11/6/2017 12:10 PM, Mr Flibble wrote:

>>              static const std::vector<std::pair<std::vector<uint32_t>,
>> poker_hand>> sValueTable
>>              {
>>                  { { 4 }, poker_hand::FourOfAKind },
>>                  { { 3, 2 }, poker_hand::FullHouse },
>>                  { { 3 }, poker_hand::ThreeOfAKind },
>>                  { { 2, 2 }, poker_hand::TwoPair },
>>                  { { 2 }, poker_hand::Pair },
>>                  { { 1 }, poker_hand::HighCard }
>>              };
>>              for (auto vte = sValueTable.begin(); vte !=
>> sValueTable.end(); ++vte)

First of all, in your for loop, vte is not a pointer.

red floyd

unread,
Nov 6, 2017, 3:47:19 PM11/6/17
to
Ignore this. I misread the code, vte is an iterator, so -> is
appropriate. I misread it as "for (auto vte: sValueTable)" for some
reason.


Alf P. Steinbach

unread,
Nov 7, 2017, 2:48:27 AM11/7/17
to
I don't see anything obviously wrong, but then I don't see obviously how
it works. :)

Still, it appears the code fights the strict typing.

Might help with auxiliary definitions like

template< class Key, class Value >
using Fastmap = std::map<
Key, Value, std::greater<Key>,
boost::fast_pool_allocator<std::pair<Key, Value>>
>;

Oh well that might be it, that the key part of the pair should be
`const`, yes?

There's an amazing little gotcha in the standard, that even something
that's /dynamically allocated/ as originally `const` can't be modified
without UB. Not directly relevant here but amazing anyway. :)


Cheers!,

- Alf

Mr Flibble

unread,
Nov 7, 2017, 8:46:48 PM11/7/17
to
No. Try again. The mistake is harmless.

/Flibble

0 new messages