Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for different ways how to recognise whether a set of cards sufficiently fulfills a requirement. It's for an obscure game you probably don't know, so for sake of discussion let's talk about Poker instead, whose basic hands are well-understood by many people.

For instance, let's stringify our hand to '♥9 ♥9 ♠J ♠J ♠J' so we can operate on it. I want to find out whether that is one of the winning hands (it is) and which one it is (it's a 'Full House'). Before I spend too much time going down dead ends, are grammars a good approach for that sort of problem? If not, what else can you recommend for the general case? Need not be something complete from CPAN, pointers to suitable Web search terms are welcome, too.

  • Comment on Is Regexp::Grammars a suitable tool for recognising card game hands?

Replies are listed 'Best First'.
Re: Is Regexp::Grammars a suitable tool for recognising card game hands?
by holli (Abbot) on Aug 08, 2009 at 22:35 UTC
    There is a whole Games namespace on CPAN. Maybe the game you are talking about is already there? If it isn't you can still check their code for inspiration.


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: Is Regexp::Grammars a suitable tool for recognising card game hands?
by JavaFan (Canon) on Aug 08, 2009 at 23:29 UTC
    You typically use a grammar if you want to parse something that isn't quite trivial, and where order matters (you typically want to construct a tree). None of that seems to apply to your problem.
Re: Is Regexp::Grammars a suitable tool for recognising card game hands?
by jethro (Monsignor) on Aug 09, 2009 at 12:38 UTC

    Here some ideas:

    1) A better representation for a poker hand would be an array. So you might store the hands as strings, but to operate on them you should transform them first to a more suitable representation.

    2) To analyze a hand you might first sort it. By number to find high cards, pairs, full houses and straights, by suit to detect flushes. Ok, sorting is not neccessary for flushes in poker, but in your obscure game it might help, for example when there are smaller flushes possible like 5 out of 8

    3) Simple counting would suffice to find most poker hands. Use an array to count the number of 2s, 3s... and you can find most of the poker hands by simply checking the numbers and noting the highest single, the highest pair, the highest triple and the highest "foursome". A simple check for triple AND pair then finds a Full House.

    4) If you categorize hands with a special identifier, you are able to compare winning hands by string comparision and sort them. The identifier aka key would be "ABCDEF", with A == 0 for High Card, 1 for Pair... up to 8 for Straight Flush. The rest would depend on A and might be even empty. For a pair B would be the rank of the pair, 02 for a 2, 13 for an ace. C would be the number of the highest single card apart from the pair. D the next highest and so on. Your example poker hand would be "61009". In this way a simple string comparision could rank arbitrary poker hands.

Re: Is Regexp::Grammars a suitable tool for recognising card game hands?
by moritz (Cardinal) on Aug 09, 2009 at 10:19 UTC
    There are two separate problems: Finding strings such as "♥9 ♥9 ♠J ♠J ♠J" in a piece of text - that can be done with a regex, no need for a full-blown grammar.

    The second problem is comparing the values of two hands, for which grammars won't help you at all.