File indexing completed on 2024-04-14 04:02:24
0001 /* 0002 This file is part of the KDE games lskat program 0003 SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "deck.h" 0009 #include "lskatglobal.h" 0010 0011 // Qt includes 0012 0013 // KF includes 0014 #include <KLocalizedString> 0015 0016 // How many cards 0017 #define NUMBER_OF_CARDS 32 0018 // How often to shuffle the cards 0019 #define SHUFFLE_AMOUNT 1000 0020 0021 // Constructor for the view 0022 Deck::Deck(quint32 seed, QObject *parent) 0023 : QObject(parent) 0024 { 0025 // Set the random seed 0026 mRandom.seed(seed); 0027 // TODO: Hardcoded 0028 // mRandom.setSeed(global_debug); 0029 shuffle(); 0030 } 0031 0032 // Destructor 0033 Deck::~Deck() 0034 { 0035 } 0036 0037 // Retrieve number of cards in this deck. 0038 int Deck::cardNumber() 0039 { 0040 // Use pixmaps as this is the same size but earlier filled. 0041 return mCards.size(); 0042 } 0043 0044 // Draw a random trump from all cards. 0045 Suite Deck::randomTrump() 0046 { 0047 int card = mRandom.bounded(NUMBER_OF_CARDS); 0048 0049 Suite suite = getSuite(card); 0050 CardType type = getCardType(card); 0051 if (type == Jack) return Grand; 0052 0053 // TODO: Hardcoded test 0054 // return Diamond; 0055 return suite; 0056 } 0057 0058 // Shuffle deck of cards 0059 void Deck::shuffle() 0060 { 0061 mCards.clear(); 0062 mCards.reserve(NUMBER_OF_CARDS); 0063 // Fill card deck with ordered cards 0064 for (int i = 0; i < NUMBER_OF_CARDS; i++) 0065 { 0066 mCards.append(i); 0067 } 0068 0069 // Shuffle cards 0070 for (int i = 0; i < SHUFFLE_AMOUNT; i++) 0071 { 0072 int c1 = mRandom.bounded(NUMBER_OF_CARDS); 0073 int c2 = mRandom.bounded(NUMBER_OF_CARDS); 0074 mCards.swapItemsAt(c1, c2); 0075 } 0076 0077 return; 0078 0079 // TODO: Hardcoded deck 0080 /*static int c[] = { 0081 Ten, Spade, Seven, Diamond, Ten, Diamond, Jack, Club, 0082 Eight, Club, Nine, Club, Nine, Heart, Jack, Diamond, 0083 Eight, Spade, Seven, Heart, Nine, Spade, Ten, Club, 0084 Ace, Diamond, Eight, Heart, Queen, Spade, Ten, Heart, 0085 0086 Ace, Club, Ace, Heart, Queen, Club, Jack, Heart, 0087 King, Heart, Ace, Spade, Queen, Diamond, Jack, Spade, 0088 King, Club, Nine, Diamond, Seven, Spade, Queen, Heart, 0089 Eight, Diamond, Seven, Club, King, Spade, King, Diamond 0090 }; 0091 0092 mCards.clear(); 0093 // Fill card deck with ordered cards 0094 for (int i = 0; i < NUMBER_OF_CARDS; i++) 0095 { 0096 int card = c[2 * i]; 0097 int suite = c[2 * i + 1]; 0098 int cc = getCard(Suite(suite), CardType(card)); 0099 0100 mCards.append(cc); 0101 }*/ 0102 } 0103 0104 // Draw a card from the deck 0105 int Deck::drawCard() 0106 { 0107 if (mCards.size() < 1) 0108 { 0109 qCCritical(LSKAT_LOG) << "No more cards to draw from card deck"; 0110 } 0111 int card = mCards.takeFirst(); 0112 return card; 0113 } 0114 0115 int Deck::getCard(Suite suite, CardType type) 0116 { 0117 return 4 * int(type) + int(suite); 0118 } 0119 0120 // Return the suite of a given card (number) 0121 Suite Deck::getSuite(int card) 0122 { 0123 return (Suite)(card % 4); 0124 } 0125 0126 // Return the card type (ace, king, ...) of a given card (number) 0127 CardType Deck::getCardType(int card) 0128 { 0129 return (CardType)(card / 4); 0130 } 0131 0132 // Get the value in points of the given card (number). 0133 int Deck::getCardValue(int card) 0134 { 0135 CardType type = getCardType(card); 0136 if (type == Ace) return 11; 0137 if (type == Ten) return 10; 0138 if (type == King) return 4; 0139 if (type == Queen) return 3; 0140 if (type == Jack) return 2; 0141 return 0; 0142 } 0143 0144 // Get verbose name of a vard 0145 QString Deck::name(int card) 0146 { 0147 if (card < 0) return i18n("no valid card"); 0148 return name(getSuite(card), getCardType(card)); 0149 } 0150 0151 // Return a verbose name for a suite 0152 QString Deck::name(Suite suite) 0153 { 0154 QString suiteName = i18n("unknown"); 0155 if (suite == Club) suiteName = i18nc("suite name", "Clubs"); 0156 if (suite == Spade) suiteName = i18nc("suite name", "Spades"); 0157 if (suite == Heart) suiteName = i18nc("suite name", "Hearts"); 0158 if (suite == Diamond) suiteName = i18nc("suite name", "Diamonds"); 0159 if (suite == Grand) suiteName = i18nc("trump name", "Grand"); 0160 return suiteName; 0161 } 0162 0163 // Return a verbose name for a cardtype 0164 QString Deck::name(CardType type) 0165 { 0166 QString typeName = i18n("unknown"); 0167 if (type == Ace) typeName = i18nc("card name", "Ace"); 0168 if (type == King) typeName = i18nc("card name", "King"); 0169 if (type == Queen) typeName = i18nc("card name", "Queen"); 0170 if (type == Jack) typeName = i18nc("card name", "Jack"); 0171 if (type == Ten) typeName = i18nc("card name", "Ten"); 0172 if (type == Nine) typeName = i18nc("card name", "Nine"); 0173 if (type == Eight) typeName = i18nc("card name", "Eight"); 0174 if (type == Seven) typeName = i18nc("card name", "Seven"); 0175 return typeName; 0176 } 0177 0178 // Returns a verbose name for a card 0179 QString Deck::name(Suite suite, CardType type) 0180 { 0181 QString suiteName = name(suite); 0182 QString typeName = name(type); 0183 return i18nc("eg jack of clubs", "%1 of %2", typeName, suiteName); 0184 } 0185 0186 #include "moc_deck.cpp"