in reply to Checking for a special matching

I wouldn't use strings as a card representation. I'd either use bits (6 bits/card will do), or 2-element arrays (rank, suit).

Testing for pairs is easy: see if there are two card with the same rank (considering that in most poker forms, a hand has 5 or 7 cards, you don't have to be smart, even a brute force algorithm runs fast enough on anything produced since Babbage). You could, for instance, use a hash for ranks, and a hash for suits. If the ranks values are "2, 1, 1, 1", it's a pair. If it's "2, 2, 1", it's two pair. If it's "3, 1, 1", it's three of a kind. It it's "3, 2", it's a full house. If it's "4, 1", it's four of a kind. If it's "5", it's five of a kind. For the suits, if all the suits are the same (easy to find out with a hash), you have a flush. To find out straights, sort the cards (by rank). If the result is a sequence, you have a straight (an ace high straight needs an extra check).

Instead of hashes, you can sort right away, and determine pairs, etc, by inspecting the sorted hand.

But regexes seems like the wrong way to do it. In fact, if you need a regex to inspect an internal representation 100 out of 99 times, you're doing it wrong.

Replies are listed 'Best First'.
Re^2: Checking for a special matching
by heatblazer (Scribe) on Apr 02, 2012 at 16:43 UTC

    Actually my idea for regex was to take the 5 array elements, turn them into one big scalar string and then match patterns for example if K occurs 2 times it`s a pair of Kings, if clubs occurs 5 times its... dunno the name the all-colors-winning pattern, if 3 of 'these' and 2 of 'this' it`s 'full house'. But the idea was to turn the array into one big string and solve the problem with a 1-shot-2-rabbits.

      But the idea was to turn the array into one big string and solve the problem with a 1-shot-2-rabbits.
      Considering that lead to a post where you said to be totally stuck, it only strengthens my claim that using a regexp is the wrong way of doing it.
Re^2: Checking for a special matching
by heatblazer (Scribe) on Apr 03, 2012 at 14:33 UTC

    I have a new edit. Check it out :)