ReactOS  0.4.14-dev-608-gd495a4f
cardstack.cpp
Go to the documentation of this file.
1 //
2 // CardLib - CardStack class
3 //
4 // Freeware
5 // Copyright J Brown 2001
6 //
7 
8 #include "cardlib.h"
9 
11 {
12  if(index >= (size_t)nNumCards) index = nNumCards - 1;
13  return cardlist[nNumCards - index - 1];
14 }
15 
16 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 
38 {
39  nNumCards = 0;
40 }
41 
43 {
44  nNumCards = 52;
45 
46  for(int i = 0; i < 52; i++)
47  cardlist[i].nValue = i;
48 }
49 
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];
69  cardlist[dest] = temp;
70  }
71 }
72 
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 
83 void CardStack::Push(const Card card)
84 {
86  cardlist[nNumCards++] = card;
87 }
88 
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 
101 {
102  Push(card);
103  return *this;
104 }
105 
107 {
108  Push(cs);
109  return *this;
110 }
111 
113 {
114  CardStack poo = *this;
115  poo.Push(card);
116  return poo;
117 }
118 
120 {
121  CardStack poo = *this;
122  poo.Push(cs);
123  return poo;
124 }
125 
126 
128 {
129  if(nNumCards > 0)
130  return cardlist[--nNumCards];
131  else
132  return 0;
133 }
134 
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 
152 {
153  if(nNumCards > 0)
154  return cardlist[nNumCards - 1];
155  else
156  return 0;
157 }
158 
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 
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 
192 void CardStack::InsertCard(size_t index, Card card)
193 {
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 
223 {
224 // for(int i = 0; i < nNumCards; i++)
225 // cout << cardlist[i].HiVal() << " ";
226 }
227 
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 
void Print()
Definition: cardstack.cpp:222
Card cardlist[MAX_CARDSTACK_SIZE]
Definition: cardstack.h:47
void Clear()
Definition: cardstack.cpp:37
void InsertCard(size_t index, Card card)
Definition: cardstack.cpp:192
void Push(const Card card)
Definition: cardstack.cpp:83
Card RemoveCard(size_t index)
Definition: cardstack.cpp:172
CardStack()
Definition: cardstack.h:11
Card Top()
Definition: cardstack.cpp:151
uint32_t cs
Definition: isohybrid.c:75
void NewDeck()
Definition: cardstack.cpp:42
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
void Reverse()
Definition: cardstack.cpp:73
GLuint index
Definition: glext.h:6031
int nNumCards
Definition: cardstack.h:48
__kernel_size_t size_t
Definition: linux.h:237
void Shuffle()
Definition: cardstack.cpp:50
Card Pop()
Definition: cardstack.cpp:127
GLuint GLuint num
Definition: glext.h:9618
#define MAX_CARDSTACK_SIZE
Definition: cardstack.h:4
CardStack & operator+=(Card card)
Definition: cardstack.cpp:100
#define index(s, c)
Definition: various.h:29
GLenum src
Definition: glext.h:6340
#define RAND_MAX
Definition: stdlib.h:99
Definition: card.h:27
Card & operator[](size_t index)
Definition: cardstack.cpp:10
static calc_node_t temp
Definition: rpn_ieee.c:38
static char * dest
Definition: rtl.c:135
CardStack operator+(Card card)
Definition: cardstack.cpp:112
static TCHAR * items[]
Definition: page1.c:45
int NumCards() const
Definition: cardstack.h:14