-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.cpp
406 lines (338 loc) · 9.17 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
403
404
405
406
// Written by Steve Coward
// PSU CS 202 Fall 2009 Project 1
//
// deck.cpp: implementation of the CDeck class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <stdlib.h> // srand & rand
#include <time.h> // system time
#include "DXUT.h"
#include "deck.h"
// constructor
CDeck::CDeck(void)
{
m_pD3D10Device = NULL;
int i;
for (i=0;i<NUM_CARD_VIEWS;i++) {
m_cardGraphics[i].pShaderResource = NULL;
m_cardGraphics[i].lpFileName[0] = '\0';
m_cardGraphics[i].lpCardDesc[0] = '\0';
m_cardGraphics[i].ID = 0;
}
resetDeck(false);
#if defined(_DEBUG)
////////////////////////////////
// Testing
// Bang on the list swap functions
////////////////////////////////
{
bool bTestResult = true;
//bTestResult = m_deck.testDriveSwap();
if (!bTestResult) {
std::cout << "ERROR in testDriveSwap()!!\n" << std::flush;
}
}
#endif // _DEBUG
return;
}
CDeck::CDeck(ID3D10Device* pD3D10Device, bool bFull)
{
int i;
setDevice(pD3D10Device);
for (i=0;i<NUM_CARD_VIEWS;i++) {
m_cardGraphics[i].pShaderResource = NULL;
m_cardGraphics[i].lpFileName[0] = '\0';
m_cardGraphics[i].lpCardDesc[0] = '\0';
m_cardGraphics[i].ID = 0;
}
resetDeck(bFull);
#if defined(_DEBUG)
////////////////////////////////
// Testing
// Bang on the list swap functions
////////////////////////////////
{
bool bTestResult = true;
//bTestResult = m_deck.testDriveSwap();
if (!bTestResult) {
std::cout << "ERROR in testDriveSwap()!!\n" << std::flush;
}
}
#endif // _DEBUG
InitSprite(getDevice(), bFull);
return;
}
// destructor
CDeck::~CDeck(void)
{
int i;
// Release graphic texture for each card
for (i=0;i<NUM_CARD_VIEWS;i++) {
if (m_cardGraphics[i].pShaderResource != NULL) {
m_cardGraphics[i].pShaderResource->Release();
m_cardGraphics[i].pShaderResource = NULL;
}
}
EmptyHand();
return;
}
// assumes that deck is full of cards
// if bFull, create a full deck, otherwise create an empty deck
// empty decks get cards from other non empty decks
HRESULT CDeck::InitSprite(ID3D10Device* pD3D10Device, bool bFull)
{
HRESULT hr = S_OK;
D3DX10_IMAGE_INFO InfoFromFile;
D3DX10_IMAGE_LOAD_INFO LoadImageInfo;
const_deckIterator deckitr;
int i;
DWORD nDirectoryLength = 255;
TCHAR lpCurrentDir[255];
if (bFull) {
// index 0 refers to back side of card
LPTSTR lpCardName[NUM_CARD_VIEWS] = { L"b1fv",
L"h1", L"h2", L"h3", L"h4", L"h5", L"h6", L"h7", L"h8", L"h9", L"h10", L"hj", L"hq", L"hk",
L"d1", L"d2", L"d3", L"d4", L"d5", L"d6", L"d7", L"d8", L"d9", L"d10", L"dj", L"dq", L"dk",
L"c1", L"c2", L"c3", L"c4", L"c5", L"c6", L"c7", L"c8", L"c9", L"c10", L"cj", L"cq", L"ck",
L"s1", L"s2", L"s3", L"s4", L"s5", L"s6", L"s7", L"s8", L"s9", L"s10", L"sj", L"sq", L"sk",
};
// set up file path
GetCurrentDirectory(nDirectoryLength, lpCurrentDir);
lstrcat(lpCurrentDir, (LPCTSTR)L"\\Graphics\\");
deckitr = getDeckIterator();
for (i=0;i<NUM_CARD_VIEWS;i++) {
if (m_cardGraphics[i].pShaderResource == NULL) {
m_cardGraphics[i].ID = i;
m_cardGraphics[i].lpCardDesc[0] = '\0';
lstrcpy((LPWSTR)m_cardGraphics[i].lpFileName, lpCurrentDir);
lstrcat((LPWSTR)m_cardGraphics[i].lpFileName, lpCardName[i]);
lstrcat((LPWSTR)m_cardGraphics[i].lpFileName, L".png");
hr = D3DX10GetImageInfoFromFile((LPWSTR)m_cardGraphics[i].lpFileName, NULL, &InfoFromFile, NULL);
if (FAILED(hr)) {
return -1;
}
LoadImageInfo.Width = InfoFromFile.Width;
LoadImageInfo.Height = InfoFromFile.Height;
LoadImageInfo.Depth = InfoFromFile.Depth;
LoadImageInfo.FirstMipLevel = 1;
LoadImageInfo.MipLevels = InfoFromFile.MipLevels;
LoadImageInfo.Usage = D3D10_USAGE_DEFAULT;
LoadImageInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE;
LoadImageInfo.CpuAccessFlags = 0;
LoadImageInfo.MiscFlags = 0;
LoadImageInfo.Format = InfoFromFile.Format;
LoadImageInfo.Filter = D3DX10_FILTER_NONE;
LoadImageInfo.MipFilter = D3DX10_FILTER_NONE;
LoadImageInfo.pSrcInfo = &InfoFromFile;
hr = D3DX10CreateShaderResourceViewFromFile(pD3D10Device, (LPWSTR)m_cardGraphics[i].lpFileName, &LoadImageInfo, NULL, &(m_cardGraphics[i].pShaderResource), NULL);
if (FAILED(hr)) {
return hr;
}
// now assign textures to each card.
// assume that deck has not been shuffled
// and is in same order as lpCardName array.
if ((deckitr != NULL ) && (i >= 1)) {
POINT pt;
pt.x = 100;
pt.y = 100;
deckitr->data->setFrontTexture(m_cardGraphics[i].pShaderResource);
deckitr->data->setBackTexture(m_cardGraphics[0].pShaderResource);
deckitr->data->setSpriteWidth(InfoFromFile.Width);
deckitr->data->setSpriteLength(InfoFromFile.Height);
deckitr->data->setFileName(m_cardGraphics[i].lpFileName);
deckitr->data->setLocation(pt);
deckitr = deckitr->next;
}
}
else {
if ((deckitr != NULL ) && (i >= 1)) {
deckitr->data->setFrontTexture(m_cardGraphics[i].pShaderResource);
deckitr->data->setBackTexture(m_cardGraphics[0].pShaderResource);
deckitr = deckitr->next;
}
}
}
}
return S_OK;
}
// return the number of cards in the deck
int CDeck::CountDeck(void)
{
return(m_deck.size());
}
// returns top card and removes it from the deck
CCard* CDeck::DealCard(void)
{
return(m_deck.pop_front());
}
// add card to the bottom of the deck
void CDeck::AddCard(CCard* thiscard)
{
m_deck.push_back(thiscard);
return;
}
// take out a card at num position
CCard* CDeck::DealCard(int num)
{
return(m_deck.erase(num));
}
// show a card at num position
CCard* CDeck::GetCard(int num) const
{
int i = 0;
const_deckIterator deckitr;
deckitr = getDeckIterator();
while ((deckitr != NULL) && (i != num)) {
deckitr = deckitr->next;
i++;
}
if (deckitr != NULL) {
return(deckitr->data);
}
else {
return(NULL);
}
}
// Turn all cards either face up or face down depending on bShow
void CDeck::ShowDeck(bool bShow)
{
const_deckIterator deckitr;
deckitr = getDeckIterator();
while (deckitr != NULL) {
deckitr->data->setFaceUp(bShow);
deckitr = deckitr->next;
}
return;
}
// finds a num card and sets its value to thiscard
void CDeck::SetCard(CCard thiscard, int num)
{
int i = 0;
const_deckIterator deckitr;
deckitr = getDeckIterator();
while ((deckitr != NULL) && (i != num)) {
deckitr = deckitr->next;
i++;
}
if (deckitr != NULL) {
deckitr->data->setCard(thiscard.getRank(), thiscard.getSuit());
}
return;
}
// shuffles deck
// Each position in the deck will have the opportunity to be
// randomly swapped with at least one other random location
// in the deck.
void CDeck::shuffle(void)
{
int i;
int x;
#if defined(_DEBUG)
// non randomly set seed for random number generator
//srand(1);
srand((unsigned int)time(NULL));
#else
// use current time to randomly set seed for random number generator
srand((unsigned int)time(NULL));
#endif // _DEBUG
for (i=0; i<m_deck.size(); i++) {
x = rand() % m_deck.size();
m_deck.swapPos(i, x);
}
return;
}
// resets deck
void CDeck::resetDeck(bool bFull)
{
CCard* pCard;
POINT pt;
pt.x = 100;
pt.y = 100;
EmptyHand();
if (bFull) {
// initialize the 52 card deck
for(CCard::Suit suit = CCard::Hearts; suit <= CCard::Spades; suit++)
{
for(CCard::Rank rank = CCard::Ace; rank <= CCard::King; rank++)
{
pCard = new CCard();
pCard->setCard(rank, suit);
pCard->setFaceUp(false);
pCard->setFileName(L"");
pCard->setLocation(pt);
pCard->setSpriteLength(0);
pCard->setSpriteWidth(0);
pCard->setFrontTexture(NULL);
pCard->setBackTexture(NULL);
AddCard(pCard);
}
}
}
if (m_pD3D10Device != NULL) {
InitSprite(m_pD3D10Device, bFull);
}
return;
}
// deletes all cards in deck.
// resets top of deck pointer.
// All memory is properly deallocated.
void CDeck::EmptyHand(void)
{
while (!m_deck.empty()) {
m_deck.erase(0);
}
return;
}
// display the cards in the deck on the console
// for console app only
void CDeck::displayDeck(void) const
{
const_deckIterator deckitr;
char chCardRank[3];
char chCardSuit[2];
deckitr = getDeckIterator();
while (deckitr != NULL) {
deckitr->data->translateCard(chCardRank, chCardSuit);
std::cout << chCardRank << chCardSuit << " ";
deckitr = deckitr->next;
}
std::cout << "\n" << std::flush;
return;
}
// computes and returns the 'value' of the deck
// This should be overloaded based on card game being implemented.
int CDeck::getDeckValue(void) const
{
return(0);
}
// ISSUE: draw procedure should be const
// ISSUE:
// This draw routine assumes that pSprite->Begin() called before and
// pSprite->End() is called after.
HRESULT CDeck::draw(ID3DX10Sprite * pSprite, D3DX10_SPRITE* pSpriteArray, int x, int y, int xOffset, int yOffset) const
{
HRESULT hResult = S_OK;
int i = 0;
int xCard, yCard;
// draw each of the player's cards
xCard = x;
yCard = y;
const_deckIterator deckitr;
deckitr = getDeckIterator();
if (deckitr != NULL) {
while (deckitr != NULL) {
deckitr->data->setLocation(xCard, yCard);
hResult = deckitr->data->draw(pSprite, pSpriteArray+i);
xCard += xOffset;
yCard -= yOffset;
i++;
deckitr = deckitr->next;
}
}
// draw the set of sprites to the back buffer
if (i>0) {
hResult = pSprite->DrawSpritesImmediate(pSpriteArray, i, 0, 0);
}
return(i);
}