I am working on an implementation of the card game UNO (mostly to run simulations to test some house rules, but that's a different story).
For those who've never played it, it's similar to Crazy Eights. Players take turns playing a card on the discard pile. The card has to match in number or color. There are also draw cards, which force the next player to draw either two or four cards. It's also quite friendly to house rules, which makes it pretty interesting.
I have a Card
class with the value
and color
of the card. What I would like to do is to create DrawCard
which extends Card
. Then, in determining the game mechanics, I would use instanceof
to test whether I have a DrawCard
.
I know that instanceof
is often problematic, but this seems to be okay to me. Usually the cards are processed the same, and only a few special cards are treated differently, and only in specialized circumstances specific to the card type (That seems like a slippery-slope thing to say though...)
I could just use markers in the Card
class (actually, each type of card already has its own 'value'), but I am extending Card
anyway in order to have some methods that other types of cards may have (it's not just a means of identification). Using instanceof
seems more general to me, as I wouldn't need to know about which values of value
require this special behaviour, and it would be easy, for example, to add a Draw 8 card to the current contents of Draw 2 and Draw 4. (Saying this makes me wonder if I could use some sort of nested enum or something, but I don't know)
I know that both solutions would work (make the program run). To me using instanceof
feels nicer, but I don't have the experience to understand whether it is okay. Is this bad design?