Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

freecellcardgame.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #ifndef FREECELL_CARD_GAME_H 
00021 #define FREECELL_CARD_GAME_H 
00022 
00023 
00024 #include "patiencecardgame.h"
00025 
00026 
00027 extern int numberOfFreeCells;
00028 
00029 
00030 class FreecellDiscardPile : public PatienceDiscardPile
00031 {
00032 public:
00033     FreecellDiscardPile(int x, int y, QCanvas *canvas) :
00034         PatienceDiscardPile(x, y, canvas) { }
00035 
00036 };
00037 
00038 
00039 class FreecellWorkingPile : public PatienceWorkingPile
00040 {
00041 public:
00042     FreecellWorkingPile(int x, int y, QCanvas *canvas) :
00043         PatienceWorkingPile(x, y, canvas) { }
00044 
00045      virtual bool isAllowedOnTop(Card *card) {
00046         if ( cardOnBottom() == NULL ) {
00047             int numberOfCardsBeingMoved = 0;
00048             Card *tempCard = card;
00049             
00050             while ((tempCard != NULL)) {
00051                 numberOfCardsBeingMoved++;
00052                 tempCard = cardInfront(tempCard); 
00053             }
00054        
00055             if (numberOfCardsBeingMoved > numberOfFreeCells)
00056                 return FALSE;
00057         }
00058 
00059         if ( card->isFacing() &&
00060               cardOnTop() == NULL )
00061             return TRUE;
00062         return PatienceWorkingPile::isAllowedOnTop( card );     
00063     }
00064     
00065 
00066     virtual bool isAllowedToBeMoved(Card *card) {
00067         int nextExpectedValue = (int)card->getValue();
00068         bool nextExpectedColor = card->isRed();
00069         int numberOfCardsBeingMoved = 0;
00070         
00071         while ((card != NULL)) {
00072             numberOfCardsBeingMoved++;
00073             if ( (int)card->getValue() != nextExpectedValue )
00074                 return FALSE;
00075             if ( card->isRed() != nextExpectedColor )
00076                 return FALSE;
00077             nextExpectedValue--;;
00078             nextExpectedColor = !nextExpectedColor;
00079             card = cardInfront(card); 
00080         }
00081        
00082         if (numberOfCardsBeingMoved <= (numberOfFreeCells + 1))
00083             return TRUE;
00084         
00085         return FALSE;
00086     }
00087     virtual void cardRemoved(Card *card) {
00088         if ( !isDealing() &&  !cardOnTop() )
00089             numberOfFreeCells++;
00090         PatienceWorkingPile::cardRemoved( card );
00091     }
00092     virtual void cardAddedToTop(Card *card) {
00093         if ( !isDealing() && cardOnBottom() == card )
00094             numberOfFreeCells--;
00095         PatienceWorkingPile::cardAddedToTop( card );
00096     }
00097 };
00098 
00099 
00100 class FreecellFreecellPile : public CardPile, public CanvasRoundRect
00101 {
00102 public:
00103     FreecellFreecellPile(int x, int y, QCanvas *canvas)
00104         : CardPile(x, y), CanvasRoundRect(x, y, canvas) { }
00105     virtual bool isAllowedOnTop(Card *card) {
00106         if ( ( cardOnTop() == NULL ) && ( card->getCardPile()->cardInfront(card) == NULL  ) )
00107             return TRUE;
00108         return FALSE;   
00109     }
00110     virtual bool isAllowedToBeMoved(Card *card) {
00111         Q_UNUSED(card);
00112         return TRUE;
00113     }
00114     virtual void cardAddedToTop(Card *card) {
00115         Q_UNUSED(card);
00116         numberOfFreeCells--;
00117     }
00118     virtual void cardRemoved(Card *card) {
00119         Q_UNUSED(card);
00120         numberOfFreeCells++;
00121     }
00122 };
00123 
00124 
00125 class FreecellCardGame : public CanvasCardGame
00126 {
00127 public:
00128     FreecellCardGame(QCanvas *c, bool snap, QWidget *parent = 0);
00129     virtual void deal(void);
00130     virtual bool haveWeWon() { 
00131         return ( discardPiles[0]->kingOnTop() &&
00132                  discardPiles[1]->kingOnTop() &&
00133                  discardPiles[2]->kingOnTop() &&
00134                  discardPiles[3]->kingOnTop() );
00135     }
00136     virtual void mousePress(QPoint p) { Q_UNUSED(p); }
00137     virtual void mouseRelease(QPoint p) { Q_UNUSED(p); }
00138 //    virtual void mouseMove(QPoint p);
00139     virtual bool mousePressCard(Card *card, QPoint p);
00140     virtual void mouseReleaseCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); }
00141 //    virtual void mouseMoveCard(Card *card, QPoint p) { Q_UNUSED(card); Q_UNUSED(p); }
00142      virtual void checkUnusable();
00143     void readConfig( Config& cfg );
00144     void writeConfig( Config& cfg );
00145     bool snapOn;
00146 private:
00147     bool checkNeeded(Card *card);
00148     FreecellFreecellPile *freecellPiles[8];
00149     FreecellWorkingPile *workingPiles[8];
00150     FreecellDiscardPile *discardPiles[4];
00151 };
00152 
00153 
00154 #endif
00155 

Generated on Sat Nov 5 16:17:26 2005 for OPIE by  doxygen 1.4.2