00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 #include <klocalizedstring.h>
00027
00028
00029 #include "deck.h"
00030
00031
00032 #define NUMBER_OF_CARDS 32
00033
00034 #define SHUFFLE_AMOUNT 1000
00035
00036
00037
00038 Deck::Deck(long seed, QObject* parent)
00039 : QObject(parent)
00040 {
00041
00042 mRandom.setSeed(seed);
00043 shuffle();
00044 }
00045
00046
00047 Deck::~Deck()
00048 {
00049 }
00050
00051
00052 int Deck::cardNumber()
00053 {
00054
00055 return mCards.size();
00056 }
00057
00058
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
00071 void Deck::shuffle()
00072 {
00073 mCards.clear();
00074
00075 for (int i=0; i<NUMBER_OF_CARDS; i++)
00076 {
00077 mCards.append(i);
00078 }
00079
00080
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
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
00103 Suite Deck::getSuite(int card)
00104 {
00105 return (Suite)(card%4);
00106 }
00107
00108
00109
00110 CardType Deck::getCardType(int card)
00111 {
00112 return (CardType)(card/4);
00113 }
00114
00115
00116
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
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
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"