tags:

views:

763

answers:

4

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

+7  A: 

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.

mmattax
+12  A: 

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
The most important thing you can learn about regex's, is when not to use them.
Brad Gilbert
I wrote regex to handle this. http://stackoverflow.com/questions/3463964/regex-to-calculate-straight-poker-hand
Topera
@Richard - I only wanted to tell that its possible, not good.
Topera
@Topera - Cool; I'm just kidding around though; I hope you didn't take offense. I'll delete my comment just in case though :)
LeguRi
@Richard - Okay! My english isn't very good, so i can't express myself in appropriate way. But i'm ok! :D
Topera
+1  A: 

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
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
isc_fausto
Oh, and thank you for the link, i will help a lot
isc_fausto
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
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
isc_fausto
+1  A: 

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