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
|
#include <kdebug.h>
#include "deck.h"
#include "dealer.h"
#include <time.h>
#include <assert.h>
const int NumberOfCards = 52;
Deck *Deck::my_deck = 0;
Deck::Deck( Dealer* parent, int m, int s )
: Pile( 0, parent ), mult( m )
{
_deck = new Card * [mult*NumberOfCards];
Q_CHECK_PTR (_deck);
// only allow 1, 2, or 4 suits
if ( s == 1 || s == 2 )
suits = s;
else
suits = 4;
makedeck();
addToDeck();
shuffle();
setAddFlags(Pile::disallow);
setRemoveFlags(Pile::disallow);
}
Deck::~Deck()
{
for (uint i=0; i < mult*NumberOfCards; i++) {
delete _deck[i];
}
m_cards.clear();
delete [] _deck;
}
// ----------------------------------------------------------------
Deck *Deck::new_deck( Dealer *parent, int m, int s )
{
my_deck = new Deck(parent, m, s);
return my_deck;
}
void Deck::makedeck()
{
int i=0;
show();
for ( uint m = 0; m < mult; m++)
{
for ( int r = Card::Ace; r <= Card::King; r++)
{
for ( int s = Card::Spades-1; s >= Card::Clubs-1 ; s--)
{
_deck[i] = new Card(static_cast<Card::Rank>(r),
static_cast<Card::Suit>(Card::Spades - (s % suits)),
dealer()->canvas());
_deck[i]->move(x(), y());
i++;
}
}
}
}
void Deck::collectAndShuffle()
{
addToDeck();
shuffle();
}
Card* Deck::nextCard()
{
CardList::Iterator c;
c = m_cards.fromLast(); // Dealing from bottom of deck ....
if ( c != m_cards.end() ) {
return *c;
} else
return 0;
}
// ----------------------------------------------------------------
static long pseudoRandomSeed = 0;
static void pseudoRandom_srand(long seed)
{
pseudoRandomSeed=seed;
}
// Documented as in
// http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q28150
//
static long pseudoRandom_random() {
pseudoRandomSeed = 214013*pseudoRandomSeed+2531011;
return (pseudoRandomSeed >> 16) & 0x7fff;
}
// Shuffle deck, assuming all cards are in m_cards
void Deck::shuffle()
{
assert(m_cards.count() == uint(mult*NumberOfCards));
assert(dealer()->gameNumber() >= 0);
pseudoRandom_srand(dealer()->gameNumber());
kdDebug(11111) << "first card " << m_cards[0]->name() << " " << dealer()->gameNumber() << endl;
Card* t;
long z;
int left = mult*NumberOfCards;
for (uint i = 0; i < mult*NumberOfCards; i++) {
z = pseudoRandom_random() % left;
t = m_cards[z];
m_cards[z] = m_cards[left-1];
m_cards[left-1] = t;
left--;
}
}
// add cards in deck[] to Deck
// FIXME: Rename to collectCards()
void Deck::addToDeck()
{
clear();
for (uint i = 0; i < mult*NumberOfCards; i++) {
_deck[i]->setTakenDown(false);
add( _deck[i], true, false );
}
}
|