xref: /reactos/sdk/lib/3rdparty/cardlib/cardcount.cpp (revision c2c66aff)
1 //
2 //    CardCount is a helper library for CardStacks.
3 //
4 //    When you initialize a CardCount object with a
5 //  cardstack, it keeps track of the number of cards
6 //    the stack contains.
7 //
8 //    e.g. CardCount count(cardstack);
9 //
10 //    Then you can do:
11 //
12 //        int num_fives = count[5]
13 //
14 //        count.Add(cardstack2);        - combine with another stack
15 //
16 //        int num_aces = count[1]        - aces low
17 //        int num_aces = count[14]    - aces high
18 //
19 //        count.Clear();
20 //
21 
22 #include "cardlib.h"
23 #include "cardcount.h"
24 
CardCount()25 CardCount::CardCount()
26 {
27     Clear();
28 }
29 
CardCount(const CardStack & cs)30 CardCount::CardCount(const CardStack &cs)
31 {
32     Init(cs);
33 }
34 
Clear()35 void CardCount::Clear()
36 {
37     for(int i = 0; i < 13; i++)
38         count[i] = 0;
39 }
40 
Add(const CardStack & cs)41 void CardCount::Add(const CardStack &cs)
42 {
43     for(int i = 0; i < cs.NumCards(); i++)
44     {
45         Card card = cs[i];
46 
47         int val = card.LoVal();
48         count[val - 1]++;
49     }
50 }
51 
Sub(const CardStack & cs)52 void CardCount::Sub(const CardStack &cs)
53 {
54     for(int i = 0; i < cs.NumCards(); i++)
55     {
56         Card card = cs[i];
57         int val = card.LoVal();
58 
59         if(count[val - 1] > 0)
60             count[val - 1]--;
61     }
62 }
63 
Init(const CardStack & cs)64 void CardCount::Init(const CardStack &cs)
65 {
66     Clear();
67     Add(cs);
68 }
69 
operator [](size_t index) const70 int CardCount::operator [] (size_t index) const
71 {
72     if(index < 1) return 0;
73     else if(index > 14)  return 0;    //if out of range
74     else if(index == 14) index = 1;    //if a "ace-high"
75 
76     return count[index - 1];
77 }
78 
79 //
80 //    Decrement specified item by one
81 //
Dec(size_t index)82 void CardCount::Dec(size_t index)
83 {
84     if(index < 1) return;
85     else if(index > 14)  return;    //if out of range
86     else if(index == 14) index = 1;    //if a "ace-high"
87 
88     index -= 1;
89 
90     if(count[index] > 0)
91         count[index]--;
92 }
93