Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could represent each card as a number, and wrap them then in both an enum and a class. If each number between 0 to 51 represents a card, you can then have an enum:</p> <pre><code>public enum Card { HeartsAce = 0, Hearts2 = 1, // ... and so on } int nCard = 16; Card myCard = (Card)nCard; </code></pre> <p>You could also have a class:</p> <pre><code>public class Card { private readonly int _nSuitNumber = 0; private readonly int _nCardNumber = 0; public Card(int a_nNumber) { _nSuitNumber = a_nNumber / 13; // 1 = Hearts, 2 = Diamonds ... _nCardNumber = a_nNumber % 13; // 1 = ace, 2 = two } } </code></pre> <p>Or better yet combine them.</p> <pre><code>public class Card { private readonly Suit _suit; private readonly Value _value; public Card(int a_nNumber) { _suit = (Suit)(a_nNumber / 13); // 0 = Hearts, 1 = Diamonds ... _value = (Value)(a_nNumber % 13 + 1); // 1 = ace, 2 = two } public Suit Suit { get { return _suit; } } public Value Value { get { return _value; } } public int ToNumber() { return (int)_suit * 13 + ((int)_value - 1); } } public enum Suit { Hearts = 0, Diamonds = 1, Clubs = 2, Spades = 3 } public enum Value { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13, } </code></pre> <p>With this class you have the benefit of both a number (0-52) and a class. The class is nothing more than a wrapper around the number. You can add whatever operations, method, properties to the class as you need. And when you store or transmit the data you need only use the number.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload