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

Was it helpful?


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.


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.

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.

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 )

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top