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

freecellcardgame.cpp

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 #include "freecellcardgame.h"
00021 
00022 
00023 extern int highestZ;
00024 int numberOfFreeCells = 4;
00025 
00026 
00027 FreecellCardGame::FreecellCardGame(QCanvas *c, bool snap, QWidget *parent) : CanvasCardGame(*c, snap, parent)
00028 {
00029     numberOfFreeCells = 4;
00030     highestZ = 0;
00031 
00032     int spaceBetweenPiles = ( qt_screen->deviceWidth() < 200 ) ? 21 : 28;
00033     int xOrigin = ( qt_screen->deviceWidth() < 200 ) ? 0 : 5;
00034     int spacing = ( qt_screen->deviceWidth() < 200 ) ? 0 : 0;
00035 
00036     for (int i = 0; i < 4; i++) {
00037         freecellPiles[i] = new FreecellFreecellPile( xOrigin + i * spaceBetweenPiles, 10, canvas() );
00038         addCardPile(freecellPiles[i]);
00039     }
00040     for (int i = 0; i < 4; i++) {
00041         discardPiles[i] = new FreecellDiscardPile( xOrigin + spacing + 6 + (i + 4) * spaceBetweenPiles, 10, canvas() );
00042         addCardPile(discardPiles[i]);
00043     }
00044     for (int i = 0; i < 8; i++) {
00045         workingPiles[i] = new FreecellWorkingPile( xOrigin + spacing + 2 + i * spaceBetweenPiles, 50, canvas() );
00046         addCardPile(workingPiles[i]);
00047     }
00048 }
00049 
00050 
00051 void FreecellCardGame::deal(void)
00052 {
00053     highestZ = 1;
00054     
00055     beginDealing();
00056     
00057     for (int i = 0; i < 52; i++) {
00058         Card *card = cards[i];
00059         card->setFace( TRUE );
00060         card->setPos( 0, 0, highestZ );
00061         card->setCardPile( workingPiles[i%8] );
00062         workingPiles[i%8]->addCardToTop( card );
00063         card->move( workingPiles[i%8]->getCardPos( card ) );
00064         card->showCard();
00065         highestZ++;
00066     }
00067 
00068     endDealing();
00069 }
00070 
00071 //      checks if smaller card with different color, that could be put on top on the
00072 //      card, is present in working or freecell pile
00073 bool FreecellCardGame::checkNeeded(Card *card)
00074 {
00075         if (card->getValue() > 2){
00076                 int i;
00077                 Card *c;
00078                 for (i=0;i<4;i++){
00079                         c = freecellPiles[i]->cardOnBottom();
00080                         if (c != NULL){
00081                                 if (card->isRed()!= c->isRed() && card->getValue()== c->getValue()+1){
00082                                         return (false); 
00083                                 }
00084                         }
00085                 }
00086                 for (i=0;i<8;i++){
00087                         c=workingPiles[i]->cardOnBottom();
00088                         while (c!=NULL){
00089                                 if (card->isRed() != c->isRed() && card->getValue() == c->getValue()+1) {
00090                                         return (false);
00091                                 }
00092                                 c=workingPiles[i]->cardInfront(c);
00093                         }
00094                 }
00095         }
00096         return(true);
00097 }
00098         
00099 //  added to move cards, on which no card can be moved, to discard pile 
00100 void FreecellCardGame::checkUnusable()
00101 {
00102         int i,j;
00103 //      printf("void FreecellCardGame::checkUnusable()\n");
00104         Card *top_one;
00105         for (i=0;i < 8;i++)
00106         {
00107                 top_one = workingPiles[i]->cardOnTop();
00108                 if (top_one != NULL)
00109                 {
00110                         j = 0;
00111                         while ((j < 4))
00112                         {
00113                                 if (discardPiles[j]->isAllowedOnTop(top_one)){
00114                                         if (checkNeeded(top_one)){
00115                                                 top_one->setCardPile(discardPiles[j]);
00116                                                 workingPiles[i]->removeCard(top_one);
00117 //                                              printf("k %d f work%d to disk%d on %d\n ",top_one->getValue(),i+1,j+1,highestZ);
00118                                                 discardPiles[j]->addCardToTop(top_one);
00119                                                 top_one->setPos(discardPiles[j]->getX(),discardPiles[j]->getY(),highestZ);
00120                                                 highestZ++;
00121                                                 j = 4;
00122                                                 checkUnusable();
00123                                         }
00124                                 }
00125                                 j++;
00126                         }
00127                 } 
00128         }
00129         for (i=0;i<4;i++){
00130                 top_one = freecellPiles[i]->cardOnTop();
00131                 if (top_one != NULL)
00132                 {
00133                         j = 0;
00134                         while ((j < 4))
00135                         {
00136                                 if (discardPiles[j]->isAllowedOnTop(top_one)){
00137                                         if (checkNeeded(top_one)){
00138                                                 top_one->setCardPile(discardPiles[j]);
00139                                                 freecellPiles[i]->removeCard(top_one);
00140 //                                              printf("k %d f work%d to disk%d on %d\n ",top_one->getValue(),i+1,j+1,highestZ);
00141                                                 discardPiles[j]->addCardToTop(top_one);
00142                                                 top_one->setPos(discardPiles[j]->getX(),discardPiles[j]->getY(),highestZ);
00143                                                 highestZ++;
00144                                                 j = 4;
00145                                                 checkUnusable();
00146                                         }
00147                                 }
00148                                 j++;
00149                         }
00150                 } 
00151         }               
00152 }
00153 
00154 
00155 bool FreecellCardGame::mousePressCard( Card *c, QPoint p )
00156 {
00157     Q_UNUSED(p);
00158     
00159     if ( !c->getCardPile()->isAllowedToBeMoved(c) ) {
00160         moving = NULL;
00161         return TRUE;
00162     }
00163 
00164     return FALSE;
00165 }
00166 
00167 
00168 void FreecellCardGame::readConfig( Config& cfg )
00169 {
00170     cfg.setGroup("GameState");
00171 
00172     // Create Cards, but don't shuffle or deal them yet
00173     createDeck();
00174 
00175     // Move the cards to their piles (deal them to their previous places)
00176     beginDealing();
00177 
00178     highestZ = 1;
00179 
00180     for (int k = 0; k < 4; k++) {
00181         QString pile;
00182         pile.sprintf( "FreeCellPile%i", k );
00183         readPile( cfg, freecellPiles[k], pile, highestZ );
00184     }
00185 
00186     for (int k = 0; k < 4; k++) {
00187         QString pile;
00188         pile.sprintf( "DiscardPile%i", k );
00189         readPile( cfg, discardPiles[k], pile, highestZ );
00190     }
00191 
00192     for (int k = 0; k < 8; k++) {
00193         QString pile;
00194         pile.sprintf( "WorkingPile%i", k );
00195         readPile( cfg, workingPiles[k], pile, highestZ );
00196     }
00197 
00198     highestZ++;
00199 
00200     endDealing();
00201 }
00202 
00203 
00204 void FreecellCardGame::writeConfig( Config& cfg )
00205 {
00206     cfg.setGroup("GameState");
00207     for ( int i = 0; i < 4; i++ ) {
00208         QString pile;
00209         pile.sprintf( "FreeCellPile%i", i );
00210         freecellPiles[i]->writeConfig( cfg, pile );
00211     }
00212     for ( int i = 0; i < 4; i++ ) {
00213         QString pile;
00214         pile.sprintf( "DiscardPile%i", i );
00215         discardPiles[i]->writeConfig( cfg, pile );
00216     }
00217     for ( int i = 0; i < 8; i++ ) {
00218         QString pile;
00219         pile.sprintf( "WorkingPile%i", i );
00220         workingPiles[i]->writeConfig( cfg, pile );
00221     }
00222 }
00223 
00224 

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