[CARDLIB]
[reactos.git] / reactos / lib / 3rdparty / cardlib / cardcount.cpp
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 "cardcount.h"
23
24 CardCount::CardCount()
25 {
26 Clear();
27 }
28
29 CardCount::CardCount(const CardStack &cs)
30 {
31 Init(cs);
32 }
33
34 void CardCount::Clear()
35 {
36 for(int i = 0; i < 13; i++)
37 count[i] = 0;
38 }
39
40 void CardCount::Add(const CardStack &cs)
41 {
42 for(int i = 0; i < cs.NumCards(); i++)
43 {
44 Card card = cs[i];
45
46 int val = card.LoVal();
47 count[val - 1]++;
48 }
49 }
50
51 void CardCount::Sub(const CardStack &cs)
52 {
53 for(int i = 0; i < cs.NumCards(); i++)
54 {
55 Card card = cs[i];
56 int val = card.LoVal();
57
58 if(count[val - 1] > 0)
59 count[val - 1]--;
60 }
61 }
62
63 void CardCount::Init(const CardStack &cs)
64 {
65 Clear();
66 Add(cs);
67 }
68
69 int CardCount::operator [] (size_t index) const
70 {
71 if(index < 1) return 0;
72 else if(index > 14) return 0; //if out of range
73 else if(index == 14) index = 1; //if a "ace-high"
74
75 return count[index - 1];
76 }
77
78 //
79 // Decrement specified item by one
80 //
81 void CardCount::Dec(size_t index)
82 {
83 if(index < 1) return;
84 else if(index > 14) return; //if out of range
85 else if(index == 14) index = 1; //if a "ace-high"
86
87 index -= 1;
88
89 if(count[index] > 0)
90 count[index]--;
91 }