-
Notifications
You must be signed in to change notification settings - Fork 1
/
Deck.cpp
402 lines (339 loc) · 8.48 KB
/
Deck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//----------------------------------------------------------------------------------------
//
// This file and all other Easy Bridge source files are copyright (C) 2002 by Steven Han.
// Use of this file is governed by the GNU General Public License.
// See the files COPYING and COPYRIGHT for details.
//
//----------------------------------------------------------------------------------------
//
// CDeck.cpp
//
#include "stdafx.h"
#include "EasyB.h"
#include "EasyBdoc.h"
#include "mainfrm.h"
#include "Card.h"
#include "Deck.h"
#include "MyBitmap.h"
#include "progopts.h"
#include "deckopts.h"
#include <sys/types.h>
#include <sys/timeb.h>
// global deck objects
CDeck deck;
//
// construction/destruction
//
CDeck::CDeck()
{
m_bInitialized = FALSE;
}
CDeck::~CDeck()
{
}
//
int CDeck::GetCardWidth() const
{
if (m_nCardWidth > 0)
{
return m_nCardWidth;
}
else
{
// not initialized yet
if (theApp.GetValue(tbLowResOption))
return SMALLCARDWIDTH;
else
return DEFCARDWIDTH;
}
}
//
int CDeck::GetCardHeight() const
{
if (m_nCardWidth > 0)
{
return m_nCardHeight;
}
else
{
// not initialized yet
if (theApp.GetValue(tbLowResOption))
return SMALLCARDHEIGHT;
else
return DEFCARDHEIGHT;
}
}
//
void CDeck::Clear()
{
if (!m_bInitialized)
return;
//
for(int i=0;i<52;i++)
m_cards[i]->Clear();
}
//
void CDeck::ClearCardBackgrounds()
{
for(int i=0;i<52;i++)
m_cards[i]->ClearBackground();
}
static TCHAR BASED_CODE szDeckSettings[] = _T("Deck Settings");
static TCHAR BASED_CODE szCurrentCardBack[] = _T("Current Card Back Index");
//
void CDeck::Initialize()
{
// read in some values
m_nCurrCardBack = theApp.GetProfileInt(szDeckSettings, szCurrentCardBack, 0);
// init bitmaps
InitializeBitmaps();
// init the deck
CDC *pDC = pMAINFRAME->GetDC();
CCard* pCard;
int nCount = 0;
//
for (int nSuit=CLUBS;nSuit<=SPADES;nSuit++)
{
for(int nValue=2;nValue<=ACE;nValue++,nCount++)
{
// create card
pCard = new CCard();
pCard->Initialize(nSuit,nValue,&m_cardBitmap[nCount],pDC);
sprintf(pCard->m_szValue,pCard->GetName());
m_cards[nCount] = pCard;
m_sortedCards[nCount] = pCard;
m_nDeckIndex[nSuit][nValue] = nCount;
}
}
// init the time
m_nPrevTime = time(NULL);
// done
m_bInitialized = TRUE;
pMAINFRAME->ReleaseDC(pDC);
}
//
void CDeck::InitializeBitmaps()
{
// init the deck
int i,nSuit,nValue,nCount=0;
CDC *pDC = pMAINFRAME->GetDC();
// see whether we're using small cards
BOOL bSmallCards = theApp.GetValue(tbLowResOption);
int nBase = bSmallCards? IDBS_CARDS_BASE : IDB_CARDS_BASE;
if (bSmallCards)
{
m_nCardWidth = SMALLCARDWIDTH;
m_nCardHeight = SMALLCARDHEIGHT;
}
else
{
m_nCardWidth = DEFCARDWIDTH;
m_nCardHeight = DEFCARDHEIGHT;
}
// init bitmaps
for (nSuit=CLUBS;nSuit<=SPADES;nSuit++)
{
for(nValue=2;nValue<=ACE;nValue++,nCount++)
{
// first delete any existing bitmap
if (m_bInitialized)
m_cardBitmap[nCount].DeleteObject();
// then load new bitmap
if (!m_cardBitmap[nCount].LoadBitmap(nBase+(nSuit*100)+nValue))
{
AfxMessageBox("Failed to load card bitmap");
pMAINFRAME->ReleaseDC(pDC);
return;
}
// update card's bitmap if needed
if (m_bInitialized)
m_sortedCards[nCount]->SetBitmap(&m_cardBitmap[nCount], pDC);
}
}
// load the mask bitmap
if (m_bInitialized)
m_maskBitmap.DeleteObject();
int nCardMask = bSmallCards? IDBS_CARD_MASK : IDB_CARD_MASK;
if (!m_maskBitmap.LoadBitmap(nCardMask))
AfxMessageBox("Failed to load card mask!");
// initialize the cardbacks imagelist
if (m_bInitialized)
m_imageListCardBacks.DeleteImageList();
int nCardWidth = deck.GetCardWidth();
int nCardHeight = deck.GetCardHeight();
m_imageListCardBacks.Create(nCardWidth, nCardHeight, ILC_COLOR24 | ILC_MASK, 0, 32);
// load the cardbacks bitmaps into the imagelist
int nCardBackBase = bSmallCards? IDBS_CARDBACK0 : IDB_CARDBACK0;
for(i=0;i<MAX_CARDBACKS;i++)
{
// delete old stuff
if (m_bInitialized)
{
m_cardBackImageListBitmap[i].DeleteObject();
m_cardBackBitmap[i].DeleteObject();
}
// use one bitmap each for the imagelist & cardback
if (!m_cardBackImageListBitmap[i].LoadBitmap(nCardBackBase + i))
break;
m_imageListCardBacks.Add(&m_cardBackImageListBitmap[i], RGB(255, 255, 255));
m_cardBackBitmap[i].LoadBitmap(nCardBackBase + i);
}
// determine the # of cardbacks loaded
m_numCardBacks = i;
if (m_nCurrCardBack >= m_numCardBacks)
m_nCurrCardBack = 0;
// done
pMAINFRAME->ReleaseDC(pDC);
}
//
void CDeck::Terminate()
{
// write out some settings
theApp.WriteProfileInt(szDeckSettings, szCurrentCardBack, m_nCurrCardBack);
//
int nSuit,nValue,nCount=0;
for (nSuit=CLUBS;nSuit<=SPADES;nSuit++)
{
for(nValue=2;nValue<=ACE;nValue++,nCount++)
{
delete m_cards[nCount];
m_cardBitmap[nCount].DeleteObject();
}
}
// delete up the cardbacks imagelist
m_imageListCardBacks.DeleteImageList();
// delete the card back bitmaps
int i;
for(i=0;i<m_numCardBacks;i++)
{
m_cardBackImageListBitmap[i].DeleteObject();
m_cardBackBitmap[i].DeleteObject();
}
// and the mask bitmap
m_maskBitmap.DeleteObject();
}
//
int CDeck::Shuffle(int nSeed, bool bSuppressSeed)
{
// copy from the ordered deck if deal numbering is enabled
// else leave shuffled
if (theApp.GetValue(tbEnableDealNumbering))
{
for(int i=0;i<52;i++)
m_cards[i] = m_sortedCards[i];
}
// then seed the random number generator
if (nSeed == 0)
{
// use the lower 16 bits of the current time since 1970 in seconds
// OR-ed with the current millitime, and the elapsed time since the last deal
// structure:
//
// +----------------+---------+--------+
// | 31-17(16) | 15-8(8) | 7-0(8) |
// +----------------+---------+--------+
// | Time in secs |Millitime| E.T. |
// +----------------+---------+--------+
// 0 -- 65,536 0-255 0-255
// (~ 18 hrs) (top 10 bits)
//
struct _timeb timebuffer;
_ftime(&timebuffer);
int nElapsedTime = timebuffer.time - m_nPrevTime;
int nLeft = timebuffer.time << 16;
int nRight = (timebuffer.millitm << 6) | (nElapsedTime & 0x00FF);
nSeed = nLeft | nRight;
//
m_nPrevTime = timebuffer.time;
}
//
if (!bSuppressSeed)
srand(nSeed);
//
int nRand,nTempSuit,nTempVal;
CCard* pTempCard;
for(int x=0;x<10;x++)
{
for(int i=0;i<52;i++)
{
// switch the ith card with a random card
nRand = GetRandomValue(51);
if (nRand == i)
nRand = GetRandomValue(51); // try again
//
pTempCard = m_cards[i];
nTempSuit = pTempCard->m_nSuit;
nTempVal = pTempCard->m_nFaceValue;
//
m_cards[i] = m_cards[nRand];
m_nDeckIndex[m_cards[nRand]->m_nSuit][m_cards[nRand]->m_nFaceValue] = i;
//
m_cards[nRand] = pTempCard;
m_nDeckIndex[nTempSuit][nTempVal] = nRand;
}
}
// done
return nSeed;
}
//
// return a pointer to the specified value card
//
CCard* CDeck::GetCard(int nDeckValue)
{
if ((nDeckValue < 0) || (nDeckValue > 51))
return NULL;
return m_sortedCards[nDeckValue];
}
//
CCard* CDeck::GetCard(LPCTSTR pszName)
{
int nValue = StringToDeckValue(pszName);
return GetCard(nValue);
}
//
CCard* CDeck::GetCard(int nSuit, int nFaceValue)
{
int nDeckValue = MAKEDECKVALUE(nSuit, nFaceValue);
return GetCard(nDeckValue);
}
//
// property get/set operations
//
LPVOID CDeck::GetValuePV(int nItem, int nIndex1, int nIndex2, int nIndex3)
{
switch(nItem)
{
case tnCardBackIndex:
return (LPVOID) m_nCurrCardBack;
case tnumCardBacks:
return (LPVOID) m_numCardBacks;
case tpvCardBacksImageList:
return (LPVOID) &m_imageListCardBacks;
default:
AfxMessageBox("Unhandled Call to CDeck::GetValue()");
return 0;
}
return 0;
}
int CDeck::GetValue(int nItem, int nIndex1, int nIndex2, int nIndex3)
{
return (int) GetValuePV(nItem, nIndex1, nIndex2, nIndex3);
}
//
int CDeck::SetValue(int nItem, int nValue, int nIndex1, int nIndex2, int nIndex3)
{
switch(nItem)
{
case tnCardBackIndex:
if ((nIndex1 >= 0) && (nIndex1 < m_numCardBacks))
m_nCurrCardBack = nValue;
break;
case tnumCardBacks:
case tpvCardBacksImageList:
break;
default:
AfxMessageBox("Unhandled Call to CDeck::SetValue()");
return 0;
}
return 0;
}