1

I'm developing a poker game in C#. At the moment I'm trying to get the players hand score using RegEx. I search the string (composed of the cards suit and number) and look for suits or numbers to match the RegEx. If i get 2 matches then the player has a pair, 3 matches he has 3 of a kind.

I have 3 classes at the moment, a Card class (with number and suit), a Deck class (that contains 52 Cards) and a Hand class that gets five cards from the shuffled deck.

Deck class has a shuffleDeck(); Hand class has the functions to calculate the score (is in these functions that I am using RegEx).

I generate the string on which I use RegEx by adding the 5 suits and numbers that the hand has.

Is this a good idea or should I do it another way, if so, how?

Thank you for your help

PS. I am one of the unexperienced programmers that want to use a newly learned tool for everything

Alec
  • 8,529
  • 8
  • 37
  • 63
seFausto
  • 522
  • 1
  • 8
  • 24

5 Answers5

14

I do not think that a regex is the appropriate way to deal with this. You probably should be using a more sophisticated representation of a hand than a string.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
7

You have not provided much detail, but what from what I have read, I assume you're not pushing the OOP very far...

I would have a Card class that has a Rank and Suit class instances. I would then have a deck class that handles shuffling / dealing...

I would then have a Hand class that would contain your poker hand of n Card objects...

In this way you can build up rules to evaluate each hand object, thus being more flexible and more extensible in the future...say if you want to make another card game / add support for another variant of poker...

Using Regular expressions to do all of this seems to be a pretty poor choice.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mmattax
  • 27,172
  • 41
  • 116
  • 149
3

I would agree with the others, that Regex seems like a bad choice. It might work with pairs, 3 of a kind, 4 of a kind. However, it might get kind of tricky (or impossible) once you start looking at hands like flushes, straights, and 2 pair.

I think the best solution would be to evaluate the cards from best hand to worst hand, as shown here, and as soon as your find a match, then that is your hand. This ensures that you don't mistake 4 of a kind for 2 pair. Or a straight flush for just a straight, or just a flush. I would go with mmattax and create an object for the card, and an object for the hand, then you can evaluate the cards in each hand to see if they meet the required criteria for each hand.

Kibbee
  • 65,369
  • 27
  • 142
  • 182
  • Actually with my current method I can know if there are 2 pairs or a full house on the hand. And with flushes i search the first hands suit if i get 5 matches the theres a flush. with this 2 functions i know most of the hands a player can have – seFausto Oct 28 '08 at 17:46
  • It's just a lot of work to do it that way, and as Kibbee points out, a flush is sometimes a straight flush, and a regex isn't appropriate for determining that. – Dave DuPlantis Oct 28 '08 at 17:54
  • since they are differente functions, if i have a straight and a flush (that return bool) the i would know that the hand has straight flush – seFausto Oct 28 '08 at 17:57
2

I think prime numbers are a good solution for that. consider :

//            D H S C    
colors =    [7,5,3,2]

//           A  Q  K  J  T  9  8  7  6  5  4  3  2    
ranks =     [61,59,53,43,41,37,31,29,23,19,17,13,11,61]

a unique card is identified by a color prime number * a rank prime number. (for example, As of Diamonds : prime = 7 * 61)

so an entiere unique deck or combinaison are identified by prime * prime * prime * prime * prime

if there is a flush of Diamonds, the 5 cards deck's primes ID must by divisble ( mod = 0 ) by the flush of diamonds ID ( 7 ^ 5 because diamonds color prime is 7 )

Dalou
  • 117
  • 6
1

Using a string to represent the hand seems like a poor decision. My recommendation would be to use an Enum to represent the Suit and another to represent the numeric value of the card.

Matthew Brubaker
  • 3,097
  • 1
  • 21
  • 18