Reputation: 3531
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.
Upvotes: 2
Views: 1427
Reputation: 26
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 Card
s, 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.
Upvotes: 1