deck.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games lskat program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Qt includes
00022 
00023 // KDE includes
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 #include <klocalizedstring.h>
00027 
00028 // Local includes
00029 #include "deck.h"
00030 
00031 // How many cards
00032 #define NUMBER_OF_CARDS   32
00033 // How often to shuffle the cards
00034 #define SHUFFLE_AMOUNT  1000
00035 
00036 
00037 // Constructor for the view
00038 Deck::Deck(long seed, QObject* parent)
00039     : QObject(parent)
00040 {
00041   // Set the random seed
00042   mRandom.setSeed(seed);
00043   shuffle();
00044 }
00045 
00046 // Destructor
00047 Deck::~Deck()
00048 {
00049 }
00050 
00051 // Retrieve number of cards in this deck.
00052 int Deck::cardNumber()
00053 {
00054   // Use pixmaps as this is the same size but earlier filled.
00055   return mCards.size();
00056 }
00057 
00058 // Draw a random trump from all cards. 
00059 Suite Deck::randomTrump()
00060 {
00061   int card = mRandom.getLong(NUMBER_OF_CARDS);
00062    
00063   Suite suite = getSuite(card);
00064   CardType type = getCardType(card);
00065   if (type == Jack) return Grand;
00066   return suite;
00067 }
00068 
00069 
00070 // Shuffle deck of cards
00071 void Deck::shuffle()
00072 {
00073    mCards.clear();
00074    // Fill card deck with ordered cards
00075    for (int i=0; i<NUMBER_OF_CARDS; i++)
00076    {
00077      mCards.append(i);
00078    }
00079 
00080    // Shuffle cards
00081    for (int i=0;i<SHUFFLE_AMOUNT;i++)
00082    {
00083      int c1 = mRandom.getLong(NUMBER_OF_CARDS);
00084      int c2 = mRandom.getLong(NUMBER_OF_CARDS);
00085      mCards.swap(c1, c2);
00086    }
00087 }
00088 
00089 
00090 // Draw a card from the deck
00091 int Deck::drawCard()
00092 {
00093   if (mCards.size() < 1)
00094   {
00095     kFatal() << "No more cards to draw from card deck " << endl;
00096   }
00097   int card = mCards.takeFirst();
00098   return card;
00099 }
00100 
00101 
00102 // Return the suite of a given card (number)
00103 Suite Deck::getSuite(int card)
00104 {
00105   return (Suite)(card%4);
00106 }
00107 
00108 
00109 // Return the card type (ace, king, ...) of a given card (number)
00110 CardType Deck::getCardType(int card)
00111 {
00112   return (CardType)(card/4);
00113 }
00114 
00115 
00116 // Get the value in points of the given card (number).
00117 int Deck::getCardValue(int card)
00118 {
00119   CardType type = getCardType(card);
00120   if (type == Ace) return  11;
00121   if (type == Ten) return  10;
00122   if (type == King) return  4;
00123   if (type == Queen) return 3;
00124   if (type == Jack) return  2;
00125   return 0;
00126 }
00127 
00128 
00129 // Get verbose name of a vard
00130 QString Deck::name(int card)
00131 {
00132   if (card < 0) return i18n("no valid card");
00133   return name(getSuite(card), getCardType(card));
00134 }
00135 
00136 
00137 // Returns a verbose name for a card
00138 QString Deck::name(Suite suite, CardType type)
00139 {
00140     QString suiteName = i18n("unknown");
00141     if (suite == Club) suiteName = i18nc("suite name", "Clubs");
00142     if (suite == Spade) suiteName = i18nc("suite name", "Spades");
00143     if (suite == Heart) suiteName = i18nc("suite name", "Hearts");
00144     if (suite == Diamond) suiteName = i18nc("suite name", "Diamonds");
00145     if (suite == Grand) suiteName = i18nc("trump name", "Grand");
00146     QString typeName = i18n("unknown");
00147     if (type == Ace) typeName = i18nc("card name", "Ace");
00148     if (type == King) typeName = i18nc("card name", "King");
00149     if (type == Queen) typeName = i18nc("card name", "Queen");
00150     if (type == Jack) typeName = i18nc("card name", "Jack");
00151     if (type == Ten) typeName = i18nc("card name", "Ten");
00152     if (type == Nine) typeName = i18nc("card name", "Nine");
00153     if (type == Eight) typeName = i18nc("card name", "Eight");
00154     if (type == Seven) typeName = i18nc("card name", "Seven");
00155 
00156     return i18nc("eg jack of clubs", "%1 of %2", typeName, suiteName);
00157 }
00158 
00159 
00160 #include "deck.moc"

Generated on Tue May 1 09:34:40 2007 for LSkat by  doxygen 1.4.6