File indexing completed on 2025-10-12 04:59:55
0001 // bjgb_shoe.cpp -*-C++-*- 0002 #include <bjgb_shoe.h> 0003 0004 #include <iomanip> 0005 #include <ostream> 0006 0007 namespace bjgb { 0008 0009 // ---------- 0010 // class Shoe 0011 // ---------- 0012 0013 // PRIVATE MANIPULATORS 0014 void Shoe::updateCache() 0015 { 0016 d_totalCards = 0; 0017 0018 for (int i = 1; i <= 10; ++i) { 0019 d_totalCards += d_rankCounts[i]; 0020 } 0021 0022 const Types::Double denom = d_totalCards; 0023 0024 for (int i = 1; i <= 10; ++i) { 0025 d_prob[i] = d_rankCounts[i] / denom; 0026 } 0027 } 0028 0029 // CREATORS 0030 Shoe::Shoe(int numDecks) 0031 { 0032 assert(1 <= numDecks); 0033 assert(numDecks <= 8); 0034 0035 d_rankCounts[0] = 0; // unused but must set 0036 0037 for (int i = 1; i <= 10; ++i) { 0038 d_rankCounts[i] = 4 * numDecks; 0039 } 0040 0041 d_rankCounts[10] *= 4; // four times as many T cards 0042 0043 updateCache(); 0044 } 0045 0046 Shoe::Shoe(const int *rankCounts) 0047 { 0048 assert(rankCounts); 0049 0050 setNumCardsOfEachRank(rankCounts); 0051 } 0052 0053 // MANIPULATORS 0054 void Shoe::setNumCardsOfEachRank(const int *rankCounts) 0055 { 0056 assert(rankCounts); 0057 0058 d_rankCounts[0] = 0; // unused but must set 0059 0060 for (int i = 0; i < 10; ++i) { 0061 assert(0 <= rankCounts[i]); 0062 assert(rankCounts[i] <= (9 == i ? 128 : 32)); 0063 0064 d_rankCounts[i + 1] = rankCounts[i]; 0065 } 0066 0067 updateCache(); 0068 } 0069 0070 } // namespace bjgb 0071 0072 // FREE OPERATORS 0073 bool bjgb::operator==(const Shoe& lhs, const Shoe& rhs) 0074 { 0075 if (lhs.numCardsTotal() != rhs.numCardsTotal()) { 0076 return false; // RETURN 0077 } 0078 0079 using namespace RankLiterals; 0080 0081 for (Rank r = A_R; r < Rank::end(); ++r) { 0082 if (lhs.numCards(r) != rhs.numCards(r)) { 0083 return false; // RETURN 0084 } 0085 } 0086 0087 return true; 0088 } 0089 0090 std::ostream& bjgb::operator<<(std::ostream& stream, const Shoe& shoe) 0091 { 0092 stream << "Shoe:\n"; 0093 0094 for (int i = 1; i <= 10; ++i) { 0095 stream << std::setw(8) << i; 0096 } 0097 0098 using namespace RankLiterals; 0099 0100 for (Rank r = A_R; r < Rank::end(); ++r) { 0101 stream << std::setw(8) << shoe.numCards(r); 0102 } 0103 0104 for (int i = 1; i <= 10; ++i) { 0105 stream << std::setw(8) << shoe.prob(i) * 100; 0106 } 0107 0108 return stream << "\n"; 0109 }