Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing Cards: Should they be enum or struct or class?

Tags:

c#

struct

I'm designing game site where many (hopefully thousands) players will simultenaously play certain card games with each other. The deck is the standart 52 card deck. Each card has a suit and a rank. The cards will be being shuffled, dealed, picked, ordered, played all the time. My question is, should Card be an enum, a struct, or a class?

  • For enum: Let each card be a byte 0..51. So a card will occupy very little space. You can represent a hand as a Bit Set of 8 bytes. You can calculate the suit and rank of a given card very quickly when the need arises: i.e. suit(n) = n/13. This will be very efficient. If you need to write methods for Cards, write it through extension methods.

  • For struct: No, that's like writing machine code. A card is a simple structure, holds very little data, is immutable, is small. It does not have much behaviour, so make it a struct and treat it as a passive data structure. You can calculate an index in 0..51 from a given card very quickly when the need arises.

  • For class: No, that is not an object-oriented way of thinking. Make a Card class. Make it immutable. Create exactly 52 instances. Let a Card Pool hold those instances. So when one needs Queen of Spades, it will ask the Card Pool for that. There will be one and only one Queen of Spades even when there are thousands of games going on. Store an index field 0..51 in Card if you want.

I'm inclining towards the last option (class) but I'm not sure. I'm not much worried about performance; I will perhaps make more serious mistakes along the way. What I'm worried about is my entire point of view may be wrong; maybe this is a very easy decision and I hesitate because I lack some piece knowledge everyone else possesses.

What do you think?

Edit: About the behaviour of cards. I think a card will only know about other cards. For example it may define a partial order on "who beats who in Bridge". It does not need to know anything about the deck. And this will be server side code; certainly it will not need to know to draw itself on the screen etc.

like image 325
Ali Ferhat Avatar asked Mar 29 '12 20:03

Ali Ferhat


1 Answers

The fundamental question you should be asking yourself when deciding between a reference type or a value type is of course is the thing I am modeling logically a value, or a thing referred to? That's why value types and reference types are called "value types" and "reference types" in the first place.

Are you planning on treating a "card" as a reference, the same way that you would treat a physical object that has identity? Suppose for example you are modeling Canasta, which is played with two standard card decks at the same time. Will you ever want to be keeping track of two different queens of spades and treating them as referentially different?

Or are you going to treat them as values, the way you would treat numbers? You don't ever say "this number six over here is different from that number six over there" because numbers are only differentiated by their values.

like image 99
Eric Lippert Avatar answered Oct 24 '22 23:10

Eric Lippert