Diffstat (limited to 'noncore/games/solitaire/carddeck.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/solitaire/carddeck.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/noncore/games/solitaire/carddeck.cpp b/noncore/games/solitaire/carddeck.cpp new file mode 100644 index 0000000..87c043a --- a/dev/null +++ b/noncore/games/solitaire/carddeck.cpp | |||
@@ -0,0 +1,81 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include <stdlib.h> | ||
21 | #include <time.h> | ||
22 | #include "card.h" | ||
23 | #include "carddeck.h" | ||
24 | |||
25 | |||
26 | CardDeck::CardDeck(int jokers) : numberOfJokers(jokers), deckCreated(FALSE) | ||
27 | { | ||
28 | cards = new (Card *)[getNumberOfCards()]; | ||
29 | } | ||
30 | |||
31 | |||
32 | CardDeck::~CardDeck() | ||
33 | { | ||
34 | for (int i = 0; i < getNumberOfCards(); i++) | ||
35 | delete cards[i]; | ||
36 | delete cards; | ||
37 | } | ||
38 | |||
39 | |||
40 | void CardDeck::createDeck() | ||
41 | { | ||
42 | if (!deckCreated) { | ||
43 | for (int i = 0; i < 52; i++) | ||
44 | cards[i] = newCard( (eValue)((i % 13) + 1), (eSuit)((i / 13) + 1), FALSE ); | ||
45 | for (int i = 0; i < getNumberOfJokers(); i++) | ||
46 | cards[52 + i] = newCard( jokerVal, jokerSuit, FALSE ); | ||
47 | deckCreated = TRUE; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | |||
52 | void CardDeck::shuffle() | ||
53 | { | ||
54 | srand(time(NULL)); | ||
55 | for (int i = 0; i < getNumberOfCards(); i++) { | ||
56 | int index = rand() % getNumberOfCards(); | ||
57 | Card *tmpCard = cards[i]; | ||
58 | cards[i] = cards[index]; | ||
59 | cards[index] = tmpCard; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | |||
64 | int CardDeck::getNumberOfCards() | ||
65 | { | ||
66 | return 52 + getNumberOfJokers(); | ||
67 | } | ||
68 | |||
69 | |||
70 | int CardDeck::getNumberOfJokers() | ||
71 | { | ||
72 | return numberOfJokers; | ||
73 | } | ||
74 | |||
75 | |||
76 | Card *CardDeck::newCard( eValue v, eSuit s, bool f ) | ||
77 | { | ||
78 | return new Card(v, s, f); | ||
79 | } | ||
80 | |||
81 | |||