/*********************************************************************



  Filename:  pr_card.cpp

  Section:   16.3   User-Defined Types: Output

  Compiler:  Borland C++     Version 5.0       Summer 1996

  C++ Distilled: Descriptions, Prescriptions and Style  By Ira Pohl



*********************************************************************/



// See also pr_card2 for print using overloaded << instead



//card output.

#include 

#include                 //for random number

#include                   //for random number seed





char  pips_symbol[14] = { '?', 'A', '2', '3', '4', '5',

                '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };

char  suit_symbol[4] = { 'c', 'd', 'h', 's' };



enum suit { clubs, diamonds, hearts, spades };



class pips {

public:

   void  assign(int n) { p = n % 13 + 1; }

   void  print() { cout << pips_symbol[p]; }

private:

   int  p;

};



class card {

public:

   suit  s;

   pips  p;

   void  assign(int n)

      { cd = n; s = suit(n / 13); p.assign(n); }

   void  pr_card()

      { p.print(); cout << suit_symbol[s] << "  "; }

private:

   int  cd;                 //a cd is from 0 to 51

};



class deck {

public:

   void  init_deck();

   void  shuffle();

   void  deal(int, int, card*);

   void  pr_deck();

private:

   card d[52];

};



void deck::pr_deck()

{

   for (int i = 0; i < 52; ++i) {

      if (i % 13 == 0)      //13 cards to a line

         cout << endl;

      d[i].pr_card();

   }

}





void  deck::init_deck()

{

   for(int i = 0; i < 52; ++i)

      d[i].assign(i);

}



void  deck::shuffle()

{

   for (int i = 0; i < 52; ++i) {

      int k = rand() % 52;

      card t = d[i]; d[i] = d[k]; d[k] = t; //swap two cards

   }

}



void deck::deal(int n, int pos, card *hand)

{

   for(int i = pos; i < pos +n; ++i)

      hand[i - pos] = d[i];

}



int main()

{

   card one_hand[9];

   deck dk;

   int  i, j, k, fcnt = 0, sval[4];

   int  ndeal, nc, nhand;



   do {

      cout << "\nEnter no. of cards in each hand (5-9): ";

      cin  >> nc;

   } while (nc < 5 || nc > 9);



   nhand = 52 / nc;

   cout << "\nEnter no. of hands to deal: ";

   cin  >> ndeal;

   randomize();

   dk.init_deck();

   dk.pr_deck();    // print using named function



   for (k = 0; k < ndeal; k += nhand) {

     if ((nhand + k) > ndeal)

        nhand = ndeal - k;

     dk.shuffle();

     for(i = 0; i < nc * nhand; i += nc) {

        for (j = 0; j < 4; ++j)

           sval[j] = 0;

        dk.deal(nc, i, one_hand);

        for (j = 0; j < nc; ++j)

           sval[one_hand[j].s] ++ ;

        for (j = 0; j < 4; ++j)

          if (sval[j] >= 5)

             fcnt++;

     }

   }

   cout << "\n\nIn " << ndeal << " ";

   cout << nc << "-card hands there were ";

   cout << fcnt << " flushes\n  ";

}