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