-rw-r--r-- | noncore/games/solitaire/patiencecardgame.cpp | 294 |
1 files changed, 158 insertions, 136 deletions
diff --git a/noncore/games/solitaire/patiencecardgame.cpp b/noncore/games/solitaire/patiencecardgame.cpp index 1b38072..96a599c 100644 --- a/noncore/games/solitaire/patiencecardgame.cpp +++ b/noncore/games/solitaire/patiencecardgame.cpp | |||
@@ -1,258 +1,280 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 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. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | #include <qgfx_qws.h> | 20 | #include <qgfx_qws.h> |
21 | #include "patiencecardgame.h" | 21 | #include "patiencecardgame.h" |
22 | 22 | ||
23 | 23 | ||
24 | int highestZ = 0; | 24 | int highestZ = 0; |
25 | 25 | ||
26 | 26 | ||
27 | PatienceCardGame::PatienceCardGame(QCanvas *c, bool snap, QWidget *parent) : CanvasCardGame(*c, snap, parent) | 27 | PatienceCardGame::PatienceCardGame(QCanvas *c, bool snap, QWidget *parent) : CanvasCardGame(*c, snap, parent) |
28 | { | 28 | { |
29 | numberOfTimesThroughDeck = 0; | 29 | numberOfTimesThroughDeck = 0; |
30 | highestZ = 0; | 30 | highestZ = 0; |
31 | 31 | ||
32 | if ( qt_screen->deviceWidth() < 200 ) { | 32 | if ( qt_screen->deviceWidth() < 200 ) |
33 | circleCross = new CanvasCircleOrCross( 7, 16, canvas() ); | 33 | { |
34 | rectangle = new CanvasRoundRect( 30, 10, canvas() ); | 34 | circleCross = new CanvasCircleOrCross( 7, 16, canvas() ); |
35 | 35 | rectangle = new CanvasRoundRect( 30, 10, canvas() ); | |
36 | for (int i = 0; i < 4; i++) { | 36 | |
37 | discardPiles[i] = new PatienceDiscardPile( 78 + i * 23, 10, canvas() ); | 37 | for (int i = 0; i < 4; i++) |
38 | addCardPile(discardPiles[i]); | 38 | { |
39 | } | 39 | discardPiles[i] = new PatienceDiscardPile( 78 + i * 23, 10, canvas() ); |
40 | for (int i = 0; i < 7; i++) { | 40 | addCardPile(discardPiles[i]); |
41 | workingPiles[i] = new PatienceWorkingPile( 5 + i * 23, 50, canvas() ); | 41 | } |
42 | addCardPile(workingPiles[i]); | 42 | for (int i = 0; i < 7; i++) |
43 | } | 43 | { |
44 | faceDownDealingPile = new PatienceFaceDownDeck( 5, 10, canvas() ); | 44 | workingPiles[i] = new PatienceWorkingPile( 5 + i * 23, 50, canvas() ); |
45 | faceUpDealingPile = new PatienceFaceUpDeck( 30, 10, canvas() ); | 45 | addCardPile(workingPiles[i]); |
46 | } else { | 46 | } |
47 | circleCross = new CanvasCircleOrCross( 7, 18, canvas() ); | 47 | faceDownDealingPile = new PatienceFaceDownDeck( 5, 10, canvas() ); |
48 | rectangle = new CanvasRoundRect( 35, 10, canvas() ); | 48 | faceUpDealingPile = new PatienceFaceUpDeck( 30, 10, canvas() ); |
49 | 49 | } | |
50 | for (int i = 0; i < 4; i++) { | 50 | else |
51 | discardPiles[i] = new PatienceDiscardPile( 110 + i * 30, 10, canvas() ); | 51 | { |
52 | addCardPile(discardPiles[i]); | 52 | circleCross = new CanvasCircleOrCross( 7, 18, canvas() ); |
53 | } | 53 | rectangle = new CanvasRoundRect( 35, 10, canvas() ); |
54 | for (int i = 0; i < 7; i++) { | 54 | |
55 | workingPiles[i] = new PatienceWorkingPile( 10 + i * 30, 50, canvas() ); | 55 | for (int i = 0; i < 4; i++) |
56 | addCardPile(workingPiles[i]); | 56 | { |
57 | } | 57 | discardPiles[i] = new PatienceDiscardPile( 110 + i * 30, 10, canvas() ); |
58 | faceDownDealingPile = new PatienceFaceDownDeck( 5, 10, canvas() ); | 58 | addCardPile(discardPiles[i]); |
59 | faceUpDealingPile = new PatienceFaceUpDeck( 35, 10, canvas() ); | 59 | } |
60 | for (int i = 0; i < 7; i++) | ||
61 | { | ||
62 | workingPiles[i] = new PatienceWorkingPile( 10 + i * 30, 50, canvas() ); | ||
63 | addCardPile(workingPiles[i]); | ||
64 | } | ||
65 | faceDownDealingPile = new PatienceFaceDownDeck( 5, 10, canvas() ); | ||
66 | faceUpDealingPile = new PatienceFaceUpDeck( 35, 10, canvas() ); | ||
60 | } | 67 | } |
61 | } | 68 | } |
62 | 69 | ||
63 | 70 | ||
64 | PatienceCardGame::~PatienceCardGame() | 71 | PatienceCardGame::~PatienceCardGame() |
65 | { | 72 | { |
66 | delete circleCross; | 73 | delete circleCross; |
67 | delete rectangle; | 74 | delete rectangle; |
68 | delete faceDownDealingPile; | 75 | delete faceDownDealingPile; |
69 | delete faceUpDealingPile; | 76 | delete faceUpDealingPile; |
70 | } | 77 | } |
71 | 78 | ||
72 | 79 | ||
73 | void PatienceCardGame::deal(void) | 80 | void PatienceCardGame::deal(void) |
74 | { | 81 | { |
75 | highestZ = 1; | 82 | highestZ = 1; |
76 | int t = 0; | 83 | int t = 0; |
77 | 84 | ||
78 | beginDealing(); | 85 | beginDealing(); |
79 | 86 | ||
80 | for (int i = 0; i < 7; i++) { | 87 | for (int i = 0; i < 7; i++) |
81 | cards[t]->setFace(TRUE); | 88 | { |
82 | for (int k = i; k < 7; k++, t++) { | 89 | cards[t]->setFace(TRUE); |
83 | Card *card = cards[t]; | 90 | for (int k = i; k < 7; k++, t++) |
84 | workingPiles[k]->addCardToTop(card); | 91 | { |
85 | card->setCardPile( workingPiles[k] ); | 92 | Card *card = cards[t]; |
86 | QPoint p = workingPiles[k]->getCardPos( card ); | 93 | workingPiles[k]->addCardToTop(card); |
87 | card->setPos( p.x(), p.y(), highestZ ); | 94 | card->setCardPile( workingPiles[k] ); |
88 | card->showCard(); | 95 | QPoint p = workingPiles[k]->getCardPos( card ); |
89 | highestZ++; | 96 | card->setPos( p.x(), p.y(), highestZ ); |
90 | } | 97 | card->showCard(); |
98 | highestZ++; | ||
99 | } | ||
91 | } | 100 | } |
92 | 101 | ||
93 | for ( ; t < 52; t++) { | 102 | for ( ; t < 52; t++) |
94 | Card *card = cards[t]; | 103 | { |
95 | faceDownDealingPile->addCardToTop(card); | 104 | Card *card = cards[t]; |
96 | card->setCardPile( faceDownDealingPile ); | 105 | faceDownDealingPile->addCardToTop(card); |
97 | QPoint p = faceDownDealingPile->getCardPos( card ); | 106 | card->setCardPile( faceDownDealingPile ); |
98 | card->setPos( p.x(), p.y(), highestZ ); | 107 | QPoint p = faceDownDealingPile->getCardPos( card ); |
99 | card->showCard(); | 108 | card->setPos( p.x(), p.y(), highestZ ); |
100 | highestZ++; | 109 | card->showCard(); |
110 | highestZ++; | ||
101 | } | 111 | } |
102 | 112 | ||
103 | endDealing(); | 113 | endDealing(); |
104 | } | 114 | } |
105 | 115 | ||
106 | 116 | ||
107 | void PatienceCardGame::readConfig( Config& cfg ) | 117 | void PatienceCardGame::readConfig( Config& cfg ) |
108 | { | 118 | { |
109 | cfg.setGroup("GameState"); | 119 | cfg.setGroup("GameState"); |
110 | 120 | ||
111 | // Do we have a config file to read in? | 121 | // Do we have a config file to read in? |
112 | if ( !cfg.hasKey("numberOfTimesThroughDeck") ) { | 122 | if ( !cfg.hasKey("numberOfTimesThroughDeck") ) |
113 | // if not, create a new game | 123 | { |
114 | newGame(); | 124 | // if not, create a new game |
115 | return; | 125 | newGame(); |
126 | return; | ||
116 | } | 127 | } |
117 | // We have a config file, lets read it in and use it | 128 | // We have a config file, lets read it in and use it |
118 | 129 | ||
119 | // Create Cards, but don't shuffle or deal them yet | 130 | // Create Cards, but don't shuffle or deal them yet |
120 | createDeck(); | 131 | createDeck(); |
121 | 132 | ||
122 | // How many times through the deck have we been | 133 | // How many times through the deck have we been |
123 | numberOfTimesThroughDeck = cfg.readNumEntry("NumberOfTimesThroughDeck"); | 134 | numberOfTimesThroughDeck = cfg.readNumEntry("numberOfTimesThroughDeck"); |
124 | 135 | ||
125 | // restore state to the circle/cross under the dealing pile | 136 | // restore state to the circle/cross under the dealing pile |
126 | if ( canTurnOverDeck() ) | 137 | if ( canTurnOverDeck() ) |
127 | circleCross->setCircle(); | 138 | circleCross->setCircle(); |
128 | else | 139 | else |
129 | circleCross->setCross(); | 140 | circleCross->setCross(); |
130 | 141 | ||
131 | // Move the cards to their piles (deal them to their previous places) | 142 | // Move the cards to their piles (deal them to their previous places) |
132 | beginDealing(); | 143 | beginDealing(); |
133 | 144 | ||
134 | highestZ = 1; | 145 | highestZ = 1; |
135 | 146 | ||
136 | for (int k = 0; k < 7; k++) { | 147 | for (int k = 0; k < 7; k++) |
137 | QString pile; | 148 | { |
138 | pile.sprintf( "WorkingPile%i", k ); | 149 | QString pile; |
139 | readPile( cfg, workingPiles[k], pile, highestZ ); | 150 | pile.sprintf( "WorkingPile%i", k ); |
151 | readPile( cfg, workingPiles[k], pile, highestZ ); | ||
140 | } | 152 | } |
141 | 153 | ||
142 | for (int k = 0; k < 4; k++) { | 154 | for (int k = 0; k < 4; k++) |
143 | QString pile; | 155 | { |
144 | pile.sprintf( "DiscardPile%i", k ); | 156 | QString pile; |
145 | readPile( cfg, discardPiles[k], pile, highestZ ); | 157 | pile.sprintf( "DiscardPile%i", k ); |
158 | readPile( cfg, discardPiles[k], pile, highestZ ); | ||
146 | } | 159 | } |
147 | 160 | ||
148 | readPile( cfg, faceDownDealingPile, "FaceDownDealingPile", highestZ ); | 161 | readPile( cfg, faceDownDealingPile, "FaceDownDealingPile", highestZ ); |
149 | readPile( cfg, faceUpDealingPile, "FaceUpDealingPile", highestZ ); | 162 | readPile( cfg, faceUpDealingPile, "FaceUpDealingPile", highestZ ); |
150 | 163 | ||
151 | highestZ++; | 164 | highestZ++; |
152 | 165 | ||
153 | endDealing(); | 166 | endDealing(); |
154 | } | 167 | } |
155 | 168 | ||
156 | 169 | ||
157 | void PatienceCardGame::writeConfig( Config& cfg ) | 170 | void PatienceCardGame::writeConfig( Config& cfg ) |
158 | { | 171 | { |
159 | cfg.setGroup("GameState"); | 172 | cfg.setGroup("GameState"); |
160 | cfg.writeEntry("numberOfTimesThroughDeck", numberOfTimesThroughDeck); | 173 | cfg.writeEntry("numberOfTimesThroughDeck", numberOfTimesThroughDeck); |
161 | 174 | ||
162 | for ( int i = 0; i < 7; i++ ) { | 175 | for ( int i = 0; i < 7; i++ ) |
163 | QString pile; | 176 | { |
164 | pile.sprintf( "WorkingPile%i", i ); | 177 | QString pile; |
165 | workingPiles[i]->writeConfig( cfg, pile ); | 178 | pile.sprintf( "WorkingPile%i", i ); |
179 | workingPiles[i]->writeConfig( cfg, pile ); | ||
166 | } | 180 | } |
167 | for ( int i = 0; i < 4; i++ ) { | 181 | for ( int i = 0; i < 4; i++ ) |
168 | QString pile; | 182 | { |
169 | pile.sprintf( "DiscardPile%i", i ); | 183 | QString pile; |
170 | discardPiles[i]->writeConfig( cfg, pile ); | 184 | pile.sprintf( "DiscardPile%i", i ); |
185 | discardPiles[i]->writeConfig( cfg, pile ); | ||
171 | } | 186 | } |
172 | faceDownDealingPile->writeConfig( cfg, "FaceDownDealingPile" ); | 187 | faceDownDealingPile->writeConfig( cfg, "FaceDownDealingPile" ); |
173 | faceUpDealingPile->writeConfig( cfg, "FaceUpDealingPile" ); | 188 | faceUpDealingPile->writeConfig( cfg, "FaceUpDealingPile" ); |
174 | } | 189 | } |
175 | 190 | ||
176 | 191 | ||
177 | bool PatienceCardGame::mousePressCard( Card *card, QPoint p ) | 192 | bool PatienceCardGame::mousePressCard( Card *card, QPoint p ) |
178 | { | 193 | { |
179 | Q_UNUSED(p); | 194 | Q_UNUSED(p); |
180 | 195 | ||
181 | CanvasCard *item = (CanvasCard *)card; | 196 | CanvasCard *item = (CanvasCard *)card; |
182 | if (item->isFacing() != TRUE) { | 197 | if (item->isFacing() != TRUE) |
183 | // From facedown stack | 198 | { |
184 | if ((item->x() == 5) && ((int)item->y() == 10)) { | 199 | // From facedown stack |
185 | item->setZ(highestZ); | 200 | if ((item->x() == 5) && ((int)item->y() == 10)) |
186 | highestZ++; | 201 | { |
187 | 202 | item->setZ(highestZ); | |
188 | // Added Code | 203 | highestZ++; |
189 | faceDownDealingPile->removeCard(item); | 204 | |
190 | faceUpDealingPile->addCardToTop(item); | 205 | // Added Code |
191 | item->setCardPile( faceUpDealingPile ); | 206 | faceDownDealingPile->removeCard(item); |
192 | 207 | faceUpDealingPile->addCardToTop(item); | |
193 | if ( qt_screen->deviceWidth() < 200 ) | 208 | item->setCardPile( faceUpDealingPile ); |
194 | item->flipTo( 30, (int)item->y() ); | 209 | |
195 | else | 210 | if ( qt_screen->deviceWidth() < 200 ) |
196 | item->flipTo( 35, (int)item->y() ); | 211 | item->flipTo( 30, (int)item->y() ); |
197 | } else return FALSE; // <- was missing, caused facedown card to react | 212 | else |
198 | // to clicking, which is wrong | 213 | item->flipTo( 35, (int)item->y() ); |
199 | moving = NULL; | 214 | } |
200 | moved = FALSE; | 215 | else return FALSE; // <- was missing, caused facedown card to react |
201 | 216 | // to clicking, which is wrong | |
202 | // move two other cards if we flip three at a time | 217 | moving = NULL; |
203 | int flipped = 1; | 218 | moved = FALSE; |
204 | QCanvasItemList l = canvas()->collisions( p ); | 219 | |
205 | for (QCanvasItemList::Iterator it = l.begin(); (it != l.end()) && (flipped != cardsDrawn()); ++it) { | 220 | // move two other cards if we flip three at a time |
206 | if ( (*it)->rtti() == canvasCardId ) { | 221 | int flipped = 1; |
207 | CanvasCard *item = (CanvasCard *)*it; | 222 | QCanvasItemList l = canvas()->collisions( p ); |
208 | if (item->animated()) | 223 | for (QCanvasItemList::Iterator it = l.begin(); (it != l.end()) && (flipped != cardsDrawn()); ++it) |
209 | continue; | 224 | { |
210 | item->setZ(highestZ); | 225 | if ( (*it)->rtti() == canvasCardId ) |
211 | highestZ++; | 226 | { |
212 | flipped++; | 227 | CanvasCard *item = (CanvasCard *)*it; |
213 | 228 | if (item->animated()) | |
214 | // Added Code | 229 | continue; |
215 | faceDownDealingPile->removeCard(item); | 230 | item->setZ(highestZ); |
216 | faceUpDealingPile->addCardToTop(item); | 231 | highestZ++; |
217 | item->setCardPile( faceUpDealingPile ); | 232 | flipped++; |
218 | 233 | ||
219 | if ( qt_screen->deviceWidth() < 200 ) | 234 | // Added Code |
220 | item->flipTo( 30, (int)item->y(), 8 * flipped ); | 235 | faceDownDealingPile->removeCard(item); |
221 | else | 236 | faceUpDealingPile->addCardToTop(item); |
222 | item->flipTo( 35, (int)item->y(), 8 * flipped ); | 237 | item->setCardPile( faceUpDealingPile ); |
223 | } | 238 | |
224 | } | 239 | if ( qt_screen->deviceWidth() < 200 ) |
225 | 240 | item->flipTo( 30, (int)item->y(), 8 * flipped ); | |
226 | return TRUE; | 241 | else |
242 | item->flipTo( 35, (int)item->y(), 8 * flipped ); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | return TRUE; | ||
227 | } | 247 | } |
228 | 248 | ||
229 | return FALSE; | 249 | return FALSE; |
230 | } | 250 | } |
231 | 251 | ||
232 | 252 | ||
233 | void PatienceCardGame::mousePress(QPoint p) | 253 | void PatienceCardGame::mousePress(QPoint p) |
234 | { | 254 | { |
235 | if ( canTurnOverDeck() && | 255 | if ( canTurnOverDeck() && |
236 | (p.x() > 5) && (p.x() < 28) && | 256 | (p.x() > 5) && (p.x() < 28) && |
237 | (p.y() > 10) && (p.y() < 46) ) { | 257 | (p.y() > 10) && (p.y() < 46) ) |
238 | 258 | { | |
239 | beginDealing(); | 259 | |
240 | Card *card = faceUpDealingPile->cardOnTop(); | 260 | beginDealing(); |
241 | while ( card ) { | 261 | Card *card = faceUpDealingPile->cardOnTop(); |
242 | card->setPos( 5, 10, highestZ ); | 262 | while ( card ) |
243 | card->setFace( FALSE ); | 263 | { |
244 | faceUpDealingPile->removeCard( card ); | 264 | card->setPos( 5, 10, highestZ ); |
245 | faceDownDealingPile->addCardToTop( card ); | 265 | card->setFace( FALSE ); |
246 | card->setCardPile( faceDownDealingPile ); | 266 | faceUpDealingPile->removeCard( card ); |
247 | card = faceUpDealingPile->cardOnTop(); | 267 | faceDownDealingPile->addCardToTop( card ); |
248 | highestZ++; | 268 | card->setCardPile( faceDownDealingPile ); |
249 | } | 269 | card = faceUpDealingPile->cardOnTop(); |
250 | endDealing(); | 270 | highestZ++; |
251 | 271 | } | |
252 | throughDeck(); | 272 | endDealing(); |
253 | 273 | ||
254 | moved = TRUE; | 274 | throughDeck(); |
275 | |||
276 | moved = TRUE; | ||
255 | } | 277 | } |
256 | } | 278 | } |
257 | 279 | ||
258 | 280 | ||