1 /*
2 * (c) Copyright 1997, Qun Zhang.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without fee,
6 * provided that the above copyright notice appear in all copies and
7 * that both that copyright notice and this permission notice appear in
8 * supporting documentation, and that the name of Qun Zhang not be used
9 * in advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. Qun Zhang make no
11 * representations about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
13 *
14 * THE ABOVE-NAMED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE ABOVE-NAMED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 */
23
24 #include <stdlib.h>
25 #include <iostream>
26 #include "Card.h"
27 #include "Deck.h"
28
29 // Class Deck
30
31 Card Deck::_globalCards[54];
32 int Deck::_globalInit = 0;
33
Deck(int numOfCards)34 Deck::Deck(int numOfCards) : _numOfCards(numOfCards), _position(0)
35 {
36 if(_globalInit == 0){
37 for(int j =0; j < 54; j++)
38 _globalCards[j] = Card(j);
39 _globalInit = 1;
40 }
41 int i;
42 for(i =0 ; i < _numOfCards; i++)
43 _cards[i] = & _globalCards[i];
44 for(i=_numOfCards; i < 54; i++)
45 _cards[i] = (Card *) 0;
46 }
47
~Deck()48 Deck::~Deck(){}
49
Shuffle()50 void Deck::Shuffle() // shaffle 4 times
51 {
52 Card * tmpcards[54];
53 Card **from = _cards;
54 Card **to = tmpcards;
55 for(int j = 0; j <=4; j++)
56 {
57 for (int i = 0; i < _numOfCards/2; i++)
58 to[i*2] = from[_numOfCards/2+i];
59 for (int i = 0; i < _numOfCards/2; i++)
60 to[i*2+1] = from[i];
61 Card **tmp = from;
62 from = to;
63 to = tmp;
64 }
65 ((Deck*)this)->_position = 0;
66 #ifdef DEBUG
67 cout << " Shaffling : " << endl;
68 for ( int i = 0; i < _numOfCards; i++ ){
69 if( i%10 == 0 ) cout << endl;
70 cout <<_cards[i]->Suit() << (int)_cards[i]->Rank() << " ";
71 }
72 cout << endl;
73 #endif
74 }
75
Cut()76 void Deck::Cut()
77 {
78 Card *tmp[54];
79
80 srand(time(0));
81 int pos = rand()%10+2;
82 int cut = _numOfCards/pos;
83
84 for(int i=_numOfCards-cut; i< _numOfCards; i++)
85 tmp[i] = _cards[i];
86 for(int i = _numOfCards-1; i >= cut; i--)
87 _cards[i] = _cards[i-cut];
88 for(int i = 0; i < cut; i++)
89 _cards[ i ] = tmp[i + _numOfCards-cut];
90 #ifdef DEBUG
91 cout << "Cutting at " << _numOfCards-cut <<endl;
92 for (int i = 0; i < _numOfCards; i++ ){
93 if( i%10 == 0 ) cout << endl;
94 cout << _cards[i]->Suit() << (int)_cards[i]->Rank() << " ";
95 }
96 cout << endl;
97 #endif
98 }
99
Wash()100 void Deck::Wash()
101 {
102 Card *buff;
103 int pos;
104
105 srand(time(0));
106 int times = rand()%30;
107
108 for(int j=0; j < times; j++ )
109 for( int i = 0; i < _numOfCards; i++)
110 {
111 pos = rand()%_numOfCards;
112 buff = _cards[pos];
113 _cards[ pos ] = _cards[_numOfCards-1];
114 _cards[_numOfCards-1] = buff;
115 }
116 #ifdef DEBUG
117 cout << "Washing " <<endl;
118 for ( int i = 0; i < _numOfCards; i++ ){
119 if( i%10 == 0 ) cout << endl;
120 cout << _cards[i]->Suit() << (int)_cards[i]->Rank() << " ";
121 }
122 cout << endl;
123 #endif
124 }
125
126