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