2

I'm making a class that recognizes poker hands. It is used in a game like poker, but it has some other cards that cannot be part of a straight/pair/etc.

The class takes a list of cards and checks their numerical values to see if they match a hand.

My problem is that in my application, there are cards that have no numerical value. Like wildcards and a few others.

The simplest thing to do, is make those cards have a negative value or something similar. I do not like this idea, for obvious reasons.

I thought of using a nullable integer for the field of the numerical value of the card, but I will always have to check if it's null before using it.

This makes sense, since I'm using cards with no value, but it adds complexity to the code, that I think is unecessary. For this reason I thought about implementing a property (C#)* that returns a non nullable negative int, if the numerical value is null. But this also returns a magic number.

Which is the proper way to do this? Avoid magic numbers completely and check for null every time, or something that I haven't thought of?

UPDATE: I think that the best thing to do, is to create a nullable Int for value, and a property* that returns the value, or throws an exception if the value is null. This way I do not have to check for null every time, just catch the exception if it occurs. Is this wrong in any way?

*think of a getter method in languages that don't have properties, ergo a method that returns the value of a variable/field.

RaidenF
  • 3,411
  • 4
  • 26
  • 42
  • 1
    Use Optional/Maybe. http://stackoverflow.com/q/16199227/139010 – Matt Ball Feb 03 '15 at 18:12
  • 1
    Couldn't those cards be "anything" in poker? If that's the case, why not have an ANY or WILD type? Is the question re: the numeric value specifically? – duffymo Feb 03 '15 at 18:14
  • They're not all wildcards. Some have no value at all and cannot be part of a hand. I'll update the description, it's not EXACTLY poker. – RaidenF Feb 03 '15 at 18:25

1 Answers1

1

Try to model the problem domain.

Start with a class Cards where a card is constructed providing rank (Ace, 2, 3, .., Jack, Queen, ..), a suit (clubs, diamonds, hearts) and a int value which depends on the game you play. In any game a card represents some value, maybe nothing (0) but never -1.

Next, complete your domain by creating a class Hand, which is a collection of Cards, and some methods to calculate the total value of the hand based on the cards it is composed of. Maybe add a class Deck that you can use to init all 52 Cards and deal from. Etc.

dicke
  • 26
  • 2
  • I have done almost the exact same thing, but some cards cannot have a value. Isn't having a value of 0 a magic number representation of not having a value? – RaidenF Feb 03 '15 at 18:31
  • Yes, but you are encapsulating the magic number behind abstraction so hopefully your logic doesn't have to worry about it. The code that evaluates your hand of Cards will have to handle the special case where the Card is an instance of WildCard, for example, instead of getting values directly. – whiterook6 Feb 03 '15 at 18:36
  • 2
    A magic number is a value with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants. That is not what '0' is in this case, it is the value a cards represents in some game. – dicke Feb 03 '15 at 18:45