1 //
2 // CardLib - Card class
3 //
4 // Freeware
5 // Copyright J Brown 2001
6 //
8 #ifndef _CARD_INCLUDED
9 #define _CARD_INCLUDED
11 enum eSuit { Clubs = 0, Diamonds = 1, Hearts = 2, Spades = 3 };
12 enum eValue { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7,
13 Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13 };
15 inline int MAKE_CARD(int Value, int Suit)
16 {
17 if(Value < 1) Value = 1;
18 if(Value == 14) Value = 1;
19 if(Value > 13) Value = 13;
21 if(Suit < 0) Suit = 0;
22 if(Suit > 3) Suit = 3;
24 return ((Value - 1) * 4 + Suit);
25 }
27 class Card
28 {
29 friend class CardStack;
31 public:
33 Card()
34 {
35 nValue = 0; //ace of spades by default
36 fFaceUp = true;
37 }
39 Card(int value, int suit) //specify a face value [1-13] and suit [0-3]
40 {
41 nValue = MAKE_CARD(value, suit);
42 fFaceUp = true;
43 }
45 Card(int index) //specify a 0-51 index
46 {
47 if(index < 0) index = 0;
48 if(index > 51) index = 51;
50 nValue = index;
51 fFaceUp = true;
52 }
54 int Suit() const
55 {
56 return (nValue % 4);
57 }
59 int LoVal() const
60 {
61 return (nValue / 4) + 1;
62 }
64 int HiVal() const
65 {
66 return ((nValue < 4) ? 14 : (nValue / 4) + 1);
67 }
69 int Idx() const //unique value (0-51 etc)
70 {
71 return nValue;
72 }
74 bool FaceUp() const
75 {
76 return fFaceUp;
77 }
79 bool FaceDown() const
80 {
81 return !fFaceUp;
82 }
84 void SetFaceUp(bool fTrue)
85 {
86 fFaceUp = fTrue;
87 }
89 bool IsBlack() const
90 {
91 return Suit() == 0 || Suit() == 3;
92 }
94 bool IsRed() const
95 {
96 return !IsBlack();
97 }
99 private:
101 int nValue;
102 bool fFaceUp;
103 };
105 #endif /* _CARD_INCLUDED */