1
557

Deck

  1. #if !defined( _DECK_H )
  2. #define _DECK_H
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. #include "Card.h"
  9. #include "Hand.h"
  10.  
  11. class Deck
  12. {
  13.     friend ostream &operator<<( ostream &, const Deck & );
  14. public:
  15.     Deck( bool swe=false) ;
  16.     void shuffle( );
  17.     Card deal();
  18.     void deal( Hand & );
  19.  
  20. private:
  21.     int top;
  22.     Card cards[ 52 ];
  23.     bool shuffleWhenEmpty;
  24.  
  25.     void check( );
  26. };
  27.  
  28. #endif
  1. #include "Deck.h"
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. Deck::Deck( bool swe ) : shuffleWhenEmpty( swe )
  10. {
  11.     for ( int s = 0; s < 4; s++ )
  12.         for ( int r = 0; r < 13; r++ ) {
  13.             cards[ 13*s+r ]._rank = rank(r);
  14.             cards[ 13*s+r ]._suit = suit(s);
  15.         }
  16.     top = 0;
  17. }
  18.  
  19. static void swap( Card &c1, Card &c2 )
  20. {
  21.     Card ct = c1;
  22.     c1 = c2;
  23.     c2 = ct;
  24. }
  25.  
  26. void Deck::shuffle( )
  27. {   // swap all cards at random
  28.     srand( time( 0 ) );
  29.  
  30.     for ( int i = 0; i < 52; i++ )
  31.         swap( cards[ i ], cards[ rand() % 52 ] );
  32.     top = 0;
  33. }
  34.  
  35. void Deck::check( )
  36. {
  37.     if ( ++top >= 52 )
  38.     {   // reset cards
  39.         if ( shuffleWhenEmpty )
  40.             shuffle();
  41.         top = 0;
  42.     }
  43. }
  44.  
  45. void Deck::deal( Hand &hand )
  46. {
  47.     for ( int i = 0; i < hand.size(); i++ )
  48.     {
  49.         hand[ i ] = cards[ top ];
  50.         check();
  51.     }
  52. }
  53.  
  54. Card Deck::deal(  )
  55. {
  56.     Card c = cards[ top ];
  57.     check();
  58.     return c;
  59. }
  60.  
  61. ostream &operator<<( ostream &os, const Deck &deck )
  62. {
  63.     for ( int s = 0; s < 4; s++ ) {
  64.         for ( int r = 0; r < 13; r++ )
  65.             os << deck.cards[ 13*s+r ] << " ";
  66.         os << endl;
  67.     }
  68.     return os;
  69. }
  70.  
  71. #if defined( STANDALONE )
  72.  
  73. #include <cstring>
  74. #include <cstdlib>
  75.  
  76. #ifdef DEBUG
  77. int debug = 1;  // 0 - off, 1 - on, 2 - more, 3 - even more... 9 - all
  78. #endif
  79.  
  80. main( int argc, char **argv )
  81. {
  82.     using namespace std;
  83.  
  84.     if ( argc > 1 && strncmp( argv[ 1 ], "-D", 2 ) == 0 )
  85.     {
  86.         if ( argc > 2 )
  87.             debug = atoi( argv[ 2 ] );
  88.         else if ( strlen( argv[ 1 ] ) > 2 )
  89.             debug = atoi( argv[ 1 ]+2 );
  90.         else
  91.             debug = 9;  // level max
  92.     }
  93.  
  94.     int i;
  95.     Hand hand;
  96.  
  97.     Deck deck = Deck( true );   // shuffleWhenEmpty=true
  98.  
  99.     cout << deck;
  100.  
  101.     deck.shuffle( );
  102.     cout << endl;
  103.     cout << deck;
  104.     cout << endl;
  105.  
  106.     for ( i = 0; i < 10; i++ )
  107.     {
  108.         deck.deal( hand );
  109.         hand.print( true );
  110.         cout << endl;
  111.     }
  112.  
  113.     // statistics
  114.     const int NDEALS = 1500000;
  115.     int stats[ 9 ];
  116.  
  117.     for ( i = 0; i < 9; i++ )
  118.         stats[ i ] = 0;
  119.     for ( i = 0; i < NDEALS; i++ )
  120.     {
  121.         deck.deal( hand );
  122.         stats[ hand.eval() ]++;
  123.     }
  124.  
  125.     static const char *headers[] =
  126.     {
  127.         "NOTHING",
  128.         "ONEPAIR",
  129.         "TWOPAIRS",
  130.         "THREEOFKIND",
  131.         "STRAIGHT",
  132.         "FLUSH",
  133.         "FULLHOUSE",
  134.         "FOUROFKIND",
  135.         "STRAIGHTFLUSH",
  136.     };
  137.  
  138.     // find longest header
  139.     int width = 0;
  140.     for ( i = 0; i < sizeof (headers)/sizeof (char *); i++ )
  141.     {
  142.         int w = strlen( headers[ i ] );
  143.         if ( w > width )
  144.              width = w;
  145.     }
  146.     // print stats
  147.     cout.fill( ' ' );
  148.     cout.precision( 2 );
  149.     cout.setf( ios::fixed, ios::floatfield );
  150.     cout.setf( ios::internal, ios::adjustfield );
  151.  
  152.     cout << "Combinations found after " << NDEALS << " deals:\n";
  153.  
  154.     for ( i = 0; i < sizeof (headers)/sizeof (char *); i++ )
  155.     {
  156.         cout.width( width );
  157.         cout << headers[ i ];
  158.         cout << "->" << stats[ i ] << "\t";
  159.         cout.width( 5 );
  160.         cout << stats[ i ]/(NDEALS/100.0) << "%\n";
  161.     }
  162. }
  163.  
  164. #endif
$ make tdeck
g++ -DDEBUG -g -c -o Hand.o Hand.cpp
g++ -DDEBUG -g -c -o Card.o Card.cpp
g++ -DDEBUG -g DSTANDALONE -DDEBUG Deck.cpp Hand.o Card.o -o tdeck
$ tdeck
...
Combinations found after 1500000 deals:
      NOTHING->735788   49.05%
      ONEPAIR->626043   41.74%
     TWOPAIRS->69556     4.64%
  THREEOFKIND->35127     2.34%
     STRAIGHT->4200      0.28%
        FLUSH->2009      0.13%
    FULLHOUSE->1004      0.07%
   FOUROFKIND->0         0.00%
STRAIGHTFLUSH->26273     1.75%

Almost one hand out of two has nothing and not a single four of a kind!

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].