home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CARDS.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  163 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4. **  CARDS.CPP - Playing card classes
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <string.h>
  13. #include "cards.hpp"
  14.  
  15. const char *suits[] = {"Diamonds", "Clubs", "Hearts", "Spades"};
  16. const char *cards[] = {"Ace", "Deuce", "Trey", "4", "5", "6", "7", "8", "9",
  17.                        "10", "Jack", "Queen", "King"};
  18.  
  19.  
  20. inline card::card(void)
  21. {
  22.       rank_ = Rank_Error;
  23.       suit_ = Suit_Error;
  24. }
  25.  
  26. inline card::card(cardSuit s, cardRank r)
  27. {
  28.       rank_ = r;
  29.       suit_ = s;
  30. }
  31.  
  32. inline cardRank card::rank(void)
  33. {
  34.       return rank_;
  35. }
  36.  
  37. inline cardSuit card::suit(void)
  38. {
  39.       return suit_;
  40. }
  41.  
  42. inline char * card::rankText(void)
  43. {
  44.       return (char *)cards[(int)rank_ - 1];
  45. }
  46.  
  47. inline char * card::suitText(void)
  48. {
  49.       return (char *)suits[(int)suit_];
  50. }
  51.  
  52. inline void card::set_rank(cardRank r)
  53. {
  54.       rank_ = r;
  55. }
  56.  
  57. inline void card::set_suit(cardSuit s)
  58. {
  59.       suit_ = s;
  60. }
  61.  
  62. inline void card::set_card(cardSuit s, cardRank r)
  63. {
  64.       rank_ = r;
  65.       suit_ = s;
  66. }
  67.  
  68. inline void card::get_card(cardSuit &s, cardRank &r)
  69. {
  70.       r = rank_;
  71.       s = suit_;
  72. }
  73.  
  74.  
  75. deck::deck(void)
  76. {
  77.       int n = 0;
  78.  
  79.       for (int s = int(Diamond); s <= int(Spade); ++s)
  80.       {
  81.             for (int c = int(Ace); c <= int(King); ++c)
  82.             {
  83.                   deck::card_[n].set_rank(cardRank(c));
  84.                   deck::card_[n].set_suit(cardSuit(s));
  85.                   ++n;
  86.             }
  87.       }
  88.       top = 0;
  89. }
  90.  
  91. inline void deck::deal(class card &c)
  92. {
  93.       if (top < Deck_Size)
  94.             c = deck::card_[top++];
  95. }
  96.  
  97. inline int deck::cards_left(void)
  98. {
  99.       return int(Deck_Size - top);
  100. }
  101.  
  102. void deck::shuffle(void)
  103. {
  104.       int used[Deck_Size], posn = 0;
  105.  
  106.       srand((unsigned)time(NULL) | 1);
  107.       memset(used, 0, Deck_Size * sizeof(int));
  108.  
  109.       for (int s = int(Diamond); s <= int(Spade); ++s)
  110.       {
  111.             for (int c = int(Ace); c <= int(King); ++c)
  112.             {
  113.                   posn = (posn + rand()) % Deck_Size;
  114.                   while (used[posn])
  115.                         posn = ++posn % Deck_Size;
  116.                   deck::card_[posn].set_rank(cardRank(c));
  117.                   deck::card_[posn].set_suit(cardSuit(s));
  118.                   used[posn] = -1;
  119.             }
  120.       }
  121.       top = 0;
  122. }
  123.  
  124. #ifdef TEST
  125.  
  126. #include <iostream.h>
  127.  
  128. void showem(class deck &d);
  129.  
  130. main()
  131. {
  132.       class deck d;
  133.  
  134.       cout << "Deck initially:" << endl << endl;
  135.       showem(d);
  136.       d.shuffle();
  137.       cout << endl << "After shuffling:" << endl << endl;
  138.       showem(d);
  139.       return EXIT_SUCCESS;
  140. }
  141.  
  142. void showem(class deck &d)
  143. {
  144.       for (int i = 0; i <= Deck_Size; ++i)
  145.       {
  146.             class card card_;
  147.  
  148.             if (0 < d.cards_left())
  149.             {
  150.                   d.deal(card_);
  151.                   cout << "Card #";
  152.                   cout.width(2);
  153.                   cout.setf(ios::left, ios::adjustfield);
  154.                   cout << (i + 1) << " - " <<
  155.                         card_.rankText() << " of " <<
  156.                         card_.suitText() << endl;
  157.             }
  158.             else  cout << "No cards left" << endl;
  159.       }
  160. }
  161.  
  162. #endif // TEST
  163.