xref: /reactos/sdk/lib/3rdparty/cardlib/cardstack.cpp (revision c2c66aff)
1 //
2 //    CardLib - CardStack class
3 //
4 //    Freeware
5 //    Copyright J Brown 2001
6 //
7 
8 #include "cardlib.h"
9 
operator [](size_t index)10 Card &CardStack::operator[] (size_t index)
11 {
12     if(index >= (size_t)nNumCards) index = nNumCards - 1;
13     return cardlist[nNumCards - index - 1];
14 }
15 
operator [](size_t index) const16 const Card &CardStack::operator[] (size_t index) const
17 {
18     if(index >= (size_t)nNumCards) index = nNumCards - 1;
19     return cardlist[nNumCards - index - 1];
20 }
21 
22 //    Subscripting operator for a constant sequence
23 //
24 /*Card CardStack::operator[] (size_t index) const
25 {
26     return cardlist[index];
27 }*/
28 
29 //
30 //    Subscripting operator for a non-const sequence
31 //
32 /*CardStack::ref  CardStack::operator[] (size_t index)
33 {
34     return ref(this, index);
35 }*/
36 
Clear()37 void CardStack::Clear()
38 {
39     nNumCards = 0;
40 }
41 
NewDeck()42 void CardStack::NewDeck()
43 {
44     nNumCards = 52;
45 
46     for(int i = 0; i < 52; i++)
47         cardlist[i].nValue = i;
48 }
49 
Shuffle()50 void CardStack::Shuffle()
51 {
52     int src, dest;
53     Card temp;
54 
55     //shuffle 8 times..
56     for(int i = 0; i < 8; i++)
57         for(dest = nNumCards - 1; dest > 0; dest--)
58         {
59             //want to do this:
60             //  bad:   src = rand() % (dest + 1)
61             //  good:  src = rand() / (RAND_MAX / (dest+1) + 1)
62 
63             //positions from 0 to dest
64             src = rand() / (RAND_MAX / (dest+1) + 1);
65 
66             //swap the cards
67             temp           = cardlist[src];
68             cardlist[src]  = cardlist[dest];
69             cardlist[dest] = temp;
70         }
71 }
72 
Reverse()73 void CardStack::Reverse()
74 {
75     for(int i = 0; i < nNumCards / 2; i++)
76     {
77         Card temp                   = cardlist[i];
78         cardlist[i]                 = cardlist[nNumCards - i - 1];
79         cardlist[nNumCards - i - 1] = temp;
80     }
81 }
82 
Push(const Card card)83 void CardStack::Push(const Card card)
84 {
85     if(nNumCards < MAX_CARDSTACK_SIZE)
86         cardlist[nNumCards++] = card;
87 }
88 
Push(const CardStack & cardstack)89 void CardStack::Push(const CardStack &cardstack)
90 {
91     if(nNumCards + cardstack.nNumCards < MAX_CARDSTACK_SIZE)
92     {
93         int num = cardstack.NumCards();
94 
95         for(int i = 0; i < num; i++)
96             cardlist[nNumCards++] = cardstack.cardlist[i];
97     }
98 }
99 
operator +=(Card card)100 CardStack& CardStack::operator += (Card card)
101 {
102     Push(card);
103     return *this;
104 }
105 
operator +=(CardStack & cs)106 CardStack& CardStack::operator += (CardStack &cs)
107 {
108     Push(cs);
109     return *this;
110 }
111 
operator +(Card card)112 CardStack CardStack::operator +  (Card card)
113 {
114     CardStack poo = *this;
115     poo.Push(card);
116     return poo;
117 }
118 
operator +(CardStack & cs)119 CardStack CardStack::operator + (CardStack &cs)
120 {
121     CardStack poo = *this;
122     poo.Push(cs);
123     return poo;
124 }
125 
126 
Pop()127 Card CardStack::Pop()
128 {
129     if(nNumCards > 0)
130         return cardlist[--nNumCards];
131     else
132         return 0;
133 }
134 
Pop(int items)135 CardStack CardStack::Pop(int items)
136 {
137     if(items <= nNumCards && nNumCards > 0)
138     {
139         CardStack cs(*this, nNumCards - items);
140 
141         nNumCards -= items;
142 
143         return cs;
144     }
145     else
146     {
147         return CardStack();
148     }
149 }
150 
Top()151 Card CardStack::Top()
152 {
153     if(nNumCards > 0)
154         return cardlist[nNumCards - 1];
155     else
156         return 0;
157 }
158 
Top(int items)159 CardStack CardStack::Top(int items)
160 {
161     if(items <= nNumCards && nNumCards > 0)
162     {
163         return CardStack (*this, nNumCards - items);
164     }
165     else
166     {
167         return CardStack();
168     }
169 
170 }
171 
RemoveCard(size_t index)172 Card CardStack::RemoveCard(size_t index)
173 {
174     if(nNumCards == 0 || index >= (size_t)nNumCards)
175         return 0;
176 
177     //put index into reverse range..
178     index = nNumCards - index - 1;
179 
180     Card temp = cardlist[index];
181 
182     nNumCards--;
183 
184     for(size_t i = index; i < (size_t)nNumCards; i++)
185     {
186         cardlist[i] = cardlist[i+1];
187     }
188 
189     return temp;
190 }
191 
InsertCard(size_t index,Card card)192 void CardStack::InsertCard(size_t index, Card card)
193 {
194     if(nNumCards == MAX_CARDSTACK_SIZE)
195         return;
196 
197     if(index > (size_t)nNumCards)
198         return;
199 
200     if((size_t)nNumCards == index)
201     {
202         cardlist[nNumCards] = card;
203         nNumCards++;
204         return;
205     }
206 
207     //put index into reverse range..
208     index = nNumCards - index - 1;
209 
210     nNumCards++;
211 
212     //make room for the card
213     for(size_t i = nNumCards; i > index; i--)
214     {
215         cardlist[i] = cardlist[i - 1];
216     }
217 
218     cardlist[index] = card;
219 }
220 
221 
Print()222 void CardStack::Print()
223 {
224 //    for(int i = 0; i < nNumCards; i++)
225 //        cout << cardlist[i].HiVal() << " ";
226 }
227 
CardStack(CardStack & copythis,size_t fromindex)228 CardStack::CardStack(CardStack &copythis, size_t fromindex)
229 {
230     nNumCards = copythis.nNumCards - fromindex;
231 
232     for(int i = 0; i < nNumCards; i++)
233         cardlist[i] = copythis.cardlist[fromindex + i];
234 }
235 
236