File indexing completed on 2023-10-03 10:41:41
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 // Fill card deck with ordered cards 0063 for (int i = 0; i < NUMBER_OF_CARDS; i++) 0064 { 0065 mCards.append(i); 0066 } 0067 0068 // Shuffle cards 0069 for (int i = 0; i < SHUFFLE_AMOUNT; i++) 0070 { 0071 int c1 = mRandom.bounded(NUMBER_OF_CARDS); 0072 int c2 = mRandom.bounded(NUMBER_OF_CARDS); 0073 mCards.swapItemsAt(c1, c2); 0074 } 0075 0076 return; 0077 0078 // TODO: Hardcoded deck 0079 /*static int c[] = { 0080 Ten, Spade, Seven, Diamond, Ten, Diamond, Jack, Club, 0081 Eight, Club, Nine, Club, Nine, Heart, Jack, Diamond, 0082 Eight, Spade, Seven, Heart, Nine, Spade, Ten, Club, 0083 Ace, Diamond, Eight, Heart, Queen, Spade, Ten, Heart, 0084 0085 Ace, Club, Ace, Heart, Queen, Club, Jack, Heart, 0086 King, Heart, Ace, Spade, Queen, Diamond, Jack, Spade, 0087 King, Club, Nine, Diamond, Seven, Spade, Queen, Heart, 0088 Eight, Diamond, Seven, Club, King, Spade, King, Diamond 0089 }; 0090 0091 mCards.clear(); 0092 // Fill card deck with ordered cards 0093 for (int i = 0; i < NUMBER_OF_CARDS; i++) 0094 { 0095 int card = c[2 * i]; 0096 int suite = c[2 * i + 1]; 0097 int cc = getCard(Suite(suite), CardType(card)); 0098 0099 mCards.append(cc); 0100 }*/ 0101 } 0102 0103 // Draw a card from the deck 0104 int Deck::drawCard() 0105 { 0106 if (mCards.size() < 1) 0107 { 0108 qCCritical(LSKAT_LOG) << "No more cards to draw from card deck"; 0109 } 0110 int card = mCards.takeFirst(); 0111 return card; 0112 } 0113 0114 int Deck::getCard(Suite suite, CardType type) 0115 { 0116 return 4 * int(type) + int(suite); 0117 } 0118 0119 // Return the suite of a given card (number) 0120 Suite Deck::getSuite(int card) 0121 { 0122 return (Suite)(card % 4); 0123 } 0124 0125 // Return the card type (ace, king, ...) of a given card (number) 0126 CardType Deck::getCardType(int card) 0127 { 0128 return (CardType)(card / 4); 0129 } 0130 0131 // Get the value in points of the given card (number). 0132 int Deck::getCardValue(int card) 0133 { 0134 CardType type = getCardType(card); 0135 if (type == Ace) return 11; 0136 if (type == Ten) return 10; 0137 if (type == King) return 4; 0138 if (type == Queen) return 3; 0139 if (type == Jack) return 2; 0140 return 0; 0141 } 0142 0143 // Get verbose name of a vard 0144 QString Deck::name(int card) 0145 { 0146 if (card < 0) return i18n("no valid card"); 0147 return name(getSuite(card), getCardType(card)); 0148 } 0149 0150 // Return a verbose name for a suite 0151 QString Deck::name(Suite suite) 0152 { 0153 QString suiteName = i18n("unknown"); 0154 if (suite == Club) suiteName = i18nc("suite name", "Clubs"); 0155 if (suite == Spade) suiteName = i18nc("suite name", "Spades"); 0156 if (suite == Heart) suiteName = i18nc("suite name", "Hearts"); 0157 if (suite == Diamond) suiteName = i18nc("suite name", "Diamonds"); 0158 if (suite == Grand) suiteName = i18nc("trump name", "Grand"); 0159 return suiteName; 0160 } 0161 0162 // Return a verbose name for a cardtype 0163 QString Deck::name(CardType type) 0164 { 0165 QString typeName = i18n("unknown"); 0166 if (type == Ace) typeName = i18nc("card name", "Ace"); 0167 if (type == King) typeName = i18nc("card name", "King"); 0168 if (type == Queen) typeName = i18nc("card name", "Queen"); 0169 if (type == Jack) typeName = i18nc("card name", "Jack"); 0170 if (type == Ten) typeName = i18nc("card name", "Ten"); 0171 if (type == Nine) typeName = i18nc("card name", "Nine"); 0172 if (type == Eight) typeName = i18nc("card name", "Eight"); 0173 if (type == Seven) typeName = i18nc("card name", "Seven"); 0174 return typeName; 0175 } 0176 0177 // Returns a verbose name for a card 0178 QString Deck::name(Suite suite, CardType type) 0179 { 0180 QString suiteName = name(suite); 0181 QString typeName = name(type); 0182 return i18nc("eg jack of clubs", "%1 of %2", typeName, suiteName); 0183 } 0184 0185 #include "moc_deck.cpp"