Diffstat (limited to 'noncore/games/solitaire/patiencecardgame.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/solitaire/patiencecardgame.cpp | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/noncore/games/solitaire/patiencecardgame.cpp b/noncore/games/solitaire/patiencecardgame.cpp new file mode 100644 index 0000000..5a9326a --- a/dev/null +++ b/noncore/games/solitaire/patiencecardgame.cpp | |||
@@ -0,0 +1,234 @@ | |||
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 "patiencecardgame.h" | ||
21 | |||
22 | |||
23 | int highestZ = 0; | ||
24 | |||
25 | |||
26 | PatienceCardGame::PatienceCardGame(QCanvas *c, bool snap, QWidget *parent) : CanvasCardGame(*c, snap, parent) | ||
27 | { | ||
28 | numberOfTimesThroughDeck = 0; | ||
29 | highestZ = 0; | ||
30 | |||
31 | circleCross = new CanvasCircleOrCross( 7, 18, canvas() ); | ||
32 | rectangle = new CanvasRoundRect( 35, 10, canvas() ); | ||
33 | |||
34 | for (int i = 0; i < 4; i++) { | ||
35 | discardPiles[i] = new PatienceDiscardPile( 110 + i * 30, 10, canvas() ); | ||
36 | addCardPile(discardPiles[i]); | ||
37 | } | ||
38 | for (int i = 0; i < 7; i++) { | ||
39 | workingPiles[i] = new PatienceWorkingPile( 10 + i * 30, 50, canvas() ); | ||
40 | addCardPile(workingPiles[i]); | ||
41 | } | ||
42 | faceDownDealingPile = new PatienceFaceDownDeck( 5, 10, canvas() ); | ||
43 | faceUpDealingPile = new PatienceFaceUpDeck( 35, 10, canvas() ); | ||
44 | } | ||
45 | |||
46 | |||
47 | PatienceCardGame::~PatienceCardGame() | ||
48 | { | ||
49 | delete circleCross; | ||
50 | delete rectangle; | ||
51 | delete faceDownDealingPile; | ||
52 | delete faceUpDealingPile; | ||
53 | } | ||
54 | |||
55 | |||
56 | void PatienceCardGame::deal(void) | ||
57 | { | ||
58 | highestZ = 1; | ||
59 | int t = 0; | ||
60 | |||
61 | beginDealing(); | ||
62 | |||
63 | for (int i = 0; i < 7; i++) { | ||
64 | cards[t]->setFace(TRUE); | ||
65 | for (int k = i; k < 7; k++, t++) { | ||
66 | Card *card = cards[t]; | ||
67 | workingPiles[k]->addCardToTop(card); | ||
68 | card->setCardPile( workingPiles[k] ); | ||
69 | QPoint p = workingPiles[k]->getCardPos( card ); | ||
70 | card->setPos( p.x(), p.y(), highestZ ); | ||
71 | card->showCard(); | ||
72 | highestZ++; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | for ( ; t < 52; t++) { | ||
77 | Card *card = cards[t]; | ||
78 | faceDownDealingPile->addCardToTop(card); | ||
79 | card->setCardPile( faceDownDealingPile ); | ||
80 | QPoint p = faceDownDealingPile->getCardPos( card ); | ||
81 | card->setPos( p.x(), p.y(), highestZ ); | ||
82 | card->showCard(); | ||
83 | highestZ++; | ||
84 | } | ||
85 | |||
86 | endDealing(); | ||
87 | } | ||
88 | |||
89 | |||
90 | void PatienceCardGame::readConfig( Config& cfg ) | ||
91 | { | ||
92 | cfg.setGroup("GameState"); | ||
93 | |||
94 | // Do we have a config file to read in? | ||
95 | if ( !cfg.hasKey("numberOfTimesThroughDeck") ) { | ||
96 | // if not, create a new game | ||
97 | newGame(); | ||
98 | return; | ||
99 | } | ||
100 | // We have a config file, lets read it in and use it | ||
101 | |||
102 | // Create Cards, but don't shuffle or deal them yet | ||
103 | createDeck(); | ||
104 | |||
105 | // How many times through the deck have we been | ||
106 | numberOfTimesThroughDeck = cfg.readNumEntry("NumberOfTimesThroughDeck"); | ||
107 | |||
108 | // restore state to the circle/cross under the dealing pile | ||
109 | if ( canTurnOverDeck() ) | ||
110 | circleCross->setCircle(); | ||
111 | else | ||
112 | circleCross->setCross(); | ||
113 | |||
114 | // Move the cards to their piles (deal them to their previous places) | ||
115 | beginDealing(); | ||
116 | |||
117 | highestZ = 1; | ||
118 | |||
119 | for (int k = 0; k < 7; k++) { | ||
120 | QString pile; | ||
121 | pile.sprintf( "WorkingPile%i", k ); | ||
122 | readPile( cfg, workingPiles[k], pile, highestZ ); | ||
123 | } | ||
124 | |||
125 | for (int k = 0; k < 4; k++) { | ||
126 | QString pile; | ||
127 | pile.sprintf( "DiscardPile%i", k ); | ||
128 | readPile( cfg, discardPiles[k], pile, highestZ ); | ||
129 | } | ||
130 | |||
131 | readPile( cfg, faceDownDealingPile, "FaceDownDealingPile", highestZ ); | ||
132 | readPile( cfg, faceUpDealingPile, "FaceUpDealingPile", highestZ ); | ||
133 | |||
134 | highestZ++; | ||
135 | |||
136 | endDealing(); | ||
137 | } | ||
138 | |||
139 | |||
140 | void PatienceCardGame::writeConfig( Config& cfg ) | ||
141 | { | ||
142 | cfg.setGroup("GameState"); | ||
143 | cfg.writeEntry("numberOfTimesThroughDeck", numberOfTimesThroughDeck); | ||
144 | |||
145 | for ( int i = 0; i < 7; i++ ) { | ||
146 | QString pile; | ||
147 | pile.sprintf( "WorkingPile%i", i ); | ||
148 | workingPiles[i]->writeConfig( cfg, pile ); | ||
149 | } | ||
150 | for ( int i = 0; i < 4; i++ ) { | ||
151 | QString pile; | ||
152 | pile.sprintf( "DiscardPile%i", i ); | ||
153 | discardPiles[i]->writeConfig( cfg, pile ); | ||
154 | } | ||
155 | faceDownDealingPile->writeConfig( cfg, "FaceDownDealingPile" ); | ||
156 | faceUpDealingPile->writeConfig( cfg, "FaceUpDealingPile" ); | ||
157 | } | ||
158 | |||
159 | |||
160 | bool PatienceCardGame::mousePressCard( Card *card, QPoint p ) | ||
161 | { | ||
162 | Q_UNUSED(p); | ||
163 | |||
164 | CanvasCard *item = (CanvasCard *)card; | ||
165 | if (item->isFacing() != TRUE) { | ||
166 | // From facedown stack | ||
167 | if ((item->x() == 5) && ((int)item->y() == 10)) { | ||
168 | item->setZ(highestZ); | ||
169 | highestZ++; | ||
170 | |||
171 | // Added Code | ||
172 | faceDownDealingPile->removeCard(item); | ||
173 | faceUpDealingPile->addCardToTop(item); | ||
174 | item->setCardPile( faceUpDealingPile ); | ||
175 | |||
176 | item->flipTo( 35, (int)item->y() ); | ||
177 | } | ||
178 | moving = NULL; | ||
179 | moved = FALSE; | ||
180 | |||
181 | // move two other cards if we flip three at a time | ||
182 | int flipped = 1; | ||
183 | QCanvasItemList l = canvas()->collisions( p ); | ||
184 | for (QCanvasItemList::Iterator it = l.begin(); (it != l.end()) && (flipped != cardsDrawn()); ++it) { | ||
185 | if ( (*it)->rtti() == canvasCardId ) { | ||
186 | CanvasCard *item = (CanvasCard *)*it; | ||
187 | if (item->animated()) | ||
188 | continue; | ||
189 | item->setZ(highestZ); | ||
190 | highestZ++; | ||
191 | flipped++; | ||
192 | |||
193 | // Added Code | ||
194 | faceDownDealingPile->removeCard(item); | ||
195 | faceUpDealingPile->addCardToTop(item); | ||
196 | item->setCardPile( faceUpDealingPile ); | ||
197 | |||
198 | item->flipTo( 35, (int)item->y(), 8 * flipped ); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | return TRUE; | ||
203 | } | ||
204 | |||
205 | return FALSE; | ||
206 | } | ||
207 | |||
208 | |||
209 | void PatienceCardGame::mousePress(QPoint p) | ||
210 | { | ||
211 | if ( canTurnOverDeck() && | ||
212 | (p.x() > 5) && (p.x() < 28) && | ||
213 | (p.y() > 10) && (p.y() < 46) ) { | ||
214 | |||
215 | beginDealing(); | ||
216 | Card *card = faceUpDealingPile->cardOnTop(); | ||
217 | while ( card ) { | ||
218 | card->setPos( 5, 10, highestZ ); | ||
219 | card->setFace( FALSE ); | ||
220 | faceUpDealingPile->removeCard( card ); | ||
221 | faceDownDealingPile->addCardToTop( card ); | ||
222 | card->setCardPile( faceDownDealingPile ); | ||
223 | card = faceUpDealingPile->cardOnTop(); | ||
224 | highestZ++; | ||
225 | } | ||
226 | endDealing(); | ||
227 | |||
228 | throughDeck(); | ||
229 | |||
230 | moved = TRUE; | ||
231 | } | ||
232 | } | ||
233 | |||
234 | |||