-rw-r--r-- | noncore/games/solitaire/canvascardgame.cpp | 3 | ||||
-rw-r--r-- | noncore/games/solitaire/canvascardgame.h | 1 | ||||
-rw-r--r-- | noncore/games/solitaire/freecellcardgame.cpp | 83 | ||||
-rw-r--r-- | noncore/games/solitaire/freecellcardgame.h | 5 |
4 files changed, 90 insertions, 2 deletions
diff --git a/noncore/games/solitaire/canvascardgame.cpp b/noncore/games/solitaire/canvascardgame.cpp index 8e07cc8..8250193 100644 --- a/noncore/games/solitaire/canvascardgame.cpp +++ b/noncore/games/solitaire/canvascardgame.cpp | |||
@@ -306,54 +306,55 @@ void CanvasCardGame::contentsMouseReleaseEvent(QMouseEvent *e) | |||
306 | Q_UNUSED(p); | 306 | Q_UNUSED(p); |
307 | 307 | ||
308 | if ( moving ) { | 308 | if ( moving ) { |
309 | 309 | ||
310 | CanvasCard *item = moving; | 310 | CanvasCard *item = moving; |
311 | 311 | ||
312 | if ( item->animated() ) | 312 | if ( item->animated() ) |
313 | return; | 313 | return; |
314 | 314 | ||
315 | if ( alphaCardPile ) | 315 | if ( alphaCardPile ) |
316 | if ( moved ) { | 316 | if ( moved ) { |
317 | 317 | ||
318 | CardPile *pile = closestPile((int)alphaCardPile->x(), (int)alphaCardPile->y(), 30); | 318 | CardPile *pile = closestPile((int)alphaCardPile->x(), (int)alphaCardPile->y(), 30); |
319 | 319 | ||
320 | if (pile && pile->isAllowedOnTop(item)) { | 320 | if (pile && pile->isAllowedOnTop(item)) { |
321 | CardPile *oldPile = item->getCardPile(); | 321 | CardPile *oldPile = item->getCardPile(); |
322 | Card *c = NULL; | 322 | Card *c = NULL; |
323 | if ( oldPile != pile) { | 323 | if ( oldPile != pile) { |
324 | while ( item ) { | 324 | while ( item ) { |
325 | item->show(); | 325 | item->show(); |
326 | if ( oldPile ) { | 326 | if ( oldPile ) { |
327 | c = oldPile->cardInfront(item); | 327 | c = oldPile->cardInfront(item); |
328 | oldPile->removeCard(item); | 328 | oldPile->removeCard(item); |
329 | } | 329 | } |
330 | pile->addCardToTop(item); | ||
331 | item->setCardPile(pile); | 330 | item->setCardPile(pile); |
332 | //item->move( pile->getCardPos(item) ); | 331 | //item->move( pile->getCardPos(item) ); |
332 | pile->addCardToTop(item); | ||
333 | QPoint p = pile->getCardPos(item); | 333 | QPoint p = pile->getCardPos(item); |
334 | item->setPos( p.x(), p.y(), highestZ ); | 334 | item->setPos( p.x(), p.y(), highestZ ); |
335 | highestZ++; | 335 | highestZ++; |
336 | checkUnusable(); // added for freecell to move card to discard pile | ||
336 | 337 | ||
337 | if (item->getValue() == king && haveWeWon()) { | 338 | if (item->getValue() == king && haveWeWon()) { |
338 | alphaCardPile->hide(); | 339 | alphaCardPile->hide(); |
339 | gameWon(); | 340 | gameWon(); |
340 | moving = NULL; | 341 | moving = NULL; |
341 | return; | 342 | return; |
342 | } | 343 | } |
343 | 344 | ||
344 | if (oldPile) { | 345 | if (oldPile) { |
345 | item = (CanvasCard *)c; | 346 | item = (CanvasCard *)c; |
346 | } else { | 347 | } else { |
347 | item = NULL; | 348 | item = NULL; |
348 | } | 349 | } |
349 | } | 350 | } |
350 | alphaCardPile->hide(); | 351 | alphaCardPile->hide(); |
351 | moving = NULL; | 352 | moving = NULL; |
352 | return; | 353 | return; |
353 | } | 354 | } |
354 | } | 355 | } |
355 | 356 | ||
356 | alphaCardPile->animatedMove(); | 357 | alphaCardPile->animatedMove(); |
357 | } | 358 | } |
358 | } | 359 | } |
359 | 360 | ||
diff --git a/noncore/games/solitaire/canvascardgame.h b/noncore/games/solitaire/canvascardgame.h index 0dfb85e..d159de6 100644 --- a/noncore/games/solitaire/canvascardgame.h +++ b/noncore/games/solitaire/canvascardgame.h | |||
@@ -57,40 +57,41 @@ public: | |||
57 | return new CanvasCard( v, s, f, canvas() ); | 57 | return new CanvasCard( v, s, f, canvas() ); |
58 | } | 58 | } |
59 | 59 | ||
60 | virtual void readConfig( Config& cfg ) { Q_UNUSED( cfg ); } | 60 | virtual void readConfig( Config& cfg ) { Q_UNUSED( cfg ); } |
61 | virtual void writeConfig( Config& cfg ) { Q_UNUSED( cfg ); } | 61 | virtual void writeConfig( Config& cfg ) { Q_UNUSED( cfg ); } |
62 | 62 | ||
63 | virtual void gameWon(); | 63 | virtual void gameWon(); |
64 | virtual bool haveWeWon() { return FALSE; } | 64 | virtual bool haveWeWon() { return FALSE; } |
65 | 65 | ||
66 | virtual bool mousePressCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); return FALSE; } | 66 | virtual bool mousePressCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); return FALSE; } |
67 | virtual void mouseReleaseCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } | 67 | virtual void mouseReleaseCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } |
68 | 68 | ||
69 | void cancelMoving() { moving = NULL; } | 69 | void cancelMoving() { moving = NULL; } |
70 | void toggleSnap() { snapOn = (snapOn == TRUE) ? FALSE : TRUE; } | 70 | void toggleSnap() { snapOn = (snapOn == TRUE) ? FALSE : TRUE; } |
71 | void toggleCardsDrawn() { numberToDraw = (numberToDraw == 1) ? 3 : 1; } | 71 | void toggleCardsDrawn() { numberToDraw = (numberToDraw == 1) ? 3 : 1; } |
72 | int cardsDrawn() { return numberToDraw; } | 72 | int cardsDrawn() { return numberToDraw; } |
73 | void setNumberToDraw(int numToDraw) { this->numberToDraw = numToDraw; } | 73 | void setNumberToDraw(int numToDraw) { this->numberToDraw = numToDraw; } |
74 | 74 | ||
75 | void readPile( Config& cfg, CardPile *pile, QString name, int& highestZ ); | 75 | void readPile( Config& cfg, CardPile *pile, QString name, int& highestZ ); |
76 | 76 | ||
77 | protected: | 77 | protected: |
78 | void contentsMousePressEvent(QMouseEvent *e); | 78 | void contentsMousePressEvent(QMouseEvent *e); |
79 | void contentsMouseReleaseEvent(QMouseEvent *e); | 79 | void contentsMouseReleaseEvent(QMouseEvent *e); |
80 | void contentsMouseMoveEvent(QMouseEvent *e); | 80 | void contentsMouseMoveEvent(QMouseEvent *e); |
81 | virtualvoid checkUnusable() { } //added for freecell | ||
81 | 82 | ||
82 | protected: | 83 | protected: |
83 | // Mouse event state variables | 84 | // Mouse event state variables |
84 | bool moved; | 85 | bool moved; |
85 | CanvasCard *moving; | 86 | CanvasCard *moving; |
86 | CanvasCardPile *alphaCardPile; | 87 | CanvasCardPile *alphaCardPile; |
87 | int cardXOff, cardYOff; | 88 | int cardXOff, cardYOff; |
88 | 89 | ||
89 | private: | 90 | private: |
90 | bool snapOn; | 91 | bool snapOn; |
91 | int numberToDraw; | 92 | int numberToDraw; |
92 | }; | 93 | }; |
93 | 94 | ||
94 | 95 | ||
95 | #endif | 96 | #endif |
96 | 97 | ||
diff --git a/noncore/games/solitaire/freecellcardgame.cpp b/noncore/games/solitaire/freecellcardgame.cpp index 98415aa..aeb32fc 100644 --- a/noncore/games/solitaire/freecellcardgame.cpp +++ b/noncore/games/solitaire/freecellcardgame.cpp | |||
@@ -48,48 +48,131 @@ FreecellCardGame::FreecellCardGame(QCanvas *c, bool snap, QWidget *parent) : Can | |||
48 | } | 48 | } |
49 | } | 49 | } |
50 | 50 | ||
51 | 51 | ||
52 | void FreecellCardGame::deal(void) | 52 | void FreecellCardGame::deal(void) |
53 | { | 53 | { |
54 | highestZ = 1; | 54 | highestZ = 1; |
55 | 55 | ||
56 | beginDealing(); | 56 | beginDealing(); |
57 | 57 | ||
58 | for (int i = 0; i < 52; i++) { | 58 | for (int i = 0; i < 52; i++) { |
59 | Card *card = cards[i]; | 59 | Card *card = cards[i]; |
60 | card->setFace( TRUE ); | 60 | card->setFace( TRUE ); |
61 | card->setPos( 0, 0, highestZ ); | 61 | card->setPos( 0, 0, highestZ ); |
62 | card->setCardPile( workingPiles[i%8] ); | 62 | card->setCardPile( workingPiles[i%8] ); |
63 | workingPiles[i%8]->addCardToTop( card ); | 63 | workingPiles[i%8]->addCardToTop( card ); |
64 | card->move( workingPiles[i%8]->getCardPos( card ) ); | 64 | card->move( workingPiles[i%8]->getCardPos( card ) ); |
65 | card->showCard(); | 65 | card->showCard(); |
66 | highestZ++; | 66 | highestZ++; |
67 | } | 67 | } |
68 | 68 | ||
69 | endDealing(); | 69 | endDealing(); |
70 | } | 70 | } |
71 | 71 | ||
72 | // checks if smaller card with different color, that could be put on top on the | ||
73 | // card, is present in working or freecell pile | ||
74 | bool FreecellCardGame::checkNeeded(Card *card) | ||
75 | { | ||
76 | if (card->getValue() > 2){ | ||
77 | int i; | ||
78 | Card *c; | ||
79 | for (i=0;i<4;i++){ | ||
80 | c = freecellPiles[i]->cardOnBottom(); | ||
81 | if (c != NULL){ | ||
82 | if (card->isRed()!= c->isRed() && card->getValue()== c->getValue()+1){ | ||
83 | return (false); | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | for (i=0;i<8;i++){ | ||
88 | c=workingPiles[i]->cardOnBottom(); | ||
89 | while (c!=NULL){ | ||
90 | if (card->isRed() != c->isRed()&& card->getValue() == c->getValue()+1) { | ||
91 | return (false); | ||
92 | } | ||
93 | c=workingPiles[i]->cardInfront(c); | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | return(true); | ||
98 | } | ||
99 | |||
100 | // added to move cards, on which no card can be moved, to discard pile | ||
101 | void FreecellCardGame::checkUnusable() | ||
102 | { | ||
103 | int i,j; | ||
104 | //printf("void FreecellCardGame::checkUnusable()\n"); | ||
105 | Card *top_one; | ||
106 | for (i=0;i < 8;i++) | ||
107 | { | ||
108 | top_one = workingPiles[i]->cardOnTop(); | ||
109 | if (top_one != NULL) | ||
110 | { | ||
111 | j = 0; | ||
112 | while ((j < 4)) | ||
113 | { | ||
114 | if (discardPiles[j]->isAllowedOnTop(top_one)){ | ||
115 | if (checkNeeded(top_one)){ | ||
116 | top_one->setCardPile(discardPiles[j]); | ||
117 | workingPiles[i]->removeCard(top_one); | ||
118 | // printf("k %d f work%d to disk%d on %d\n ",top_one->getValue(),i+1,j+1,highestZ); | ||
119 | discardPiles[j]->addCardToTop(top_one); | ||
120 | top_one->setPos(discardPiles[j]->getX(),discardPiles[j]->getY(),highestZ); | ||
121 | highestZ++; | ||
122 | j = 4; | ||
123 | checkUnusable(); | ||
124 | } | ||
125 | } | ||
126 | j++; | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | for (i=0;i<4;i++){ | ||
131 | top_one = freecellPiles[i]->cardOnTop(); | ||
132 | if (top_one != NULL) | ||
133 | { | ||
134 | j = 0; | ||
135 | while ((j < 4)) | ||
136 | { | ||
137 | if (discardPiles[j]->isAllowedOnTop(top_one)){ | ||
138 | if (checkNeeded(top_one)){ | ||
139 | top_one->setCardPile(discardPiles[j]); | ||
140 | freecellPiles[i]->removeCard(top_one); | ||
141 | // printf("k %d f work%d to disk%d on %d\n ",top_one->getValue(),i+1,j+1,highestZ); | ||
142 | discardPiles[j]->addCardToTop(top_one); | ||
143 | top_one->setPos(discardPiles[j]->getX(),discardPiles[j]->getY(),highestZ); | ||
144 | highestZ++; | ||
145 | j = 4; | ||
146 | checkUnusable(); | ||
147 | } | ||
148 | } | ||
149 | j++; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
72 | 155 | ||
73 | bool FreecellCardGame::mousePressCard( Card *c, QPoint p ) | 156 | bool FreecellCardGame::mousePressCard( Card *c, QPoint p ) |
74 | { | 157 | { |
75 | Q_UNUSED(p); | 158 | Q_UNUSED(p); |
76 | 159 | ||
77 | if ( !c->getCardPile()->isAllowedToBeMoved(c) ) { | 160 | if ( !c->getCardPile()->isAllowedToBeMoved(c) ) { |
78 | moving = NULL; | 161 | moving = NULL; |
79 | return TRUE; | 162 | return TRUE; |
80 | } | 163 | } |
81 | 164 | ||
82 | return FALSE; | 165 | return FALSE; |
83 | } | 166 | } |
84 | 167 | ||
85 | 168 | ||
86 | void FreecellCardGame::readConfig( Config& cfg ) | 169 | void FreecellCardGame::readConfig( Config& cfg ) |
87 | { | 170 | { |
88 | cfg.setGroup("GameState"); | 171 | cfg.setGroup("GameState"); |
89 | 172 | ||
90 | // Create Cards, but don't shuffle or deal them yet | 173 | // Create Cards, but don't shuffle or deal them yet |
91 | createDeck(); | 174 | createDeck(); |
92 | 175 | ||
93 | // Move the cards to their piles (deal them to their previous places) | 176 | // Move the cards to their piles (deal them to their previous places) |
94 | beginDealing(); | 177 | beginDealing(); |
95 | 178 | ||
diff --git a/noncore/games/solitaire/freecellcardgame.h b/noncore/games/solitaire/freecellcardgame.h index f1b09ab..2df751b 100644 --- a/noncore/games/solitaire/freecellcardgame.h +++ b/noncore/games/solitaire/freecellcardgame.h | |||
@@ -25,64 +25,65 @@ | |||
25 | 25 | ||
26 | 26 | ||
27 | extern int numberOfFreeCells; | 27 | extern int numberOfFreeCells; |
28 | 28 | ||
29 | 29 | ||
30 | class FreecellDiscardPile : public PatienceDiscardPile | 30 | class FreecellDiscardPile : public PatienceDiscardPile |
31 | { | 31 | { |
32 | public: | 32 | public: |
33 | FreecellDiscardPile(int x, int y, QCanvas *canvas) : | 33 | FreecellDiscardPile(int x, int y, QCanvas *canvas) : |
34 | PatienceDiscardPile(x, y, canvas) { } | 34 | PatienceDiscardPile(x, y, canvas) { } |
35 | 35 | ||
36 | }; | 36 | }; |
37 | 37 | ||
38 | 38 | ||
39 | class FreecellWorkingPile : public PatienceWorkingPile | 39 | class FreecellWorkingPile : public PatienceWorkingPile |
40 | { | 40 | { |
41 | public: | 41 | public: |
42 | FreecellWorkingPile(int x, int y, QCanvas *canvas) : | 42 | FreecellWorkingPile(int x, int y, QCanvas *canvas) : |
43 | PatienceWorkingPile(x, y, canvas) { } | 43 | PatienceWorkingPile(x, y, canvas) { } |
44 | 44 | ||
45 | virtual bool isAllowedOnTop(Card *card) { | 45 | virtual bool isAllowedOnTop(Card *card) { |
46 | if ( cardOnBottom() == NULL ) { | 46 | if ( cardOnBottom() == NULL ) { |
47 | int numberOfCardsBeingMoved = 0; | 47 | int numberOfCardsBeingMoved = 0; |
48 | Card *tempCard = card; | 48 | Card *tempCard = card; |
49 | 49 | ||
50 | while ((tempCard != NULL)) { | 50 | while ((tempCard != NULL)) { |
51 | numberOfCardsBeingMoved++; | 51 | numberOfCardsBeingMoved++; |
52 | tempCard = cardInfront(tempCard); | 52 | tempCard = cardInfront(tempCard); |
53 | } | 53 | } |
54 | 54 | ||
55 | if (numberOfCardsBeingMoved > numberOfFreeCells) | 55 | if (numberOfCardsBeingMoved > numberOfFreeCells) |
56 | return FALSE; | 56 | return FALSE; |
57 | } | 57 | } |
58 | 58 | ||
59 | if ( card->isFacing() && | 59 | if ( card->isFacing() && |
60 | cardOnTop() == NULL ) | 60 | cardOnTop() == NULL ) |
61 | return TRUE; | 61 | return TRUE; |
62 | return PatienceWorkingPile::isAllowedOnTop( card ); | 62 | return PatienceWorkingPile::isAllowedOnTop( card ); |
63 | } | 63 | } |
64 | 64 | ||
65 | |||
65 | virtual bool isAllowedToBeMoved(Card *card) { | 66 | virtual bool isAllowedToBeMoved(Card *card) { |
66 | int nextExpectedValue = (int)card->getValue(); | 67 | int nextExpectedValue = (int)card->getValue(); |
67 | bool nextExpectedColor = card->isRed(); | 68 | bool nextExpectedColor = card->isRed(); |
68 | int numberOfCardsBeingMoved = 0; | 69 | int numberOfCardsBeingMoved = 0; |
69 | 70 | ||
70 | while ((card != NULL)) { | 71 | while ((card != NULL)) { |
71 | numberOfCardsBeingMoved++; | 72 | numberOfCardsBeingMoved++; |
72 | if ( (int)card->getValue() != nextExpectedValue ) | 73 | if ( (int)card->getValue() != nextExpectedValue ) |
73 | return FALSE; | 74 | return FALSE; |
74 | if ( card->isRed() != nextExpectedColor ) | 75 | if ( card->isRed() != nextExpectedColor ) |
75 | return FALSE; | 76 | return FALSE; |
76 | nextExpectedValue--;; | 77 | nextExpectedValue--;; |
77 | nextExpectedColor = !nextExpectedColor; | 78 | nextExpectedColor = !nextExpectedColor; |
78 | card = cardInfront(card); | 79 | card = cardInfront(card); |
79 | } | 80 | } |
80 | 81 | ||
81 | if (numberOfCardsBeingMoved <= (numberOfFreeCells + 1)) | 82 | if (numberOfCardsBeingMoved <= (numberOfFreeCells + 1)) |
82 | return TRUE; | 83 | return TRUE; |
83 | 84 | ||
84 | return FALSE; | 85 | return FALSE; |
85 | } | 86 | } |
86 | virtual void cardRemoved(Card *card) { | 87 | virtual void cardRemoved(Card *card) { |
87 | if ( !isDealing() && !cardOnTop() ) | 88 | if ( !isDealing() && !cardOnTop() ) |
88 | numberOfFreeCells++; | 89 | numberOfFreeCells++; |
@@ -117,36 +118,38 @@ public: | |||
117 | virtual void cardRemoved(Card *card) { | 118 | virtual void cardRemoved(Card *card) { |
118 | Q_UNUSED(card); | 119 | Q_UNUSED(card); |
119 | numberOfFreeCells++; | 120 | numberOfFreeCells++; |
120 | } | 121 | } |
121 | }; | 122 | }; |
122 | 123 | ||
123 | 124 | ||
124 | class FreecellCardGame : public CanvasCardGame | 125 | class FreecellCardGame : public CanvasCardGame |
125 | { | 126 | { |
126 | public: | 127 | public: |
127 | FreecellCardGame(QCanvas *c, bool snap, QWidget *parent = 0); | 128 | FreecellCardGame(QCanvas *c, bool snap, QWidget *parent = 0); |
128 | virtual void deal(void); | 129 | virtual void deal(void); |
129 | virtual bool haveWeWon() { | 130 | virtual bool haveWeWon() { |
130 | return ( discardPiles[0]->kingOnTop() && | 131 | return ( discardPiles[0]->kingOnTop() && |
131 | discardPiles[1]->kingOnTop() && | 132 | discardPiles[1]->kingOnTop() && |
132 | discardPiles[2]->kingOnTop() && | 133 | discardPiles[2]->kingOnTop() && |
133 | discardPiles[3]->kingOnTop() ); | 134 | discardPiles[3]->kingOnTop() ); |
134 | } | 135 | } |
135 | virtual void mousePress(QPoint p) { Q_UNUSED(p); } | 136 | virtual void mousePress(QPoint p) { Q_UNUSED(p); } |
136 | virtual void mouseRelease(QPoint p) { Q_UNUSED(p); } | 137 | virtual void mouseRelease(QPoint p) { Q_UNUSED(p); } |
137 | // virtual void mouseMove(QPoint p); | 138 | // virtual void mouseMove(QPoint p); |
138 | virtual bool mousePressCard(Card *card, QPoint p); | 139 | virtual bool mousePressCard(Card *card, QPoint p); |
139 | virtual void mouseReleaseCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } | 140 | virtual void mouseReleaseCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } |
140 | // virtual void mouseMoveCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } | 141 | // virtual void mouseMoveCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); } |
142 | virtual void checkUnusable(); | ||
141 | void readConfig( Config& cfg ); | 143 | void readConfig( Config& cfg ); |
142 | void writeConfig( Config& cfg ); | 144 | void writeConfig( Config& cfg ); |
143 | bool snapOn; | 145 | bool snapOn; |
144 | private: | 146 | private: |
147 | bool checkNeeded(Card *card); | ||
145 | FreecellFreecellPile *freecellPiles[8]; | 148 | FreecellFreecellPile *freecellPiles[8]; |
146 | FreecellWorkingPile *workingPiles[8]; | 149 | FreecellWorkingPile *workingPiles[8]; |
147 | FreecellDiscardPile *discardPiles[4]; | 150 | FreecellDiscardPile *discardPiles[4]; |
148 | }; | 151 | }; |
149 | 152 | ||
150 | 153 | ||
151 | #endif | 154 | #endif |
152 | 155 | ||